1
0
mirror of https://github.com/koloml/furbooru-tagging-assistant.git synced 2026-02-06 23:32:58 +00:00

2 Commits

Author SHA1 Message Date
fb626a3928 Covering tag-related util function with tests 2025-06-25 19:29:07 +04:00
4060e6c44b Covering utils with tests 2025-06-25 19:13:04 +04:00
2 changed files with 76 additions and 0 deletions

View 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
View 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);
});
});