feat(19-03): add tag filtering to global item search and migrate owner count
- searchGlobalItems now accepts tagNames param with AND intersection logic - Owner count uses items.globalItemId instead of removed itemGlobalLinks - Removed linkItemToGlobal and unlinkItemFromGlobal functions - Route handlers now async with tags query param support - Rewrote tests to async PGlite pattern, added tag filtering tests Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,52 +1,71 @@
|
||||
import { beforeEach, describe, expect, it } from "bun:test";
|
||||
import { Hono } from "hono";
|
||||
import { globalItems, itemGlobalLinks, items } from "../../src/db/schema.ts";
|
||||
import {
|
||||
globalItemTags,
|
||||
globalItems,
|
||||
items,
|
||||
tags,
|
||||
} from "../../src/db/schema.ts";
|
||||
import { globalItemRoutes } from "../../src/server/routes/global-items.ts";
|
||||
import { itemRoutes } from "../../src/server/routes/items.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
type TestDb = ReturnType<typeof createTestDb>;
|
||||
type TestDb = Awaited<ReturnType<typeof createTestDb>>;
|
||||
|
||||
function createTestApp() {
|
||||
const db = createTestDb();
|
||||
async function createTestApp() {
|
||||
const { db, userId } = await createTestDb();
|
||||
const app = new Hono();
|
||||
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
c.set("userId", userId);
|
||||
await next();
|
||||
});
|
||||
|
||||
app.route("/api/global-items", globalItemRoutes);
|
||||
app.route("/api/items", itemRoutes);
|
||||
return { app, db };
|
||||
return { app, db, userId };
|
||||
}
|
||||
|
||||
function insertGlobalItem(db: TestDb, brand: string, model: string) {
|
||||
return db
|
||||
async function insertGlobalItem(
|
||||
db: TestDb["db"],
|
||||
brand: string,
|
||||
model: string,
|
||||
) {
|
||||
const [row] = await db
|
||||
.insert(globalItems)
|
||||
.values({ brand, model, category: "bags" })
|
||||
.returning()
|
||||
.get();
|
||||
.returning();
|
||||
return row;
|
||||
}
|
||||
|
||||
function insertItem(db: TestDb, name: string) {
|
||||
return db.insert(items).values({ name, categoryId: 1 }).returning().get();
|
||||
async function insertItem(
|
||||
db: TestDb["db"],
|
||||
name: string,
|
||||
userId: number,
|
||||
opts?: { globalItemId?: number },
|
||||
) {
|
||||
const [row] = await db
|
||||
.insert(items)
|
||||
.values({ name, categoryId: 1, userId, globalItemId: opts?.globalItemId })
|
||||
.returning();
|
||||
return row;
|
||||
}
|
||||
|
||||
describe("Global Item Routes", () => {
|
||||
let app: Hono;
|
||||
let db: TestDb;
|
||||
let db: TestDb["db"];
|
||||
let userId: number;
|
||||
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
beforeEach(async () => {
|
||||
const testApp = await createTestApp();
|
||||
app = testApp.app;
|
||||
db = testApp.db;
|
||||
userId = testApp.userId;
|
||||
});
|
||||
|
||||
describe("GET /api/global-items", () => {
|
||||
it("returns 200 with all global items", async () => {
|
||||
insertGlobalItem(db, "Revelate Designs", "Terrapin System");
|
||||
insertGlobalItem(db, "Apidura", "Handlebar Pack");
|
||||
await insertGlobalItem(db, "Revelate Designs", "Terrapin System");
|
||||
await insertGlobalItem(db, "Apidura", "Handlebar Pack");
|
||||
|
||||
const res = await app.request("/api/global-items");
|
||||
expect(res.status).toBe(200);
|
||||
@@ -56,25 +75,47 @@ describe("Global Item Routes", () => {
|
||||
});
|
||||
|
||||
it("filters results by query parameter", async () => {
|
||||
insertGlobalItem(db, "Revelate Designs", "Terrapin System");
|
||||
insertGlobalItem(db, "Apidura", "Handlebar Pack");
|
||||
await insertGlobalItem(db, "Revelate Designs", "Terrapin System");
|
||||
await insertGlobalItem(db, "Apidura", "Handlebar Pack");
|
||||
|
||||
const res = await app.request("/api/global-items?q=tent");
|
||||
const res = await app.request("/api/global-items?q=revelate");
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const body = await res.json();
|
||||
// "tent" doesn't match "Terrapin" or "Handlebar" — expect 0
|
||||
// Actually let's search for something that matches
|
||||
const res2 = await app.request("/api/global-items?q=revelate");
|
||||
const body2 = await res2.json();
|
||||
expect(body2).toHaveLength(1);
|
||||
expect(body2[0].brand).toBe("Revelate Designs");
|
||||
expect(body).toHaveLength(1);
|
||||
expect(body[0].brand).toBe("Revelate Designs");
|
||||
});
|
||||
|
||||
it("filters results by tags parameter", async () => {
|
||||
const gi1 = await insertGlobalItem(
|
||||
db,
|
||||
"Revelate Designs",
|
||||
"Terrapin System",
|
||||
);
|
||||
await insertGlobalItem(db, "Apidura", "Handlebar Pack");
|
||||
|
||||
const [tag] = await db
|
||||
.insert(tags)
|
||||
.values({ name: "ultralight" })
|
||||
.returning();
|
||||
await db
|
||||
.insert(globalItemTags)
|
||||
.values({ globalItemId: gi1.id, tagId: tag.id });
|
||||
|
||||
const res = await app.request(
|
||||
"/api/global-items?tags=ultralight",
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const body = await res.json();
|
||||
expect(body).toHaveLength(1);
|
||||
expect(body[0].brand).toBe("Revelate Designs");
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/global-items/:id", () => {
|
||||
it("returns item with ownerCount", async () => {
|
||||
const gi = insertGlobalItem(db, "MSR", "PocketRocket 2");
|
||||
const gi = await insertGlobalItem(db, "MSR", "PocketRocket 2");
|
||||
|
||||
const res = await app.request(`/api/global-items/${gi.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
@@ -84,6 +125,19 @@ describe("Global Item Routes", () => {
|
||||
expect(body.ownerCount).toBe(0);
|
||||
});
|
||||
|
||||
it("returns ownerCount from items.globalItemId", async () => {
|
||||
const gi = await insertGlobalItem(db, "MSR", "PocketRocket 2");
|
||||
await insertItem(db, "My Stove", userId, {
|
||||
globalItemId: gi.id,
|
||||
});
|
||||
|
||||
const res = await app.request(`/api/global-items/${gi.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const body = await res.json();
|
||||
expect(body.ownerCount).toBe(1);
|
||||
});
|
||||
|
||||
it("returns 404 for non-existent id", async () => {
|
||||
const res = await app.request("/api/global-items/999");
|
||||
expect(res.status).toBe(404);
|
||||
@@ -94,84 +148,4 @@ describe("Global Item Routes", () => {
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/items/:id/link", () => {
|
||||
it("returns 201 when linking item to global item", async () => {
|
||||
const gi = insertGlobalItem(db, "MSR", "PocketRocket 2");
|
||||
const item = insertItem(db, "My Stove");
|
||||
|
||||
const res = await app.request(`/api/items/${item.id}/link`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ globalItemId: gi.id }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.itemId).toBe(item.id);
|
||||
expect(body.globalItemId).toBe(gi.id);
|
||||
});
|
||||
|
||||
it("returns 409 when item already linked", async () => {
|
||||
const gi = insertGlobalItem(db, "MSR", "PocketRocket 2");
|
||||
const item = insertItem(db, "My Stove");
|
||||
|
||||
// Link once
|
||||
await app.request(`/api/items/${item.id}/link`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ globalItemId: gi.id }),
|
||||
});
|
||||
|
||||
// Link again — should conflict
|
||||
const res = await app.request(`/api/items/${item.id}/link`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ globalItemId: gi.id }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
});
|
||||
|
||||
it("returns 404 when item does not exist", async () => {
|
||||
const gi = insertGlobalItem(db, "MSR", "PocketRocket 2");
|
||||
|
||||
const res = await app.request("/api/items/999/link", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ globalItemId: gi.id }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/items/:id/link", () => {
|
||||
it("returns 200 when unlinking", async () => {
|
||||
const gi = insertGlobalItem(db, "MSR", "PocketRocket 2");
|
||||
const item = insertItem(db, "My Stove");
|
||||
|
||||
// Link first
|
||||
await app.request(`/api/items/${item.id}/link`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ globalItemId: gi.id }),
|
||||
});
|
||||
|
||||
// Unlink
|
||||
const res = await app.request(`/api/items/${item.id}/link`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it("returns 404 when item does not exist", async () => {
|
||||
const res = await app.request("/api/items/999/link", {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user