// Production API acceptance: real server, real file persistence, real HTTP. import test from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createApp } from "../server.js"; async function withServer(run) { const dir = mkdtempSync(join(tmpdir(), "workbench-")); const dataFile = join(dir, "data.json"); const app = createApp({ dataFile }); const server = app.listen(0, "127.0.0.1"); await new Promise((resolve) => server.once("listening", resolve)); const base = `http://127.0.0.1:${server.address().port}`; try { await run({ base, dataFile }); } finally { await new Promise((resolve) => server.close(resolve)); rmSync(dir, { recursive: true, force: true }); } } test("creates, searches, updates and reports linked statistics with persistence", async () => { await withServer(async ({ base, dataFile }) => { const created = await fetch(`${base}/api/tasks`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ title: "测量超导比特", priority: "high" }), }); assert.equal(created.status, 201); const { task } = await created.json(); assert.equal(task.title, "测量超导比特"); assert.equal(task.status, "open"); const second = await fetch(`${base}/api/tasks`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ title: "整理实验记录" }), }); assert.equal(second.status, 201); const list = await (await fetch(`${base}/api/tasks?q=超导`)).json(); assert.equal(list.tasks.length, 1); assert.equal(list.tasks[0].id, task.id); const invalid = await fetch(`${base}/api/tasks`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ title: " " }), }); assert.equal(invalid.status, 400); assert.equal((await invalid.json()).error.code, "task.title_required"); const badPriority = await fetch(`${base}/api/tasks`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ title: "x", priority: "urgent" }), }); assert.equal(badPriority.status, 400); const update = await fetch(`${base}/api/tasks/${task.id}/status`, { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify({ status: "done" }), }); assert.equal(update.status, 200); const missing = await fetch(`${base}/api/tasks/999/status`, { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify({ status: "done" }), }); assert.equal(missing.status, 404); assert.equal((await missing.json()).error.code, "task.not_found"); const stats = await (await fetch(`${base}/api/stats`)).json(); assert.equal(stats.total, 2); assert.equal(stats.byStatus.done, 1); assert.equal(stats.byStatus.open, 1); assert.equal(stats.byPriority.high, 1); // Persistence is a real file: a new app instance reads the same state. const reloaded = JSON.parse(readFileSync(dataFile, "utf8")); assert.equal(reloaded.tasks.length, 2); assert.equal(reloaded.nextId, 3); }); });