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 validateAiAccountConnection: (typeof import("../src/lib/boss-master-agent"))["validateAiAccountConnection"]; let readState: (typeof import("../src/lib/boss-data"))["readState"]; let deviceHeartbeatRoute: (typeof import("../src/app/api/device-heartbeat/route"))["POST"]; async function setup() { if (runtimeRoot) return; runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-ai-account-")); process.env.BOSS_RUNTIME_ROOT = runtimeRoot; process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json"); const [masterAgent, data, heartbeatModule] = await Promise.all([ import("../src/lib/boss-master-agent.ts"), import("../src/lib/boss-data.ts"), import("../src/app/api/device-heartbeat/route.ts"), ]); validateAiAccountConnection = masterAgent.validateAiAccountConnection; readState = data.readState; deviceHeartbeatRoute = heartbeatModule.POST; } test.after(async () => { if (runtimeRoot) { await rm(runtimeRoot, { recursive: true, force: true }); } }); test("validateAiAccountConnection explains master node login happens on the bound device", async () => { await setup(); const result = await validateAiAccountConnection("master-codex-primary"); assert.equal(result.ok, true); assert.equal(result.status, "ready"); assert.match(result.message, /不在手机里直接登录/); assert.match(result.message, /Mac Studio|本机 Codex/); }); test("validateAiAccountConnection reports degraded when the bound master node device is offline", async () => { await setup(); const state = await readState(); const device = state.devices.find((item) => item.id === "mac-studio"); assert.ok(device, "expected default mac-studio seed device"); const heartbeatResponse = await deviceHeartbeatRoute( new NextRequest("http://127.0.0.1:3000/api/device-heartbeat", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ deviceId: device!.id, token: device!.token, name: device!.name, avatar: device!.avatar, account: device!.account, status: "offline", quota5h: device!.quota5h, quota7d: device!.quota7d, projects: device!.projects, endpoint: device!.endpoint, }), }), ); assert.equal(heartbeatResponse.status, 200); const result = await validateAiAccountConnection("master-codex-primary"); assert.equal(result.ok, false); assert.equal(result.status, "degraded"); assert.match(result.message, /当前不在线|不在线/); });