|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { splitSearchClassTerms } from "./helper"; |
| 3 | +import { JSON_SCHEMA } from "./types"; |
| 4 | + |
| 5 | +describe("splitSearchClassTerms", () => { |
| 6 | + // Setup mock language category mapping |
| 7 | + const mockLangCategoryMap = { |
| 8 | + org: "org", |
| 9 | + 組織: "org", |
| 10 | + from: "from", |
| 11 | + から: "from", |
| 12 | + to: "to", |
| 13 | + まで: "to", |
| 14 | + search: "search", |
| 15 | + 検索: "search", |
| 16 | + description: "description", |
| 17 | + 説明: "description", |
| 18 | + type: "type", |
| 19 | + タイプ: "type", |
| 20 | + topic: "topic", |
| 21 | + トピック: "topic", |
| 22 | + vtuber: "vtuber", |
| 23 | + lang: "lang", |
| 24 | + 言語: "lang", |
| 25 | + has_song: "has_song", |
| 26 | + 歌あり: "has_song", |
| 27 | + } as Record<string, keyof typeof JSON_SCHEMA>; |
| 28 | + |
| 29 | + it("should correctly split valid category and search term", () => { |
| 30 | + const result = splitSearchClassTerms("org:hololive", mockLangCategoryMap); |
| 31 | + expect(result).toEqual(["org", "hololive"]); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should handle Japanese colons and categories", () => { |
| 35 | + const result = splitSearchClassTerms( |
| 36 | + "組織:ホロライブ", |
| 37 | + mockLangCategoryMap, |
| 38 | + ); |
| 39 | + expect(result).toEqual(["org", "ホロライブ"]); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should correctly handle date categories", () => { |
| 43 | + const result = splitSearchClassTerms( |
| 44 | + "from:2024-01-01", |
| 45 | + mockLangCategoryMap, |
| 46 | + ); |
| 47 | + expect(result).toEqual(["from", "2024-01-01"]); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should handle Japanese date categories", () => { |
| 51 | + const result = splitSearchClassTerms( |
| 52 | + "から:2024-01-01", |
| 53 | + mockLangCategoryMap, |
| 54 | + ); |
| 55 | + expect(result).toEqual(["from", "2024-01-01"]); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should return undefined category for invalid category prefix", () => { |
| 59 | + const result = splitSearchClassTerms("invalid:term", mockLangCategoryMap); |
| 60 | + expect(result).toEqual([undefined, "invalid:term"]); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should handle search terms without category prefix", () => { |
| 64 | + const result = splitSearchClassTerms("searchterm", mockLangCategoryMap); |
| 65 | + expect(result).toEqual([undefined, "searchterm"]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should handle empty search value after category", () => { |
| 69 | + const result = splitSearchClassTerms("org:", mockLangCategoryMap); |
| 70 | + expect(result).toEqual(["org", ""]); |
| 71 | + const result2 = splitSearchClassTerms("org", mockLangCategoryMap); |
| 72 | + expect(result2).toEqual(["org", ""]); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should handle whitespace around category and value", () => { |
| 76 | + const result = splitSearchClassTerms( |
| 77 | + " type : stream ", |
| 78 | + mockLangCategoryMap, |
| 79 | + ); |
| 80 | + expect(result).toEqual(["type", "stream"]); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should handle array type categories", () => { |
| 84 | + const result = splitSearchClassTerms("topic:music", mockLangCategoryMap); |
| 85 | + expect(result).toEqual(["topic", "music"]); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should handle string type categories", () => { |
| 89 | + const result = splitSearchClassTerms( |
| 90 | + "description:test content", |
| 91 | + mockLangCategoryMap, |
| 92 | + ); |
| 93 | + expect(result).toEqual(["description", "test content"]); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should preserve colons in search term when no valid category", () => { |
| 97 | + const result = splitSearchClassTerms( |
| 98 | + "something:else:here", |
| 99 | + mockLangCategoryMap, |
| 100 | + ); |
| 101 | + expect(result).toEqual([undefined, "something:else:here"]); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should handle empty string input", () => { |
| 105 | + const result = splitSearchClassTerms("", mockLangCategoryMap); |
| 106 | + expect(result).toEqual([undefined, ""]); |
| 107 | + }); |
| 108 | +}); |
0 commit comments