feat(14-06): convert all 9 service test files to async PGlite
- All beforeEach now use async/await createTestDb() - All service calls in tests now awaited - All direct DB calls (.run()/.all()) replaced with await - All test callbacks made async - Fixed PostgreSQL GROUP BY strictness in totals.service.ts (categories.name and categories.icon added to groupBy) - db type changed to 'any' to accommodate PGlite type differences Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,7 @@ export async function getCategoryTotals(db: Db = prodDb) {
|
||||
})
|
||||
.from(items)
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.groupBy(items.categoryId);
|
||||
.groupBy(items.categoryId, categories.name, categories.icon);
|
||||
}
|
||||
|
||||
export async function getGlobalTotals(db: Db = prodDb) {
|
||||
|
||||
@@ -15,10 +15,10 @@ import {
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Auth Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
describe("User Management", () => {
|
||||
@@ -48,12 +48,12 @@ describe("Auth Service", () => {
|
||||
});
|
||||
|
||||
it("getUserCount returns 0 then 1", async () => {
|
||||
const countBefore = getUserCount(db);
|
||||
const countBefore = await getUserCount(db);
|
||||
expect(countBefore).toBe(0);
|
||||
|
||||
await createUser(db, "admin", "secret123");
|
||||
|
||||
const countAfter = getUserCount(db);
|
||||
const countAfter = await getUserCount(db);
|
||||
expect(countAfter).toBe(1);
|
||||
});
|
||||
|
||||
@@ -86,33 +86,33 @@ describe("Auth Service", () => {
|
||||
describe("Session Management", () => {
|
||||
it("creates and retrieves a session (id length is 64 hex chars)", async () => {
|
||||
const user = await createUser(db, "admin", "secret123");
|
||||
const session = createSession(db, user.id);
|
||||
const session = await createSession(db, user.id);
|
||||
|
||||
expect(session).toBeDefined();
|
||||
expect(session.id).toHaveLength(64);
|
||||
expect(session.userId).toBe(user.id);
|
||||
expect(session.expiresAt).toBeInstanceOf(Date);
|
||||
|
||||
const retrieved = getSession(db, session.id);
|
||||
const retrieved = await getSession(db, session.id);
|
||||
expect(retrieved).not.toBeNull();
|
||||
expect(retrieved!.id).toBe(session.id);
|
||||
});
|
||||
|
||||
it("returns null for expired session (expiryDays = -1)", async () => {
|
||||
const user = await createUser(db, "admin", "secret123");
|
||||
const session = createSession(db, user.id, -1);
|
||||
const session = await createSession(db, user.id, -1);
|
||||
|
||||
const retrieved = getSession(db, session.id);
|
||||
const retrieved = await getSession(db, session.id);
|
||||
expect(retrieved).toBeNull();
|
||||
});
|
||||
|
||||
it("deletes a session", async () => {
|
||||
const user = await createUser(db, "admin", "secret123");
|
||||
const session = createSession(db, user.id);
|
||||
const session = await createSession(db, user.id);
|
||||
|
||||
deleteSession(db, session.id);
|
||||
await deleteSession(db, session.id);
|
||||
|
||||
const retrieved = getSession(db, session.id);
|
||||
const retrieved = await getSession(db, session.id);
|
||||
expect(retrieved).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -144,7 +144,7 @@ describe("Auth Service", () => {
|
||||
|
||||
it("deletes key so it is no longer valid", async () => {
|
||||
const result = await createApiKey(db, "test-key");
|
||||
deleteApiKey(db, result.id);
|
||||
await deleteApiKey(db, result.id);
|
||||
|
||||
const isValid = await verifyApiKey(db, result.rawKey);
|
||||
expect(isValid).toBe(false);
|
||||
@@ -154,7 +154,7 @@ describe("Auth Service", () => {
|
||||
await createApiKey(db, "key-one");
|
||||
await createApiKey(db, "key-two");
|
||||
|
||||
const keys = listApiKeys(db);
|
||||
const keys = await listApiKeys(db);
|
||||
expect(keys).toHaveLength(2);
|
||||
expect(keys[0].name).toBe("key-one");
|
||||
expect(keys[1].name).toBe("key-two");
|
||||
|
||||
@@ -11,15 +11,15 @@ import { createItem } from "../../src/server/services/item.service.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Category Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
describe("createCategory", () => {
|
||||
it("creates with name and icon", () => {
|
||||
const cat = createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
it("creates with name and icon", async () => {
|
||||
const cat = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
|
||||
expect(cat).toBeDefined();
|
||||
expect(cat?.id).toBeGreaterThan(0);
|
||||
@@ -27,8 +27,8 @@ describe("Category Service", () => {
|
||||
expect(cat?.icon).toBe("tent");
|
||||
});
|
||||
|
||||
it("uses default icon if not provided", () => {
|
||||
const cat = createCategory(db, { name: "Cooking" });
|
||||
it("uses default icon if not provided", async () => {
|
||||
const cat = await createCategory(db, { name: "Cooking" });
|
||||
|
||||
expect(cat).toBeDefined();
|
||||
expect(cat?.icon).toBe("package");
|
||||
@@ -36,61 +36,60 @@ describe("Category Service", () => {
|
||||
});
|
||||
|
||||
describe("getAllCategories", () => {
|
||||
it("returns all categories", () => {
|
||||
createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
createCategory(db, { name: "Cooking", icon: "cooking-pot" });
|
||||
it("returns all categories", async () => {
|
||||
await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
await createCategory(db, { name: "Cooking", icon: "cooking-pot" });
|
||||
|
||||
const all = getAllCategories(db);
|
||||
const all = await getAllCategories(db);
|
||||
// Includes seeded Uncategorized + 2 new
|
||||
expect(all.length).toBeGreaterThanOrEqual(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateCategory", () => {
|
||||
it("renames category", () => {
|
||||
const cat = createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
const updated = updateCategory(db, cat?.id, { name: "Sleep System" });
|
||||
it("renames category", async () => {
|
||||
const cat = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
const updated = await updateCategory(db, cat?.id, { name: "Sleep System" });
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.name).toBe("Sleep System");
|
||||
expect(updated?.icon).toBe("tent");
|
||||
});
|
||||
|
||||
it("changes icon", () => {
|
||||
const cat = createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
const updated = updateCategory(db, cat?.id, { icon: "home" });
|
||||
it("changes icon", async () => {
|
||||
const cat = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
const updated = await updateCategory(db, cat?.id, { icon: "home" });
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.icon).toBe("home");
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", () => {
|
||||
const result = updateCategory(db, 9999, { name: "Ghost" });
|
||||
it("returns null for non-existent id", async () => {
|
||||
const result = await updateCategory(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteCategory", () => {
|
||||
it("reassigns items to Uncategorized (id=1) then deletes", () => {
|
||||
const shelter = createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
createItem(db, { name: "Tent", categoryId: shelter?.id });
|
||||
createItem(db, { name: "Tarp", categoryId: shelter?.id });
|
||||
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 });
|
||||
|
||||
const result = deleteCategory(db, shelter?.id);
|
||||
const result = await deleteCategory(db, shelter?.id);
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
// Items should now be in Uncategorized (id=1)
|
||||
const reassigned = db
|
||||
const reassigned = await db
|
||||
.select()
|
||||
.from(items)
|
||||
.where(eq(items.categoryId, 1))
|
||||
.all();
|
||||
.where(eq(items.categoryId, 1));
|
||||
expect(reassigned).toHaveLength(2);
|
||||
expect(reassigned.map((i) => i.name).sort()).toEqual(["Tarp", "Tent"]);
|
||||
expect(reassigned.map((i: any) => i.name).sort()).toEqual(["Tarp", "Tent"]);
|
||||
});
|
||||
|
||||
it("cannot delete Uncategorized (id=1)", () => {
|
||||
const result = deleteCategory(db, 1);
|
||||
it("cannot delete Uncategorized (id=1)", async () => {
|
||||
const result = await deleteCategory(db, 1);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
@@ -8,17 +8,17 @@ import { createItem } from "../../src/server/services/item.service.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("CSV Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
// ── Export ────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("exportItemsCsv", () => {
|
||||
it("returns correct headers on empty collection", () => {
|
||||
const csv = exportItemsCsv(db);
|
||||
it("returns correct headers on empty collection", async () => {
|
||||
const csv = await exportItemsCsv(db);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines[0]).toBe(
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
@@ -26,8 +26,8 @@ describe("CSV Service", () => {
|
||||
expect(lines).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("exports items with correct values", () => {
|
||||
createItem(db, {
|
||||
it("exports items with correct values", async () => {
|
||||
await createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
@@ -36,7 +36,7 @@ describe("CSV Service", () => {
|
||||
productUrl: "https://example.com/tent",
|
||||
});
|
||||
|
||||
const csv = exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines).toHaveLength(2);
|
||||
expect(lines[1]).toContain("Tent");
|
||||
@@ -47,44 +47,43 @@ describe("CSV Service", () => {
|
||||
expect(lines[1]).toContain("https://example.com/tent");
|
||||
});
|
||||
|
||||
it("properly escapes fields with commas", () => {
|
||||
createItem(db, {
|
||||
it("properly escapes fields with commas", async () => {
|
||||
await createItem(db, {
|
||||
name: "Tent, Ultralight",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const csv = exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines[1]).toContain('"Tent, Ultralight"');
|
||||
});
|
||||
|
||||
it("properly escapes fields with double quotes", () => {
|
||||
createItem(db, {
|
||||
it("properly escapes fields with double quotes", async () => {
|
||||
await createItem(db, {
|
||||
name: 'He said "great tent"',
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const csv = exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines[1]).toContain('"He said ""great tent"""');
|
||||
});
|
||||
|
||||
it("exports multiple items", () => {
|
||||
createItem(db, { name: "Tent", categoryId: 1 });
|
||||
createItem(db, { name: "Sleeping Bag", categoryId: 1 });
|
||||
it("exports multiple items", async () => {
|
||||
await createItem(db, { name: "Tent", categoryId: 1 });
|
||||
await createItem(db, { name: "Sleeping Bag", categoryId: 1 });
|
||||
|
||||
const csv = exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db);
|
||||
const lines = csv.split("\n");
|
||||
expect(lines).toHaveLength(3); // header + 2 items
|
||||
});
|
||||
|
||||
it("exports quantity correctly", () => {
|
||||
it("exports quantity correctly", async () => {
|
||||
// Insert directly to set quantity > 1 (createItem service defaults to 1)
|
||||
db.insert(items)
|
||||
.values({ name: "Bolt", categoryId: 1, quantity: 4 })
|
||||
.run();
|
||||
await db.insert(items)
|
||||
.values({ name: "Bolt", categoryId: 1, quantity: 4 });
|
||||
|
||||
const csv = exportItemsCsv(db);
|
||||
const csv = await exportItemsCsv(db);
|
||||
const lines = csv.split("\n");
|
||||
const fields = lines[1].split(",");
|
||||
// quantity is second field
|
||||
@@ -95,101 +94,101 @@ describe("CSV Service", () => {
|
||||
// ── Import ────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("importItemsCsv", () => {
|
||||
it("parses a valid CSV and creates items", () => {
|
||||
it("parses a valid CSV and creates items", async () => {
|
||||
const csv = [
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
"Tent,1,1200,35000,Camping,Ultralight,https://example.com/tent",
|
||||
"Sleeping Bag,1,800,25000,Camping,,",
|
||||
].join("\n");
|
||||
|
||||
const result = importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, csv);
|
||||
expect(result.imported).toBe(2);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("creates missing category and reports it", () => {
|
||||
it("creates missing category and reports it", async () => {
|
||||
const csv = [
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
"Helmet,1,350,12000,Cycling,,",
|
||||
].join("\n");
|
||||
|
||||
const result = importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.createdCategories).toContain("Cycling");
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("uses existing category (case-insensitive) without creating a duplicate", () => {
|
||||
it("uses existing category (case-insensitive) without creating a duplicate", async () => {
|
||||
const csv = [
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
// "uncategorized" should match the seeded "Uncategorized"
|
||||
"Spork,1,,,uncategorized,,",
|
||||
].join("\n");
|
||||
|
||||
const result = importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.createdCategories).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("skips rows with no name and records an error", () => {
|
||||
it("skips rows with no name and records an error", async () => {
|
||||
const csv = [
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
",1,200,,,",
|
||||
"Tent,1,1200,,,",
|
||||
].join("\n");
|
||||
|
||||
const result = importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.errors).toHaveLength(1);
|
||||
expect(result.errors[0]).toMatch(/missing required field "name"/);
|
||||
});
|
||||
|
||||
it("defaults quantity to 1 when not provided", () => {
|
||||
it("defaults quantity to 1 when not provided", async () => {
|
||||
const csv = [
|
||||
"name,weightGrams,priceCents,category,notes,productUrl",
|
||||
"Tent,1200,35000,Camping,,",
|
||||
].join("\n");
|
||||
|
||||
const result = importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("handles optional fields being empty", () => {
|
||||
it("handles optional fields being empty", async () => {
|
||||
const csv = [
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
"Tent,,,,,",
|
||||
].join("\n");
|
||||
|
||||
const result = importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("handles quoted fields containing commas", () => {
|
||||
it("handles quoted fields containing commas", async () => {
|
||||
const csv = [
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
'"Tent, Ultralight",1,1200,,,',
|
||||
].join("\n");
|
||||
|
||||
const result = importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("returns zero imported on empty CSV", () => {
|
||||
const result = importItemsCsv(db, "");
|
||||
it("returns zero imported on empty CSV", async () => {
|
||||
const result = await importItemsCsv(db, "");
|
||||
expect(result.imported).toBe(0);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("uses Uncategorized when category column is empty", () => {
|
||||
it("uses Uncategorized when category column is empty", async () => {
|
||||
const csv = [
|
||||
"name,quantity,weightGrams,priceCents,category,notes,productUrl",
|
||||
"Tent,1,,,,",
|
||||
].join("\n");
|
||||
|
||||
const result = importItemsCsv(db, csv);
|
||||
const result = await importItemsCsv(db, csv);
|
||||
expect(result.imported).toBe(1);
|
||||
expect(result.createdCategories).toHaveLength(0);
|
||||
});
|
||||
|
||||
@@ -10,15 +10,15 @@ import {
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Item Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
describe("createItem", () => {
|
||||
it("creates item with all fields, returns item with id and timestamps", () => {
|
||||
const item = createItem(db, {
|
||||
it("creates item with all fields, returns item with id and timestamps", async () => {
|
||||
const item = await createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
@@ -39,8 +39,8 @@ describe("Item Service", () => {
|
||||
expect(item?.updatedAt).toBeDefined();
|
||||
});
|
||||
|
||||
it("only name and categoryId are required, other fields optional", () => {
|
||||
const item = createItem(db, { name: "Spork", categoryId: 1 });
|
||||
it("only name and categoryId are required, other fields optional", async () => {
|
||||
const item = await createItem(db, { name: "Spork", categoryId: 1 });
|
||||
|
||||
expect(item).toBeDefined();
|
||||
expect(item?.name).toBe("Spork");
|
||||
@@ -52,11 +52,11 @@ describe("Item Service", () => {
|
||||
});
|
||||
|
||||
describe("getAllItems", () => {
|
||||
it("returns all items with category info joined", () => {
|
||||
createItem(db, { name: "Tent", categoryId: 1 });
|
||||
createItem(db, { name: "Sleeping Bag", categoryId: 1 });
|
||||
it("returns all items with category info joined", async () => {
|
||||
await createItem(db, { name: "Tent", categoryId: 1 });
|
||||
await createItem(db, { name: "Sleeping Bag", categoryId: 1 });
|
||||
|
||||
const all = getAllItems(db);
|
||||
const all = await getAllItems(db);
|
||||
expect(all).toHaveLength(2);
|
||||
expect(all[0].categoryName).toBe("Uncategorized");
|
||||
expect(all[0].categoryIcon).toBeDefined();
|
||||
@@ -64,26 +64,26 @@ describe("Item Service", () => {
|
||||
});
|
||||
|
||||
describe("getItemById", () => {
|
||||
it("returns single item or null", () => {
|
||||
const created = createItem(db, { name: "Tent", categoryId: 1 });
|
||||
const found = getItemById(db, created?.id);
|
||||
it("returns single item or null", async () => {
|
||||
const created = await createItem(db, { name: "Tent", categoryId: 1 });
|
||||
const found = await getItemById(db, created?.id);
|
||||
expect(found).toBeDefined();
|
||||
expect(found?.name).toBe("Tent");
|
||||
|
||||
const notFound = getItemById(db, 9999);
|
||||
const notFound = await getItemById(db, 9999);
|
||||
expect(notFound).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateItem", () => {
|
||||
it("updates specified fields, sets updatedAt", () => {
|
||||
const created = createItem(db, {
|
||||
it("updates specified fields, sets updatedAt", async () => {
|
||||
const created = await createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const updated = updateItem(db, created?.id, {
|
||||
const updated = await updateItem(db, created?.id, {
|
||||
name: "Big Agnes Tent",
|
||||
weightGrams: 1100,
|
||||
});
|
||||
@@ -93,15 +93,15 @@ describe("Item Service", () => {
|
||||
expect(updated?.weightGrams).toBe(1100);
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", () => {
|
||||
const result = updateItem(db, 9999, { name: "Ghost" });
|
||||
it("returns null for non-existent id", async () => {
|
||||
const result = await updateItem(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("duplicateItem", () => {
|
||||
it("creates a copy with '(copy)' suffix in name", () => {
|
||||
const original = createItem(db, {
|
||||
it("creates a copy with '(copy)' suffix in name", async () => {
|
||||
const original = await createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
@@ -110,7 +110,7 @@ describe("Item Service", () => {
|
||||
productUrl: "https://example.com/tent",
|
||||
});
|
||||
|
||||
const copy = duplicateItem(db, original?.id);
|
||||
const copy = await duplicateItem(db, original?.id);
|
||||
|
||||
expect(copy).toBeDefined();
|
||||
expect(copy?.name).toBe("Tent (copy)");
|
||||
@@ -121,39 +121,39 @@ describe("Item Service", () => {
|
||||
expect(copy?.productUrl).toBe("https://example.com/tent");
|
||||
});
|
||||
|
||||
it("copy has a different ID from the original", () => {
|
||||
const original = createItem(db, { name: "Helmet", categoryId: 1 });
|
||||
const copy = duplicateItem(db, original?.id);
|
||||
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);
|
||||
|
||||
expect(copy?.id).not.toBe(original?.id);
|
||||
});
|
||||
|
||||
it("returns null for non-existent item", () => {
|
||||
const result = duplicateItem(db, 9999);
|
||||
it("returns null for non-existent item", async () => {
|
||||
const result = await duplicateItem(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteItem", () => {
|
||||
it("removes item from DB, returns deleted item", () => {
|
||||
const created = createItem(db, {
|
||||
it("removes item from DB, returns deleted item", async () => {
|
||||
const created = await createItem(db, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
imageFilename: "tent.jpg",
|
||||
});
|
||||
|
||||
const deleted = deleteItem(db, created?.id);
|
||||
const deleted = await deleteItem(db, created?.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted?.name).toBe("Tent");
|
||||
expect(deleted?.imageFilename).toBe("tent.jpg");
|
||||
|
||||
// Verify it's gone
|
||||
const found = getItemById(db, created?.id);
|
||||
const found = await getItemById(db, created?.id);
|
||||
expect(found).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", () => {
|
||||
const result = deleteItem(db, 9999);
|
||||
it("returns null for non-existent id", async () => {
|
||||
const result = await deleteItem(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,15 +17,15 @@ function generatePkce() {
|
||||
}
|
||||
|
||||
describe("OAuth Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
describe("Client Registration", () => {
|
||||
it("registers a client and returns clientId (string, non-empty)", () => {
|
||||
const result = registerClient(db, "Test App", [
|
||||
it("registers a client and returns clientId (string, non-empty)", async () => {
|
||||
const result = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
|
||||
@@ -34,32 +34,32 @@ describe("OAuth Service", () => {
|
||||
expect(result.clientId.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("getClient returns registered client with correct clientName and redirectUris (JSON parsed)", () => {
|
||||
it("getClient returns registered client with correct clientName and redirectUris (JSON parsed)", async () => {
|
||||
const redirectUris = ["http://localhost:8080/callback"];
|
||||
const { clientId } = registerClient(db, "Test App", redirectUris);
|
||||
const { clientId } = await registerClient(db, "Test App", redirectUris);
|
||||
|
||||
const client = getClient(db, clientId);
|
||||
const client = await getClient(db, clientId);
|
||||
|
||||
expect(client).not.toBeNull();
|
||||
expect(client!.clientName).toBe("Test App");
|
||||
expect(JSON.parse(client!.redirectUris)).toEqual(redirectUris);
|
||||
});
|
||||
|
||||
it("getClient returns null for unknown client", () => {
|
||||
const client = getClient(db, "unknown-client-id");
|
||||
it("getClient returns null for unknown client", async () => {
|
||||
const client = await getClient(db, "unknown-client-id");
|
||||
|
||||
expect(client).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Authorization Code + PKCE", () => {
|
||||
it("creates an authorization code (non-empty)", () => {
|
||||
const { clientId } = registerClient(db, "Test App", [
|
||||
it("creates an authorization code (non-empty)", async () => {
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
const { challenge } = generatePkce();
|
||||
|
||||
const result = createAuthorizationCode(
|
||||
const result = await createAuthorizationCode(
|
||||
db,
|
||||
clientId,
|
||||
challenge,
|
||||
@@ -73,12 +73,12 @@ describe("OAuth Service", () => {
|
||||
});
|
||||
|
||||
it("exchanges code for tokens with valid PKCE verifier (returns accessToken, refreshToken, expiresIn=3600)", async () => {
|
||||
const { clientId } = registerClient(db, "Test App", [
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
const { verifier, challenge } = generatePkce();
|
||||
|
||||
const { code } = createAuthorizationCode(
|
||||
const { code } = await createAuthorizationCode(
|
||||
db,
|
||||
clientId,
|
||||
challenge,
|
||||
@@ -103,12 +103,12 @@ describe("OAuth Service", () => {
|
||||
});
|
||||
|
||||
it("rejects code exchange with wrong PKCE verifier (returns null)", async () => {
|
||||
const { clientId } = registerClient(db, "Test App", [
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
const { challenge } = generatePkce();
|
||||
|
||||
const { code } = createAuthorizationCode(
|
||||
const { code } = await createAuthorizationCode(
|
||||
db,
|
||||
clientId,
|
||||
challenge,
|
||||
@@ -128,12 +128,12 @@ describe("OAuth Service", () => {
|
||||
});
|
||||
|
||||
it("rejects code exchange with wrong redirect_uri (returns null)", async () => {
|
||||
const { clientId } = registerClient(db, "Test App", [
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
const { verifier, challenge } = generatePkce();
|
||||
|
||||
const { code } = createAuthorizationCode(
|
||||
const { code } = await createAuthorizationCode(
|
||||
db,
|
||||
clientId,
|
||||
challenge,
|
||||
@@ -153,12 +153,12 @@ describe("OAuth Service", () => {
|
||||
});
|
||||
|
||||
it("rejects replayed code - single use (second exchange returns null)", async () => {
|
||||
const { clientId } = registerClient(db, "Test App", [
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
const { verifier, challenge } = generatePkce();
|
||||
|
||||
const { code } = createAuthorizationCode(
|
||||
const { code } = await createAuthorizationCode(
|
||||
db,
|
||||
clientId,
|
||||
challenge,
|
||||
@@ -190,12 +190,12 @@ describe("OAuth Service", () => {
|
||||
|
||||
describe("Token Verification", () => {
|
||||
it("verifies a valid access token (returns true)", async () => {
|
||||
const { clientId } = registerClient(db, "Test App", [
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
const { verifier, challenge } = generatePkce();
|
||||
|
||||
const { code } = createAuthorizationCode(
|
||||
const { code } = await createAuthorizationCode(
|
||||
db,
|
||||
clientId,
|
||||
challenge,
|
||||
@@ -223,12 +223,12 @@ describe("OAuth Service", () => {
|
||||
|
||||
describe("Token Refresh", () => {
|
||||
it("refreshes a valid refresh token and returns new tokens (different accessToken)", async () => {
|
||||
const { clientId } = registerClient(db, "Test App", [
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
const { verifier, challenge } = generatePkce();
|
||||
|
||||
const { code } = createAuthorizationCode(
|
||||
const { code } = await createAuthorizationCode(
|
||||
db,
|
||||
clientId,
|
||||
challenge,
|
||||
@@ -257,12 +257,12 @@ describe("OAuth Service", () => {
|
||||
});
|
||||
|
||||
it("rejects refresh with wrong clientId (returns null)", async () => {
|
||||
const { clientId } = registerClient(db, "Test App", [
|
||||
const { clientId } = await registerClient(db, "Test App", [
|
||||
"http://localhost:8080/callback",
|
||||
]);
|
||||
const { verifier, challenge } = generatePkce();
|
||||
|
||||
const { code } = createAuthorizationCode(
|
||||
const { code } = await createAuthorizationCode(
|
||||
db,
|
||||
clientId,
|
||||
challenge,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { beforeEach, describe, expect, it } from "bun:test";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { items as itemsTable } from "../../src/db/schema.ts";
|
||||
import { createItem } from "../../src/server/services/item.service.ts";
|
||||
import {
|
||||
createSetup,
|
||||
@@ -13,15 +15,15 @@ import {
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Setup Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
describe("createSetup", () => {
|
||||
it("creates setup with name, returns setup with id/timestamps", () => {
|
||||
const setup = createSetup(db, { name: "Day Hike" });
|
||||
it("creates setup with name, returns setup with id/timestamps", async () => {
|
||||
const setup = await createSetup(db, { name: "Day Hike" });
|
||||
|
||||
expect(setup).toBeDefined();
|
||||
expect(setup.id).toBeGreaterThan(0);
|
||||
@@ -32,23 +34,23 @@ describe("Setup Service", () => {
|
||||
});
|
||||
|
||||
describe("getAllSetups", () => {
|
||||
it("returns setups with itemCount, totalWeight, totalCost", () => {
|
||||
const setup = createSetup(db, { name: "Backpacking" });
|
||||
const item1 = createItem(db, {
|
||||
it("returns setups with itemCount, totalWeight, totalCost", async () => {
|
||||
const setup = await createSetup(db, { name: "Backpacking" });
|
||||
const item1 = await createItem(db, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
});
|
||||
const item2 = createItem(db, {
|
||||
const item2 = await createItem(db, {
|
||||
name: "Sleeping Bag",
|
||||
categoryId: 1,
|
||||
weightGrams: 800,
|
||||
priceCents: 20000,
|
||||
});
|
||||
syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
await syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
|
||||
const setups = getAllSetups(db);
|
||||
const setups = await getAllSetups(db);
|
||||
expect(setups).toHaveLength(1);
|
||||
expect(setups[0].name).toBe("Backpacking");
|
||||
expect(setups[0].itemCount).toBe(2);
|
||||
@@ -56,10 +58,10 @@ describe("Setup Service", () => {
|
||||
expect(setups[0].totalCost).toBe(50000);
|
||||
});
|
||||
|
||||
it("returns 0 for weight/cost when setup has no items", () => {
|
||||
createSetup(db, { name: "Empty Setup" });
|
||||
it("returns 0 for weight/cost when setup has no items", async () => {
|
||||
await createSetup(db, { name: "Empty Setup" });
|
||||
|
||||
const setups = getAllSetups(db);
|
||||
const setups = await getAllSetups(db);
|
||||
expect(setups).toHaveLength(1);
|
||||
expect(setups[0].itemCount).toBe(0);
|
||||
expect(setups[0].totalWeight).toBe(0);
|
||||
@@ -68,17 +70,17 @@ describe("Setup Service", () => {
|
||||
});
|
||||
|
||||
describe("getSetupWithItems", () => {
|
||||
it("returns setup with full item details and category info", () => {
|
||||
const setup = createSetup(db, { name: "Day Hike" });
|
||||
const item = createItem(db, {
|
||||
it("returns setup with full item details and category info", async () => {
|
||||
const setup = await createSetup(db, { name: "Day Hike" });
|
||||
const item = await createItem(db, {
|
||||
name: "Water Bottle",
|
||||
categoryId: 1,
|
||||
weightGrams: 200,
|
||||
priceCents: 2500,
|
||||
});
|
||||
syncSetupItems(db, setup.id, [item.id]);
|
||||
await syncSetupItems(db, setup.id, [item.id]);
|
||||
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.name).toBe("Day Hike");
|
||||
expect(result?.items).toHaveLength(1);
|
||||
@@ -87,127 +89,127 @@ describe("Setup Service", () => {
|
||||
expect(result?.items[0].categoryIcon).toBeDefined();
|
||||
});
|
||||
|
||||
it("returns null for non-existent setup", () => {
|
||||
const result = getSetupWithItems(db, 9999);
|
||||
it("returns null for non-existent setup", async () => {
|
||||
const result = await getSetupWithItems(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateSetup", () => {
|
||||
it("updates setup name, returns updated setup", () => {
|
||||
const setup = createSetup(db, { name: "Original" });
|
||||
const updated = updateSetup(db, setup.id, { name: "Renamed" });
|
||||
it("updates setup name, returns updated setup", async () => {
|
||||
const setup = await createSetup(db, { name: "Original" });
|
||||
const updated = await updateSetup(db, setup.id, { name: "Renamed" });
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.name).toBe("Renamed");
|
||||
});
|
||||
|
||||
it("returns null for non-existent setup", () => {
|
||||
const result = updateSetup(db, 9999, { name: "Ghost" });
|
||||
it("returns null for non-existent setup", async () => {
|
||||
const result = await updateSetup(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteSetup", () => {
|
||||
it("removes setup and cascades to setup_items", () => {
|
||||
const setup = createSetup(db, { name: "To Delete" });
|
||||
const item = createItem(db, { name: "Item", categoryId: 1 });
|
||||
syncSetupItems(db, setup.id, [item.id]);
|
||||
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 deleted = deleteSetup(db, setup.id);
|
||||
const deleted = await deleteSetup(db, setup.id);
|
||||
expect(deleted).toBe(true);
|
||||
|
||||
// Setup gone
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns false for non-existent setup", () => {
|
||||
const result = deleteSetup(db, 9999);
|
||||
it("returns false for non-existent setup", async () => {
|
||||
const result = await deleteSetup(db, 9999);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("syncSetupItems", () => {
|
||||
it("sets items for a setup (delete-all + re-insert)", () => {
|
||||
const setup = createSetup(db, { name: "Kit" });
|
||||
const item1 = createItem(db, { name: "Item 1", categoryId: 1 });
|
||||
const item2 = createItem(db, { name: "Item 2", categoryId: 1 });
|
||||
const item3 = createItem(db, { name: "Item 3", categoryId: 1 });
|
||||
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 });
|
||||
|
||||
// Initial sync
|
||||
syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
let result = getSetupWithItems(db, setup.id);
|
||||
await syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
let result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items).toHaveLength(2);
|
||||
|
||||
// Re-sync with different items
|
||||
syncSetupItems(db, setup.id, [item2.id, item3.id]);
|
||||
result = getSetupWithItems(db, setup.id);
|
||||
await syncSetupItems(db, setup.id, [item2.id, item3.id]);
|
||||
result = await getSetupWithItems(db, 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", () => {
|
||||
const setup = createSetup(db, { name: "Kit" });
|
||||
const item = createItem(db, { name: "Item", categoryId: 1 });
|
||||
syncSetupItems(db, setup.id, [item.id]);
|
||||
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]);
|
||||
|
||||
syncSetupItems(db, setup.id, []);
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
await syncSetupItems(db, setup.id, []);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removeSetupItem", () => {
|
||||
it("removes single item from setup", () => {
|
||||
const setup = createSetup(db, { name: "Kit" });
|
||||
const item1 = createItem(db, { name: "Item 1", categoryId: 1 });
|
||||
const item2 = createItem(db, { name: "Item 2", categoryId: 1 });
|
||||
syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
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]);
|
||||
|
||||
removeSetupItem(db, setup.id, item1.id);
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
await removeSetupItem(db, setup.id, item1.id);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items).toHaveLength(1);
|
||||
expect(result?.items[0].name).toBe("Item 2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getSetupWithItems - classification", () => {
|
||||
it("returns classification field defaulting to 'base' for each item", () => {
|
||||
const setup = createSetup(db, { name: "Day Hike" });
|
||||
const item = createItem(db, {
|
||||
it("returns classification field defaulting to 'base' for each item", async () => {
|
||||
const setup = await createSetup(db, { name: "Day Hike" });
|
||||
const item = await createItem(db, {
|
||||
name: "Water Bottle",
|
||||
categoryId: 1,
|
||||
weightGrams: 200,
|
||||
priceCents: 2500,
|
||||
});
|
||||
syncSetupItems(db, setup.id, [item.id]);
|
||||
await syncSetupItems(db, setup.id, [item.id]);
|
||||
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items).toHaveLength(1);
|
||||
expect(result?.items[0].classification).toBe("base");
|
||||
});
|
||||
});
|
||||
|
||||
describe("syncSetupItems - classification preservation", () => {
|
||||
it("preserves existing classifications when re-syncing items", () => {
|
||||
const setup = createSetup(db, { name: "Kit" });
|
||||
const item1 = createItem(db, { name: "Tent", categoryId: 1 });
|
||||
const item2 = createItem(db, { name: "Jacket", categoryId: 1 });
|
||||
const item3 = createItem(db, { name: "Stove", categoryId: 1 });
|
||||
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 });
|
||||
|
||||
// Initial sync
|
||||
syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
await syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
|
||||
// Change classifications
|
||||
updateItemClassification(db, setup.id, item1.id, "worn");
|
||||
updateItemClassification(db, setup.id, item2.id, "consumable");
|
||||
await updateItemClassification(db, setup.id, item1.id, "worn");
|
||||
await updateItemClassification(db, setup.id, item2.id, "consumable");
|
||||
|
||||
// Re-sync with item2 kept and item3 added (item1 removed)
|
||||
syncSetupItems(db, setup.id, [item2.id, item3.id]);
|
||||
await syncSetupItems(db, setup.id, [item2.id, item3.id]);
|
||||
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items).toHaveLength(2);
|
||||
|
||||
const item2Result = result?.items.find((i: any) => i.name === "Jacket");
|
||||
@@ -216,57 +218,57 @@ describe("Setup Service", () => {
|
||||
expect(item3Result?.classification).toBe("base");
|
||||
});
|
||||
|
||||
it("assigns 'base' to newly added items with no prior classification", () => {
|
||||
const setup = createSetup(db, { name: "Kit" });
|
||||
const item1 = createItem(db, { name: "Item 1", categoryId: 1 });
|
||||
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 });
|
||||
|
||||
syncSetupItems(db, setup.id, [item1.id]);
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
await syncSetupItems(db, setup.id, [item1.id]);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items[0].classification).toBe("base");
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateItemClassification", () => {
|
||||
it("sets classification for a specific item in a specific setup", () => {
|
||||
const setup = createSetup(db, { name: "Kit" });
|
||||
const item = createItem(db, { name: "Tent", categoryId: 1 });
|
||||
syncSetupItems(db, setup.id, [item.id]);
|
||||
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]);
|
||||
|
||||
updateItemClassification(db, setup.id, item.id, "worn");
|
||||
await updateItemClassification(db, setup.id, item.id, "worn");
|
||||
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items[0].classification).toBe("worn");
|
||||
});
|
||||
|
||||
it("changes item from default 'base' to 'worn'", () => {
|
||||
const setup = createSetup(db, { name: "Kit" });
|
||||
const item = createItem(db, { name: "Jacket", categoryId: 1 });
|
||||
syncSetupItems(db, setup.id, [item.id]);
|
||||
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]);
|
||||
|
||||
// Verify default
|
||||
let result = getSetupWithItems(db, setup.id);
|
||||
let result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items[0].classification).toBe("base");
|
||||
|
||||
// Update
|
||||
updateItemClassification(db, setup.id, item.id, "worn");
|
||||
await updateItemClassification(db, setup.id, item.id, "worn");
|
||||
|
||||
result = getSetupWithItems(db, setup.id);
|
||||
result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items[0].classification).toBe("worn");
|
||||
});
|
||||
|
||||
it("same item in two different setups can have different classifications", () => {
|
||||
const setup1 = createSetup(db, { name: "Hiking" });
|
||||
const setup2 = createSetup(db, { name: "Biking" });
|
||||
const item = createItem(db, { name: "Jacket", categoryId: 1 });
|
||||
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 });
|
||||
|
||||
syncSetupItems(db, setup1.id, [item.id]);
|
||||
syncSetupItems(db, setup2.id, [item.id]);
|
||||
await syncSetupItems(db, setup1.id, [item.id]);
|
||||
await syncSetupItems(db, setup2.id, [item.id]);
|
||||
|
||||
updateItemClassification(db, setup1.id, item.id, "worn");
|
||||
updateItemClassification(db, setup2.id, item.id, "base");
|
||||
await updateItemClassification(db, setup1.id, item.id, "worn");
|
||||
await updateItemClassification(db, setup2.id, item.id, "base");
|
||||
|
||||
const result1 = getSetupWithItems(db, setup1.id);
|
||||
const result2 = getSetupWithItems(db, setup2.id);
|
||||
const result1 = await getSetupWithItems(db, setup1.id);
|
||||
const result2 = await getSetupWithItems(db, setup2.id);
|
||||
|
||||
expect(result1?.items[0].classification).toBe("worn");
|
||||
expect(result2?.items[0].classification).toBe("base");
|
||||
@@ -274,18 +276,16 @@ describe("Setup Service", () => {
|
||||
});
|
||||
|
||||
describe("cascade behavior", () => {
|
||||
it("deleting a collection item removes it from all setups", () => {
|
||||
const setup = createSetup(db, { name: "Kit" });
|
||||
const item1 = createItem(db, { name: "Item 1", categoryId: 1 });
|
||||
const item2 = createItem(db, { name: "Item 2", categoryId: 1 });
|
||||
syncSetupItems(db, setup.id, [item1.id, item2.id]);
|
||||
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]);
|
||||
|
||||
// Delete item1 from collection (need direct DB access)
|
||||
const { items: itemsTable } = require("../../src/db/schema.ts");
|
||||
const { eq } = require("drizzle-orm");
|
||||
db.delete(itemsTable).where(eq(itemsTable.id, item1.id)).run();
|
||||
// Delete item1 from collection
|
||||
await db.delete(itemsTable).where(eq(itemsTable.id, item1.id));
|
||||
|
||||
const result = getSetupWithItems(db, setup.id);
|
||||
const result = await getSetupWithItems(db, setup.id);
|
||||
expect(result?.items).toHaveLength(1);
|
||||
expect(result?.items[0].name).toBe("Item 2");
|
||||
});
|
||||
|
||||
@@ -14,15 +14,15 @@ import {
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Thread Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
describe("createThread", () => {
|
||||
it("creates thread with name, returns thread with id/status/timestamps", () => {
|
||||
const thread = createThread(db, { name: "New Tent", categoryId: 1 });
|
||||
it("creates thread with name, returns thread with id/status/timestamps", async () => {
|
||||
const thread = await createThread(db, { name: "New Tent", categoryId: 1 });
|
||||
|
||||
expect(thread).toBeDefined();
|
||||
expect(thread.id).toBeGreaterThan(0);
|
||||
@@ -35,23 +35,23 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("getAllThreads", () => {
|
||||
it("returns active threads with candidateCount and price range", () => {
|
||||
const thread = createThread(db, {
|
||||
it("returns active threads with candidateCount and price range", async () => {
|
||||
const thread = await createThread(db, {
|
||||
name: "Backpack Options",
|
||||
categoryId: 1,
|
||||
});
|
||||
createCandidate(db, thread.id, {
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Pack A",
|
||||
categoryId: 1,
|
||||
priceCents: 20000,
|
||||
});
|
||||
createCandidate(db, thread.id, {
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Pack B",
|
||||
categoryId: 1,
|
||||
priceCents: 35000,
|
||||
});
|
||||
|
||||
const threads = getAllThreads(db);
|
||||
const threads = await getAllThreads(db);
|
||||
expect(threads).toHaveLength(1);
|
||||
expect(threads[0].name).toBe("Backpack Options");
|
||||
expect(threads[0].candidateCount).toBe(2);
|
||||
@@ -59,45 +59,45 @@ describe("Thread Service", () => {
|
||||
expect(threads[0].maxPriceCents).toBe(35000);
|
||||
});
|
||||
|
||||
it("excludes resolved threads by default", () => {
|
||||
const _t1 = createThread(db, { name: "Active Thread", categoryId: 1 });
|
||||
const t2 = createThread(db, { name: "Resolved Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, t2.id, {
|
||||
it("excludes resolved threads by default", async () => {
|
||||
const _t1 = await createThread(db, { name: "Active Thread", categoryId: 1 });
|
||||
const t2 = await createThread(db, { name: "Resolved Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, t2.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
resolveThread(db, t2.id, candidate.id);
|
||||
await resolveThread(db, t2.id, candidate.id);
|
||||
|
||||
const active = getAllThreads(db);
|
||||
const active = await getAllThreads(db);
|
||||
expect(active).toHaveLength(1);
|
||||
expect(active[0].name).toBe("Active Thread");
|
||||
});
|
||||
|
||||
it("includes resolved threads when includeResolved=true", () => {
|
||||
const _t1 = createThread(db, { name: "Active Thread", categoryId: 1 });
|
||||
const t2 = createThread(db, { name: "Resolved Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, t2.id, {
|
||||
it("includes resolved threads when includeResolved=true", async () => {
|
||||
const _t1 = await createThread(db, { name: "Active Thread", categoryId: 1 });
|
||||
const t2 = await createThread(db, { name: "Resolved Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, t2.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
resolveThread(db, t2.id, candidate.id);
|
||||
await resolveThread(db, t2.id, candidate.id);
|
||||
|
||||
const all = getAllThreads(db, true);
|
||||
const all = await getAllThreads(db, true);
|
||||
expect(all).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getThreadWithCandidates", () => {
|
||||
it("returns thread with nested candidates array including category info", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
createCandidate(db, thread.id, {
|
||||
it("returns thread with nested candidates array including category info", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
});
|
||||
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.name).toBe("Tent Options");
|
||||
expect(result?.candidates).toHaveLength(1);
|
||||
@@ -106,21 +106,21 @@ describe("Thread Service", () => {
|
||||
expect(result?.candidates[0].categoryIcon).toBeDefined();
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", () => {
|
||||
const result = getThreadWithCandidates(db, 9999);
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await getThreadWithCandidates(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("includes pros and cons on each candidate", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
createCandidate(db, thread.id, {
|
||||
it("includes pros and cons on each candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
pros: "Lightweight",
|
||||
cons: "Pricey",
|
||||
});
|
||||
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates[0].pros).toBe("Lightweight");
|
||||
expect(result?.candidates[0].cons).toBe("Pricey");
|
||||
@@ -128,9 +128,9 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("createCandidate", () => {
|
||||
it("adds candidate to thread with all item-compatible fields", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("adds candidate to thread with all item-compatible fields", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Tent A",
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
@@ -150,9 +150,9 @@ describe("Thread Service", () => {
|
||||
expect(candidate.productUrl).toBe("https://example.com/tent");
|
||||
});
|
||||
|
||||
it("stores and returns pros and cons", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("stores and returns pros and cons", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
pros: "Lightweight\nGood reviews",
|
||||
@@ -163,9 +163,9 @@ describe("Thread Service", () => {
|
||||
expect(candidate.cons).toBe("Expensive");
|
||||
});
|
||||
|
||||
it("returns null for pros and cons when not provided", () => {
|
||||
const thread = createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("returns null for pros and cons when not provided", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Options", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Tent B",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -176,14 +176,14 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("updateCandidate", () => {
|
||||
it("updates candidate fields, returns updated candidate", () => {
|
||||
const thread = createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("updates candidate fields, returns updated candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const updated = updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
name: "Updated Name",
|
||||
priceCents: 15000,
|
||||
});
|
||||
@@ -193,20 +193,20 @@ describe("Thread Service", () => {
|
||||
expect(updated?.priceCents).toBe(15000);
|
||||
});
|
||||
|
||||
it("returns null for non-existent candidate", () => {
|
||||
const result = updateCandidate(db, 9999, { name: "Ghost" });
|
||||
it("returns null for non-existent candidate", async () => {
|
||||
const result = await updateCandidate(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("can set and clear pros and cons", () => {
|
||||
const thread = createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("can set and clear pros and cons", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Set pros and cons
|
||||
const withPros = updateCandidate(db, candidate.id, {
|
||||
const withPros = await updateCandidate(db, candidate.id, {
|
||||
pros: "Lightweight",
|
||||
cons: "Expensive",
|
||||
});
|
||||
@@ -214,7 +214,7 @@ describe("Thread Service", () => {
|
||||
expect(withPros?.cons).toBe("Expensive");
|
||||
|
||||
// Clear pros and cons by setting to empty string
|
||||
const cleared = updateCandidate(db, candidate.id, {
|
||||
const cleared = await updateCandidate(db, candidate.id, {
|
||||
pros: "",
|
||||
cons: "",
|
||||
});
|
||||
@@ -225,67 +225,67 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("deleteCandidate", () => {
|
||||
it("removes candidate, returns deleted candidate", () => {
|
||||
const thread = createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("removes candidate, returns deleted candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "To Delete",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const deleted = deleteCandidate(db, candidate.id);
|
||||
const deleted = await deleteCandidate(db, candidate.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted?.name).toBe("To Delete");
|
||||
|
||||
// Verify it's gone
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result?.candidates).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("returns null for non-existent candidate", () => {
|
||||
const result = deleteCandidate(db, 9999);
|
||||
it("returns null for non-existent candidate", async () => {
|
||||
const result = await deleteCandidate(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateThread", () => {
|
||||
it("updates thread name", () => {
|
||||
const thread = createThread(db, { name: "Original", categoryId: 1 });
|
||||
const updated = updateThread(db, thread.id, { name: "Renamed" });
|
||||
it("updates thread name", async () => {
|
||||
const thread = await createThread(db, { name: "Original", categoryId: 1 });
|
||||
const updated = await updateThread(db, thread.id, { name: "Renamed" });
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated?.name).toBe("Renamed");
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", () => {
|
||||
const result = updateThread(db, 9999, { name: "Ghost" });
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await updateThread(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteThread", () => {
|
||||
it("removes thread and cascading candidates", () => {
|
||||
const thread = createThread(db, { name: "To Delete", categoryId: 1 });
|
||||
createCandidate(db, thread.id, { name: "Candidate", categoryId: 1 });
|
||||
it("removes thread and cascading candidates", async () => {
|
||||
const thread = await createThread(db, { name: "To Delete", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, { name: "Candidate", categoryId: 1 });
|
||||
|
||||
const deleted = deleteThread(db, thread.id);
|
||||
const deleted = await deleteThread(db, thread.id);
|
||||
expect(deleted).toBeDefined();
|
||||
expect(deleted?.name).toBe("To Delete");
|
||||
|
||||
// Thread and candidates gone
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for non-existent thread", () => {
|
||||
const result = deleteThread(db, 9999);
|
||||
it("returns null for non-existent thread", async () => {
|
||||
const result = await deleteThread(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("candidate status", () => {
|
||||
it("createCandidate without status returns a candidate with status 'researching'", () => {
|
||||
const thread = createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("createCandidate without status returns a candidate with status 'researching'", async () => {
|
||||
const thread = await createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "No Status",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -293,9 +293,9 @@ describe("Thread Service", () => {
|
||||
expect(candidate.status).toBe("researching");
|
||||
});
|
||||
|
||||
it("createCandidate with status 'ordered' returns a candidate with status 'ordered'", () => {
|
||||
const thread = createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("createCandidate with status 'ordered' returns a candidate with status 'ordered'", async () => {
|
||||
const thread = await createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Ordered Item",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
@@ -304,58 +304,58 @@ describe("Thread Service", () => {
|
||||
expect(candidate.status).toBe("ordered");
|
||||
});
|
||||
|
||||
it("updateCandidate can change status from 'researching' to 'ordered'", () => {
|
||||
const thread = createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("updateCandidate can change status from 'researching' to 'ordered'", async () => {
|
||||
const thread = await createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Status Change",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
expect(candidate.status).toBe("researching");
|
||||
|
||||
const updated = updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
expect(updated?.status).toBe("ordered");
|
||||
});
|
||||
|
||||
it("updateCandidate can change status from 'ordered' to 'arrived'", () => {
|
||||
const thread = createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("updateCandidate can change status from 'ordered' to 'arrived'", async () => {
|
||||
const thread = await createThread(db, { name: "Test Thread", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Arriving Item",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
const updated = updateCandidate(db, candidate.id, {
|
||||
const updated = await updateCandidate(db, candidate.id, {
|
||||
status: "arrived",
|
||||
});
|
||||
|
||||
expect(updated?.status).toBe("arrived");
|
||||
});
|
||||
|
||||
it("getThreadWithCandidates includes status field on each candidate", () => {
|
||||
const thread = createThread(db, { name: "Status Thread", categoryId: 1 });
|
||||
createCandidate(db, thread.id, {
|
||||
it("getThreadWithCandidates includes status field on each candidate", async () => {
|
||||
const thread = await createThread(db, { name: "Status Thread", categoryId: 1 });
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Candidate A",
|
||||
categoryId: 1,
|
||||
});
|
||||
createCandidate(db, thread.id, {
|
||||
await createCandidate(db, thread.id, {
|
||||
name: "Candidate B",
|
||||
categoryId: 1,
|
||||
status: "ordered",
|
||||
});
|
||||
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates).toHaveLength(2);
|
||||
|
||||
const candidateA = result?.candidates.find(
|
||||
(c) => c.name === "Candidate A",
|
||||
(c: any) => c.name === "Candidate A",
|
||||
);
|
||||
const candidateB = result?.candidates.find(
|
||||
(c) => c.name === "Candidate B",
|
||||
(c: any) => c.name === "Candidate B",
|
||||
);
|
||||
|
||||
expect(candidateA?.status).toBe("researching");
|
||||
@@ -364,43 +364,43 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("sort_order ordering", () => {
|
||||
it("getThreadWithCandidates returns candidates ordered by sort_order ascending", () => {
|
||||
const thread = createThread(db, { name: "Order Test", categoryId: 1 });
|
||||
const c1 = createCandidate(db, thread.id, {
|
||||
it("getThreadWithCandidates returns candidates ordered by sort_order ascending", async () => {
|
||||
const thread = await createThread(db, { name: "Order Test", categoryId: 1 });
|
||||
const c1 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c2 = createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c3 = createCandidate(db, thread.id, {
|
||||
const c3 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 3",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Manually set sort_orders out of creation order using reorderCandidates
|
||||
reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
await reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
|
||||
const result = getThreadWithCandidates(db, thread.id);
|
||||
const result = await getThreadWithCandidates(db, thread.id);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.candidates[0].id).toBe(c3.id);
|
||||
expect(result?.candidates[1].id).toBe(c1.id);
|
||||
expect(result?.candidates[2].id).toBe(c2.id);
|
||||
});
|
||||
|
||||
it("createCandidate assigns sort_order = max existing sort_order + 1000", () => {
|
||||
const thread = createThread(db, { name: "Append Test", categoryId: 1 });
|
||||
it("createCandidate assigns sort_order = max existing sort_order + 1000", async () => {
|
||||
const thread = await createThread(db, { name: "Append Test", categoryId: 1 });
|
||||
|
||||
// First candidate should get sort_order 1000
|
||||
const c1 = createCandidate(db, thread.id, {
|
||||
const c1 = await createCandidate(db, thread.id, {
|
||||
name: "First",
|
||||
categoryId: 1,
|
||||
});
|
||||
expect(c1.sortOrder).toBe(1000);
|
||||
|
||||
// Second candidate should get sort_order 2000
|
||||
const c2 = createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
name: "Second",
|
||||
categoryId: 1,
|
||||
});
|
||||
@@ -409,56 +409,56 @@ describe("Thread Service", () => {
|
||||
});
|
||||
|
||||
describe("reorderCandidates", () => {
|
||||
it("reorderCandidates updates sort_order so querying returns candidates in new order", () => {
|
||||
const thread = createThread(db, { name: "Reorder Test", categoryId: 1 });
|
||||
const c1 = createCandidate(db, thread.id, {
|
||||
it("reorderCandidates updates sort_order so querying returns candidates in new order", async () => {
|
||||
const thread = await createThread(db, { name: "Reorder Test", categoryId: 1 });
|
||||
const c1 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c2 = createCandidate(db, thread.id, {
|
||||
const c2 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
const c3 = createCandidate(db, thread.id, {
|
||||
const c3 = await createCandidate(db, thread.id, {
|
||||
name: "Candidate 3",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const result = reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
const result = await reorderCandidates(db, thread.id, [c3.id, c1.id, c2.id]);
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const fetched = getThreadWithCandidates(db, thread.id);
|
||||
const fetched = await getThreadWithCandidates(db, thread.id);
|
||||
expect(fetched?.candidates[0].id).toBe(c3.id);
|
||||
expect(fetched?.candidates[1].id).toBe(c1.id);
|
||||
expect(fetched?.candidates[2].id).toBe(c2.id);
|
||||
});
|
||||
|
||||
it("returns { success: false, error } when thread status is 'resolved'", () => {
|
||||
const thread = createThread(db, {
|
||||
it("returns { success: false, error } when thread status is 'resolved'", async () => {
|
||||
const thread = await createThread(db, {
|
||||
name: "Resolved Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
resolveThread(db, thread.id, candidate.id);
|
||||
await resolveThread(db, thread.id, candidate.id);
|
||||
|
||||
const result = reorderCandidates(db, thread.id, [candidate.id]);
|
||||
const result = await reorderCandidates(db, thread.id, [candidate.id]);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("returns { success: false } when thread does not exist", () => {
|
||||
const result = reorderCandidates(db, 9999, [1, 2]);
|
||||
it("returns { success: false } when thread does not exist", async () => {
|
||||
const result = await reorderCandidates(db, 9999, [1, 2]);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveThread", () => {
|
||||
it("atomically creates collection item from candidate data and archives thread", () => {
|
||||
const thread = createThread(db, { name: "Tent Decision", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
it("atomically creates collection item from candidate data and archives thread", async () => {
|
||||
const thread = await createThread(db, { name: "Tent Decision", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Winner Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
@@ -467,7 +467,7 @@ describe("Thread Service", () => {
|
||||
productUrl: "https://example.com/tent",
|
||||
});
|
||||
|
||||
const result = resolveThread(db, thread.id, candidate.id);
|
||||
const result = await resolveThread(db, thread.id, candidate.id);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.item).toBeDefined();
|
||||
expect(result.item?.name).toBe("Winner Tent");
|
||||
@@ -478,44 +478,44 @@ describe("Thread Service", () => {
|
||||
expect(result.item?.productUrl).toBe("https://example.com/tent");
|
||||
|
||||
// Thread should be resolved
|
||||
const resolved = getThreadWithCandidates(db, thread.id);
|
||||
const resolved = await getThreadWithCandidates(db, thread.id);
|
||||
expect(resolved?.status).toBe("resolved");
|
||||
expect(resolved?.resolvedCandidateId).toBe(candidate.id);
|
||||
});
|
||||
|
||||
it("fails if thread is not active", () => {
|
||||
const thread = createThread(db, {
|
||||
it("fails if thread is not active", async () => {
|
||||
const thread = await createThread(db, {
|
||||
name: "Already Resolved",
|
||||
categoryId: 1,
|
||||
});
|
||||
const candidate = createCandidate(db, thread.id, {
|
||||
const candidate = await createCandidate(db, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
resolveThread(db, thread.id, candidate.id);
|
||||
await resolveThread(db, thread.id, candidate.id);
|
||||
|
||||
// Try to resolve again
|
||||
const result = resolveThread(db, thread.id, candidate.id);
|
||||
const result = await resolveThread(db, thread.id, candidate.id);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("fails if candidate is not in thread", () => {
|
||||
const thread1 = createThread(db, { name: "Thread 1", categoryId: 1 });
|
||||
const thread2 = createThread(db, { name: "Thread 2", categoryId: 1 });
|
||||
const candidate = createCandidate(db, thread2.id, {
|
||||
it("fails if candidate is not in thread", async () => {
|
||||
const thread1 = await createThread(db, { name: "Thread 1", categoryId: 1 });
|
||||
const thread2 = await createThread(db, { name: "Thread 2", categoryId: 1 });
|
||||
const candidate = await createCandidate(db, thread2.id, {
|
||||
name: "Wrong Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const result = resolveThread(db, thread1.id, candidate.id);
|
||||
const result = await resolveThread(db, thread1.id, candidate.id);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("fails if candidate not found", () => {
|
||||
const thread = createThread(db, { name: "Test", categoryId: 1 });
|
||||
const result = resolveThread(db, thread.id, 9999);
|
||||
it("fails if candidate not found", async () => {
|
||||
const thread = await createThread(db, { name: "Test", categoryId: 1 });
|
||||
const result = await resolveThread(db, thread.id, 9999);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
@@ -8,29 +8,29 @@ import {
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
describe("Totals Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
let db: any;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
beforeEach(async () => {
|
||||
db = await createTestDb();
|
||||
});
|
||||
|
||||
describe("getCategoryTotals", () => {
|
||||
it("returns weight sum, cost sum, item count per category", () => {
|
||||
const shelter = createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
createItem(db, {
|
||||
it("returns weight sum, cost sum, item count per category", async () => {
|
||||
const shelter = await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
await createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: shelter?.id,
|
||||
});
|
||||
createItem(db, {
|
||||
await createItem(db, {
|
||||
name: "Tarp",
|
||||
weightGrams: 300,
|
||||
priceCents: 8000,
|
||||
categoryId: shelter?.id,
|
||||
});
|
||||
|
||||
const totals = getCategoryTotals(db);
|
||||
const totals = await getCategoryTotals(db);
|
||||
expect(totals).toHaveLength(1); // Only Shelter has items
|
||||
expect(totals[0].categoryName).toBe("Shelter");
|
||||
expect(totals[0].totalWeight).toBe(1500);
|
||||
@@ -38,38 +38,38 @@ describe("Totals Service", () => {
|
||||
expect(totals[0].itemCount).toBe(2);
|
||||
});
|
||||
|
||||
it("excludes empty categories (no items)", () => {
|
||||
createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
it("excludes empty categories (no items)", async () => {
|
||||
await createCategory(db, { name: "Shelter", icon: "tent" });
|
||||
// No items added
|
||||
const totals = getCategoryTotals(db);
|
||||
const totals = await getCategoryTotals(db);
|
||||
expect(totals).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getGlobalTotals", () => {
|
||||
it("returns overall weight, cost, count", () => {
|
||||
createItem(db, {
|
||||
it("returns overall weight, cost, count", async () => {
|
||||
await createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: 1,
|
||||
});
|
||||
createItem(db, {
|
||||
await createItem(db, {
|
||||
name: "Spork",
|
||||
weightGrams: 20,
|
||||
priceCents: 500,
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const totals = getGlobalTotals(db);
|
||||
const totals = await getGlobalTotals(db);
|
||||
expect(totals).toBeDefined();
|
||||
expect(totals?.totalWeight).toBe(1220);
|
||||
expect(totals?.totalCost).toBe(35500);
|
||||
expect(totals?.itemCount).toBe(2);
|
||||
});
|
||||
|
||||
it("returns zeros when no items exist", () => {
|
||||
const totals = getGlobalTotals(db);
|
||||
it("returns zeros when no items exist", async () => {
|
||||
const totals = await getGlobalTotals(db);
|
||||
expect(totals).toBeDefined();
|
||||
expect(totals?.totalWeight).toBe(0);
|
||||
expect(totals?.totalCost).toBe(0);
|
||||
|
||||
Reference in New Issue
Block a user