mirror of
https://github.com/koloml/furbooru-tagging-assistant.git
synced 2026-02-06 23:32:58 +00:00
Compare commits
2 Commits
0.5.0
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
| fb626a3928 | |||
| 4060e6c44b |
33
tests/lib/booru/tag-utils.spec.ts
Normal file
33
tests/lib/booru/tag-utils.spec.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { buildTagsAndAliasesMap } from "$lib/booru/tag-utils";
|
||||
|
||||
describe('buildTagsAndAliasesMap', () => {
|
||||
const exampleTag = 'safe';
|
||||
const exampleTagAlias = 'rating:safe';
|
||||
const tagsAndAliases = [exampleTag, exampleTagAlias, 'anthro', 'cat', 'feline', 'mammal', 'male', 'boy'];
|
||||
const tagsOnly = [exampleTag, 'anthro', 'cat', 'feline', 'mammal', 'male'];
|
||||
const mapping = buildTagsAndAliasesMap(tagsAndAliases, tagsOnly);
|
||||
|
||||
it('should return a map of tags', () => {
|
||||
expect(mapping).toBeInstanceOf(Map);
|
||||
});
|
||||
|
||||
it('should point aliases to their original tags', () => {
|
||||
expect(mapping.get(exampleTagAlias)).toBe(exampleTag);
|
||||
});
|
||||
|
||||
it('should point tags to themselves', () => {
|
||||
expect(mapping.get(exampleTag)).toBe(exampleTag);
|
||||
});
|
||||
|
||||
it('should ignore broken tag aliases and show a warning', () => {
|
||||
vi.spyOn(console, 'warn');
|
||||
|
||||
const brokenMapping = buildTagsAndAliasesMap(
|
||||
['broken alias', 'tag1', 'tag2'],
|
||||
['tag1', 'tag2'],
|
||||
);
|
||||
|
||||
expect(console.warn).toBeCalledTimes(1);
|
||||
expect(brokenMapping.has('broken alias')).toBe(false);
|
||||
});
|
||||
});
|
||||
43
tests/lib/utils.spec.ts
Normal file
43
tests/lib/utils.spec.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { randomString } from "$tests/utils";
|
||||
import { escapeRegExp, findDeepObject } from "$lib/utils";
|
||||
import { randomInt } from "node:crypto";
|
||||
|
||||
describe('findDeepObject', () => {
|
||||
const targetObject = {
|
||||
somewhere: {
|
||||
deep: {
|
||||
stringValue: randomString(),
|
||||
numericValue: randomInt(0, 1000),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
it('should just return null when nothing is found', () => {
|
||||
const nonExistentValue = findDeepObject(targetObject, ['completely', 'wrong']);
|
||||
expect(nonExistentValue).toBe(null);
|
||||
});
|
||||
|
||||
it('should retrieve something stored deep inside object', () => {
|
||||
const returnedDeepObject = findDeepObject(targetObject, ['somewhere', 'deep']);
|
||||
expect(returnedDeepObject).toBe(targetObject.somewhere.deep);
|
||||
});
|
||||
|
||||
it('should return null if value located on given path is not an object', () => {
|
||||
const returnedForStringValue = findDeepObject(targetObject, ['somewhere', 'deep', 'stringValue']);
|
||||
expect(returnedForStringValue).not.toBe(targetObject.somewhere.deep.stringValue);
|
||||
expect(returnedForStringValue).toBe(null);
|
||||
|
||||
const returnedForNumericValue = findDeepObject(targetObject, ['somewhere', 'deep', 'numericValue']);
|
||||
expect(returnedForNumericValue).not.toBe(targetObject.somewhere.deep.numericValue);
|
||||
expect(returnedForNumericValue).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('escapeRegExp', () => {
|
||||
const specialCharactersToMatch = "$[(?:)]{}()*./\\+?|^";
|
||||
|
||||
it('should sufficiently enough escape special characters', () => {
|
||||
const generatedRegExp = new RegExp(`^${escapeRegExp(specialCharactersToMatch)}$`, 'm');
|
||||
expect(generatedRegExp.test(specialCharactersToMatch)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user