54 lines
2.8 KiB
JavaScript
54 lines
2.8 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { unlink } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
|
|
const api = process.env.AI_DRAMA_API_BASE || "http://127.0.0.1:8787";
|
|
|
|
async function request(path, headers, options = {}) {
|
|
const response = await fetch(`${api}${path}`, {
|
|
...options,
|
|
headers: { "content-type": "application/json", ...(headers || {}), ...(options.headers || {}) }
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
return { response, payload };
|
|
}
|
|
|
|
async function login(email) {
|
|
const result = await request("/api/auth/login", null, { method: "POST", body: JSON.stringify({ email, password: "Demo@123456" }) });
|
|
assert.equal(result.response.ok, true, `${email} 登录失败`);
|
|
return { authorization: `Bearer ${result.payload.session.token}` };
|
|
}
|
|
|
|
const owner = await login("producer@local.test");
|
|
const writer = await login("writer@local.test");
|
|
let createdRelativePath = "";
|
|
|
|
try {
|
|
const denied = await request("/api/system/readiness", writer);
|
|
assert.equal(denied.response.status, 403, "普通用户不应读取系统生产就绪度");
|
|
|
|
const readiness = await request("/api/system/readiness", owner);
|
|
assert.equal(readiness.response.ok, true, "系统管理员读取生产就绪度失败");
|
|
assert.ok(Array.isArray(readiness.payload.checks), "生产就绪度缺少检查项");
|
|
assert.equal(readiness.payload.activeRuntime.database, "node:sqlite", "当前业务数据库运行时记录不准确");
|
|
assert.ok(readiness.payload.checks.some((check) => check.key === "database-backup"), "生产就绪度缺少备份检查");
|
|
|
|
const created = await request("/api/system/backups", owner, { method: "POST", body: "{}" });
|
|
assert.equal(created.response.status, 201, `创建数据库快照失败:${JSON.stringify(created.payload)}`);
|
|
assert.ok(created.payload.backup?.relativePath, "数据库快照缺少相对路径");
|
|
assert.ok(Number(created.payload.backup.bytes) > 0, "数据库快照文件为空");
|
|
createdRelativePath = created.payload.backup.relativePath;
|
|
|
|
const backups = await request("/api/system/backups", owner);
|
|
assert.equal(backups.response.ok, true, "读取数据库快照列表失败");
|
|
assert.ok(backups.payload.backups.some((backup) => backup.relativePath === createdRelativePath), "数据库快照没有出现在列表中");
|
|
|
|
const readinessAfterBackup = await request("/api/system/readiness", owner);
|
|
assert.equal(readinessAfterBackup.response.ok, true, "创建快照后读取生产就绪度失败");
|
|
assert.ok(readinessAfterBackup.payload.backups.latest?.relativePath === createdRelativePath, "生产就绪度没有反映最近快照");
|
|
|
|
console.log(`readiness smoke passed: ${api}`);
|
|
} finally {
|
|
if (createdRelativePath) await unlink(resolve(process.cwd(), createdRelativePath)).catch(() => {});
|
|
}
|