import test from "node:test"; import assert from "node:assert/strict"; import os from "node:os"; import path from "node:path"; import { mkdtemp, rm } from "node:fs/promises"; import { NextRequest } from "next/server"; let runtimeRoot = ""; let data: typeof import("../src/lib/boss-data"); let authCookie = ""; let postScan: (typeof import("../src/app/api/v1/admin/risks/scan/route"))["POST"]; let getOverview: (typeof import("../src/app/api/v1/admin/overview/route"))["GET"]; async function setup() { if (runtimeRoot) return; runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-risk-sla-notifications-")); process.env.BOSS_RUNTIME_ROOT = runtimeRoot; process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json"); const [dataModule, authModule, scanRoute, overviewRoute] = await Promise.all([ import("../src/lib/boss-data.ts"), import("../src/lib/boss-auth.ts"), import("../src/app/api/v1/admin/risks/scan/route.ts"), import("../src/app/api/v1/admin/overview/route.ts"), ]); data = dataModule; authCookie = authModule.AUTH_SESSION_COOKIE; postScan = scanRoute.POST; getOverview = overviewRoute.GET; } test.after(async () => { if (runtimeRoot) await rm(runtimeRoot, { recursive: true, force: true }); }); test.beforeEach(async () => { await setup(); const now = "2026-04-27T17:30:00+08:00"; const state = await data.readState(); await data.writeState({ ...state, adminNotifications: [], authAccounts: [ { id: "account-owner", account: "owner@example.com", passwordHash: "hash", displayName: "平台管理员", role: "highest_admin", createdAt: now, updatedAt: now, }, { id: "account-customer", account: "customer@example.com", passwordHash: "hash", displayName: "客户负责人", role: "admin", companyId: "tenant-a", createdAt: now, updatedAt: now, }, ], devices: [ { id: "mac-a", name: "客户 Mac", avatar: "M", account: "customer@example.com", companyId: "tenant-a", source: "production", status: "online", projects: ["project-a"], quota5h: 0, quota7d: 0, lastSeenAt: now, }, ], projects: [], opsFaults: [ { faultId: "fault-overdue", faultKey: "LOCAL_AGENT.OFFLINE", severity: "critical", status: "opened", nodeId: "mac-a", serviceName: "local-agent", projectId: "project-a", traceId: "trace-overdue", runbookId: "runbook-local-agent", firstSeenAt: "2026-04-27T12:00:00+08:00", lastSeenAt: "2026-04-27T13:00:00+08:00", ownerAccount: "customer@example.com", slaDueAt: "2026-04-27T14:00:00+08:00", summary: "local-agent 离线超过 SLA", suggestedNextAction: "联系客户重启本地节点", autoRepairable: false, }, ], threadContextAlerts: [], }); }); async function adminRequest(url: string, init: RequestInit = {}) { const session = await data.createAuthSession({ account: "owner@example.com", role: "highest_admin", displayName: "平台管理员", loginMethod: "password", }); return new NextRequest(url, { ...init, headers: { ...(init.headers ?? {}), cookie: `${authCookie}=${session.sessionToken}`, }, }); } test("risk SLA scan creates idempotent overdue notifications", async () => { const first = await postScan(await adminRequest("http://127.0.0.1:3000/api/v1/admin/risks/scan", { method: "POST", })); assert.equal(first.status, 200); const firstPayload = await first.json(); assert.equal(firstPayload.created.length, 1); assert.equal(firstPayload.notifications[0].kind, "risk_sla_overdue"); assert.equal(firstPayload.notifications[0].companyId, "tenant-a"); const second = await postScan(await adminRequest("http://127.0.0.1:3000/api/v1/admin/risks/scan", { method: "POST", })); assert.equal(second.status, 200); const secondPayload = await second.json(); assert.equal(secondPayload.created.length, 0); assert.equal(secondPayload.notifications.length, 1); }); test("admin overview includes open risk notifications", async () => { await postScan(await adminRequest("http://127.0.0.1:3000/api/v1/admin/risks/scan", { method: "POST" })); const response = await getOverview(await adminRequest("http://127.0.0.1:3000/api/v1/admin/overview")); assert.equal(response.status, 200); const payload = await response.json(); assert.equal(payload.summary.openNotifications, 1); assert.equal(payload.notifications[0].riskId, "ops-fault:fault-overdue"); });