feat(38-01): admin tag routes + route registration + integration tests

- Create admin-tags.ts with GET list, GET single, POST, PUT (cycle guard → 400), DELETE
- Register /tags route in admin.ts
- Add 13-test integration suite covering CRUD, cycle detection, orphan behavior
This commit is contained in:
2026-04-19 22:28:02 +02:00
parent 8cefdf625b
commit 311ebe8afe
3 changed files with 267 additions and 0 deletions

View File

@@ -0,0 +1,183 @@
import { beforeEach, describe, expect, it } from "bun:test";
import { Hono } from "hono";
import { tags } from "../../src/db/schema.ts";
import { adminTagRoutes } from "../../src/server/routes/admin-tags.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp(db: any) {
const app = new Hono();
app.use("*", async (c, next) => {
c.set("db", db);
await next();
});
app.route("/api/admin/tags", adminTagRoutes);
return app;
}
async function insertTag(db: any, name: string, parentId?: number | null) {
const [row] = await db
.insert(tags)
.values({ name, parentId: parentId ?? null })
.returning();
return row!;
}
describe("Admin Tag Routes", () => {
let app: Hono;
let db: Awaited<ReturnType<typeof createTestDb>>["db"];
beforeEach(async () => {
const testDb = await createTestDb();
db = testDb.db;
app = createTestApp(db);
});
describe("GET /api/admin/tags", () => {
it("returns 200 with empty array when no tags", async () => {
const res = await app.request("/api/admin/tags");
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual([]);
});
it("returns tags with id, name, parentId, itemCount fields after seeding", async () => {
await insertTag(db, "bikepacking");
const res = await app.request("/api/admin/tags");
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toHaveLength(1);
expect(body[0]).toMatchObject({
id: expect.any(Number),
name: "bikepacking",
parentId: null,
itemCount: expect.any(Number),
});
});
});
describe("POST /api/admin/tags", () => {
it("returns 201 with created tag", async () => {
const res = await app.request("/api/admin/tags", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "ultralight" }),
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body).toMatchObject({ id: expect.any(Number), name: "ultralight", parentId: null });
});
it("creates tag with parentId", async () => {
const parent = await insertTag(db, "gear");
const res = await app.request("/api/admin/tags", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "clothing", parentId: parent.id }),
});
expect(res.status).toBe(201);
const body = await res.json();
expect(body.parentId).toBe(parent.id);
});
it("returns 400 for empty name", async () => {
const res = await app.request("/api/admin/tags", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "" }),
});
expect(res.status).toBe(400);
});
});
describe("PUT /api/admin/tags/:id", () => {
it("renames a tag", async () => {
const tag = await insertTag(db, "old-name");
const res = await app.request(`/api/admin/tags/${tag.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "new-name" }),
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.name).toBe("new-name");
});
it("updates parentId", async () => {
const parent = await insertTag(db, "parent");
const child = await insertTag(db, "child");
const res = await app.request(`/api/admin/tags/${child.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ parentId: parent.id }),
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.parentId).toBe(parent.id);
});
it("sets parentId to null (reparent to top-level)", async () => {
const parent = await insertTag(db, "parent");
const child = await insertTag(db, "child", parent.id);
const res = await app.request(`/api/admin/tags/${child.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ parentId: null }),
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.parentId).toBeNull();
});
it("returns 400 for cycle (A->B->C, try to set A as child of C)", async () => {
const a = await insertTag(db, "A");
const b = await insertTag(db, "B", a.id);
const c = await insertTag(db, "C", b.id);
const res = await app.request(`/api/admin/tags/${a.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ parentId: c.id }),
});
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("Cycle detected");
});
it("returns 404 for non-existent id", async () => {
const res = await app.request("/api/admin/tags/99999", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "ghost" }),
});
expect(res.status).toBe(404);
});
});
describe("DELETE /api/admin/tags/:id", () => {
it("returns 200 with { success: true }", async () => {
const tag = await insertTag(db, "to-delete");
const res = await app.request(`/api/admin/tags/${tag.id}`, {
method: "DELETE",
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual({ success: true });
});
it("children become orphans (parentId null) after parent deletion", async () => {
const parent = await insertTag(db, "parent");
const child = await insertTag(db, "child", parent.id);
await app.request(`/api/admin/tags/${parent.id}`, { method: "DELETE" });
const res = await app.request("/api/admin/tags");
const body = await res.json();
const childRow = body.find((t: any) => t.id === child.id);
expect(childRow?.parentId).toBeNull();
});
it("returns 404 for non-existent id", async () => {
const res = await app.request("/api/admin/tags/99999", {
method: "DELETE",
});
expect(res.status).toBe(404);
});
});
});