feat(16-04): update all service tests to pass userId and add isolation tests
- Destructure { db, userId } from createTestDb() in all 8 service test files
- Pass userId to every service function call
- Add cross-user isolation tests for items, categories, threads, setups
- Add composite unique constraint test for categories
- Update verifyApiKey assertions to check { userId } return
- Update verifyAccessToken assertions to check { userId } return
- Pass userId to exchangeCode and refreshAccessToken calls
This commit is contained in:
@@ -11,18 +11,22 @@ import {
|
||||
updateCandidate,
|
||||
updateThread,
|
||||
} from "../../src/server/services/thread.service.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { createSecondTestUser, createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Thread Service", () => {
|
||||
let db: any;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
({ db, userId } = await createTestDb());
|
||||
});
|
||||
|
||||
describe("createThread", () => {
|
||||
it("creates thread with name, returns thread with id/status/timestamps", async () => {
|
||||
const thread = await createThread(db, { name: "New Tent", categoryId: 1 });
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "New Tent",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
expect(thread).toBeDefined();
|
||||
expect(thread.id).toBeGreaterThan(0);
|
||||
@@ -36,22 +40,22 @@ describe("Thread Service", () => {
|
||||
|
||||
describe("getAllThreads", () => {
|
||||
it("returns active threads with candidateCount and price range", async () => {
|
||||
const thread = await createThread(db, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Backpack Options",
|
||||
categoryId: 1,
|
||||
});
|
||||
await createCandidate(db, thread.id, {
|
||||
await createCandidate(db, userId, thread.id, {
|
||||
name: "Pack A",
|
||||
categoryId: 1,
|
||||
priceCents: 20000,
|
||||
});
|
||||
await createCandidate(db, thread.id, {
|
||||
await createCandidate(db, userId, thread.id, {
|
||||
name: "Pack B",
|
||||
categoryId: 1,
|
||||
priceCents: 35000,
|
||||
});
|
||||
|
||||
const threads = await getAllThreads(db);
|
||||
const threads = await getAllThreads(db, userId);
|
||||
expect(threads).toHaveLength(1);
|
||||
expect(threads[0].name).toBe("Backpack Options");
|
||||
expect(threads[0].candidateCount).toBe(2);
|
||||
@@ -60,44 +64,59 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
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, {
|
||||
const _t1 = await createThread(db, userId, {
|
||||
name: "Active Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const t2 = await createThread(db, userId, {
|
||||
name: "Resolved Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, t2.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
await resolveThread(db, t2.id, candidate.id);
|
||||
await resolveThread(db, userId, t2.id, candidate.id);
|
||||
|
||||
const active = await getAllThreads(db);
|
||||
const active = await getAllThreads(db, userId);
|
||||
expect(active).toHaveLength(1);
|
||||
expect(active[0].name).toBe("Active Thread");
|
||||
});
|
||||
|
||||
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, {
|
||||
const _t1 = await createThread(db, userId, {
|
||||
name: "Active Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const t2 = await createThread(db, userId, {
|
||||
name: "Resolved Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, t2.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
await resolveThread(db, t2.id, candidate.id);
|
||||
await resolveThread(db, userId, t2.id, candidate.id);
|
||||
|
||||
const all = await getAllThreads(db, true);
|
||||
const all = await getAllThreads(db, userId, true);
|
||||
expect(all).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getThreadWithCandidates", () => {
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Tent Options",
|
||||
categoryId: 1,
|
||||
});
|
||||
await createCandidate(db, userId, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
});
|
||||
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, userId, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.name).toBe("Tent Options");
|
||||
expect(result?.candidates).toHaveLength(1);
|
||||
@@ -107,20 +126,23 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await getThreadWithCandidates(db, 9999);
|
||||
const result = await getThreadWithCandidates(db, userId, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("includes pros and cons on each candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Tent Options",
|
||||
categoryId: 1,
|
||||
});
|
||||
await createCandidate(db, userId, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
pros: "Lightweight",
|
||||
cons: "Pricey",
|
||||
});
|
||||
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, userId, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates[0].pros).toBe("Lightweight");
|
||||
expect(result?.candidates[0].cons).toBe("Pricey");
|
||||
@@ -129,8 +151,11 @@ describe("Thread Service", () => {
|
||||
|
||||
describe("createCandidate", () => {
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Tent Options",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Tent A",
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
@@ -151,8 +176,11 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Tent Options",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
pros: "Lightweight\nGood reviews",
|
||||
@@ -164,8 +192,11 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Tent Options",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Tent B",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -177,13 +208,16 @@ describe("Thread Service", () => {
|
||||
|
||||
describe("updateCandidate", () => {
|
||||
it("updates candidate fields, returns updated candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Test",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, userId, candidate.id, {
|
||||
name: "Updated Name",
|
||||
priceCents: 15000,
|
||||
});
|
||||
@@ -194,19 +228,24 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
it("returns null for non-existent candidate", async () => {
|
||||
const result = await updateCandidate(db, 9999, { name: "Ghost" });
|
||||
const result = await updateCandidate(db, userId, 9999, {
|
||||
name: "Ghost",
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Test",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Set pros and cons
|
||||
const withPros = await updateCandidate(db, candidate.id, {
|
||||
const withPros = await updateCandidate(db, userId, candidate.id, {
|
||||
pros: "Lightweight",
|
||||
cons: "Expensive",
|
||||
});
|
||||
@@ -214,7 +253,7 @@ describe("Thread Service", () => {
|
||||
expect(withPros?.cons).toBe("Expensive");
|
||||
|
||||
// Clear pros and cons by setting to empty string
|
||||
const cleared = await updateCandidate(db, candidate.id, {
|
||||
const cleared = await updateCandidate(db, userId, candidate.id, {
|
||||
pros: "",
|
||||
cons: "",
|
||||
});
|
||||
@@ -226,66 +265,83 @@ describe("Thread Service", () => {
|
||||
|
||||
describe("deleteCandidate", () => {
|
||||
it("removes candidate, returns deleted candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Test",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "To Delete",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const deleted = await deleteCandidate(db, candidate.id);
|
||||
const deleted = await deleteCandidate(db, userId, candidate.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted?.name).toBe("To Delete");
|
||||
|
||||
// Verify it's gone
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, userId, thread.id);
|
||||
expect(result?.candidates).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("returns null for non-existent candidate", async () => {
|
||||
const result = await deleteCandidate(db, 9999);
|
||||
const result = await deleteCandidate(db, userId, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateThread", () => {
|
||||
it("updates thread name", async () => {
|
||||
const thread = await createThread(db, { name: "Original", categoryId: 1 });
|
||||
const updated = await updateThread(db, thread.id, { name: "Renamed" });
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
const updated = await updateThread(db, userId, thread.id, {
|
||||
name: "Renamed",
|
||||
});
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.name).toBe("Renamed");
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await updateThread(db, 9999, { name: "Ghost" });
|
||||
const result = await updateThread(db, userId, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteThread", () => {
|
||||
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 thread = await createThread(db, userId, {
|
||||
name: "To Delete",
|
||||
categoryId: 1,
|
||||
});
|
||||
await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const deleted = await deleteThread(db, thread.id);
|
||||
const deleted = await deleteThread(db, userId, thread.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted?.name).toBe("To Delete");
|
||||
|
||||
// Thread and candidates gone
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, userId, thread.id);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await deleteThread(db, 9999);
|
||||
const result = await deleteThread(db, userId, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("candidate status", () => {
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Test Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "No Status",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -294,8 +350,11 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Test Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Ordered Item",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
@@ -305,15 +364,18 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Test Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Status Change",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
expect(candidate.status).toBe("researching");
|
||||
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, userId, candidate.id, {
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
@@ -321,14 +383,17 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Test Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Arriving Item",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, userId, candidate.id, {
|
||||
status: "arrived",
|
||||
});
|
||||
|
||||
@@ -336,18 +401,21 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
it("getThreadWithCandidates includes status field on each candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Status Thread", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Status Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate A",
|
||||
categoryId: 1,
|
||||
});
|
||||
await createCandidate(db, thread.id, {
|
||||
await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate B",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, userId, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates).toHaveLength(2);
|
||||
|
||||
@@ -365,24 +433,27 @@ describe("Thread Service", () => {
|
||||
|
||||
describe("sort_order ordering", () => {
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Order Test",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c1 = await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c3 = await createCandidate(db, thread.id, {
|
||||
const c3 = await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate 3",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Manually set sort_orders out of creation order using reorderCandidates
|
||||
await reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
await reorderCandidates(db, userId, thread.id, [c3.id, c1.id, c2.id]);
|
||||
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, userId, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates[0].id).toBe(c3.id);
|
||||
expect(result?.candidates[1].id).toBe(c1.id);
|
||||
@@ -390,17 +461,20 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
it("createCandidate assigns sort_order = max existing sort_order + 1000", async () => {
|
||||
const thread = await createThread(db, { name: "Append Test", categoryId: 1 });
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Append Test",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// First candidate should get sort_order 1000
|
||||
const c1 = await createCandidate(db, thread.id, {
|
||||
const c1 = await createCandidate(db, userId, thread.id, {
|
||||
name: "First",
|
||||
categoryId: 1,
|
||||
});
|
||||
expect(c1.sortOrder).toBe(1000);
|
||||
|
||||
// Second candidate should get sort_order 2000
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, userId, thread.id, {
|
||||
name: "Second",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -410,55 +484,67 @@ describe("Thread Service", () => {
|
||||
|
||||
describe("reorderCandidates", () => {
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Reorder Test",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c1 = await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c3 = await createCandidate(db, thread.id, {
|
||||
const c3 = await createCandidate(db, userId, thread.id, {
|
||||
name: "Candidate 3",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const result = await reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
const result = await reorderCandidates(db, userId, thread.id, [
|
||||
c3.id,
|
||||
c1.id,
|
||||
c2.id,
|
||||
]);
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const fetched = await getThreadWithCandidates(db, thread.id);
|
||||
const fetched = await getThreadWithCandidates(db, userId, 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'", async () => {
|
||||
const thread = await createThread(db, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Resolved Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
await resolveThread(db, thread.id, candidate.id);
|
||||
await resolveThread(db, userId, thread.id, candidate.id);
|
||||
|
||||
const result = await reorderCandidates(db, thread.id, [candidate.id]);
|
||||
const result = await reorderCandidates(db, userId, thread.id, [
|
||||
candidate.id,
|
||||
]);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("returns { success: false } when thread does not exist", async () => {
|
||||
const result = await reorderCandidates(db, 9999, [1, 2]);
|
||||
const result = await reorderCandidates(db, userId, 9999, [1, 2]);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveThread", () => {
|
||||
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, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Tent Decision",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Winner Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
@@ -467,7 +553,7 @@ describe("Thread Service", () => {
|
||||
productUrl: "https://example.com/tent",
|
||||
});
|
||||
|
||||
const result = await resolveThread(db, thread.id, candidate.id);
|
||||
const result = await resolveThread(db, userId, thread.id, candidate.id);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.item).toBeDefined();
|
||||
expect(result.item?.name).toBe("Winner Tent");
|
||||
@@ -478,46 +564,96 @@ describe("Thread Service", () => {
|
||||
expect(result.item?.productUrl).toBe("https://example.com/tent");
|
||||
|
||||
// Thread should be resolved
|
||||
const resolved = await getThreadWithCandidates(db, thread.id);
|
||||
const resolved = await getThreadWithCandidates(
|
||||
db,
|
||||
userId,
|
||||
thread.id,
|
||||
);
|
||||
expect(resolved?.status).toBe("resolved");
|
||||
expect(resolved?.resolvedCandidateId).toBe(candidate.id);
|
||||
});
|
||||
|
||||
it("fails if thread is not active", async () => {
|
||||
const thread = await createThread(db, {
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Already Resolved",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
const candidate = await createCandidate(db, userId, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
await resolveThread(db, thread.id, candidate.id);
|
||||
await resolveThread(db, userId, thread.id, candidate.id);
|
||||
|
||||
// Try to resolve again
|
||||
const result = await resolveThread(db, thread.id, candidate.id);
|
||||
const result = await resolveThread(
|
||||
db,
|
||||
userId,
|
||||
thread.id,
|
||||
candidate.id,
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
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, {
|
||||
const thread1 = await createThread(db, userId, {
|
||||
name: "Thread 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const thread2 = await createThread(db, userId, {
|
||||
name: "Thread 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = await createCandidate(db, userId, thread2.id, {
|
||||
name: "Wrong Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const result = await resolveThread(db, thread1.id, candidate.id);
|
||||
const result = await resolveThread(
|
||||
db,
|
||||
userId,
|
||||
thread1.id,
|
||||
candidate.id,
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("fails if candidate not found", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const result = await resolveThread(db, thread.id, 9999);
|
||||
const thread = await createThread(db, userId, {
|
||||
name: "Test",
|
||||
categoryId: 1,
|
||||
});
|
||||
const result = await resolveThread(db, userId, thread.id, 9999);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("cross-user isolation", () => {
|
||||
it("user cannot see other user's threads", async () => {
|
||||
const userId2 = await createSecondTestUser(db);
|
||||
|
||||
await createThread(db, userId, {
|
||||
name: "User 1 Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const user2Categories = await db.query.categories.findMany({
|
||||
where: (cats: any, { eq }: any) => eq(cats.userId, userId2),
|
||||
});
|
||||
const user2CatId = user2Categories[0].id;
|
||||
await createThread(db, userId2, {
|
||||
name: "User 2 Thread",
|
||||
categoryId: user2CatId,
|
||||
});
|
||||
|
||||
const user1Threads = await getAllThreads(db, userId);
|
||||
const user2Threads = await getAllThreads(db, userId2);
|
||||
|
||||
expect(user1Threads).toHaveLength(1);
|
||||
expect(user1Threads[0].name).toBe("User 1 Thread");
|
||||
expect(user2Threads).toHaveLength(1);
|
||||
expect(user2Threads[0].name).toBe("User 2 Thread");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user