|
| 1 | +import { describe, it, expect, beforeEach } from "vitest"; |
| 2 | +import { openDatabase } from "../db/schema.js"; |
| 3 | +import { prepareStatements } from "../db/statements.js"; |
| 4 | +import { |
| 5 | + now, requireItem, buildChecklistTree, fullItem, allSummaries, summarize, |
| 6 | + wouldCycle, requireVersion, bumpVersion, CHECKLIST_MAX_DEPTH, |
| 7 | +} from "../db/queries.js"; |
| 8 | +import { NotFoundError, VersionConflictError } from "../db/errors.js"; |
| 9 | + |
| 10 | +function makeDb() { |
| 11 | + const db = openDatabase(":memory:"); |
| 12 | + const stmts = prepareStatements(db); |
| 13 | + return { db, stmts }; |
| 14 | +} |
| 15 | + |
| 16 | +function createItem(stmts, title = "Test", status = "open") { |
| 17 | + const ts = now(); |
| 18 | + const result = stmts.createItem.run(title, status, "", ts, ts); |
| 19 | + return stmts.getItem.get(result.lastInsertRowid); |
| 20 | +} |
| 21 | + |
| 22 | +describe("now()", () => { |
| 23 | + it("returns an ISO string", () => { |
| 24 | + expect(new Date(now()).toISOString()).toBe(now()); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +describe("requireItem()", () => { |
| 29 | + it("returns item when found", () => { |
| 30 | + const { stmts } = makeDb(); |
| 31 | + const item = createItem(stmts); |
| 32 | + expect(requireItem(stmts, item.id).id).toBe(item.id); |
| 33 | + }); |
| 34 | + |
| 35 | + it("throws NotFoundError when missing", () => { |
| 36 | + const { stmts } = makeDb(); |
| 37 | + expect(() => requireItem(stmts, 999)).toThrow(NotFoundError); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("requireVersion()", () => { |
| 42 | + it("passes when version matches", () => { |
| 43 | + const { stmts } = makeDb(); |
| 44 | + const item = createItem(stmts); |
| 45 | + expect(() => requireVersion(stmts, item.id, item.version)).not.toThrow(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("throws VersionConflictError when version is stale", () => { |
| 49 | + const { stmts } = makeDb(); |
| 50 | + const item = createItem(stmts); |
| 51 | + expect(() => requireVersion(stmts, item.id, 99)).toThrow(VersionConflictError); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe("bumpVersion()", () => { |
| 56 | + it("increments version", () => { |
| 57 | + const { stmts } = makeDb(); |
| 58 | + const item = createItem(stmts); |
| 59 | + bumpVersion(stmts, item.id, item.version); |
| 60 | + const updated = stmts.getItem.get(item.id); |
| 61 | + expect(updated.version).toBe(item.version + 1); |
| 62 | + }); |
| 63 | + |
| 64 | + it("throws VersionConflictError on concurrent bump", () => { |
| 65 | + const { stmts } = makeDb(); |
| 66 | + const item = createItem(stmts); |
| 67 | + bumpVersion(stmts, item.id, item.version); |
| 68 | + expect(() => bumpVersion(stmts, item.id, item.version)).toThrow(VersionConflictError); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("buildChecklistTree()", () => { |
| 73 | + it("returns empty array for item with no checklist", () => { |
| 74 | + const { stmts } = makeDb(); |
| 75 | + const item = createItem(stmts); |
| 76 | + expect(buildChecklistTree(stmts, item.id, null)).toEqual([]); |
| 77 | + }); |
| 78 | + |
| 79 | + it("throws when depth exceeds max", () => { |
| 80 | + const { stmts } = makeDb(); |
| 81 | + const item = createItem(stmts); |
| 82 | + // Insert checklist items directly at exactly max depth |
| 83 | + let parentId = null; |
| 84 | + for (let i = 0; i < CHECKLIST_MAX_DEPTH; i++) { |
| 85 | + const r = stmts.addChecklist.run(item.id, parentId, `level ${i}`, i); |
| 86 | + parentId = Number(r.lastInsertRowid); |
| 87 | + } |
| 88 | + // Reading this tree should throw |
| 89 | + expect(() => buildChecklistTree(stmts, item.id, null)).toThrow(/maximum allowed depth/); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe("fullItem()", () => { |
| 94 | + it("returns item with checklist, dependencies, comments arrays", () => { |
| 95 | + const { stmts } = makeDb(); |
| 96 | + const item = createItem(stmts); |
| 97 | + const full = fullItem(stmts, item.id); |
| 98 | + expect(full.checklist).toBeInstanceOf(Array); |
| 99 | + expect(full.dependencies).toBeInstanceOf(Array); |
| 100 | + expect(full.comments).toBeInstanceOf(Array); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe("allSummaries()", () => { |
| 105 | + it("returns all items when no filter", () => { |
| 106 | + const { stmts } = makeDb(); |
| 107 | + createItem(stmts, "A", "open"); |
| 108 | + createItem(stmts, "B", "done"); |
| 109 | + expect(allSummaries(stmts).length).toBe(2); |
| 110 | + }); |
| 111 | + |
| 112 | + it("filters by status", () => { |
| 113 | + const { stmts } = makeDb(); |
| 114 | + createItem(stmts, "A", "open"); |
| 115 | + createItem(stmts, "B", "done"); |
| 116 | + expect(allSummaries(stmts, "open").length).toBe(1); |
| 117 | + }); |
| 118 | + |
| 119 | + it("excludes archived when includeArchived is false", () => { |
| 120 | + const { stmts } = makeDb(); |
| 121 | + const item = createItem(stmts, "A", "open"); |
| 122 | + stmts.updateItem.run("A", "", "archived", now(), item.id, item.version); |
| 123 | + expect(allSummaries(stmts, null, { includeArchived: false }).length).toBe(0); |
| 124 | + expect(allSummaries(stmts, null, { includeArchived: true }).length).toBe(1); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe("wouldCycle()", () => { |
| 129 | + it("returns false when no cycle", () => { |
| 130 | + const { stmts } = makeDb(); |
| 131 | + const a = createItem(stmts, "A"); |
| 132 | + const b = createItem(stmts, "B"); |
| 133 | + expect(wouldCycle(stmts, b.id, a.id)).toBe(false); |
| 134 | + }); |
| 135 | + |
| 136 | + it("returns true for direct cycle", () => { |
| 137 | + const { stmts } = makeDb(); |
| 138 | + const a = createItem(stmts, "A"); |
| 139 | + const b = createItem(stmts, "B"); |
| 140 | + // B depends on A |
| 141 | + stmts.addDep.run(b.id, a.id); |
| 142 | + // Now check if A depending on B would cycle |
| 143 | + expect(wouldCycle(stmts, a.id, b.id)).toBe(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it("returns true for transitive cycle", () => { |
| 147 | + const { stmts } = makeDb(); |
| 148 | + const a = createItem(stmts, "A"); |
| 149 | + const b = createItem(stmts, "B"); |
| 150 | + const c = createItem(stmts, "C"); |
| 151 | + stmts.addDep.run(b.id, a.id); // B -> A |
| 152 | + stmts.addDep.run(c.id, b.id); // C -> B |
| 153 | + // A -> C would create A -> C -> B -> A |
| 154 | + expect(wouldCycle(stmts, a.id, c.id)).toBe(true); |
| 155 | + }); |
| 156 | +}); |
0 commit comments