|
1 | 1 | /** |
2 | | - * Unit tests for LocalFileStorage — starter scars timestamp fix |
| 2 | + * Unit tests for LocalFileStorage |
3 | 3 | * |
4 | | - * Verifies that loadStarterScars stamps created_at to install time |
5 | | - * instead of using hardcoded dates from starter-scars.json. |
| 4 | + * Covers: loadStarterScars, keywordSearch learning_type propagation, |
| 5 | + * list() is_active filter handling, and learning_type filtering. |
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
@@ -82,3 +82,157 @@ describe("LocalFileStorage.loadStarterScars", () => { |
82 | 82 | expect(learnings).toHaveLength(2); |
83 | 83 | }); |
84 | 84 | }); |
| 85 | + |
| 86 | +describe("LocalFileStorage.keywordSearch — learning_type propagation", () => { |
| 87 | + let tmpDir: string; |
| 88 | + let storage: LocalFileStorage; |
| 89 | + |
| 90 | + beforeEach(() => { |
| 91 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "gitmem-test-")); |
| 92 | + storage = new LocalFileStorage(tmpDir); |
| 93 | + |
| 94 | + // Seed learnings with different types |
| 95 | + const learnings = [ |
| 96 | + { |
| 97 | + id: "scar-1", |
| 98 | + title: "Database migration rollback", |
| 99 | + description: "Always test rollback before migrating", |
| 100 | + learning_type: "scar", |
| 101 | + severity: "high", |
| 102 | + keywords: ["database", "migration"], |
| 103 | + is_active: true, |
| 104 | + }, |
| 105 | + { |
| 106 | + id: "pattern-1", |
| 107 | + title: "Database connection pooling pattern", |
| 108 | + description: "Use connection pooling for database access", |
| 109 | + learning_type: "pattern", |
| 110 | + severity: "low", |
| 111 | + keywords: ["database", "connection"], |
| 112 | + is_active: true, |
| 113 | + }, |
| 114 | + { |
| 115 | + id: "win-1", |
| 116 | + title: "Database query optimization win", |
| 117 | + description: "Indexing improved database query speed 10x", |
| 118 | + learning_type: "win", |
| 119 | + severity: "medium", |
| 120 | + keywords: ["database", "optimization"], |
| 121 | + is_active: true, |
| 122 | + }, |
| 123 | + ]; |
| 124 | + const filePath = path.join(tmpDir, "learnings.json"); |
| 125 | + fs.writeFileSync(filePath, JSON.stringify(learnings)); |
| 126 | + }); |
| 127 | + |
| 128 | + afterEach(() => { |
| 129 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 130 | + }); |
| 131 | + |
| 132 | + it("returns learning_type for each result", async () => { |
| 133 | + const results = await storage.keywordSearch("database", 10); |
| 134 | + expect(results.length).toBeGreaterThanOrEqual(3); |
| 135 | + |
| 136 | + const types = results.map((r) => r.learning_type); |
| 137 | + expect(types).toContain("scar"); |
| 138 | + expect(types).toContain("pattern"); |
| 139 | + expect(types).toContain("win"); |
| 140 | + }); |
| 141 | + |
| 142 | + it("does not default all results to scar", async () => { |
| 143 | + const results = await storage.keywordSearch("database", 10); |
| 144 | + const nonScars = results.filter((r) => r.learning_type !== "scar"); |
| 145 | + expect(nonScars.length).toBeGreaterThanOrEqual(2); |
| 146 | + }); |
| 147 | + |
| 148 | + it("preserves learning_type for patterns", async () => { |
| 149 | + const results = await storage.keywordSearch("connection pooling", 5); |
| 150 | + const pattern = results.find((r) => r.id === "pattern-1"); |
| 151 | + expect(pattern).toBeDefined(); |
| 152 | + expect(pattern!.learning_type).toBe("pattern"); |
| 153 | + }); |
| 154 | + |
| 155 | + it("preserves learning_type for wins", async () => { |
| 156 | + const results = await storage.keywordSearch("optimization", 5); |
| 157 | + const win = results.find((r) => r.id === "win-1"); |
| 158 | + expect(win).toBeDefined(); |
| 159 | + expect(win!.learning_type).toBe("win"); |
| 160 | + }); |
| 161 | +}); |
| 162 | + |
| 163 | +describe("LocalFileStorage.list — is_active filter", () => { |
| 164 | + let tmpDir: string; |
| 165 | + let storage: LocalFileStorage; |
| 166 | + |
| 167 | + beforeEach(() => { |
| 168 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "gitmem-test-")); |
| 169 | + storage = new LocalFileStorage(tmpDir); |
| 170 | + |
| 171 | + // Seed learnings: some with is_active, some without (should default to active) |
| 172 | + const learnings = [ |
| 173 | + { |
| 174 | + id: "active-explicit", |
| 175 | + title: "Explicitly active", |
| 176 | + learning_type: "scar", |
| 177 | + severity: "high", |
| 178 | + is_active: true, |
| 179 | + created_at: "2026-02-22T00:00:00Z", |
| 180 | + }, |
| 181 | + { |
| 182 | + id: "active-implicit", |
| 183 | + title: "Implicitly active (no is_active field)", |
| 184 | + learning_type: "pattern", |
| 185 | + severity: "low", |
| 186 | + created_at: "2026-02-21T00:00:00Z", |
| 187 | + // NOTE: no is_active field — should be treated as active |
| 188 | + }, |
| 189 | + { |
| 190 | + id: "archived", |
| 191 | + title: "Archived learning", |
| 192 | + learning_type: "scar", |
| 193 | + severity: "medium", |
| 194 | + is_active: false, |
| 195 | + created_at: "2026-02-20T00:00:00Z", |
| 196 | + }, |
| 197 | + ]; |
| 198 | + const filePath = path.join(tmpDir, "learnings.json"); |
| 199 | + fs.writeFileSync(filePath, JSON.stringify(learnings)); |
| 200 | + }); |
| 201 | + |
| 202 | + afterEach(() => { |
| 203 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 204 | + }); |
| 205 | + |
| 206 | + it("includes records without is_active when filtering for active", async () => { |
| 207 | + const results = await storage.list("learnings", { |
| 208 | + filters: { is_active: "eq.true" }, |
| 209 | + }); |
| 210 | + const ids = results.map((r) => r.id); |
| 211 | + expect(ids).toContain("active-explicit"); |
| 212 | + expect(ids).toContain("active-implicit"); |
| 213 | + expect(ids).not.toContain("archived"); |
| 214 | + }); |
| 215 | + |
| 216 | + it("excludes archived records", async () => { |
| 217 | + const results = await storage.list("learnings", { |
| 218 | + filters: { is_active: "eq.true" }, |
| 219 | + }); |
| 220 | + expect(results).toHaveLength(2); |
| 221 | + }); |
| 222 | + |
| 223 | + it("filters by learning_type correctly", async () => { |
| 224 | + const results = await storage.list("learnings", { |
| 225 | + filters: { learning_type: "pattern" }, |
| 226 | + }); |
| 227 | + expect(results).toHaveLength(1); |
| 228 | + expect(results[0].id).toBe("active-implicit"); |
| 229 | + }); |
| 230 | + |
| 231 | + it("combines is_active and learning_type filters", async () => { |
| 232 | + const results = await storage.list("learnings", { |
| 233 | + filters: { is_active: "eq.true", learning_type: "scar" }, |
| 234 | + }); |
| 235 | + expect(results).toHaveLength(1); |
| 236 | + expect(results[0].id).toBe("active-explicit"); |
| 237 | + }); |
| 238 | +}); |
0 commit comments