test(01-02): add failing tests for item, category, and totals services
- Item CRUD tests: create, getAll, getById, update, delete - Category CRUD tests: create, getAll, update, delete with reassignment - Totals tests: per-category and global aggregates Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
98
tests/services/category.service.test.ts
Normal file
98
tests/services/category.service.test.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import {
|
||||
getAllCategories,
|
||||
createCategory,
|
||||
updateCategory,
|
||||
deleteCategory,
|
||||
} from "../../src/server/services/category.service.ts";
|
||||
import { createItem } from "../../src/server/services/item.service.ts";
|
||||
import { items } from "../../src/db/schema.ts";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
describe("Category Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
});
|
||||
|
||||
describe("createCategory", () => {
|
||||
it("creates with name and emoji", () => {
|
||||
const cat = createCategory(db, { name: "Shelter", emoji: "\u{26FA}" });
|
||||
|
||||
expect(cat).toBeDefined();
|
||||
expect(cat!.id).toBeGreaterThan(0);
|
||||
expect(cat!.name).toBe("Shelter");
|
||||
expect(cat!.emoji).toBe("\u{26FA}");
|
||||
});
|
||||
|
||||
it("uses default emoji if not provided", () => {
|
||||
const cat = createCategory(db, { name: "Cooking" });
|
||||
|
||||
expect(cat).toBeDefined();
|
||||
expect(cat!.emoji).toBe("\u{1F4E6}");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllCategories", () => {
|
||||
it("returns all categories", () => {
|
||||
createCategory(db, { name: "Shelter", emoji: "\u{26FA}" });
|
||||
createCategory(db, { name: "Cooking", emoji: "\u{1F373}" });
|
||||
|
||||
const all = getAllCategories(db);
|
||||
// Includes seeded Uncategorized + 2 new
|
||||
expect(all.length).toBeGreaterThanOrEqual(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateCategory", () => {
|
||||
it("renames category", () => {
|
||||
const cat = createCategory(db, { name: "Shelter", emoji: "\u{26FA}" });
|
||||
const updated = updateCategory(db, cat!.id, { name: "Sleep System" });
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated!.name).toBe("Sleep System");
|
||||
expect(updated!.emoji).toBe("\u{26FA}");
|
||||
});
|
||||
|
||||
it("changes emoji", () => {
|
||||
const cat = createCategory(db, { name: "Shelter", emoji: "\u{26FA}" });
|
||||
const updated = updateCategory(db, cat!.id, { emoji: "\u{1F3E0}" });
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated!.emoji).toBe("\u{1F3E0}");
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", () => {
|
||||
const result = 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", emoji: "\u{26FA}" });
|
||||
createItem(db, { name: "Tent", categoryId: shelter!.id });
|
||||
createItem(db, { name: "Tarp", categoryId: shelter!.id });
|
||||
|
||||
const result = deleteCategory(db, shelter!.id);
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
// Items should now be in Uncategorized (id=1)
|
||||
const reassigned = db
|
||||
.select()
|
||||
.from(items)
|
||||
.where(eq(items.categoryId, 1))
|
||||
.all();
|
||||
expect(reassigned).toHaveLength(2);
|
||||
expect(reassigned.map((i) => i.name).sort()).toEqual(["Tarp", "Tent"]);
|
||||
});
|
||||
|
||||
it("cannot delete Uncategorized (id=1)", () => {
|
||||
const result = deleteCategory(db, 1);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
127
tests/services/item.service.test.ts
Normal file
127
tests/services/item.service.test.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import {
|
||||
getAllItems,
|
||||
getItemById,
|
||||
createItem,
|
||||
updateItem,
|
||||
deleteItem,
|
||||
} from "../../src/server/services/item.service.ts";
|
||||
|
||||
describe("Item Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
});
|
||||
|
||||
describe("createItem", () => {
|
||||
it("creates item with all fields, returns item with id and timestamps", () => {
|
||||
const item = createItem(
|
||||
db,
|
||||
{
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: 1,
|
||||
notes: "Ultralight 2-person",
|
||||
productUrl: "https://example.com/tent",
|
||||
},
|
||||
);
|
||||
|
||||
expect(item).toBeDefined();
|
||||
expect(item!.id).toBeGreaterThan(0);
|
||||
expect(item!.name).toBe("Tent");
|
||||
expect(item!.weightGrams).toBe(1200);
|
||||
expect(item!.priceCents).toBe(35000);
|
||||
expect(item!.categoryId).toBe(1);
|
||||
expect(item!.notes).toBe("Ultralight 2-person");
|
||||
expect(item!.productUrl).toBe("https://example.com/tent");
|
||||
expect(item!.createdAt).toBeDefined();
|
||||
expect(item!.updatedAt).toBeDefined();
|
||||
});
|
||||
|
||||
it("only name and categoryId are required, other fields optional", () => {
|
||||
const item = createItem(db, { name: "Spork", categoryId: 1 });
|
||||
|
||||
expect(item).toBeDefined();
|
||||
expect(item!.name).toBe("Spork");
|
||||
expect(item!.weightGrams).toBeNull();
|
||||
expect(item!.priceCents).toBeNull();
|
||||
expect(item!.notes).toBeNull();
|
||||
expect(item!.productUrl).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllItems", () => {
|
||||
it("returns all items with category info joined", () => {
|
||||
createItem(db, { name: "Tent", categoryId: 1 });
|
||||
createItem(db, { name: "Sleeping Bag", categoryId: 1 });
|
||||
|
||||
const all = getAllItems(db);
|
||||
expect(all).toHaveLength(2);
|
||||
expect(all[0].categoryName).toBe("Uncategorized");
|
||||
expect(all[0].categoryEmoji).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getItemById", () => {
|
||||
it("returns single item or null", () => {
|
||||
const created = createItem(db, { name: "Tent", categoryId: 1 });
|
||||
const found = getItemById(db, created!.id);
|
||||
expect(found).toBeDefined();
|
||||
expect(found!.name).toBe("Tent");
|
||||
|
||||
const notFound = getItemById(db, 9999);
|
||||
expect(notFound).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateItem", () => {
|
||||
it("updates specified fields, sets updatedAt", () => {
|
||||
const created = createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const updated = updateItem(db, created!.id, {
|
||||
name: "Big Agnes Tent",
|
||||
weightGrams: 1100,
|
||||
});
|
||||
|
||||
expect(updated).toBeDefined();
|
||||
expect(updated!.name).toBe("Big Agnes Tent");
|
||||
expect(updated!.weightGrams).toBe(1100);
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", () => {
|
||||
const result = updateItem(db, 9999, { name: "Ghost" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteItem", () => {
|
||||
it("removes item from DB, returns deleted item", () => {
|
||||
const created = createItem(db, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
imageFilename: "tent.jpg",
|
||||
});
|
||||
|
||||
const deleted = 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);
|
||||
expect(found).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for non-existent id", () => {
|
||||
const result = deleteItem(db, 9999);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
79
tests/services/totals.test.ts
Normal file
79
tests/services/totals.test.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { createItem } from "../../src/server/services/item.service.ts";
|
||||
import { createCategory } from "../../src/server/services/category.service.ts";
|
||||
import {
|
||||
getCategoryTotals,
|
||||
getGlobalTotals,
|
||||
} from "../../src/server/services/totals.service.ts";
|
||||
|
||||
describe("Totals Service", () => {
|
||||
let db: ReturnType<typeof createTestDb>;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
});
|
||||
|
||||
describe("getCategoryTotals", () => {
|
||||
it("returns weight sum, cost sum, item count per category", () => {
|
||||
const shelter = createCategory(db, { name: "Shelter", emoji: "\u{26FA}" });
|
||||
createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: shelter!.id,
|
||||
});
|
||||
createItem(db, {
|
||||
name: "Tarp",
|
||||
weightGrams: 300,
|
||||
priceCents: 8000,
|
||||
categoryId: shelter!.id,
|
||||
});
|
||||
|
||||
const totals = getCategoryTotals(db);
|
||||
expect(totals).toHaveLength(1); // Only Shelter has items
|
||||
expect(totals[0].categoryName).toBe("Shelter");
|
||||
expect(totals[0].totalWeight).toBe(1500);
|
||||
expect(totals[0].totalCost).toBe(43000);
|
||||
expect(totals[0].itemCount).toBe(2);
|
||||
});
|
||||
|
||||
it("excludes empty categories (no items)", () => {
|
||||
createCategory(db, { name: "Shelter", emoji: "\u{26FA}" });
|
||||
// No items added
|
||||
const totals = getCategoryTotals(db);
|
||||
expect(totals).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getGlobalTotals", () => {
|
||||
it("returns overall weight, cost, count", () => {
|
||||
createItem(db, {
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: 1,
|
||||
});
|
||||
createItem(db, {
|
||||
name: "Spork",
|
||||
weightGrams: 20,
|
||||
priceCents: 500,
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const totals = 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);
|
||||
expect(totals).toBeDefined();
|
||||
expect(totals!.totalWeight).toBe(0);
|
||||
expect(totals!.totalCost).toBe(0);
|
||||
expect(totals!.itemCount).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user