- 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>
98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
import { beforeEach, describe, expect, it } from "bun:test";
|
|
import { eq } from "drizzle-orm";
|
|
import { items } from "../../src/db/schema.ts";
|
|
import {
|
|
createCategory,
|
|
deleteCategory,
|
|
getAllCategories,
|
|
updateCategory,
|
|
} from "../../src/server/services/category.service.ts";
|
|
import { createItem } from "../../src/server/services/item.service.ts";
|
|
import { createTestDb } from "../helpers/db.ts";
|
|
|
|
describe("Category Service", () => {
|
|
let db: any;
|
|
|
|
beforeEach(async () => {
|
|
db = await createTestDb();
|
|
});
|
|
|
|
describe("createCategory", () => {
|
|
it("creates with name and icon", async () => {
|
|
const cat = await createCategory(db, { name: "Shelter", icon: "tent" });
|
|
|
|
expect(cat).toBeDefined();
|
|
expect(cat?.id).toBeGreaterThan(0);
|
|
expect(cat?.name).toBe("Shelter");
|
|
expect(cat?.icon).toBe("tent");
|
|
});
|
|
|
|
it("uses default icon if not provided", async () => {
|
|
const cat = await createCategory(db, { name: "Cooking" });
|
|
|
|
expect(cat).toBeDefined();
|
|
expect(cat?.icon).toBe("package");
|
|
});
|
|
});
|
|
|
|
describe("getAllCategories", () => {
|
|
it("returns all categories", async () => {
|
|
await createCategory(db, { name: "Shelter", icon: "tent" });
|
|
await createCategory(db, { name: "Cooking", icon: "cooking-pot" });
|
|
|
|
const all = await getAllCategories(db);
|
|
// Includes seeded Uncategorized + 2 new
|
|
expect(all.length).toBeGreaterThanOrEqual(3);
|
|
});
|
|
});
|
|
|
|
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" });
|
|
|
|
expect(updated).toBeDefined();
|
|
expect(updated?.name).toBe("Sleep System");
|
|
expect(updated?.icon).toBe("tent");
|
|
});
|
|
|
|
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", async () => {
|
|
const result = await updateCategory(db, 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 });
|
|
|
|
const result = await deleteCategory(db, shelter?.id);
|
|
expect(result.success).toBe(true);
|
|
|
|
// Items should now be in Uncategorized (id=1)
|
|
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"]);
|
|
});
|
|
|
|
it("cannot delete Uncategorized (id=1)", async () => {
|
|
const result = await deleteCategory(db, 1);
|
|
expect(result.success).toBe(false);
|
|
expect(result.error).toBeDefined();
|
|
});
|
|
});
|
|
});
|