import test from "node:test"; import assert from "node:assert/strict"; import { withApp } from "./helpers.js"; test("admin content search filters by name", async () => { await withApp(async ({ api, loginAdmin }) => { const cookie = await loginAdmin(); const hit = await api("/api/admin/entries?q=ripgrep&status=all", { cookie }); assert.equal(hit.json.data.total, 1); assert.equal(hit.json.data.items[0].title, "ripgrep"); const miss = await api("/api/admin/entries?q=zzzz-not-exist&status=all", { cookie }); assert.equal(miss.json.data.total, 0); // 也能按 slug 命中 const bySlug = await api("/api/admin/entries?q=uptime-kuma&status=all", { cookie }); assert.equal(bySlug.json.data.total, 1); }); }); test("admin content page renders the search box and 20 rows per page", async () => { await withApp(async ({ base, api, loginAdmin }) => { const cookie = await loginAdmin(); const res = await fetch(`${base}/admin?q=ripgrep`, { headers: { cookie } }); const html = await res.text(); assert.equal(res.status, 200); assert.ok(html.includes('name="q"'), "有名称搜索框"); assert.ok(html.includes("每页 20 条"), "标注每页 20 条"); const perPage = await api("/api/admin/entries?status=all&page=1", { cookie }); assert.equal(perPage.json.data.perPage, 20); }); }); test("admin nav links point at server-rendered sections", async () => { await withApp(async ({ base, loginAdmin }) => { const cookie = await loginAdmin(); const res = await fetch(`${base}/admin?section=sec-comments`, { headers: { cookie } }); const html = await res.text(); assert.equal(res.status, 200); assert.ok(html.includes('href="/admin?section=sec-contacts"'), "菜单是真链接"); assert.ok( html.includes('
'), "服务端直接渲染出当前分区" ); }); }); test("pending entries show 预览 and active entries show 查看", async () => { await withApp(async ({ base, api, loginAdmin }) => { const cookie = await loginAdmin(); const active = await fetch(`${base}/admin?status=active`, { headers: { cookie } }); const activeHtml = await active.text(); assert.ok(activeHtml.includes(">查看"), "active 条目按钮是「查看」"); assert.ok(!activeHtml.includes(">预览"), "active 条目不出现「预览」"); await api("/api/ingest/entry", { method: "POST", body: { title: "pending-probe", url: "https://example.com/pending-probe", type: "工具" }, headers: { "X-Ingest-Key": "secret-key" }, }); const pending = await fetch(`${base}/admin?status=pending`, { headers: { cookie } }); const pendingHtml = await pending.text(); assert.ok(pendingHtml.includes(">预览"), "pending 条目按钮是「预览」"); }, { aiIngestKey: "secret-key" }); }); test("comment form does not prefill a nickname", async () => { await withApp(async ({ base, loginAdmin }) => { const cookie = await loginAdmin(); const res = await fetch(`${base}/entries/ripgrep`, { headers: { cookie } }); const html = await res.text(); const form = html.slice(html.indexOf('class="comment-form"')); assert.ok(form.includes('name="contact"'), "评论表单有联系方式字段"); assert.ok(!/name="authorName"[^>]*value="admin"/.test(form), "昵称没有预置 admin"); }); });