|
| 1 | +import type { Kysely } from "kysely"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 3 | + |
| 4 | +import { ContentRepository } from "../../../src/database/repositories/content.js"; |
| 5 | +import type { Database } from "../../../src/database/types.js"; |
| 6 | +import { SchemaRegistry } from "../../../src/schema/registry.js"; |
| 7 | +import { FTSManager } from "../../../src/search/fts-manager.js"; |
| 8 | +import { searchWithDb } from "../../../src/search/query.js"; |
| 9 | +import { setupTestDatabase, teardownTestDatabase } from "../../utils/test-db.js"; |
| 10 | + |
| 11 | +describe("FTS repair", () => { |
| 12 | + let db: Kysely<Database>; |
| 13 | + let registry: SchemaRegistry; |
| 14 | + let repo: ContentRepository; |
| 15 | + let ftsManager: FTSManager; |
| 16 | + let gameId: string; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + db = await setupTestDatabase(); |
| 20 | + registry = new SchemaRegistry(db); |
| 21 | + repo = new ContentRepository(db); |
| 22 | + ftsManager = new FTSManager(db); |
| 23 | + |
| 24 | + await registry.createCollection({ |
| 25 | + slug: "game", |
| 26 | + label: "Games", |
| 27 | + labelSingular: "Game", |
| 28 | + supports: ["search"], |
| 29 | + }); |
| 30 | + await registry.createField("game", { |
| 31 | + slug: "title", |
| 32 | + label: "Title", |
| 33 | + type: "string", |
| 34 | + searchable: true, |
| 35 | + }); |
| 36 | + await registry.createField("game", { |
| 37 | + slug: "blurb", |
| 38 | + label: "Blurb", |
| 39 | + type: "text", |
| 40 | + searchable: true, |
| 41 | + }); |
| 42 | + |
| 43 | + const created = await repo.create({ |
| 44 | + type: "game", |
| 45 | + slug: "trail-of-cthulhu", |
| 46 | + status: "published", |
| 47 | + publishedAt: new Date().toISOString(), |
| 48 | + data: { |
| 49 | + title: "Trail of Cthulhu", |
| 50 | + blurb: "Investigative horror in the Cthulhu mythos.", |
| 51 | + }, |
| 52 | + }); |
| 53 | + gameId = created.id; |
| 54 | + |
| 55 | + await ftsManager.enableSearch("game"); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(async () => { |
| 59 | + await teardownTestDatabase(db); |
| 60 | + }); |
| 61 | + |
| 62 | + it("recreates a missing FTS table when search remains enabled", async () => { |
| 63 | + expect(await ftsManager.ftsTableExists("game")).toBe(true); |
| 64 | + |
| 65 | + await ftsManager.dropFtsTable("game"); |
| 66 | + |
| 67 | + expect(await ftsManager.ftsTableExists("game")).toBe(false); |
| 68 | + expect( |
| 69 | + await searchWithDb(db, "cthulhu", { |
| 70 | + collections: ["game"], |
| 71 | + status: "published", |
| 72 | + }), |
| 73 | + ).toEqual({ items: [] }); |
| 74 | + |
| 75 | + await expect(ftsManager.verifyAndRepairAll()).resolves.toBe(1); |
| 76 | + expect(await ftsManager.ftsTableExists("game")).toBe(true); |
| 77 | + |
| 78 | + const repaired = await searchWithDb(db, "cthulhu", { |
| 79 | + collections: ["game"], |
| 80 | + status: "published", |
| 81 | + }); |
| 82 | + |
| 83 | + expect(repaired.items).toHaveLength(1); |
| 84 | + expect(repaired.items[0]?.slug).toBe("trail-of-cthulhu"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("keeps the FTS index in sync after soft delete", async () => { |
| 88 | + await expect(repo.delete("game", gameId)).resolves.toBe(true); |
| 89 | + await expect(ftsManager.verifyAndRepairAll()).resolves.toBe(0); |
| 90 | + }); |
| 91 | +}); |
0 commit comments