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:
@@ -8,15 +8,16 @@ import {
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Auth Service", () => {
|
||||
let db: Awaited<ReturnType<typeof createTestDb>>;
|
||||
let db: any;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
({ db, userId } = await createTestDb());
|
||||
});
|
||||
|
||||
describe("API Key Management", () => {
|
||||
it("creates key and returns raw key once (length > 16, prefix matches first 8 chars)", async () => {
|
||||
const result = await createApiKey(db, "test-key");
|
||||
const result = await createApiKey(db, userId, "test-key");
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result.rawKey).toBeDefined();
|
||||
@@ -25,33 +26,34 @@ describe("Auth Service", () => {
|
||||
expect(result.name).toBe("test-key");
|
||||
});
|
||||
|
||||
it("verifies valid key returns true", async () => {
|
||||
const result = await createApiKey(db, "test-key");
|
||||
const isValid = await verifyApiKey(db, result.rawKey);
|
||||
it("verifies valid key returns { userId }", async () => {
|
||||
const result = await createApiKey(db, userId, "test-key");
|
||||
const verified = await verifyApiKey(db, result.rawKey);
|
||||
|
||||
expect(isValid).toBe(true);
|
||||
expect(verified).not.toBeNull();
|
||||
expect(verified?.userId).toBe(userId);
|
||||
});
|
||||
|
||||
it("rejects invalid key returns false", async () => {
|
||||
await createApiKey(db, "test-key");
|
||||
const isValid = await verifyApiKey(db, "invalidkey12345678");
|
||||
it("rejects invalid key returns null", async () => {
|
||||
await createApiKey(db, userId, "test-key");
|
||||
const verified = await verifyApiKey(db, "invalidkey12345678");
|
||||
|
||||
expect(isValid).toBe(false);
|
||||
expect(verified).toBeNull();
|
||||
});
|
||||
|
||||
it("deletes key so it is no longer valid", async () => {
|
||||
const result = await createApiKey(db, "test-key");
|
||||
await deleteApiKey(db, result.id);
|
||||
const result = await createApiKey(db, userId, "test-key");
|
||||
await deleteApiKey(db, userId, result.id);
|
||||
|
||||
const isValid = await verifyApiKey(db, result.rawKey);
|
||||
expect(isValid).toBe(false);
|
||||
const verified = await verifyApiKey(db, result.rawKey);
|
||||
expect(verified).toBeNull();
|
||||
});
|
||||
|
||||
it("listApiKeys returns keys without hashes", async () => {
|
||||
await createApiKey(db, "key-one");
|
||||
await createApiKey(db, "key-two");
|
||||
await createApiKey(db, userId, "key-one");
|
||||
await createApiKey(db, userId, "key-two");
|
||||
|
||||
const keys = await listApiKeys(db);
|
||||
const keys = await listApiKeys(db, userId);
|
||||
expect(keys).toHaveLength(2);
|
||||
expect(keys[0].name).toBe("key-one");
|
||||
expect(keys[1].name).toBe("key-two");
|
||||
|
||||
@@ -8,18 +8,22 @@ import {
|
||||
updateCategory,
|
||||
} from "../../src/server/services/category.service.ts";
|
||||
import { createItem } from "../../src/server/services/item.service.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { createSecondTestUser, createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Category Service", () => {
|
||||
let db: any;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
({ db, userId } = await createTestDb());
|
||||
});
|
||||
|
||||
describe("createCategory", () => {
|
||||
it("creates with name and icon", async () => {
|
||||
const cat = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
const cat = await createCategory(db, userId, {
|
||||
name: "Shelter",
|
||||
icon: "tent",
|
||||
});
|
||||
|
||||
expect(cat).toBeDefined();
|
||||
expect(cat?.id).toBeGreaterThan(0);
|
||||
@@ -28,7 +32,7 @@ describe("Category Service", () => {
|
||||
});
|
||||
|
||||
it("uses default icon if not provided", async () => {
|
||||
const cat = await createCategory(db, { name: "Cooking" });
|
||||
const cat = await createCategory(db, userId, { name: "Cooking" });
|
||||
|
||||
expect(cat).toBeDefined();
|
||||
expect(cat?.icon).toBe("package");
|
||||
@@ -37,10 +41,13 @@ describe("Category Service", () => {
|
||||
|
||||
describe("getAllCategories", () => {
|
||||
it("returns all categories", async () => {
|
||||
await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
await createCategory(db, { name: "Cooking", icon: "cooking-pot" });
|
||||
await createCategory(db, userId, { name: "Shelter", icon: "tent" });
|
||||
await createCategory(db, userId, {
|
||||
name: "Cooking",
|
||||
icon: "cooking-pot",
|
||||
});
|
||||
|
||||
const all = await getAllCategories(db);
|
||||
const all = await getAllCategories(db, userId);
|
||||
// Includes seeded Uncategorized + 2 new
|
||||
expect(all.length).toBeGreaterThanOrEqual(3);
|
||||
});
|
||||
@@ -48,8 +55,13 @@ describe("Category Service", () => {
|
||||
|
||||
describe("updateCategory", () => {
|
||||
it("renames category", async () => {
|
||||
const cat = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
const updated = await updateCategory(db, cat?.id, { name: "Sleep System" });
|
||||
const cat = await createCategory(db, userId, {
|
||||
name: "Shelter",
|
||||
icon: "tent",
|
||||
});
|
||||
const updated = await updateCategory(db, userId, cat?.id, {
|
||||
name: "Sleep System",
|
||||
});
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.name).toBe("Sleep System");
|
||||
@@ -57,41 +69,81 @@ describe("Category Service", () => {
|
||||
});
|
||||
|
||||
it("changes icon", async () => {
|
||||
const cat = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
const updated = await updateCategory(db, cat?.id, { icon: "home" });
|
||||
const cat = await createCategory(db, userId, {
|
||||
name: "Shelter",
|
||||
icon: "tent",
|
||||
});
|
||||
const updated = await updateCategory(db, userId, cat?.id, {
|
||||
icon: "home",
|
||||
});
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.icon).toBe("home");
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", async () => {
|
||||
const result = await updateCategory(db, 9999, { name: "Ghost" });
|
||||
const result = await updateCategory(db, userId, 9999, {
|
||||
name: "Ghost",
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteCategory", () => {
|
||||
it("reassigns items to Uncategorized (id=1) then deletes", async () => {
|
||||
const shelter = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
await createItem(db, { name: "Tent", categoryId: shelter?.id });
|
||||
await createItem(db, { name: "Tarp", categoryId: shelter?.id });
|
||||
it("reassigns items to Uncategorized then deletes", async () => {
|
||||
const shelter = await createCategory(db, userId, {
|
||||
name: "Shelter",
|
||||
icon: "tent",
|
||||
});
|
||||
await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
categoryId: shelter?.id,
|
||||
});
|
||||
await createItem(db, userId, {
|
||||
name: "Tarp",
|
||||
categoryId: shelter?.id,
|
||||
});
|
||||
|
||||
const result = await deleteCategory(db, shelter?.id);
|
||||
const result = await deleteCategory(db, userId, shelter?.id);
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
// Items should now be in Uncategorized (id=1)
|
||||
// Items should now be in the Uncategorized category (id=1 for first user)
|
||||
const reassigned = await db
|
||||
.select()
|
||||
.from(items)
|
||||
.where(eq(items.categoryId, 1));
|
||||
expect(reassigned).toHaveLength(2);
|
||||
expect(reassigned.map((i: any) => i.name).sort()).toEqual(["Tarp", "Tent"]);
|
||||
expect(reassigned.map((i: any) => i.name).sort()).toEqual([
|
||||
"Tarp",
|
||||
"Tent",
|
||||
]);
|
||||
});
|
||||
|
||||
it("cannot delete Uncategorized (id=1)", async () => {
|
||||
const result = await deleteCategory(db, 1);
|
||||
it("cannot delete Uncategorized", async () => {
|
||||
const result = await deleteCategory(db, userId, 1);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("composite unique constraint", () => {
|
||||
it("two users can have categories with the same name", async () => {
|
||||
const userId2 = await createSecondTestUser(db);
|
||||
|
||||
const cat1 = await createCategory(db, userId, {
|
||||
name: "Cycling",
|
||||
icon: "bike",
|
||||
});
|
||||
const cat2 = await createCategory(db, userId2, {
|
||||
name: "Cycling",
|
||||
icon: "bike",
|
||||
});
|
||||
|
||||
expect(cat1).toBeDefined();
|
||||
expect(cat2).toBeDefined();
|
||||
expect(cat1?.name).toBe("Cycling");
|
||||
expect(cat2?.name).toBe("Cycling");
|
||||
expect(cat1?.id).not.toBe(cat2?.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,16 +9,17 @@ import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("CSV Service", () => {
|
||||
let db: any;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
({ db, userId } = await createTestDb());
|
||||
});
|
||||
|
||||
// ── Export ────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("exportItemsCsv", () => {
|
||||
it("returns correct headers on empty collection", async () => {
|
||||
const csv = await exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db, userId);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines[0]).toBe(
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
@@ -27,7 +28,7 @@ describe("CSV Service", () => {
|
||||
});
|
||||
|
||||
it("exports items with correct values", async () => {
|
||||
await createItem(db, {
|
||||
await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
@@ -36,7 +37,7 @@ describe("CSV Service", () => {
|
||||
productUrl: "https://example.com/tent",
|
||||
});
|
||||
|
||||
const csv = await exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db, userId);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines).toHaveLength(2);
|
||||
expect(lines[1]).toContain("Tent");
|
||||
@@ -48,42 +49,46 @@ describe("CSV Service", () => {
|
||||
});
|
||||
|
||||
it("properly escapes fields with commas", async () => {
|
||||
await createItem(db, {
|
||||
await createItem(db, userId, {
|
||||
name: "Tent, Ultralight",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const csv = await exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db, userId);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines[1]).toContain('"Tent, Ultralight"');
|
||||
});
|
||||
|
||||
it("properly escapes fields with double quotes", async () => {
|
||||
await createItem(db, {
|
||||
await createItem(db, userId, {
|
||||
name: 'He said "great tent"',
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const csv = await exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db, userId);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines[1]).toContain('"He said ""great tent"""');
|
||||
});
|
||||
|
||||
it("exports multiple items", async () => {
|
||||
await createItem(db, { name: "Tent", categoryId: 1 });
|
||||
await createItem(db, { name: "Sleeping Bag", categoryId: 1 });
|
||||
await createItem(db, userId, { name: "Tent", categoryId: 1 });
|
||||
await createItem(db, userId, {
|
||||
name: "Sleeping Bag",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const csv = await exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db, userId);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines).toHaveLength(3); // header + 2 items
|
||||
});
|
||||
|
||||
it("exports quantity correctly", async () => {
|
||||
// Insert directly to set quantity > 1 (createItem service defaults to 1)
|
||||
await db.insert(items)
|
||||
.values({ name: "Bolt", categoryId: 1, quantity: 4 });
|
||||
await db
|
||||
.insert(items)
|
||||
.values({ name: "Bolt", categoryId: 1, quantity: 4, userId });
|
||||
|
||||
const csv = await exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db, userId);
|
||||
const lines = csv.split("\n");
|
||||
const fields = lines[1].split(",");
|
||||
// quantity is second field
|
||||
@@ -101,7 +106,7 @@ describe("CSV Service", () => {
|
||||
"Sleeping Bag,1,800,25000,Camping,,",
|
||||
].join("\n");
|
||||
|
||||
const result = await importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, userId, csv);
|
||||
expect(result.imported).toBe(2);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
@@ -112,7 +117,7 @@ describe("CSV Service", () => {
|
||||
"Helmet,1,350,12000,Cycling,,",
|
||||
].join("\n");
|
||||
|
||||
const result = await importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, userId, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.createdCategories).toContain("Cycling");
|
||||
expect(result.errors).toHaveLength(0);
|
||||
@@ -125,7 +130,7 @@ describe("CSV Service", () => {
|
||||
"Spork,1,,,uncategorized,,",
|
||||
].join("\n");
|
||||
|
||||
const result = await importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, userId, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.createdCategories).toHaveLength(0);
|
||||
});
|
||||
@@ -137,7 +142,7 @@ describe("CSV Service", () => {
|
||||
"Tent,1,1200,,,",
|
||||
].join("\n");
|
||||
|
||||
const result = await importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, userId, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.errors).toHaveLength(1);
|
||||
expect(result.errors[0]).toMatch(/missing required field "name"/);
|
||||
@@ -149,7 +154,7 @@ describe("CSV Service", () => {
|
||||
"Tent,1200,35000,Camping,,",
|
||||
].join("\n");
|
||||
|
||||
const result = await importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, userId, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
@@ -160,7 +165,7 @@ describe("CSV Service", () => {
|
||||
"Tent,,,,,",
|
||||
].join("\n");
|
||||
|
||||
const result = await importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, userId, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
@@ -171,13 +176,13 @@ describe("CSV Service", () => {
|
||||
'"Tent, Ultralight",1,1200,,,',
|
||||
].join("\n");
|
||||
|
||||
const result = await importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, userId, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("returns zero imported on empty CSV", async () => {
|
||||
const result = await importItemsCsv(db, "");
|
||||
const result = await importItemsCsv(db, userId, "");
|
||||
expect(result.imported).toBe(0);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
@@ -188,7 +193,7 @@ describe("CSV Service", () => {
|
||||
"Tent,1,,,,",
|
||||
].join("\n");
|
||||
|
||||
const result = await importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, userId, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.createdCategories).toHaveLength(0);
|
||||
});
|
||||
|
||||
@@ -7,18 +7,19 @@ import {
|
||||
getItemById,
|
||||
updateItem,
|
||||
} from "../../src/server/services/item.service.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { createSecondTestUser, createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Item Service", () => {
|
||||
let db: any;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
({ db, userId } = await createTestDb());
|
||||
});
|
||||
|
||||
describe("createItem", () => {
|
||||
it("creates item with all fields, returns item with id and timestamps", async () => {
|
||||
const item = await createItem(db, {
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
@@ -40,7 +41,10 @@ describe("Item Service", () => {
|
||||
});
|
||||
|
||||
it("only name and categoryId are required, other fields optional", async () => {
|
||||
const item = await createItem(db, { name: "Spork", categoryId: 1 });
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Spork",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
expect(item).toBeDefined();
|
||||
expect(item?.name).toBe("Spork");
|
||||
@@ -53,10 +57,10 @@ describe("Item Service", () => {
|
||||
|
||||
describe("getAllItems", () => {
|
||||
it("returns all items with category info joined", async () => {
|
||||
await createItem(db, { name: "Tent", categoryId: 1 });
|
||||
await createItem(db, { name: "Sleeping Bag", categoryId: 1 });
|
||||
await createItem(db, userId, { name: "Tent", categoryId: 1 });
|
||||
await createItem(db, userId, { name: "Sleeping Bag", categoryId: 1 });
|
||||
|
||||
const all = await getAllItems(db);
|
||||
const all = await getAllItems(db, userId);
|
||||
expect(all).toHaveLength(2);
|
||||
expect(all[0].categoryName).toBe("Uncategorized");
|
||||
expect(all[0].categoryIcon).toBeDefined();
|
||||
@@ -65,25 +69,28 @@ describe("Item Service", () => {
|
||||
|
||||
describe("getItemById", () => {
|
||||
it("returns single item or null", async () => {
|
||||
const created = await createItem(db, { name: "Tent", categoryId: 1 });
|
||||
const found = await getItemById(db, created?.id);
|
||||
const created = await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
});
|
||||
const found = await getItemById(db, userId, created?.id);
|
||||
expect(found).toBeDefined();
|
||||
expect(found?.name).toBe("Tent");
|
||||
|
||||
const notFound = await getItemById(db, 9999);
|
||||
const notFound = await getItemById(db, userId, 9999);
|
||||
expect(notFound).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateItem", () => {
|
||||
it("updates specified fields, sets updatedAt", async () => {
|
||||
const created = await createItem(db, {
|
||||
const created = await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const updated = await updateItem(db, created?.id, {
|
||||
const updated = await updateItem(db, userId, created?.id, {
|
||||
name: "Big Agnes Tent",
|
||||
weightGrams: 1100,
|
||||
});
|
||||
@@ -94,14 +101,14 @@ describe("Item Service", () => {
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", async () => {
|
||||
const result = await updateItem(db, 9999, { name: "Ghost" });
|
||||
const result = await updateItem(db, userId, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("duplicateItem", () => {
|
||||
it("creates a copy with '(copy)' suffix in name", async () => {
|
||||
const original = await createItem(db, {
|
||||
const original = await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
@@ -110,7 +117,7 @@ describe("Item Service", () => {
|
||||
productUrl: "https://example.com/tent",
|
||||
});
|
||||
|
||||
const copy = await duplicateItem(db, original?.id);
|
||||
const copy = await duplicateItem(db, userId, original?.id);
|
||||
|
||||
expect(copy).toBeDefined();
|
||||
expect(copy?.name).toBe("Tent (copy)");
|
||||
@@ -122,38 +129,79 @@ describe("Item Service", () => {
|
||||
});
|
||||
|
||||
it("copy has a different ID from the original", async () => {
|
||||
const original = await createItem(db, { name: "Helmet", categoryId: 1 });
|
||||
const copy = await duplicateItem(db, original?.id);
|
||||
const original = await createItem(db, userId, {
|
||||
name: "Helmet",
|
||||
categoryId: 1,
|
||||
});
|
||||
const copy = await duplicateItem(db, userId, original?.id);
|
||||
|
||||
expect(copy?.id).not.toBe(original?.id);
|
||||
});
|
||||
|
||||
it("returns null for non-existent item", async () => {
|
||||
const result = await duplicateItem(db, 9999);
|
||||
const result = await duplicateItem(db, userId, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteItem", () => {
|
||||
it("removes item from DB, returns deleted item", async () => {
|
||||
const created = await createItem(db, {
|
||||
const created = await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
imageFilename: "tent.jpg",
|
||||
});
|
||||
|
||||
const deleted = await deleteItem(db, created?.id);
|
||||
const deleted = await deleteItem(db, userId, created?.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted?.name).toBe("Tent");
|
||||
expect(deleted?.imageFilename).toBe("tent.jpg");
|
||||
|
||||
// Verify it's gone
|
||||
const found = await getItemById(db, created?.id);
|
||||
const found = await getItemById(db, userId, created?.id);
|
||||
expect(found).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", async () => {
|
||||
const result = await deleteItem(db, 9999);
|
||||
const result = await deleteItem(db, userId, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("cross-user isolation", () => {
|
||||
it("user cannot see other user's items", async () => {
|
||||
const userId2 = await createSecondTestUser(db);
|
||||
await createItem(db, userId, {
|
||||
name: "User 1 Tent",
|
||||
categoryId: 1,
|
||||
});
|
||||
// User 2 needs their own Uncategorized category; createSecondTestUser seeds one
|
||||
const user2Categories = await db.query.categories.findMany({
|
||||
where: (cats: any, { eq }: any) => eq(cats.userId, userId2),
|
||||
});
|
||||
const user2CatId = user2Categories[0].id;
|
||||
await createItem(db, userId2, {
|
||||
name: "User 2 Bag",
|
||||
categoryId: user2CatId,
|
||||
});
|
||||
|
||||
const user1Items = await getAllItems(db, userId);
|
||||
const user2Items = await getAllItems(db, userId2);
|
||||
|
||||
expect(user1Items).toHaveLength(1);
|
||||
expect(user1Items[0].name).toBe("User 1 Tent");
|
||||
expect(user2Items).toHaveLength(1);
|
||||
expect(user2Items[0].name).toBe("User 2 Bag");
|
||||
});
|
||||
|
||||
it("getItemById returns null for another user's item", async () => {
|
||||
const created = await createItem(db, userId, {
|
||||
name: "My Item",
|
||||
categoryId: 1,
|
||||
});
|
||||
const userId2 = await createSecondTestUser(db);
|
||||
|
||||
const result = await getItemById(db, userId2, created?.id);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,9 +18,10 @@ function generatePkce() {
|
||||
|
||||
describe("OAuth Service", () => {
|
||||
let db: any;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
({ db, userId } = await createTestDb());
|
||||
});
|
||||
|
||||
describe("Client Registration", () => {
|
||||
@@ -92,6 +93,7 @@ describe("OAuth Service", () => {
|
||||
verifier,
|
||||
clientId,
|
||||
"http://localhost:8080/callback",
|
||||
userId,
|
||||
);
|
||||
|
||||
expect(tokens).not.toBeNull();
|
||||
@@ -122,6 +124,7 @@ describe("OAuth Service", () => {
|
||||
"wrongverifier",
|
||||
clientId,
|
||||
"http://localhost:8080/callback",
|
||||
userId,
|
||||
);
|
||||
|
||||
expect(tokens).toBeNull();
|
||||
@@ -147,6 +150,7 @@ describe("OAuth Service", () => {
|
||||
verifier,
|
||||
clientId,
|
||||
"http://localhost:9999/wrong",
|
||||
userId,
|
||||
);
|
||||
|
||||
expect(tokens).toBeNull();
|
||||
@@ -173,6 +177,7 @@ describe("OAuth Service", () => {
|
||||
verifier,
|
||||
clientId,
|
||||
"http://localhost:8080/callback",
|
||||
userId,
|
||||
);
|
||||
expect(first).not.toBeNull();
|
||||
|
||||
@@ -183,13 +188,14 @@ describe("OAuth Service", () => {
|
||||
verifier,
|
||||
clientId,
|
||||
"http://localhost:8080/callback",
|
||||
userId,
|
||||
);
|
||||
expect(second).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Token Verification", () => {
|
||||
it("verifies a valid access token (returns true)", async () => {
|
||||
it("verifies a valid access token returns { userId }", async () => {
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
@@ -209,15 +215,17 @@ describe("OAuth Service", () => {
|
||||
verifier,
|
||||
clientId,
|
||||
"http://localhost:8080/callback",
|
||||
userId,
|
||||
);
|
||||
|
||||
const isValid = await verifyAccessToken(db, tokens!.accessToken);
|
||||
expect(isValid).toBe(true);
|
||||
const verified = await verifyAccessToken(db, tokens!.accessToken);
|
||||
expect(verified).not.toBeNull();
|
||||
expect(verified?.userId).toBe(userId);
|
||||
});
|
||||
|
||||
it("rejects an unknown token (returns false)", async () => {
|
||||
const isValid = await verifyAccessToken(db, "unknowntoken12345678");
|
||||
expect(isValid).toBe(false);
|
||||
it("rejects an unknown token (returns null)", async () => {
|
||||
const verified = await verifyAccessToken(db, "unknowntoken12345678");
|
||||
expect(verified).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -242,12 +250,14 @@ describe("OAuth Service", () => {
|
||||
verifier,
|
||||
clientId,
|
||||
"http://localhost:8080/callback",
|
||||
userId,
|
||||
);
|
||||
|
||||
const newTokens = await refreshAccessToken(
|
||||
db,
|
||||
tokens!.refreshToken,
|
||||
clientId,
|
||||
userId,
|
||||
);
|
||||
|
||||
expect(newTokens).not.toBeNull();
|
||||
@@ -276,12 +286,14 @@ describe("OAuth Service", () => {
|
||||
verifier,
|
||||
clientId,
|
||||
"http://localhost:8080/callback",
|
||||
userId,
|
||||
);
|
||||
|
||||
const newTokens = await refreshAccessToken(
|
||||
db,
|
||||
tokens!.refreshToken,
|
||||
"wrong-client-id",
|
||||
userId,
|
||||
);
|
||||
|
||||
expect(newTokens).toBeNull();
|
||||
|
||||
@@ -12,18 +12,19 @@ import {
|
||||
updateItemClassification,
|
||||
updateSetup,
|
||||
} from "../../src/server/services/setup.service.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { createSecondTestUser, createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Setup Service", () => {
|
||||
let db: any;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
({ db, userId } = await createTestDb());
|
||||
});
|
||||
|
||||
describe("createSetup", () => {
|
||||
it("creates setup with name, returns setup with id/timestamps", async () => {
|
||||
const setup = await createSetup(db, { name: "Day Hike" });
|
||||
const setup = await createSetup(db, userId, { name: "Day Hike" });
|
||||
|
||||
expect(setup).toBeDefined();
|
||||
expect(setup.id).toBeGreaterThan(0);
|
||||
@@ -35,22 +36,24 @@ describe("Setup Service", () => {
|
||||
|
||||
describe("getAllSetups", () => {
|
||||
it("returns setups with itemCount, totalWeight, totalCost", async () => {
|
||||
const setup = await createSetup(db, { name: "Backpacking" });
|
||||
const item1 = await createItem(db, {
|
||||
const setup = await createSetup(db, userId, {
|
||||
name: "Backpacking",
|
||||
});
|
||||
const item1 = await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
});
|
||||
const item2 = await createItem(db, {
|
||||
const item2 = await createItem(db, userId, {
|
||||
name: "Sleeping Bag",
|
||||
categoryId: 1,
|
||||
weightGrams: 800,
|
||||
priceCents: 20000,
|
||||
});
|
||||
await syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
await syncSetupItems(db, userId, setup.id, [item1.id, item2.id]);
|
||||
|
||||
const setups = await getAllSetups(db);
|
||||
const setups = await getAllSetups(db, userId);
|
||||
expect(setups).toHaveLength(1);
|
||||
expect(setups[0].name).toBe("Backpacking");
|
||||
expect(setups[0].itemCount).toBe(2);
|
||||
@@ -59,9 +62,9 @@ describe("Setup Service", () => {
|
||||
});
|
||||
|
||||
it("returns 0 for weight/cost when setup has no items", async () => {
|
||||
await createSetup(db, { name: "Empty Setup" });
|
||||
await createSetup(db, userId, { name: "Empty Setup" });
|
||||
|
||||
const setups = await getAllSetups(db);
|
||||
const setups = await getAllSetups(db, userId);
|
||||
expect(setups).toHaveLength(1);
|
||||
expect(setups[0].itemCount).toBe(0);
|
||||
expect(setups[0].totalWeight).toBe(0);
|
||||
@@ -71,16 +74,16 @@ describe("Setup Service", () => {
|
||||
|
||||
describe("getSetupWithItems", () => {
|
||||
it("returns setup with full item details and category info", async () => {
|
||||
const setup = await createSetup(db, { name: "Day Hike" });
|
||||
const item = await createItem(db, {
|
||||
const setup = await createSetup(db, userId, { name: "Day Hike" });
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Water Bottle",
|
||||
categoryId: 1,
|
||||
weightGrams: 200,
|
||||
priceCents: 2500,
|
||||
});
|
||||
await syncSetupItems(db, setup.id, [item.id]);
|
||||
await syncSetupItems(db, userId, setup.id, [item.id]);
|
||||
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.name).toBe("Day Hike");
|
||||
expect(result?.items).toHaveLength(1);
|
||||
@@ -90,86 +93,111 @@ describe("Setup Service", () => {
|
||||
});
|
||||
|
||||
it("returns null for non-existent setup", async () => {
|
||||
const result = await getSetupWithItems(db, 9999);
|
||||
const result = await getSetupWithItems(db, userId, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateSetup", () => {
|
||||
it("updates setup name, returns updated setup", async () => {
|
||||
const setup = await createSetup(db, { name: "Original" });
|
||||
const updated = await updateSetup(db, setup.id, { name: "Renamed" });
|
||||
const setup = await createSetup(db, userId, { name: "Original" });
|
||||
const updated = await updateSetup(db, userId, setup.id, {
|
||||
name: "Renamed",
|
||||
});
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.name).toBe("Renamed");
|
||||
});
|
||||
|
||||
it("returns null for non-existent setup", async () => {
|
||||
const result = await updateSetup(db, 9999, { name: "Ghost" });
|
||||
const result = await updateSetup(db, userId, 9999, {
|
||||
name: "Ghost",
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteSetup", () => {
|
||||
it("removes setup and cascades to setup_items", async () => {
|
||||
const setup = await createSetup(db, { name: "To Delete" });
|
||||
const item = await createItem(db, { name: "Item", categoryId: 1 });
|
||||
await syncSetupItems(db, setup.id, [item.id]);
|
||||
const setup = await createSetup(db, userId, { name: "To Delete" });
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Item",
|
||||
categoryId: 1,
|
||||
});
|
||||
await syncSetupItems(db, userId, setup.id, [item.id]);
|
||||
|
||||
const deleted = await deleteSetup(db, setup.id);
|
||||
const deleted = await deleteSetup(db, userId, setup.id);
|
||||
expect(deleted).toBe(true);
|
||||
|
||||
// Setup gone
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns false for non-existent setup", async () => {
|
||||
const result = await deleteSetup(db, 9999);
|
||||
const result = await deleteSetup(db, userId, 9999);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("syncSetupItems", () => {
|
||||
it("sets items for a setup (delete-all + re-insert)", async () => {
|
||||
const setup = await createSetup(db, { name: "Kit" });
|
||||
const item1 = await createItem(db, { name: "Item 1", categoryId: 1 });
|
||||
const item2 = await createItem(db, { name: "Item 2", categoryId: 1 });
|
||||
const item3 = await createItem(db, { name: "Item 3", categoryId: 1 });
|
||||
const setup = await createSetup(db, userId, { name: "Kit" });
|
||||
const item1 = await createItem(db, userId, {
|
||||
name: "Item 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const item2 = await createItem(db, userId, {
|
||||
name: "Item 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
const item3 = await createItem(db, userId, {
|
||||
name: "Item 3",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Initial sync
|
||||
await syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
let result = await getSetupWithItems(db, setup.id);
|
||||
await syncSetupItems(db, userId, setup.id, [item1.id, item2.id]);
|
||||
let result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items).toHaveLength(2);
|
||||
|
||||
// Re-sync with different items
|
||||
await syncSetupItems(db, setup.id, [item2.id, item3.id]);
|
||||
result = await getSetupWithItems(db, setup.id);
|
||||
await syncSetupItems(db, userId, setup.id, [item2.id, item3.id]);
|
||||
result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items).toHaveLength(2);
|
||||
const names = result?.items.map((i: any) => i.name).sort();
|
||||
expect(names).toEqual(["Item 2", "Item 3"]);
|
||||
});
|
||||
|
||||
it("syncing with empty array clears all items", async () => {
|
||||
const setup = await createSetup(db, { name: "Kit" });
|
||||
const item = await createItem(db, { name: "Item", categoryId: 1 });
|
||||
await syncSetupItems(db, setup.id, [item.id]);
|
||||
const setup = await createSetup(db, userId, { name: "Kit" });
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Item",
|
||||
categoryId: 1,
|
||||
});
|
||||
await syncSetupItems(db, userId, setup.id, [item.id]);
|
||||
|
||||
await syncSetupItems(db, setup.id, []);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
await syncSetupItems(db, userId, setup.id, []);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removeSetupItem", () => {
|
||||
it("removes single item from setup", async () => {
|
||||
const setup = await createSetup(db, { name: "Kit" });
|
||||
const item1 = await createItem(db, { name: "Item 1", categoryId: 1 });
|
||||
const item2 = await createItem(db, { name: "Item 2", categoryId: 1 });
|
||||
await syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
const setup = await createSetup(db, userId, { name: "Kit" });
|
||||
const item1 = await createItem(db, userId, {
|
||||
name: "Item 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const item2 = await createItem(db, userId, {
|
||||
name: "Item 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
await syncSetupItems(db, userId, setup.id, [item1.id, item2.id]);
|
||||
|
||||
await removeSetupItem(db, setup.id, item1.id);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
await removeSetupItem(db, userId, setup.id, item1.id);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items).toHaveLength(1);
|
||||
expect(result?.items[0].name).toBe("Item 2");
|
||||
});
|
||||
@@ -177,16 +205,16 @@ describe("Setup Service", () => {
|
||||
|
||||
describe("getSetupWithItems - classification", () => {
|
||||
it("returns classification field defaulting to 'base' for each item", async () => {
|
||||
const setup = await createSetup(db, { name: "Day Hike" });
|
||||
const item = await createItem(db, {
|
||||
const setup = await createSetup(db, userId, { name: "Day Hike" });
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Water Bottle",
|
||||
categoryId: 1,
|
||||
weightGrams: 200,
|
||||
priceCents: 2500,
|
||||
});
|
||||
await syncSetupItems(db, setup.id, [item.id]);
|
||||
await syncSetupItems(db, userId, setup.id, [item.id]);
|
||||
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items).toHaveLength(1);
|
||||
expect(result?.items[0].classification).toBe("base");
|
||||
});
|
||||
@@ -194,81 +222,142 @@ describe("Setup Service", () => {
|
||||
|
||||
describe("syncSetupItems - classification preservation", () => {
|
||||
it("preserves existing classifications when re-syncing items", async () => {
|
||||
const setup = await createSetup(db, { name: "Kit" });
|
||||
const item1 = await createItem(db, { name: "Tent", categoryId: 1 });
|
||||
const item2 = await createItem(db, { name: "Jacket", categoryId: 1 });
|
||||
const item3 = await createItem(db, { name: "Stove", categoryId: 1 });
|
||||
const setup = await createSetup(db, userId, { name: "Kit" });
|
||||
const item1 = await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
});
|
||||
const item2 = await createItem(db, userId, {
|
||||
name: "Jacket",
|
||||
categoryId: 1,
|
||||
});
|
||||
const item3 = await createItem(db, userId, {
|
||||
name: "Stove",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Initial sync
|
||||
await syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
await syncSetupItems(db, userId, setup.id, [item1.id, item2.id]);
|
||||
|
||||
// Change classifications
|
||||
await updateItemClassification(db, setup.id, item1.id, "worn");
|
||||
await updateItemClassification(db, setup.id, item2.id, "consumable");
|
||||
await updateItemClassification(
|
||||
db,
|
||||
userId,
|
||||
setup.id,
|
||||
item1.id,
|
||||
"worn",
|
||||
);
|
||||
await updateItemClassification(
|
||||
db,
|
||||
userId,
|
||||
setup.id,
|
||||
item2.id,
|
||||
"consumable",
|
||||
);
|
||||
|
||||
// Re-sync with item2 kept and item3 added (item1 removed)
|
||||
await syncSetupItems(db, setup.id, [item2.id, item3.id]);
|
||||
await syncSetupItems(db, userId, setup.id, [item2.id, item3.id]);
|
||||
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items).toHaveLength(2);
|
||||
|
||||
const item2Result = result?.items.find((i: any) => i.name === "Jacket");
|
||||
const item3Result = result?.items.find((i: any) => i.name === "Stove");
|
||||
const item2Result = result?.items.find(
|
||||
(i: any) => i.name === "Jacket",
|
||||
);
|
||||
const item3Result = result?.items.find(
|
||||
(i: any) => i.name === "Stove",
|
||||
);
|
||||
expect(item2Result?.classification).toBe("consumable");
|
||||
expect(item3Result?.classification).toBe("base");
|
||||
});
|
||||
|
||||
it("assigns 'base' to newly added items with no prior classification", async () => {
|
||||
const setup = await createSetup(db, { name: "Kit" });
|
||||
const item1 = await createItem(db, { name: "Item 1", categoryId: 1 });
|
||||
const setup = await createSetup(db, userId, { name: "Kit" });
|
||||
const item1 = await createItem(db, userId, {
|
||||
name: "Item 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
await syncSetupItems(db, setup.id, [item1.id]);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
await syncSetupItems(db, userId, setup.id, [item1.id]);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items[0].classification).toBe("base");
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateItemClassification", () => {
|
||||
it("sets classification for a specific item in a specific setup", async () => {
|
||||
const setup = await createSetup(db, { name: "Kit" });
|
||||
const item = await createItem(db, { name: "Tent", categoryId: 1 });
|
||||
await syncSetupItems(db, setup.id, [item.id]);
|
||||
const setup = await createSetup(db, userId, { name: "Kit" });
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
});
|
||||
await syncSetupItems(db, userId, setup.id, [item.id]);
|
||||
|
||||
await updateItemClassification(db, setup.id, item.id, "worn");
|
||||
await updateItemClassification(
|
||||
db,
|
||||
userId,
|
||||
setup.id,
|
||||
item.id,
|
||||
"worn",
|
||||
);
|
||||
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items[0].classification).toBe("worn");
|
||||
});
|
||||
|
||||
it("changes item from default 'base' to 'worn'", async () => {
|
||||
const setup = await createSetup(db, { name: "Kit" });
|
||||
const item = await createItem(db, { name: "Jacket", categoryId: 1 });
|
||||
await syncSetupItems(db, setup.id, [item.id]);
|
||||
const setup = await createSetup(db, userId, { name: "Kit" });
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Jacket",
|
||||
categoryId: 1,
|
||||
});
|
||||
await syncSetupItems(db, userId, setup.id, [item.id]);
|
||||
|
||||
// Verify default
|
||||
let result = await getSetupWithItems(db, setup.id);
|
||||
let result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items[0].classification).toBe("base");
|
||||
|
||||
// Update
|
||||
await updateItemClassification(db, setup.id, item.id, "worn");
|
||||
await updateItemClassification(
|
||||
db,
|
||||
userId,
|
||||
setup.id,
|
||||
item.id,
|
||||
"worn",
|
||||
);
|
||||
|
||||
result = await getSetupWithItems(db, setup.id);
|
||||
result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items[0].classification).toBe("worn");
|
||||
});
|
||||
|
||||
it("same item in two different setups can have different classifications", async () => {
|
||||
const setup1 = await createSetup(db, { name: "Hiking" });
|
||||
const setup2 = await createSetup(db, { name: "Biking" });
|
||||
const item = await createItem(db, { name: "Jacket", categoryId: 1 });
|
||||
const setup1 = await createSetup(db, userId, { name: "Hiking" });
|
||||
const setup2 = await createSetup(db, userId, { name: "Biking" });
|
||||
const item = await createItem(db, userId, {
|
||||
name: "Jacket",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
await syncSetupItems(db, setup1.id, [item.id]);
|
||||
await syncSetupItems(db, setup2.id, [item.id]);
|
||||
await syncSetupItems(db, userId, setup1.id, [item.id]);
|
||||
await syncSetupItems(db, userId, setup2.id, [item.id]);
|
||||
|
||||
await updateItemClassification(db, setup1.id, item.id, "worn");
|
||||
await updateItemClassification(db, setup2.id, item.id, "base");
|
||||
await updateItemClassification(
|
||||
db,
|
||||
userId,
|
||||
setup1.id,
|
||||
item.id,
|
||||
"worn",
|
||||
);
|
||||
await updateItemClassification(
|
||||
db,
|
||||
userId,
|
||||
setup2.id,
|
||||
item.id,
|
||||
"base",
|
||||
);
|
||||
|
||||
const result1 = await getSetupWithItems(db, setup1.id);
|
||||
const result2 = await getSetupWithItems(db, setup2.id);
|
||||
const result1 = await getSetupWithItems(db, userId, setup1.id);
|
||||
const result2 = await getSetupWithItems(db, userId, setup2.id);
|
||||
|
||||
expect(result1?.items[0].classification).toBe("worn");
|
||||
expect(result2?.items[0].classification).toBe("base");
|
||||
@@ -277,17 +366,40 @@ describe("Setup Service", () => {
|
||||
|
||||
describe("cascade behavior", () => {
|
||||
it("deleting a collection item removes it from all setups", async () => {
|
||||
const setup = await createSetup(db, { name: "Kit" });
|
||||
const item1 = await createItem(db, { name: "Item 1", categoryId: 1 });
|
||||
const item2 = await createItem(db, { name: "Item 2", categoryId: 1 });
|
||||
await syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
const setup = await createSetup(db, userId, { name: "Kit" });
|
||||
const item1 = await createItem(db, userId, {
|
||||
name: "Item 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const item2 = await createItem(db, userId, {
|
||||
name: "Item 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
await syncSetupItems(db, userId, setup.id, [item1.id, item2.id]);
|
||||
|
||||
// Delete item1 from collection
|
||||
await db.delete(itemsTable).where(eq(itemsTable.id, item1.id));
|
||||
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, userId, setup.id);
|
||||
expect(result?.items).toHaveLength(1);
|
||||
expect(result?.items[0].name).toBe("Item 2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("cross-user isolation", () => {
|
||||
it("user cannot see other user's setups", async () => {
|
||||
const userId2 = await createSecondTestUser(db);
|
||||
|
||||
await createSetup(db, userId, { name: "User 1 Setup" });
|
||||
await createSetup(db, userId2, { name: "User 2 Setup" });
|
||||
|
||||
const user1Setups = await getAllSetups(db, userId);
|
||||
const user2Setups = await getAllSetups(db, userId2);
|
||||
|
||||
expect(user1Setups).toHaveLength(1);
|
||||
expect(user1Setups[0].name).toBe("User 1 Setup");
|
||||
expect(user2Setups).toHaveLength(1);
|
||||
expect(user2Setups[0].name).toBe("User 2 Setup");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,28 +9,32 @@ import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Totals Service", () => {
|
||||
let db: any;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
({ db, userId } = await createTestDb());
|
||||
});
|
||||
|
||||
describe("getCategoryTotals", () => {
|
||||
it("returns weight sum, cost sum, item count per category", async () => {
|
||||
const shelter = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
await createItem(db, {
|
||||
const shelter = await createCategory(db, userId, {
|
||||
name: "Shelter",
|
||||
icon: "tent",
|
||||
});
|
||||
await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: shelter?.id,
|
||||
});
|
||||
await createItem(db, {
|
||||
await createItem(db, userId, {
|
||||
name: "Tarp",
|
||||
weightGrams: 300,
|
||||
priceCents: 8000,
|
||||
categoryId: shelter?.id,
|
||||
});
|
||||
|
||||
const totals = await getCategoryTotals(db);
|
||||
const totals = await getCategoryTotals(db, userId);
|
||||
expect(totals).toHaveLength(1); // Only Shelter has items
|
||||
expect(totals[0].categoryName).toBe("Shelter");
|
||||
expect(totals[0].totalWeight).toBe(1500);
|
||||
@@ -39,29 +43,29 @@ describe("Totals Service", () => {
|
||||
});
|
||||
|
||||
it("excludes empty categories (no items)", async () => {
|
||||
await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
await createCategory(db, userId, { name: "Shelter", icon: "tent" });
|
||||
// No items added
|
||||
const totals = await getCategoryTotals(db);
|
||||
const totals = await getCategoryTotals(db, userId);
|
||||
expect(totals).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getGlobalTotals", () => {
|
||||
it("returns overall weight, cost, count", async () => {
|
||||
await createItem(db, {
|
||||
await createItem(db, userId, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: 1,
|
||||
});
|
||||
await createItem(db, {
|
||||
await createItem(db, userId, {
|
||||
name: "Spork",
|
||||
weightGrams: 20,
|
||||
priceCents: 500,
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const totals = await getGlobalTotals(db);
|
||||
const totals = await getGlobalTotals(db, userId);
|
||||
expect(totals).toBeDefined();
|
||||
expect(totals?.totalWeight).toBe(1220);
|
||||
expect(totals?.totalCost).toBe(35500);
|
||||
@@ -69,7 +73,7 @@ describe("Totals Service", () => {
|
||||
});
|
||||
|
||||
it("returns zeros when no items exist", async () => {
|
||||
const totals = await getGlobalTotals(db);
|
||||
const totals = await getGlobalTotals(db, userId);
|
||||
expect(totals).toBeDefined();
|
||||
expect(totals?.totalWeight).toBe(0);
|
||||
expect(totals?.totalCost).toBe(0);
|
||||
|
||||
Reference in New Issue
Block a user