feat(14-06): convert all 9 service test files to async PGlite
- All beforeEach now use async/await createTestDb() - All service calls in tests now awaited - All direct DB calls (.run()/.all()) replaced with await - All test callbacks made async - Fixed PostgreSQL GROUP BY strictness in totals.service.ts (categories.name and categories.icon added to groupBy) - db type changed to 'any' to accommodate PGlite type differences Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -14,15 +14,15 @@ import {
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Thread Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
describe("createThread", () => {
|
||||
it("creates thread with name, returns thread with id/status/timestamps", () => {
|
||||
const thread = createThread(db, { name: "New Tent", categoryId: 1 });
|
||||
it("creates thread with name, returns thread with id/status/timestamps", async () => {
|
||||
const thread = await createThread(db, { name: "New Tent", categoryId: 1 });
|
||||
|
||||
expect(thread).toBeDefined();
|
||||
expect(thread.id).toBeGreaterThan(0);
|
||||
@@ -35,23 +35,23 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("getAllThreads", () => {
|
||||
it("returns active threads with candidateCount and price range", () => {
|
||||
const thread = createThread(db, {
|
||||
it("returns active threads with candidateCount and price range", async () => {
|
||||
const thread = await createThread(db, {
|
||||
name: "Backpack Options",
|
||||
categoryId: 1,
|
||||
});
|
||||
createCandidate(db, thread.id, {
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Pack A",
|
||||
categoryId: 1,
|
||||
priceCents: 20000,
|
||||
});
|
||||
createCandidate(db, thread.id, {
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Pack B",
|
||||
categoryId: 1,
|
||||
priceCents: 35000,
|
||||
});
|
||||
|
||||
const threads = getAllThreads(db);
|
||||
const threads = await getAllThreads(db);
|
||||
expect(threads).toHaveLength(1);
|
||||
expect(threads[0].name).toBe("Backpack Options");
|
||||
expect(threads[0].candidateCount).toBe(2);
|
||||
@@ -59,45 +59,45 @@ describe("Thread Service", () => {
|
||||
expect(threads[0].maxPriceCents).toBe(35000);
|
||||
});
|
||||
|
||||
it("excludes resolved threads by default", () => {
|
||||
const _t1 = createThread(db, { name: "Active Thread", categoryId: 1 });
|
||||
const t2 = createThread(db, { name: "Resolved Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, t2.id, {
|
||||
it("excludes resolved threads by default", async () => {
|
||||
const _t1 = await createThread(db, { name: "Active Thread", categoryId: 1 });
|
||||
const t2 = await createThread(db, { name: "Resolved Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, t2.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
resolveThread(db, t2.id, candidate.id);
|
||||
await resolveThread(db, t2.id, candidate.id);
|
||||
|
||||
const active = getAllThreads(db);
|
||||
const active = await getAllThreads(db);
|
||||
expect(active).toHaveLength(1);
|
||||
expect(active[0].name).toBe("Active Thread");
|
||||
});
|
||||
|
||||
it("includes resolved threads when includeResolved=true", () => {
|
||||
const _t1 = createThread(db, { name: "Active Thread", categoryId: 1 });
|
||||
const t2 = createThread(db, { name: "Resolved Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, t2.id, {
|
||||
it("includes resolved threads when includeResolved=true", async () => {
|
||||
const _t1 = await createThread(db, { name: "Active Thread", categoryId: 1 });
|
||||
const t2 = await createThread(db, { name: "Resolved Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, t2.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
resolveThread(db, t2.id, candidate.id);
|
||||
await resolveThread(db, t2.id, candidate.id);
|
||||
|
||||
const all = getAllThreads(db, true);
|
||||
const all = await getAllThreads(db, true);
|
||||
expect(all).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getThreadWithCandidates", () => {
|
||||
it("returns thread with nested candidates array including category info", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
createCandidate(db, thread.id, {
|
||||
it("returns thread with nested candidates array including category info", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
});
|
||||
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.name).toBe("Tent Options");
|
||||
expect(result?.candidates).toHaveLength(1);
|
||||
@@ -106,21 +106,21 @@ describe("Thread Service", () => {
|
||||
expect(result?.candidates[0].categoryIcon).toBeDefined();
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", () => {
|
||||
const result = getThreadWithCandidates(db, 9999);
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await getThreadWithCandidates(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("includes pros and cons on each candidate", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
createCandidate(db, thread.id, {
|
||||
it("includes pros and cons on each candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
pros: "Lightweight",
|
||||
cons: "Pricey",
|
||||
});
|
||||
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates[0].pros).toBe("Lightweight");
|
||||
expect(result?.candidates[0].cons).toBe("Pricey");
|
||||
@@ -128,9 +128,9 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("createCandidate", () => {
|
||||
it("adds candidate to thread with all item-compatible fields", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("adds candidate to thread with all item-compatible fields", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Tent A",
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
@@ -150,9 +150,9 @@ describe("Thread Service", () => {
|
||||
expect(candidate.productUrl).toBe("https://example.com/tent");
|
||||
});
|
||||
|
||||
it("stores and returns pros and cons", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("stores and returns pros and cons", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
pros: "Lightweight\nGood reviews",
|
||||
@@ -163,9 +163,9 @@ describe("Thread Service", () => {
|
||||
expect(candidate.cons).toBe("Expensive");
|
||||
});
|
||||
|
||||
it("returns null for pros and cons when not provided", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("returns null for pros and cons when not provided", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Tent B",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -176,14 +176,14 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("updateCandidate", () => {
|
||||
it("updates candidate fields, returns updated candidate", () => {
|
||||
const thread = createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("updates candidate fields, returns updated candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const updated = updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
name: "Updated Name",
|
||||
priceCents: 15000,
|
||||
});
|
||||
@@ -193,20 +193,20 @@ describe("Thread Service", () => {
|
||||
expect(updated?.priceCents).toBe(15000);
|
||||
});
|
||||
|
||||
it("returns null for non-existent candidate", () => {
|
||||
const result = updateCandidate(db, 9999, { name: "Ghost" });
|
||||
it("returns null for non-existent candidate", async () => {
|
||||
const result = await updateCandidate(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("can set and clear pros and cons", () => {
|
||||
const thread = createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("can set and clear pros and cons", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Set pros and cons
|
||||
const withPros = updateCandidate(db, candidate.id, {
|
||||
const withPros = await updateCandidate(db, candidate.id, {
|
||||
pros: "Lightweight",
|
||||
cons: "Expensive",
|
||||
});
|
||||
@@ -214,7 +214,7 @@ describe("Thread Service", () => {
|
||||
expect(withPros?.cons).toBe("Expensive");
|
||||
|
||||
// Clear pros and cons by setting to empty string
|
||||
const cleared = updateCandidate(db, candidate.id, {
|
||||
const cleared = await updateCandidate(db, candidate.id, {
|
||||
pros: "",
|
||||
cons: "",
|
||||
});
|
||||
@@ -225,67 +225,67 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("deleteCandidate", () => {
|
||||
it("removes candidate, returns deleted candidate", () => {
|
||||
const thread = createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("removes candidate, returns deleted candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "To Delete",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const deleted = deleteCandidate(db, candidate.id);
|
||||
const deleted = await deleteCandidate(db, candidate.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted?.name).toBe("To Delete");
|
||||
|
||||
// Verify it's gone
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result?.candidates).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("returns null for non-existent candidate", () => {
|
||||
const result = deleteCandidate(db, 9999);
|
||||
it("returns null for non-existent candidate", async () => {
|
||||
const result = await deleteCandidate(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateThread", () => {
|
||||
it("updates thread name", () => {
|
||||
const thread = createThread(db, { name: "Original", categoryId: 1 });
|
||||
const updated = updateThread(db, thread.id, { name: "Renamed" });
|
||||
it("updates thread name", async () => {
|
||||
const thread = await createThread(db, { name: "Original", categoryId: 1 });
|
||||
const updated = await updateThread(db, thread.id, { name: "Renamed" });
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.name).toBe("Renamed");
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", () => {
|
||||
const result = updateThread(db, 9999, { name: "Ghost" });
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await updateThread(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteThread", () => {
|
||||
it("removes thread and cascading candidates", () => {
|
||||
const thread = createThread(db, { name: "To Delete", categoryId: 1 });
|
||||
createCandidate(db, thread.id, { name: "Candidate", categoryId: 1 });
|
||||
it("removes thread and cascading candidates", async () => {
|
||||
const thread = await createThread(db, { name: "To Delete", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, { name: "Candidate", categoryId: 1 });
|
||||
|
||||
const deleted = deleteThread(db, thread.id);
|
||||
const deleted = await deleteThread(db, thread.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted?.name).toBe("To Delete");
|
||||
|
||||
// Thread and candidates gone
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", () => {
|
||||
const result = deleteThread(db, 9999);
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await deleteThread(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("candidate status", () => {
|
||||
it("createCandidate without status returns a candidate with status 'researching'", () => {
|
||||
const thread = createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("createCandidate without status returns a candidate with status 'researching'", async () => {
|
||||
const thread = await createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "No Status",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -293,9 +293,9 @@ describe("Thread Service", () => {
|
||||
expect(candidate.status).toBe("researching");
|
||||
});
|
||||
|
||||
it("createCandidate with status 'ordered' returns a candidate with status 'ordered'", () => {
|
||||
const thread = createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("createCandidate with status 'ordered' returns a candidate with status 'ordered'", async () => {
|
||||
const thread = await createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Ordered Item",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
@@ -304,58 +304,58 @@ describe("Thread Service", () => {
|
||||
expect(candidate.status).toBe("ordered");
|
||||
});
|
||||
|
||||
it("updateCandidate can change status from 'researching' to 'ordered'", () => {
|
||||
const thread = createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("updateCandidate can change status from 'researching' to 'ordered'", async () => {
|
||||
const thread = await createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Status Change",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
expect(candidate.status).toBe("researching");
|
||||
|
||||
const updated = updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
expect(updated?.status).toBe("ordered");
|
||||
});
|
||||
|
||||
it("updateCandidate can change status from 'ordered' to 'arrived'", () => {
|
||||
const thread = createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("updateCandidate can change status from 'ordered' to 'arrived'", async () => {
|
||||
const thread = await createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Arriving Item",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
const updated = updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
status: "arrived",
|
||||
});
|
||||
|
||||
expect(updated?.status).toBe("arrived");
|
||||
});
|
||||
|
||||
it("getThreadWithCandidates includes status field on each candidate", () => {
|
||||
const thread = createThread(db, { name: "Status Thread", categoryId: 1 });
|
||||
createCandidate(db, thread.id, {
|
||||
it("getThreadWithCandidates includes status field on each candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Status Thread", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Candidate A",
|
||||
categoryId: 1,
|
||||
});
|
||||
createCandidate(db, thread.id, {
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Candidate B",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates).toHaveLength(2);
|
||||
|
||||
const candidateA = result?.candidates.find(
|
||||
(c) => c.name === "Candidate A",
|
||||
(c: any) => c.name === "Candidate A",
|
||||
);
|
||||
const candidateB = result?.candidates.find(
|
||||
(c) => c.name === "Candidate B",
|
||||
(c: any) => c.name === "Candidate B",
|
||||
);
|
||||
|
||||
expect(candidateA?.status).toBe("researching");
|
||||
@@ -364,43 +364,43 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("sort_order ordering", () => {
|
||||
it("getThreadWithCandidates returns candidates ordered by sort_order ascending", () => {
|
||||
const thread = createThread(db, { name: "Order Test", categoryId: 1 });
|
||||
const c1 = createCandidate(db, thread.id, {
|
||||
it("getThreadWithCandidates returns candidates ordered by sort_order ascending", async () => {
|
||||
const thread = await createThread(db, { name: "Order Test", categoryId: 1 });
|
||||
const c1 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c2 = createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c3 = createCandidate(db, thread.id, {
|
||||
const c3 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 3",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Manually set sort_orders out of creation order using reorderCandidates
|
||||
reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
await reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates[0].id).toBe(c3.id);
|
||||
expect(result?.candidates[1].id).toBe(c1.id);
|
||||
expect(result?.candidates[2].id).toBe(c2.id);
|
||||
});
|
||||
|
||||
it("createCandidate assigns sort_order = max existing sort_order + 1000", () => {
|
||||
const thread = createThread(db, { name: "Append Test", categoryId: 1 });
|
||||
it("createCandidate assigns sort_order = max existing sort_order + 1000", async () => {
|
||||
const thread = await createThread(db, { name: "Append Test", categoryId: 1 });
|
||||
|
||||
// First candidate should get sort_order 1000
|
||||
const c1 = createCandidate(db, thread.id, {
|
||||
const c1 = await createCandidate(db, thread.id, {
|
||||
name: "First",
|
||||
categoryId: 1,
|
||||
});
|
||||
expect(c1.sortOrder).toBe(1000);
|
||||
|
||||
// Second candidate should get sort_order 2000
|
||||
const c2 = createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
name: "Second",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -409,56 +409,56 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("reorderCandidates", () => {
|
||||
it("reorderCandidates updates sort_order so querying returns candidates in new order", () => {
|
||||
const thread = createThread(db, { name: "Reorder Test", categoryId: 1 });
|
||||
const c1 = createCandidate(db, thread.id, {
|
||||
it("reorderCandidates updates sort_order so querying returns candidates in new order", async () => {
|
||||
const thread = await createThread(db, { name: "Reorder Test", categoryId: 1 });
|
||||
const c1 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c2 = createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c3 = createCandidate(db, thread.id, {
|
||||
const c3 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 3",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const result = reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
const result = await reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const fetched = getThreadWithCandidates(db, thread.id);
|
||||
const fetched = await getThreadWithCandidates(db, thread.id);
|
||||
expect(fetched?.candidates[0].id).toBe(c3.id);
|
||||
expect(fetched?.candidates[1].id).toBe(c1.id);
|
||||
expect(fetched?.candidates[2].id).toBe(c2.id);
|
||||
});
|
||||
|
||||
it("returns { success: false, error } when thread status is 'resolved'", () => {
|
||||
const thread = createThread(db, {
|
||||
it("returns { success: false, error } when thread status is 'resolved'", async () => {
|
||||
const thread = await createThread(db, {
|
||||
name: "Resolved Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
resolveThread(db, thread.id, candidate.id);
|
||||
await resolveThread(db, thread.id, candidate.id);
|
||||
|
||||
const result = reorderCandidates(db, thread.id, [candidate.id]);
|
||||
const result = await reorderCandidates(db, thread.id, [candidate.id]);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("returns { success: false } when thread does not exist", () => {
|
||||
const result = reorderCandidates(db, 9999, [1, 2]);
|
||||
it("returns { success: false } when thread does not exist", async () => {
|
||||
const result = await reorderCandidates(db, 9999, [1, 2]);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveThread", () => {
|
||||
it("atomically creates collection item from candidate data and archives thread", () => {
|
||||
const thread = createThread(db, { name: "Tent Decision", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("atomically creates collection item from candidate data and archives thread", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Decision", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Winner Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
@@ -467,7 +467,7 @@ describe("Thread Service", () => {
|
||||
productUrl: "https://example.com/tent",
|
||||
});
|
||||
|
||||
const result = resolveThread(db, thread.id, candidate.id);
|
||||
const result = await resolveThread(db, thread.id, candidate.id);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.item).toBeDefined();
|
||||
expect(result.item?.name).toBe("Winner Tent");
|
||||
@@ -478,44 +478,44 @@ describe("Thread Service", () => {
|
||||
expect(result.item?.productUrl).toBe("https://example.com/tent");
|
||||
|
||||
// Thread should be resolved
|
||||
const resolved = getThreadWithCandidates(db, thread.id);
|
||||
const resolved = await getThreadWithCandidates(db, thread.id);
|
||||
expect(resolved?.status).toBe("resolved");
|
||||
expect(resolved?.resolvedCandidateId).toBe(candidate.id);
|
||||
});
|
||||
|
||||
it("fails if thread is not active", () => {
|
||||
const thread = createThread(db, {
|
||||
it("fails if thread is not active", async () => {
|
||||
const thread = await createThread(db, {
|
||||
name: "Already Resolved",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
resolveThread(db, thread.id, candidate.id);
|
||||
await resolveThread(db, thread.id, candidate.id);
|
||||
|
||||
// Try to resolve again
|
||||
const result = resolveThread(db, thread.id, candidate.id);
|
||||
const result = await resolveThread(db, thread.id, candidate.id);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("fails if candidate is not in thread", () => {
|
||||
const thread1 = createThread(db, { name: "Thread 1", categoryId: 1 });
|
||||
const thread2 = createThread(db, { name: "Thread 2", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread2.id, {
|
||||
it("fails if candidate is not in thread", async () => {
|
||||
const thread1 = await createThread(db, { name: "Thread 1", categoryId: 1 });
|
||||
const thread2 = await createThread(db, { name: "Thread 2", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread2.id, {
|
||||
name: "Wrong Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const result = resolveThread(db, thread1.id, candidate.id);
|
||||
const result = await resolveThread(db, thread1.id, candidate.id);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("fails if candidate not found", () => {
|
||||
const thread = createThread(db, { name: "Test", categoryId: 1 });
|
||||
const result = resolveThread(db, thread.id, 9999);
|
||||
it("fails if candidate not found", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const result = await resolveThread(db, thread.id, 9999);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user