chore: auto-fix Biome formatting and configure lint rules
All checks were successful
CI / ci (push) Successful in 15s

Run biome check --write --unsafe to fix tabs, import ordering, and
non-null assertions across entire codebase. Disable a11y rules not
applicable to this single-user app. Exclude auto-generated routeTree.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-15 19:51:34 +01:00
parent 4d0452b7b3
commit b496462df5
63 changed files with 4752 additions and 4672 deletions

View File

@@ -3,11 +3,11 @@ import { drizzle } from "drizzle-orm/bun-sqlite";
import * as schema from "../../src/db/schema.ts";
export function createTestDb() {
const sqlite = new Database(":memory:");
sqlite.run("PRAGMA foreign_keys = ON");
const sqlite = new Database(":memory:");
sqlite.run("PRAGMA foreign_keys = ON");
// Create tables matching the Drizzle schema
sqlite.run(`
// Create tables matching the Drizzle schema
sqlite.run(`
CREATE TABLE categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
@@ -16,7 +16,7 @@ export function createTestDb() {
)
`);
sqlite.run(`
sqlite.run(`
CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
@@ -31,7 +31,7 @@ export function createTestDb() {
)
`);
sqlite.run(`
sqlite.run(`
CREATE TABLE threads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
@@ -43,7 +43,7 @@ export function createTestDb() {
)
`);
sqlite.run(`
sqlite.run(`
CREATE TABLE thread_candidates (
id INTEGER PRIMARY KEY AUTOINCREMENT,
thread_id INTEGER NOT NULL REFERENCES threads(id) ON DELETE CASCADE,
@@ -59,7 +59,7 @@ export function createTestDb() {
)
`);
sqlite.run(`
sqlite.run(`
CREATE TABLE setups (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
@@ -68,7 +68,7 @@ export function createTestDb() {
)
`);
sqlite.run(`
sqlite.run(`
CREATE TABLE setup_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
setup_id INTEGER NOT NULL REFERENCES setups(id) ON DELETE CASCADE,
@@ -76,19 +76,19 @@ export function createTestDb() {
)
`);
sqlite.run(`
sqlite.run(`
CREATE TABLE settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
`);
const db = drizzle(sqlite, { schema });
const db = drizzle(sqlite, { schema });
// Seed default Uncategorized category
db.insert(schema.categories)
.values({ name: "Uncategorized", icon: "package" })
.run();
// Seed default Uncategorized category
db.insert(schema.categories)
.values({ name: "Uncategorized", icon: "package" })
.run();
return db;
return db;
}

View File

@@ -1,91 +1,91 @@
import { describe, it, expect, beforeEach } from "bun:test";
import { beforeEach, describe, expect, it } from "bun:test";
import { Hono } from "hono";
import { createTestDb } from "../helpers/db.ts";
import { categoryRoutes } from "../../src/server/routes/categories.ts";
import { itemRoutes } from "../../src/server/routes/items.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
const app = new Hono();
const db = createTestDb();
const app = new Hono();
// Inject test DB into context for all routes
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
// Inject test DB into context for all routes
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
app.route("/api/categories", categoryRoutes);
app.route("/api/items", itemRoutes);
return { app, db };
app.route("/api/categories", categoryRoutes);
app.route("/api/items", itemRoutes);
return { app, db };
}
describe("Category Routes", () => {
let app: Hono;
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
app = testApp.app;
});
beforeEach(() => {
const testApp = createTestApp();
app = testApp.app;
});
it("POST /api/categories creates category", async () => {
const res = await app.request("/api/categories", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Shelter", icon: "tent" }),
});
it("POST /api/categories creates category", async () => {
const res = await app.request("/api/categories", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Shelter", icon: "tent" }),
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("Shelter");
expect(body.icon).toBe("tent");
expect(body.id).toBeGreaterThan(0);
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("Shelter");
expect(body.icon).toBe("tent");
expect(body.id).toBeGreaterThan(0);
});
it("GET /api/categories returns all categories", async () => {
const res = await app.request("/api/categories");
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
// At minimum, Uncategorized is seeded
expect(body.length).toBeGreaterThanOrEqual(1);
});
it("GET /api/categories returns all categories", async () => {
const res = await app.request("/api/categories");
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
// At minimum, Uncategorized is seeded
expect(body.length).toBeGreaterThanOrEqual(1);
});
it("DELETE /api/categories/:id reassigns items", async () => {
// Create category
const catRes = await app.request("/api/categories", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Shelter", icon: "tent" }),
});
const cat = await catRes.json();
it("DELETE /api/categories/:id reassigns items", async () => {
// Create category
const catRes = await app.request("/api/categories", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Shelter", icon: "tent" }),
});
const cat = await catRes.json();
// Create item in that category
await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Tent", categoryId: cat.id }),
});
// Create item in that category
await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Tent", categoryId: cat.id }),
});
// Delete the category
const delRes = await app.request(`/api/categories/${cat.id}`, {
method: "DELETE",
});
expect(delRes.status).toBe(200);
// Delete the category
const delRes = await app.request(`/api/categories/${cat.id}`, {
method: "DELETE",
});
expect(delRes.status).toBe(200);
// Verify items are now in Uncategorized
const itemsRes = await app.request("/api/items");
const items = await itemsRes.json();
const tent = items.find((i: any) => i.name === "Tent");
expect(tent.categoryId).toBe(1);
});
// Verify items are now in Uncategorized
const itemsRes = await app.request("/api/items");
const items = await itemsRes.json();
const tent = items.find((i: any) => i.name === "Tent");
expect(tent.categoryId).toBe(1);
});
it("DELETE /api/categories/1 returns 400 (cannot delete Uncategorized)", async () => {
const res = await app.request("/api/categories/1", {
method: "DELETE",
});
it("DELETE /api/categories/1 returns 400 (cannot delete Uncategorized)", async () => {
const res = await app.request("/api/categories/1", {
method: "DELETE",
});
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("Uncategorized");
});
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("Uncategorized");
});
});

View File

@@ -1,121 +1,121 @@
import { describe, it, expect, beforeEach } from "bun:test";
import { beforeEach, describe, expect, it } from "bun:test";
import { Hono } from "hono";
import { createTestDb } from "../helpers/db.ts";
import { itemRoutes } from "../../src/server/routes/items.ts";
import { categoryRoutes } from "../../src/server/routes/categories.ts";
import { itemRoutes } from "../../src/server/routes/items.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
const app = new Hono();
const db = createTestDb();
const app = new Hono();
// Inject test DB into context for all routes
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
// Inject test DB into context for all routes
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
app.route("/api/items", itemRoutes);
app.route("/api/categories", categoryRoutes);
return { app, db };
app.route("/api/items", itemRoutes);
app.route("/api/categories", categoryRoutes);
return { app, db };
}
describe("Item Routes", () => {
let app: Hono;
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
app = testApp.app;
});
beforeEach(() => {
const testApp = createTestApp();
app = testApp.app;
});
it("POST /api/items with valid data returns 201", async () => {
const res = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "Tent",
weightGrams: 1200,
priceCents: 35000,
categoryId: 1,
}),
});
it("POST /api/items with valid data returns 201", async () => {
const res = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "Tent",
weightGrams: 1200,
priceCents: 35000,
categoryId: 1,
}),
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("Tent");
expect(body.id).toBeGreaterThan(0);
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("Tent");
expect(body.id).toBeGreaterThan(0);
});
it("POST /api/items with missing name returns 400", async () => {
const res = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ categoryId: 1 }),
});
it("POST /api/items with missing name returns 400", async () => {
const res = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ categoryId: 1 }),
});
expect(res.status).toBe(400);
});
expect(res.status).toBe(400);
});
it("GET /api/items returns array", async () => {
// Create an item first
await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Tent", categoryId: 1 }),
});
it("GET /api/items returns array", async () => {
// Create an item first
await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Tent", categoryId: 1 }),
});
const res = await app.request("/api/items");
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThanOrEqual(1);
});
const res = await app.request("/api/items");
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThanOrEqual(1);
});
it("PUT /api/items/:id updates fields", async () => {
// Create first
const createRes = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "Tent",
weightGrams: 1200,
categoryId: 1,
}),
});
const created = await createRes.json();
it("PUT /api/items/:id updates fields", async () => {
// Create first
const createRes = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "Tent",
weightGrams: 1200,
categoryId: 1,
}),
});
const created = await createRes.json();
// Update
const res = await app.request(`/api/items/${created.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Big Agnes Tent", weightGrams: 1100 }),
});
// Update
const res = await app.request(`/api/items/${created.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Big Agnes Tent", weightGrams: 1100 }),
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Big Agnes Tent");
expect(body.weightGrams).toBe(1100);
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Big Agnes Tent");
expect(body.weightGrams).toBe(1100);
});
it("DELETE /api/items/:id returns success", async () => {
// Create first
const createRes = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Tent", categoryId: 1 }),
});
const created = await createRes.json();
it("DELETE /api/items/:id returns success", async () => {
// Create first
const createRes = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Tent", categoryId: 1 }),
});
const created = await createRes.json();
const res = await app.request(`/api/items/${created.id}`, {
method: "DELETE",
});
const res = await app.request(`/api/items/${created.id}`, {
method: "DELETE",
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
});
it("GET /api/items/:id returns 404 for non-existent item", async () => {
const res = await app.request("/api/items/9999");
expect(res.status).toBe(404);
});
it("GET /api/items/:id returns 404 for non-existent item", async () => {
const res = await app.request("/api/items/9999");
expect(res.status).toBe(404);
});
});

View File

@@ -1,229 +1,244 @@
import { describe, it, expect, beforeEach } from "bun:test";
import { beforeEach, describe, expect, it } from "bun:test";
import { Hono } from "hono";
import { createTestDb } from "../helpers/db.ts";
import { setupRoutes } from "../../src/server/routes/setups.ts";
import { itemRoutes } from "../../src/server/routes/items.ts";
import { setupRoutes } from "../../src/server/routes/setups.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
const app = new Hono();
const db = createTestDb();
const app = new Hono();
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
app.route("/api/setups", setupRoutes);
app.route("/api/items", itemRoutes);
return { app, db };
app.route("/api/setups", setupRoutes);
app.route("/api/items", itemRoutes);
return { app, db };
}
async function createSetupViaAPI(app: Hono, name: string) {
const res = await app.request("/api/setups", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name }),
});
return res.json();
const res = await app.request("/api/setups", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name }),
});
return res.json();
}
async function createItemViaAPI(app: Hono, data: any) {
const res = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
return res.json();
const res = await app.request("/api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
return res.json();
}
describe("Setup Routes", () => {
let app: Hono;
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
app = testApp.app;
});
beforeEach(() => {
const testApp = createTestApp();
app = testApp.app;
});
describe("POST /api/setups", () => {
it("with valid body returns 201 + setup object", async () => {
const res = await app.request("/api/setups", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Day Hike" }),
});
describe("POST /api/setups", () => {
it("with valid body returns 201 + setup object", async () => {
const res = await app.request("/api/setups", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Day Hike" }),
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("Day Hike");
expect(body.id).toBeGreaterThan(0);
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("Day Hike");
expect(body.id).toBeGreaterThan(0);
});
it("with empty name returns 400", async () => {
const res = await app.request("/api/setups", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "" }),
});
it("with empty name returns 400", async () => {
const res = await app.request("/api/setups", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "" }),
});
expect(res.status).toBe(400);
});
});
expect(res.status).toBe(400);
});
});
describe("GET /api/setups", () => {
it("returns array of setups with totals", async () => {
const setup = await createSetupViaAPI(app, "Backpacking");
const item = await createItemViaAPI(app, {
name: "Tent",
categoryId: 1,
weightGrams: 1200,
priceCents: 30000,
});
describe("GET /api/setups", () => {
it("returns array of setups with totals", async () => {
const setup = await createSetupViaAPI(app, "Backpacking");
const item = await createItemViaAPI(app, {
name: "Tent",
categoryId: 1,
weightGrams: 1200,
priceCents: 30000,
});
// Sync items
await app.request(`/api/setups/${setup.id}/items`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemIds: [item.id] }),
});
// Sync items
await app.request(`/api/setups/${setup.id}/items`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemIds: [item.id] }),
});
const res = await app.request("/api/setups");
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThanOrEqual(1);
expect(body[0].itemCount).toBeDefined();
expect(body[0].totalWeight).toBeDefined();
expect(body[0].totalCost).toBeDefined();
});
});
const res = await app.request("/api/setups");
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThanOrEqual(1);
expect(body[0].itemCount).toBeDefined();
expect(body[0].totalWeight).toBeDefined();
expect(body[0].totalCost).toBeDefined();
});
});
describe("GET /api/setups/:id", () => {
it("returns setup with items", async () => {
const setup = await createSetupViaAPI(app, "Day Hike");
const item = await createItemViaAPI(app, {
name: "Water Bottle",
categoryId: 1,
weightGrams: 200,
priceCents: 2500,
});
describe("GET /api/setups/:id", () => {
it("returns setup with items", async () => {
const setup = await createSetupViaAPI(app, "Day Hike");
const item = await createItemViaAPI(app, {
name: "Water Bottle",
categoryId: 1,
weightGrams: 200,
priceCents: 2500,
});
await app.request(`/api/setups/${setup.id}/items`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemIds: [item.id] }),
});
await app.request(`/api/setups/${setup.id}/items`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemIds: [item.id] }),
});
const res = await app.request(`/api/setups/${setup.id}`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Day Hike");
expect(body.items).toHaveLength(1);
expect(body.items[0].name).toBe("Water Bottle");
});
const res = await app.request(`/api/setups/${setup.id}`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Day Hike");
expect(body.items).toHaveLength(1);
expect(body.items[0].name).toBe("Water Bottle");
});
it("returns 404 for non-existent setup", async () => {
const res = await app.request("/api/setups/9999");
expect(res.status).toBe(404);
});
});
it("returns 404 for non-existent setup", async () => {
const res = await app.request("/api/setups/9999");
expect(res.status).toBe(404);
});
});
describe("PUT /api/setups/:id", () => {
it("updates setup name", async () => {
const setup = await createSetupViaAPI(app, "Original");
describe("PUT /api/setups/:id", () => {
it("updates setup name", async () => {
const setup = await createSetupViaAPI(app, "Original");
const res = await app.request(`/api/setups/${setup.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Renamed" }),
});
const res = await app.request(`/api/setups/${setup.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Renamed" }),
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Renamed");
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Renamed");
});
it("returns 404 for non-existent setup", async () => {
const res = await app.request("/api/setups/9999", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Ghost" }),
});
it("returns 404 for non-existent setup", async () => {
const res = await app.request("/api/setups/9999", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Ghost" }),
});
expect(res.status).toBe(404);
});
});
expect(res.status).toBe(404);
});
});
describe("DELETE /api/setups/:id", () => {
it("removes setup", async () => {
const setup = await createSetupViaAPI(app, "To Delete");
describe("DELETE /api/setups/:id", () => {
it("removes setup", async () => {
const setup = await createSetupViaAPI(app, "To Delete");
const res = await app.request(`/api/setups/${setup.id}`, {
method: "DELETE",
});
const res = await app.request(`/api/setups/${setup.id}`, {
method: "DELETE",
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
// Verify gone
const getRes = await app.request(`/api/setups/${setup.id}`);
expect(getRes.status).toBe(404);
});
// Verify gone
const getRes = await app.request(`/api/setups/${setup.id}`);
expect(getRes.status).toBe(404);
});
it("returns 404 for non-existent setup", async () => {
const res = await app.request("/api/setups/9999", { method: "DELETE" });
expect(res.status).toBe(404);
});
});
it("returns 404 for non-existent setup", async () => {
const res = await app.request("/api/setups/9999", { method: "DELETE" });
expect(res.status).toBe(404);
});
});
describe("PUT /api/setups/:id/items", () => {
it("syncs items to setup", async () => {
const setup = await createSetupViaAPI(app, "Kit");
const item1 = await createItemViaAPI(app, { name: "Item 1", categoryId: 1 });
const item2 = await createItemViaAPI(app, { name: "Item 2", categoryId: 1 });
describe("PUT /api/setups/:id/items", () => {
it("syncs items to setup", async () => {
const setup = await createSetupViaAPI(app, "Kit");
const item1 = await createItemViaAPI(app, {
name: "Item 1",
categoryId: 1,
});
const item2 = await createItemViaAPI(app, {
name: "Item 2",
categoryId: 1,
});
const res = await app.request(`/api/setups/${setup.id}/items`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemIds: [item1.id, item2.id] }),
});
const res = await app.request(`/api/setups/${setup.id}/items`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemIds: [item1.id, item2.id] }),
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
// Verify items
const getRes = await app.request(`/api/setups/${setup.id}`);
const getBody = await getRes.json();
expect(getBody.items).toHaveLength(2);
});
});
// Verify items
const getRes = await app.request(`/api/setups/${setup.id}`);
const getBody = await getRes.json();
expect(getBody.items).toHaveLength(2);
});
});
describe("DELETE /api/setups/:id/items/:itemId", () => {
it("removes single item from setup", async () => {
const setup = await createSetupViaAPI(app, "Kit");
const item1 = await createItemViaAPI(app, { name: "Item 1", categoryId: 1 });
const item2 = await createItemViaAPI(app, { name: "Item 2", categoryId: 1 });
describe("DELETE /api/setups/:id/items/:itemId", () => {
it("removes single item from setup", async () => {
const setup = await createSetupViaAPI(app, "Kit");
const item1 = await createItemViaAPI(app, {
name: "Item 1",
categoryId: 1,
});
const item2 = await createItemViaAPI(app, {
name: "Item 2",
categoryId: 1,
});
// Sync both items
await app.request(`/api/setups/${setup.id}/items`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemIds: [item1.id, item2.id] }),
});
// Sync both items
await app.request(`/api/setups/${setup.id}/items`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemIds: [item1.id, item2.id] }),
});
// Remove one
const res = await app.request(`/api/setups/${setup.id}/items/${item1.id}`, {
method: "DELETE",
});
// Remove one
const res = await app.request(
`/api/setups/${setup.id}/items/${item1.id}`,
{
method: "DELETE",
},
);
expect(res.status).toBe(200);
expect(res.status).toBe(200);
// Verify only one remains
const getRes = await app.request(`/api/setups/${setup.id}`);
const getBody = await getRes.json();
expect(getBody.items).toHaveLength(1);
expect(getBody.items[0].name).toBe("Item 2");
});
});
// Verify only one remains
const getRes = await app.request(`/api/setups/${setup.id}`);
const getBody = await getRes.json();
expect(getBody.items).toHaveLength(1);
expect(getBody.items[0].name).toBe("Item 2");
});
});
});

View File

@@ -1,300 +1,300 @@
import { describe, it, expect, beforeEach } from "bun:test";
import { beforeEach, describe, expect, it } from "bun:test";
import { Hono } from "hono";
import { createTestDb } from "../helpers/db.ts";
import { threadRoutes } from "../../src/server/routes/threads.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
const app = new Hono();
const db = createTestDb();
const app = new Hono();
// Inject test DB into context for all routes
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
// Inject test DB into context for all routes
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
app.route("/api/threads", threadRoutes);
return { app, db };
app.route("/api/threads", threadRoutes);
return { app, db };
}
async function createThreadViaAPI(app: Hono, name: string, categoryId = 1) {
const res = await app.request("/api/threads", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, categoryId }),
});
return res.json();
const res = await app.request("/api/threads", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, categoryId }),
});
return res.json();
}
async function createCandidateViaAPI(app: Hono, threadId: number, data: any) {
const res = await app.request(`/api/threads/${threadId}/candidates`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
return res.json();
const res = await app.request(`/api/threads/${threadId}/candidates`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
return res.json();
}
describe("Thread Routes", () => {
let app: Hono;
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
app = testApp.app;
});
beforeEach(() => {
const testApp = createTestApp();
app = testApp.app;
});
describe("POST /api/threads", () => {
it("with valid body returns 201 + thread object", async () => {
const res = await app.request("/api/threads", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "New Tent", categoryId: 1 }),
});
describe("POST /api/threads", () => {
it("with valid body returns 201 + thread object", async () => {
const res = await app.request("/api/threads", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "New Tent", categoryId: 1 }),
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("New Tent");
expect(body.id).toBeGreaterThan(0);
expect(body.status).toBe("active");
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("New Tent");
expect(body.id).toBeGreaterThan(0);
expect(body.status).toBe("active");
});
it("with empty name returns 400", async () => {
const res = await app.request("/api/threads", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "" }),
});
it("with empty name returns 400", async () => {
const res = await app.request("/api/threads", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "" }),
});
expect(res.status).toBe(400);
});
});
expect(res.status).toBe(400);
});
});
describe("GET /api/threads", () => {
it("returns array of active threads with metadata", async () => {
const thread = await createThreadViaAPI(app, "Backpack Options");
await createCandidateViaAPI(app, thread.id, {
name: "Pack A",
categoryId: 1,
priceCents: 20000,
});
describe("GET /api/threads", () => {
it("returns array of active threads with metadata", async () => {
const thread = await createThreadViaAPI(app, "Backpack Options");
await createCandidateViaAPI(app, thread.id, {
name: "Pack A",
categoryId: 1,
priceCents: 20000,
});
const res = await app.request("/api/threads");
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThanOrEqual(1);
expect(body[0].candidateCount).toBeDefined();
});
const res = await app.request("/api/threads");
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThanOrEqual(1);
expect(body[0].candidateCount).toBeDefined();
});
it("?includeResolved=true includes archived threads", async () => {
const t1 = await createThreadViaAPI(app, "Active");
const t2 = await createThreadViaAPI(app, "To Resolve");
const candidate = await createCandidateViaAPI(app, t2.id, {
name: "Winner",
categoryId: 1,
});
it("?includeResolved=true includes archived threads", async () => {
const _t1 = await createThreadViaAPI(app, "Active");
const t2 = await createThreadViaAPI(app, "To Resolve");
const candidate = await createCandidateViaAPI(app, t2.id, {
name: "Winner",
categoryId: 1,
});
// Resolve thread
await app.request(`/api/threads/${t2.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
// Resolve thread
await app.request(`/api/threads/${t2.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
// Default excludes resolved
const defaultRes = await app.request("/api/threads");
const defaultBody = await defaultRes.json();
expect(defaultBody).toHaveLength(1);
// Default excludes resolved
const defaultRes = await app.request("/api/threads");
const defaultBody = await defaultRes.json();
expect(defaultBody).toHaveLength(1);
// With includeResolved includes all
const allRes = await app.request("/api/threads?includeResolved=true");
const allBody = await allRes.json();
expect(allBody).toHaveLength(2);
});
});
// With includeResolved includes all
const allRes = await app.request("/api/threads?includeResolved=true");
const allBody = await allRes.json();
expect(allBody).toHaveLength(2);
});
});
describe("GET /api/threads/:id", () => {
it("returns thread with candidates", async () => {
const thread = await createThreadViaAPI(app, "Tent Options");
await createCandidateViaAPI(app, thread.id, {
name: "Tent A",
categoryId: 1,
priceCents: 30000,
});
describe("GET /api/threads/:id", () => {
it("returns thread with candidates", async () => {
const thread = await createThreadViaAPI(app, "Tent Options");
await createCandidateViaAPI(app, thread.id, {
name: "Tent A",
categoryId: 1,
priceCents: 30000,
});
const res = await app.request(`/api/threads/${thread.id}`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Tent Options");
expect(body.candidates).toHaveLength(1);
expect(body.candidates[0].name).toBe("Tent A");
});
const res = await app.request(`/api/threads/${thread.id}`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Tent Options");
expect(body.candidates).toHaveLength(1);
expect(body.candidates[0].name).toBe("Tent A");
});
it("returns 404 for non-existent thread", async () => {
const res = await app.request("/api/threads/9999");
expect(res.status).toBe(404);
});
});
it("returns 404 for non-existent thread", async () => {
const res = await app.request("/api/threads/9999");
expect(res.status).toBe(404);
});
});
describe("PUT /api/threads/:id", () => {
it("updates thread name", async () => {
const thread = await createThreadViaAPI(app, "Original");
describe("PUT /api/threads/:id", () => {
it("updates thread name", async () => {
const thread = await createThreadViaAPI(app, "Original");
const res = await app.request(`/api/threads/${thread.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Renamed" }),
});
const res = await app.request(`/api/threads/${thread.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Renamed" }),
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Renamed");
});
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Renamed");
});
});
describe("DELETE /api/threads/:id", () => {
it("removes thread", async () => {
const thread = await createThreadViaAPI(app, "To Delete");
describe("DELETE /api/threads/:id", () => {
it("removes thread", async () => {
const thread = await createThreadViaAPI(app, "To Delete");
const res = await app.request(`/api/threads/${thread.id}`, {
method: "DELETE",
});
const res = await app.request(`/api/threads/${thread.id}`, {
method: "DELETE",
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
// Verify gone
const getRes = await app.request(`/api/threads/${thread.id}`);
expect(getRes.status).toBe(404);
});
});
// Verify gone
const getRes = await app.request(`/api/threads/${thread.id}`);
expect(getRes.status).toBe(404);
});
});
describe("POST /api/threads/:id/candidates", () => {
it("adds candidate, returns 201", async () => {
const thread = await createThreadViaAPI(app, "Test");
describe("POST /api/threads/:id/candidates", () => {
it("adds candidate, returns 201", async () => {
const thread = await createThreadViaAPI(app, "Test");
const res = await app.request(`/api/threads/${thread.id}/candidates`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "Candidate A",
categoryId: 1,
priceCents: 25000,
weightGrams: 500,
}),
});
const res = await app.request(`/api/threads/${thread.id}/candidates`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "Candidate A",
categoryId: 1,
priceCents: 25000,
weightGrams: 500,
}),
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("Candidate A");
expect(body.threadId).toBe(thread.id);
});
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.name).toBe("Candidate A");
expect(body.threadId).toBe(thread.id);
});
});
describe("PUT /api/threads/:threadId/candidates/:candidateId", () => {
it("updates candidate", async () => {
const thread = await createThreadViaAPI(app, "Test");
const candidate = await createCandidateViaAPI(app, thread.id, {
name: "Original",
categoryId: 1,
});
describe("PUT /api/threads/:threadId/candidates/:candidateId", () => {
it("updates candidate", async () => {
const thread = await createThreadViaAPI(app, "Test");
const candidate = await createCandidateViaAPI(app, thread.id, {
name: "Original",
categoryId: 1,
});
const res = await app.request(
`/api/threads/${thread.id}/candidates/${candidate.id}`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Updated" }),
},
);
const res = await app.request(
`/api/threads/${thread.id}/candidates/${candidate.id}`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Updated" }),
},
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Updated");
});
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("Updated");
});
});
describe("DELETE /api/threads/:threadId/candidates/:candidateId", () => {
it("removes candidate", async () => {
const thread = await createThreadViaAPI(app, "Test");
const candidate = await createCandidateViaAPI(app, thread.id, {
name: "To Remove",
categoryId: 1,
});
describe("DELETE /api/threads/:threadId/candidates/:candidateId", () => {
it("removes candidate", async () => {
const thread = await createThreadViaAPI(app, "Test");
const candidate = await createCandidateViaAPI(app, thread.id, {
name: "To Remove",
categoryId: 1,
});
const res = await app.request(
`/api/threads/${thread.id}/candidates/${candidate.id}`,
{ method: "DELETE" },
);
const res = await app.request(
`/api/threads/${thread.id}/candidates/${candidate.id}`,
{ method: "DELETE" },
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
});
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.success).toBe(true);
});
});
describe("POST /api/threads/:id/resolve", () => {
it("with valid candidateId returns 200 + created item", async () => {
const thread = await createThreadViaAPI(app, "Tent Decision");
const candidate = await createCandidateViaAPI(app, thread.id, {
name: "Winner",
categoryId: 1,
priceCents: 30000,
});
describe("POST /api/threads/:id/resolve", () => {
it("with valid candidateId returns 200 + created item", async () => {
const thread = await createThreadViaAPI(app, "Tent Decision");
const candidate = await createCandidateViaAPI(app, thread.id, {
name: "Winner",
categoryId: 1,
priceCents: 30000,
});
const res = await app.request(`/api/threads/${thread.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
const res = await app.request(`/api/threads/${thread.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.item).toBeDefined();
expect(body.item.name).toBe("Winner");
expect(body.item.priceCents).toBe(30000);
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.item).toBeDefined();
expect(body.item.name).toBe("Winner");
expect(body.item.priceCents).toBe(30000);
});
it("on already-resolved thread returns 400", async () => {
const thread = await createThreadViaAPI(app, "Already Resolved");
const candidate = await createCandidateViaAPI(app, thread.id, {
name: "Winner",
categoryId: 1,
});
it("on already-resolved thread returns 400", async () => {
const thread = await createThreadViaAPI(app, "Already Resolved");
const candidate = await createCandidateViaAPI(app, thread.id, {
name: "Winner",
categoryId: 1,
});
// Resolve first time
await app.request(`/api/threads/${thread.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
// Resolve first time
await app.request(`/api/threads/${thread.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
// Try again
const res = await app.request(`/api/threads/${thread.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
// Try again
const res = await app.request(`/api/threads/${thread.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
expect(res.status).toBe(400);
});
expect(res.status).toBe(400);
});
it("with wrong candidateId returns 400", async () => {
const t1 = await createThreadViaAPI(app, "Thread 1");
const t2 = await createThreadViaAPI(app, "Thread 2");
const candidate = await createCandidateViaAPI(app, t2.id, {
name: "Wrong Thread",
categoryId: 1,
});
it("with wrong candidateId returns 400", async () => {
const t1 = await createThreadViaAPI(app, "Thread 1");
const t2 = await createThreadViaAPI(app, "Thread 2");
const candidate = await createCandidateViaAPI(app, t2.id, {
name: "Wrong Thread",
categoryId: 1,
});
const res = await app.request(`/api/threads/${t1.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
const res = await app.request(`/api/threads/${t1.id}/resolve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ candidateId: candidate.id }),
});
expect(res.status).toBe(400);
});
});
expect(res.status).toBe(400);
});
});
});

View File

@@ -1,98 +1,98 @@
import { describe, it, expect, beforeEach } from "bun:test";
import { createTestDb } from "../helpers/db.ts";
import { beforeEach, describe, expect, it } from "bun:test";
import { eq } from "drizzle-orm";
import { items } from "../../src/db/schema.ts";
import {
getAllCategories,
createCategory,
updateCategory,
deleteCategory,
createCategory,
deleteCategory,
getAllCategories,
updateCategory,
} 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";
import { createTestDb } from "../helpers/db.ts";
describe("Category Service", () => {
let db: ReturnType<typeof createTestDb>;
let db: ReturnType<typeof createTestDb>;
beforeEach(() => {
db = createTestDb();
});
beforeEach(() => {
db = createTestDb();
});
describe("createCategory", () => {
it("creates with name and icon", () => {
const cat = createCategory(db, { name: "Shelter", icon: "tent" });
describe("createCategory", () => {
it("creates with name and icon", () => {
const cat = createCategory(db, { name: "Shelter", icon: "tent" });
expect(cat).toBeDefined();
expect(cat!.id).toBeGreaterThan(0);
expect(cat!.name).toBe("Shelter");
expect(cat!.icon).toBe("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", () => {
const cat = createCategory(db, { name: "Cooking" });
it("uses default icon if not provided", () => {
const cat = createCategory(db, { name: "Cooking" });
expect(cat).toBeDefined();
expect(cat!.icon).toBe("package");
});
});
expect(cat).toBeDefined();
expect(cat?.icon).toBe("package");
});
});
describe("getAllCategories", () => {
it("returns all categories", () => {
createCategory(db, { name: "Shelter", icon: "tent" });
createCategory(db, { name: "Cooking", icon: "cooking-pot" });
describe("getAllCategories", () => {
it("returns all categories", () => {
createCategory(db, { name: "Shelter", icon: "tent" });
createCategory(db, { name: "Cooking", icon: "cooking-pot" });
const all = getAllCategories(db);
// Includes seeded Uncategorized + 2 new
expect(all.length).toBeGreaterThanOrEqual(3);
});
});
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", icon: "tent" });
const updated = updateCategory(db, cat!.id, { name: "Sleep System" });
describe("updateCategory", () => {
it("renames category", () => {
const cat = createCategory(db, { name: "Shelter", icon: "tent" });
const updated = updateCategory(db, cat?.id, { name: "Sleep System" });
expect(updated).toBeDefined();
expect(updated!.name).toBe("Sleep System");
expect(updated!.icon).toBe("tent");
});
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", () => {
const cat = createCategory(db, { name: "Shelter", icon: "tent" });
const updated = updateCategory(db, cat?.id, { icon: "home" });
expect(updated).toBeDefined();
expect(updated!.icon).toBe("home");
});
expect(updated).toBeDefined();
expect(updated?.icon).toBe("home");
});
it("returns null for non-existent id", () => {
const result = updateCategory(db, 9999, { name: "Ghost" });
expect(result).toBeNull();
});
});
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", icon: "tent" });
createItem(db, { name: "Tent", categoryId: shelter!.id });
createItem(db, { name: "Tarp", categoryId: shelter!.id });
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 });
const result = deleteCategory(db, shelter!.id);
expect(result.success).toBe(true);
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"]);
});
// 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();
});
});
it("cannot delete Uncategorized (id=1)", () => {
const result = deleteCategory(db, 1);
expect(result.success).toBe(false);
expect(result.error).toBeDefined();
});
});
});

View File

@@ -1,127 +1,124 @@
import { describe, it, expect, beforeEach } from "bun:test";
import { createTestDb } from "../helpers/db.ts";
import { beforeEach, describe, expect, it } from "bun:test";
import {
getAllItems,
getItemById,
createItem,
updateItem,
deleteItem,
createItem,
deleteItem,
getAllItems,
getItemById,
updateItem,
} from "../../src/server/services/item.service.ts";
import { createTestDb } from "../helpers/db.ts";
describe("Item Service", () => {
let db: ReturnType<typeof createTestDb>;
let db: ReturnType<typeof createTestDb>;
beforeEach(() => {
db = 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",
},
);
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();
});
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 });
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();
});
});
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 });
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].categoryIcon).toBeDefined();
});
});
const all = getAllItems(db);
expect(all).toHaveLength(2);
expect(all[0].categoryName).toBe("Uncategorized");
expect(all[0].categoryIcon).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");
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();
});
});
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,
});
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,
});
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);
});
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();
});
});
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",
});
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");
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();
});
// 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();
});
});
it("returns null for non-existent id", () => {
const result = deleteItem(db, 9999);
expect(result).toBeNull();
});
});
});

View File

@@ -1,192 +1,192 @@
import { describe, it, expect, beforeEach } from "bun:test";
import { createTestDb } from "../helpers/db.ts";
import {
getAllSetups,
getSetupWithItems,
createSetup,
updateSetup,
deleteSetup,
syncSetupItems,
removeSetupItem,
} from "../../src/server/services/setup.service.ts";
import { beforeEach, describe, expect, it } from "bun:test";
import { createItem } from "../../src/server/services/item.service.ts";
import {
createSetup,
deleteSetup,
getAllSetups,
getSetupWithItems,
removeSetupItem,
syncSetupItems,
updateSetup,
} from "../../src/server/services/setup.service.ts";
import { createTestDb } from "../helpers/db.ts";
describe("Setup Service", () => {
let db: ReturnType<typeof createTestDb>;
let db: ReturnType<typeof createTestDb>;
beforeEach(() => {
db = createTestDb();
});
beforeEach(() => {
db = createTestDb();
});
describe("createSetup", () => {
it("creates setup with name, returns setup with id/timestamps", () => {
const setup = createSetup(db, { name: "Day Hike" });
describe("createSetup", () => {
it("creates setup with name, returns setup with id/timestamps", () => {
const setup = createSetup(db, { name: "Day Hike" });
expect(setup).toBeDefined();
expect(setup.id).toBeGreaterThan(0);
expect(setup.name).toBe("Day Hike");
expect(setup.createdAt).toBeDefined();
expect(setup.updatedAt).toBeDefined();
});
});
expect(setup).toBeDefined();
expect(setup.id).toBeGreaterThan(0);
expect(setup.name).toBe("Day Hike");
expect(setup.createdAt).toBeDefined();
expect(setup.updatedAt).toBeDefined();
});
});
describe("getAllSetups", () => {
it("returns setups with itemCount, totalWeight, totalCost", () => {
const setup = createSetup(db, { name: "Backpacking" });
const item1 = createItem(db, {
name: "Tent",
categoryId: 1,
weightGrams: 1200,
priceCents: 30000,
});
const item2 = createItem(db, {
name: "Sleeping Bag",
categoryId: 1,
weightGrams: 800,
priceCents: 20000,
});
syncSetupItems(db, setup.id, [item1.id, item2.id]);
describe("getAllSetups", () => {
it("returns setups with itemCount, totalWeight, totalCost", () => {
const setup = createSetup(db, { name: "Backpacking" });
const item1 = createItem(db, {
name: "Tent",
categoryId: 1,
weightGrams: 1200,
priceCents: 30000,
});
const item2 = createItem(db, {
name: "Sleeping Bag",
categoryId: 1,
weightGrams: 800,
priceCents: 20000,
});
syncSetupItems(db, setup.id, [item1.id, item2.id]);
const setups = getAllSetups(db);
expect(setups).toHaveLength(1);
expect(setups[0].name).toBe("Backpacking");
expect(setups[0].itemCount).toBe(2);
expect(setups[0].totalWeight).toBe(2000);
expect(setups[0].totalCost).toBe(50000);
});
const setups = getAllSetups(db);
expect(setups).toHaveLength(1);
expect(setups[0].name).toBe("Backpacking");
expect(setups[0].itemCount).toBe(2);
expect(setups[0].totalWeight).toBe(2000);
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", () => {
createSetup(db, { name: "Empty Setup" });
const setups = getAllSetups(db);
expect(setups).toHaveLength(1);
expect(setups[0].itemCount).toBe(0);
expect(setups[0].totalWeight).toBe(0);
expect(setups[0].totalCost).toBe(0);
});
});
const setups = getAllSetups(db);
expect(setups).toHaveLength(1);
expect(setups[0].itemCount).toBe(0);
expect(setups[0].totalWeight).toBe(0);
expect(setups[0].totalCost).toBe(0);
});
});
describe("getSetupWithItems", () => {
it("returns setup with full item details and category info", () => {
const setup = createSetup(db, { name: "Day Hike" });
const item = createItem(db, {
name: "Water Bottle",
categoryId: 1,
weightGrams: 200,
priceCents: 2500,
});
syncSetupItems(db, setup.id, [item.id]);
describe("getSetupWithItems", () => {
it("returns setup with full item details and category info", () => {
const setup = createSetup(db, { name: "Day Hike" });
const item = createItem(db, {
name: "Water Bottle",
categoryId: 1,
weightGrams: 200,
priceCents: 2500,
});
syncSetupItems(db, setup.id, [item.id]);
const result = getSetupWithItems(db, setup.id);
expect(result).toBeDefined();
expect(result!.name).toBe("Day Hike");
expect(result!.items).toHaveLength(1);
expect(result!.items[0].name).toBe("Water Bottle");
expect(result!.items[0].categoryName).toBe("Uncategorized");
expect(result!.items[0].categoryIcon).toBeDefined();
});
const result = getSetupWithItems(db, setup.id);
expect(result).toBeDefined();
expect(result?.name).toBe("Day Hike");
expect(result?.items).toHaveLength(1);
expect(result?.items[0].name).toBe("Water Bottle");
expect(result?.items[0].categoryName).toBe("Uncategorized");
expect(result?.items[0].categoryIcon).toBeDefined();
});
it("returns null for non-existent setup", () => {
const result = getSetupWithItems(db, 9999);
expect(result).toBeNull();
});
});
it("returns null for non-existent setup", () => {
const result = 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" });
describe("updateSetup", () => {
it("updates setup name, returns updated setup", () => {
const setup = createSetup(db, { name: "Original" });
const updated = updateSetup(db, setup.id, { name: "Renamed" });
expect(updated).toBeDefined();
expect(updated!.name).toBe("Renamed");
});
expect(updated).toBeDefined();
expect(updated?.name).toBe("Renamed");
});
it("returns null for non-existent setup", () => {
const result = updateSetup(db, 9999, { name: "Ghost" });
expect(result).toBeNull();
});
});
it("returns null for non-existent setup", () => {
const result = 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]);
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]);
const deleted = deleteSetup(db, setup.id);
expect(deleted).toBe(true);
const deleted = deleteSetup(db, setup.id);
expect(deleted).toBe(true);
// Setup gone
const result = getSetupWithItems(db, setup.id);
expect(result).toBeNull();
});
// Setup gone
const result = getSetupWithItems(db, setup.id);
expect(result).toBeNull();
});
it("returns false for non-existent setup", () => {
const result = deleteSetup(db, 9999);
expect(result).toBe(false);
});
});
it("returns false for non-existent setup", () => {
const result = 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 });
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 });
// Initial sync
syncSetupItems(db, setup.id, [item1.id, item2.id]);
let result = getSetupWithItems(db, setup.id);
expect(result!.items).toHaveLength(2);
// Initial sync
syncSetupItems(db, setup.id, [item1.id, item2.id]);
let result = 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);
expect(result!.items).toHaveLength(2);
const names = result!.items.map((i: any) => i.name).sort();
expect(names).toEqual(["Item 2", "Item 3"]);
});
// Re-sync with different items
syncSetupItems(db, setup.id, [item2.id, item3.id]);
result = 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", () => {
const setup = createSetup(db, { name: "Kit" });
const item = createItem(db, { name: "Item", categoryId: 1 });
syncSetupItems(db, setup.id, [item.id]);
syncSetupItems(db, setup.id, []);
const result = getSetupWithItems(db, setup.id);
expect(result!.items).toHaveLength(0);
});
});
syncSetupItems(db, setup.id, []);
const result = 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]);
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]);
removeSetupItem(db, setup.id, item1.id);
const result = getSetupWithItems(db, setup.id);
expect(result!.items).toHaveLength(1);
expect(result!.items[0].name).toBe("Item 2");
});
});
removeSetupItem(db, setup.id, item1.id);
const result = getSetupWithItems(db, setup.id);
expect(result?.items).toHaveLength(1);
expect(result?.items[0].name).toBe("Item 2");
});
});
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]);
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]);
// 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 (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();
const result = getSetupWithItems(db, setup.id);
expect(result!.items).toHaveLength(1);
expect(result!.items[0].name).toBe("Item 2");
});
});
const result = getSetupWithItems(db, setup.id);
expect(result?.items).toHaveLength(1);
expect(result?.items[0].name).toBe("Item 2");
});
});
});

View File

@@ -1,280 +1,285 @@
import { describe, it, expect, beforeEach } from "bun:test";
import { createTestDb } from "../helpers/db.ts";
import { beforeEach, describe, expect, it } from "bun:test";
import {
createThread,
getAllThreads,
getThreadWithCandidates,
createCandidate,
updateCandidate,
deleteCandidate,
updateThread,
deleteThread,
resolveThread,
createCandidate,
createThread,
deleteCandidate,
deleteThread,
getAllThreads,
getThreadWithCandidates,
resolveThread,
updateCandidate,
updateThread,
} from "../../src/server/services/thread.service.ts";
import { createItem } from "../../src/server/services/item.service.ts";
import { createTestDb } from "../helpers/db.ts";
describe("Thread Service", () => {
let db: ReturnType<typeof createTestDb>;
let db: ReturnType<typeof createTestDb>;
beforeEach(() => {
db = createTestDb();
});
beforeEach(() => {
db = createTestDb();
});
describe("createThread", () => {
it("creates thread with name, returns thread with id/status/timestamps", () => {
const thread = createThread(db, { name: "New Tent", categoryId: 1 });
describe("createThread", () => {
it("creates thread with name, returns thread with id/status/timestamps", () => {
const thread = createThread(db, { name: "New Tent", categoryId: 1 });
expect(thread).toBeDefined();
expect(thread.id).toBeGreaterThan(0);
expect(thread.name).toBe("New Tent");
expect(thread.status).toBe("active");
expect(thread.resolvedCandidateId).toBeNull();
expect(thread.createdAt).toBeDefined();
expect(thread.updatedAt).toBeDefined();
});
});
expect(thread).toBeDefined();
expect(thread.id).toBeGreaterThan(0);
expect(thread.name).toBe("New Tent");
expect(thread.status).toBe("active");
expect(thread.resolvedCandidateId).toBeNull();
expect(thread.createdAt).toBeDefined();
expect(thread.updatedAt).toBeDefined();
});
});
describe("getAllThreads", () => {
it("returns active threads with candidateCount and price range", () => {
const thread = createThread(db, { name: "Backpack Options", categoryId: 1 });
createCandidate(db, thread.id, {
name: "Pack A",
categoryId: 1,
priceCents: 20000,
});
createCandidate(db, thread.id, {
name: "Pack B",
categoryId: 1,
priceCents: 35000,
});
describe("getAllThreads", () => {
it("returns active threads with candidateCount and price range", () => {
const thread = createThread(db, {
name: "Backpack Options",
categoryId: 1,
});
createCandidate(db, thread.id, {
name: "Pack A",
categoryId: 1,
priceCents: 20000,
});
createCandidate(db, thread.id, {
name: "Pack B",
categoryId: 1,
priceCents: 35000,
});
const threads = getAllThreads(db);
expect(threads).toHaveLength(1);
expect(threads[0].name).toBe("Backpack Options");
expect(threads[0].candidateCount).toBe(2);
expect(threads[0].minPriceCents).toBe(20000);
expect(threads[0].maxPriceCents).toBe(35000);
});
const threads = getAllThreads(db);
expect(threads).toHaveLength(1);
expect(threads[0].name).toBe("Backpack Options");
expect(threads[0].candidateCount).toBe(2);
expect(threads[0].minPriceCents).toBe(20000);
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, {
name: "Winner",
categoryId: 1,
});
resolveThread(db, t2.id, candidate.id);
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, {
name: "Winner",
categoryId: 1,
});
resolveThread(db, t2.id, candidate.id);
const active = getAllThreads(db);
expect(active).toHaveLength(1);
expect(active[0].name).toBe("Active Thread");
});
const active = 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, {
name: "Winner",
categoryId: 1,
});
resolveThread(db, t2.id, candidate.id);
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, {
name: "Winner",
categoryId: 1,
});
resolveThread(db, t2.id, candidate.id);
const all = getAllThreads(db, true);
expect(all).toHaveLength(2);
});
});
const all = 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, {
name: "Tent A",
categoryId: 1,
weightGrams: 1200,
priceCents: 30000,
});
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, {
name: "Tent A",
categoryId: 1,
weightGrams: 1200,
priceCents: 30000,
});
const result = getThreadWithCandidates(db, thread.id);
expect(result).toBeDefined();
expect(result!.name).toBe("Tent Options");
expect(result!.candidates).toHaveLength(1);
expect(result!.candidates[0].name).toBe("Tent A");
expect(result!.candidates[0].categoryName).toBe("Uncategorized");
expect(result!.candidates[0].categoryIcon).toBeDefined();
});
const result = getThreadWithCandidates(db, thread.id);
expect(result).toBeDefined();
expect(result?.name).toBe("Tent Options");
expect(result?.candidates).toHaveLength(1);
expect(result?.candidates[0].name).toBe("Tent A");
expect(result?.candidates[0].categoryName).toBe("Uncategorized");
expect(result?.candidates[0].categoryIcon).toBeDefined();
});
it("returns null for non-existent thread", () => {
const result = getThreadWithCandidates(db, 9999);
expect(result).toBeNull();
});
});
it("returns null for non-existent thread", () => {
const result = getThreadWithCandidates(db, 9999);
expect(result).toBeNull();
});
});
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, {
name: "Tent A",
weightGrams: 1200,
priceCents: 30000,
categoryId: 1,
notes: "Ultralight 2-person",
productUrl: "https://example.com/tent",
});
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, {
name: "Tent A",
weightGrams: 1200,
priceCents: 30000,
categoryId: 1,
notes: "Ultralight 2-person",
productUrl: "https://example.com/tent",
});
expect(candidate).toBeDefined();
expect(candidate.id).toBeGreaterThan(0);
expect(candidate.threadId).toBe(thread.id);
expect(candidate.name).toBe("Tent A");
expect(candidate.weightGrams).toBe(1200);
expect(candidate.priceCents).toBe(30000);
expect(candidate.categoryId).toBe(1);
expect(candidate.notes).toBe("Ultralight 2-person");
expect(candidate.productUrl).toBe("https://example.com/tent");
});
});
expect(candidate).toBeDefined();
expect(candidate.id).toBeGreaterThan(0);
expect(candidate.threadId).toBe(thread.id);
expect(candidate.name).toBe("Tent A");
expect(candidate.weightGrams).toBe(1200);
expect(candidate.priceCents).toBe(30000);
expect(candidate.categoryId).toBe(1);
expect(candidate.notes).toBe("Ultralight 2-person");
expect(candidate.productUrl).toBe("https://example.com/tent");
});
});
describe("updateCandidate", () => {
it("updates candidate fields, returns updated candidate", () => {
const thread = createThread(db, { name: "Test", categoryId: 1 });
const candidate = createCandidate(db, thread.id, {
name: "Original",
categoryId: 1,
});
describe("updateCandidate", () => {
it("updates candidate fields, returns updated candidate", () => {
const thread = createThread(db, { name: "Test", categoryId: 1 });
const candidate = createCandidate(db, thread.id, {
name: "Original",
categoryId: 1,
});
const updated = updateCandidate(db, candidate.id, {
name: "Updated Name",
priceCents: 15000,
});
const updated = updateCandidate(db, candidate.id, {
name: "Updated Name",
priceCents: 15000,
});
expect(updated).toBeDefined();
expect(updated!.name).toBe("Updated Name");
expect(updated!.priceCents).toBe(15000);
});
expect(updated).toBeDefined();
expect(updated?.name).toBe("Updated Name");
expect(updated?.priceCents).toBe(15000);
});
it("returns null for non-existent candidate", () => {
const result = updateCandidate(db, 9999, { name: "Ghost" });
expect(result).toBeNull();
});
});
it("returns null for non-existent candidate", () => {
const result = updateCandidate(db, 9999, { name: "Ghost" });
expect(result).toBeNull();
});
});
describe("deleteCandidate", () => {
it("removes candidate, returns deleted candidate", () => {
const thread = createThread(db, { name: "Test", categoryId: 1 });
const candidate = createCandidate(db, thread.id, {
name: "To Delete",
categoryId: 1,
});
describe("deleteCandidate", () => {
it("removes candidate, returns deleted candidate", () => {
const thread = createThread(db, { name: "Test", categoryId: 1 });
const candidate = createCandidate(db, thread.id, {
name: "To Delete",
categoryId: 1,
});
const deleted = deleteCandidate(db, candidate.id);
expect(deleted).toBeDefined();
expect(deleted!.name).toBe("To Delete");
const deleted = deleteCandidate(db, candidate.id);
expect(deleted).toBeDefined();
expect(deleted?.name).toBe("To Delete");
// Verify it's gone
const result = getThreadWithCandidates(db, thread.id);
expect(result!.candidates).toHaveLength(0);
});
// Verify it's gone
const result = getThreadWithCandidates(db, thread.id);
expect(result?.candidates).toHaveLength(0);
});
it("returns null for non-existent candidate", () => {
const result = deleteCandidate(db, 9999);
expect(result).toBeNull();
});
});
it("returns null for non-existent candidate", () => {
const result = 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" });
describe("updateThread", () => {
it("updates thread name", () => {
const thread = createThread(db, { name: "Original", categoryId: 1 });
const updated = updateThread(db, thread.id, { name: "Renamed" });
expect(updated).toBeDefined();
expect(updated!.name).toBe("Renamed");
});
expect(updated).toBeDefined();
expect(updated?.name).toBe("Renamed");
});
it("returns null for non-existent thread", () => {
const result = updateThread(db, 9999, { name: "Ghost" });
expect(result).toBeNull();
});
});
it("returns null for non-existent thread", () => {
const result = 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 });
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 });
const deleted = deleteThread(db, thread.id);
expect(deleted).toBeDefined();
expect(deleted!.name).toBe("To Delete");
const deleted = deleteThread(db, thread.id);
expect(deleted).toBeDefined();
expect(deleted?.name).toBe("To Delete");
// Thread and candidates gone
const result = getThreadWithCandidates(db, thread.id);
expect(result).toBeNull();
});
// Thread and candidates gone
const result = getThreadWithCandidates(db, thread.id);
expect(result).toBeNull();
});
it("returns null for non-existent thread", () => {
const result = deleteThread(db, 9999);
expect(result).toBeNull();
});
});
it("returns null for non-existent thread", () => {
const result = deleteThread(db, 9999);
expect(result).toBeNull();
});
});
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, {
name: "Winner Tent",
weightGrams: 1200,
priceCents: 30000,
categoryId: 1,
notes: "Best choice",
productUrl: "https://example.com/tent",
});
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, {
name: "Winner Tent",
weightGrams: 1200,
priceCents: 30000,
categoryId: 1,
notes: "Best choice",
productUrl: "https://example.com/tent",
});
const result = resolveThread(db, thread.id, candidate.id);
expect(result.success).toBe(true);
expect(result.item).toBeDefined();
expect(result.item!.name).toBe("Winner Tent");
expect(result.item!.weightGrams).toBe(1200);
expect(result.item!.priceCents).toBe(30000);
expect(result.item!.categoryId).toBe(1);
expect(result.item!.notes).toBe("Best choice");
expect(result.item!.productUrl).toBe("https://example.com/tent");
const result = resolveThread(db, thread.id, candidate.id);
expect(result.success).toBe(true);
expect(result.item).toBeDefined();
expect(result.item?.name).toBe("Winner Tent");
expect(result.item?.weightGrams).toBe(1200);
expect(result.item?.priceCents).toBe(30000);
expect(result.item?.categoryId).toBe(1);
expect(result.item?.notes).toBe("Best choice");
expect(result.item?.productUrl).toBe("https://example.com/tent");
// Thread should be resolved
const resolved = getThreadWithCandidates(db, thread.id);
expect(resolved!.status).toBe("resolved");
expect(resolved!.resolvedCandidateId).toBe(candidate.id);
});
// Thread should be resolved
const resolved = 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, { name: "Already Resolved", categoryId: 1 });
const candidate = createCandidate(db, thread.id, {
name: "Winner",
categoryId: 1,
});
resolveThread(db, thread.id, candidate.id);
it("fails if thread is not active", () => {
const thread = createThread(db, {
name: "Already Resolved",
categoryId: 1,
});
const candidate = createCandidate(db, thread.id, {
name: "Winner",
categoryId: 1,
});
resolveThread(db, thread.id, candidate.id);
// Try to resolve again
const result = resolveThread(db, thread.id, candidate.id);
expect(result.success).toBe(false);
expect(result.error).toBeDefined();
});
// Try to resolve again
const result = 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, {
name: "Wrong Thread",
categoryId: 1,
});
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, {
name: "Wrong Thread",
categoryId: 1,
});
const result = resolveThread(db, thread1.id, candidate.id);
expect(result.success).toBe(false);
expect(result.error).toBeDefined();
});
const result = 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);
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);
expect(result.success).toBe(false);
expect(result.error).toBeDefined();
});
});
});

View File

@@ -1,79 +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 { beforeEach, describe, expect, it } from "bun:test";
import { createCategory } from "../../src/server/services/category.service.ts";
import { createItem } from "../../src/server/services/item.service.ts";
import {
getCategoryTotals,
getGlobalTotals,
getCategoryTotals,
getGlobalTotals,
} from "../../src/server/services/totals.service.ts";
import { createTestDb } from "../helpers/db.ts";
describe("Totals Service", () => {
let db: ReturnType<typeof createTestDb>;
let db: ReturnType<typeof createTestDb>;
beforeEach(() => {
db = createTestDb();
});
beforeEach(() => {
db = createTestDb();
});
describe("getCategoryTotals", () => {
it("returns weight sum, cost sum, item count per category", () => {
const shelter = createCategory(db, { name: "Shelter", icon: "tent" });
createItem(db, {
name: "Tent",
weightGrams: 1200,
priceCents: 35000,
categoryId: shelter!.id,
});
createItem(db, {
name: "Tarp",
weightGrams: 300,
priceCents: 8000,
categoryId: shelter!.id,
});
describe("getCategoryTotals", () => {
it("returns weight sum, cost sum, item count per category", () => {
const shelter = createCategory(db, { name: "Shelter", icon: "tent" });
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);
});
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", icon: "tent" });
// No items added
const totals = getCategoryTotals(db);
expect(totals).toHaveLength(0);
});
});
it("excludes empty categories (no items)", () => {
createCategory(db, { name: "Shelter", icon: "tent" });
// 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,
});
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);
});
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);
});
});
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);
});
});
});