|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + matchesKeyPattern, |
| 4 | + filterEntriesByPattern, |
| 5 | + formatDisplayValue, |
| 6 | +} from "./key-matching"; |
| 7 | + |
| 8 | +describe("matchesKeyPattern", () => { |
| 9 | + it("should match keys with prefix matching", () => { |
| 10 | + const patterns = ["api", "settings"]; |
| 11 | + |
| 12 | + expect(matchesKeyPattern("api/users", patterns)).toBe(true); |
| 13 | + expect(matchesKeyPattern("api/posts", patterns)).toBe(true); |
| 14 | + expect(matchesKeyPattern("settings/theme", patterns)).toBe(true); |
| 15 | + expect(matchesKeyPattern("other/key", patterns)).toBe(false); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should match keys with glob patterns", () => { |
| 19 | + const patterns = ["api/*/users", "settings/*"]; |
| 20 | + |
| 21 | + expect(matchesKeyPattern("api/v1/users", patterns)).toBe(true); |
| 22 | + expect(matchesKeyPattern("api/v2/users", patterns)).toBe(true); |
| 23 | + expect(matchesKeyPattern("settings/theme", patterns)).toBe(true); |
| 24 | + expect(matchesKeyPattern("settings/notifications", patterns)).toBe(true); |
| 25 | + expect(matchesKeyPattern("api/users", patterns)).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should return false for empty patterns", () => { |
| 29 | + expect(matchesKeyPattern("any/key", [])).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should handle complex glob patterns", () => { |
| 33 | + const patterns = ["steps/*/type", "learningGoals/*/goal"]; |
| 34 | + |
| 35 | + expect(matchesKeyPattern("steps/0/type", patterns)).toBe(true); |
| 36 | + expect(matchesKeyPattern("steps/1/type", patterns)).toBe(true); |
| 37 | + expect(matchesKeyPattern("learningGoals/0/goal", patterns)).toBe(true); |
| 38 | + expect(matchesKeyPattern("steps/0/name", patterns)).toBe(false); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe("filterEntriesByPattern", () => { |
| 43 | + it("should filter entries that match patterns", () => { |
| 44 | + const entries: [string, any][] = [ |
| 45 | + ["api/users", "Users API"], |
| 46 | + ["api/posts", "Posts API"], |
| 47 | + ["settings/theme", "Dark"], |
| 48 | + ["other/key", "Value"], |
| 49 | + ]; |
| 50 | + const patterns = ["api", "settings"]; |
| 51 | + |
| 52 | + const result = filterEntriesByPattern(entries, patterns); |
| 53 | + |
| 54 | + expect(result).toHaveLength(3); |
| 55 | + expect(result).toEqual([ |
| 56 | + ["api/users", "Users API"], |
| 57 | + ["api/posts", "Posts API"], |
| 58 | + ["settings/theme", "Dark"], |
| 59 | + ]); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should return empty array when no matches", () => { |
| 63 | + const entries: [string, any][] = [ |
| 64 | + ["key1", "value1"], |
| 65 | + ["key2", "value2"], |
| 66 | + ]; |
| 67 | + const patterns = ["nonexistent"]; |
| 68 | + |
| 69 | + const result = filterEntriesByPattern(entries, patterns); |
| 70 | + |
| 71 | + expect(result).toHaveLength(0); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("formatDisplayValue", () => { |
| 76 | + it("should return short strings as-is", () => { |
| 77 | + expect(formatDisplayValue("Hello")).toBe("Hello"); |
| 78 | + expect(formatDisplayValue("Short text")).toBe("Short text"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should truncate long strings", () => { |
| 82 | + const longString = "a".repeat(100); |
| 83 | + const result = formatDisplayValue(longString); |
| 84 | + |
| 85 | + expect(result).toHaveLength(53); // 50 chars + "..." |
| 86 | + expect(result.endsWith("...")).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should use custom max length", () => { |
| 90 | + const text = "Hello, World!"; |
| 91 | + const result = formatDisplayValue(text, 5); |
| 92 | + |
| 93 | + expect(result).toBe("Hello..."); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should stringify non-string values", () => { |
| 97 | + expect(formatDisplayValue(42)).toBe("42"); |
| 98 | + expect(formatDisplayValue(true)).toBe("true"); |
| 99 | + expect(formatDisplayValue({ key: "value" })).toBe('{"key":"value"}'); |
| 100 | + expect(formatDisplayValue(null)).toBe("null"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should handle arrays", () => { |
| 104 | + expect(formatDisplayValue([1, 2, 3])).toBe("[1,2,3]"); |
| 105 | + }); |
| 106 | +}); |
0 commit comments