import test from "node:test"; import assert from "node:assert/strict"; import { withApp } from "./helpers.js"; test("seeded entries are searchable", async () => { await withApp(async ({ api }) => { const res = await api("/api/entries?q=ripgrep"); assert.equal(res.status, 200); assert.ok(res.json.data.total >= 1); assert.equal(res.json.data.items[0].title, "ripgrep"); }); }); test("random endpoint returns an active entry", async () => { await withApp(async ({ api }) => { const res = await api("/api/entries/random"); assert.equal(res.status, 200); assert.equal(res.json.data.status, "active"); }); }); test("invalid status transition is rejected", async () => { await withApp(async ({ api, loginAdmin }) => { const cookie = await loginAdmin(); const found = await api("/api/entries?q=ripgrep"); const id = found.json.data.items[0].id; const res = await api(`/api/entries/${id}/status`, { method: "PATCH", body: { status: "active" }, cookie, }); assert.equal(res.status, 400); }); }); test("greyed -> active is allowed", async () => { await withApp(async ({ api, loginAdmin }) => { const cookie = await loginAdmin(); const found = await api("/api/entries?q=ripgrep"); const id = found.json.data.items[0].id; const grey = await api(`/api/entries/${id}/status`, { method: "PATCH", body: { status: "greyed" }, cookie, }); assert.equal(grey.json.data.status, "greyed"); const back = await api(`/api/entries/${id}/status`, { method: "PATCH", body: { status: "active" }, cookie, }); assert.equal(back.json.data.status, "active"); }); }); test("AI ingest without a valid key is forbidden", async () => { await withApp(async ({ api }) => { const res = await api("/api/ingest/entry", { method: "POST", body: { title: "x", url: "https://github.com/a/b", type: "工具" }, }); assert.equal(res.status, 403); }); }); test("AI ingest accepts X-Ingest-Key header", async () => { await withApp(async ({ api }) => { const res = await api("/api/ingest/entry", { method: "POST", body: { title: "probe", url: "https://github.com/x/key", type: "工具" }, headers: { "X-Ingest-Key": "secret-key" }, }); assert.equal(res.status, 201); assert.equal(res.json.data.created, true); assert.equal(res.json.data.status, "pending"); }, { aiIngestKey: "secret-key" }); }); test("AI ingest accepts Authorization: Bearer header", async () => { await withApp(async ({ api }) => { const res = await api("/api/ingest/entry", { method: "POST", body: { title: "probe", url: "https://github.com/x/bearer", type: "工具" }, headers: { Authorization: "Bearer secret-key" }, }); assert.equal(res.status, 201); assert.equal(res.json.data.created, true); }, { aiIngestKey: "secret-key" }); }); test("AI ingest accepts raw Authorization token", async () => { await withApp(async ({ api }) => { const res = await api("/api/ingest/entry", { method: "POST", body: { title: "probe", url: "https://github.com/x/raw", type: "工具" }, headers: { Authorization: "secret-key" }, }); assert.equal(res.status, 201); assert.equal(res.json.data.created, true); }, { aiIngestKey: "secret-key" }); }); test("AI ingest rejects a wrong Authorization token", async () => { await withApp(async ({ api }) => { const res = await api("/api/ingest/entry", { method: "POST", body: { title: "probe", url: "https://github.com/x/wrong", type: "工具" }, headers: { Authorization: "Bearer nope" }, }); assert.equal(res.status, 403); }, { aiIngestKey: "secret-key" }); }); test("unauthenticated writes to entries are forbidden", async () => { await withApp(async ({ api }) => { const res = await api("/api/entries", { method: "POST", body: { title: "x", url: "https://github.com/a/c", type: "工具" }, }); assert.equal(res.status, 403); }); });