From fb626a392857c7e9070c84f7d62f009c0ee549f8 Mon Sep 17 00:00:00 2001 From: KoloMl Date: Wed, 25 Jun 2025 19:29:07 +0400 Subject: [PATCH] Covering tag-related util function with tests --- tests/lib/booru/tag-utils.spec.ts | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/lib/booru/tag-utils.spec.ts diff --git a/tests/lib/booru/tag-utils.spec.ts b/tests/lib/booru/tag-utils.spec.ts new file mode 100644 index 0000000..3dc0923 --- /dev/null +++ b/tests/lib/booru/tag-utils.spec.ts @@ -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); + }); +});