Files
pikasTech-unidesk/scripts/src/platform-db-monitor-reset.test.ts
T
2026-07-13 06:00:09 +00:00

71 lines
3.3 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { rootPath } from "./config";
import { readYamlRecord } from "./platform-infra-ops-library";
import { monitorResetScript, monitorStatusScript, monitorStatusSucceeded, runPlatformDbCommand } from "./platform-db";
const configPath = "config/platform-db/postgres-nc01.yaml";
const parsed = readYamlRecord<Record<string, any>>(rootPath(configPath), "postgres-host-cluster");
const pg = { objects: parsed.objects };
const target = parsed.managedOperations.monitorReset;
describe("platform-db Monitor 专属空库入口", () => {
test("plan 精确显示 YAML 目标且默认零写入", async () => {
const result = await runPlatformDbCommand({} as never, ["postgres", "monitor", "plan", "--config", configPath]);
expect(result).toMatchObject({
ok: true,
target: "web-probe-monitor",
config: configPath,
database: "web_probe_monitor",
owner: "web_probe_monitor",
schema: "monitor",
secretRef: "platform-db/web-probe-monitor-nc01-db.env",
mutation: false,
valuesPrinted: false,
});
expect(JSON.stringify(result)).not.toContain("PASSWORD");
expect(JSON.stringify(result)).not.toContain("DATABASE_URL");
});
test("reset 未确认时只返回计划", async () => {
const result = await runPlatformDbCommand({} as never, ["postgres", "monitor", "reset", "--config", configPath]);
expect(result).toMatchObject({ ok: true, mutation: false, database: "web_probe_monitor" });
expect(result).not.toHaveProperty("confirmed");
});
test("确认脚本只重建 Monitor database 并应用 central schema", () => {
const script = monitorResetScript(pg as never, target);
expect(script).toContain("database='web_probe_monitor'");
expect(script).toContain("owner='web_probe_monitor'");
expect(script).toContain('drop database if exists "web_probe_monitor";');
expect(script).toContain("create schema if not exists monitor;");
expect(script).toContain('set role "web_probe_monitor";');
expect(script).not.toContain("drop database agentrun_v02");
expect(script).not.toContain("drop database decision_center");
});
test("status/reset 验证 schema ready、业务 0 行与其他数据库 OID 不变", () => {
const status = monitorStatusScript(pg as never, target);
const reset = monitorResetScript(pg as never, target);
expect(status).toContain('\"schemaReady\":%s');
expect(status).toContain('\"businessRows\":%s');
expect(status).toContain('\"siblingDatabaseOids\":\"%s\"');
expect(reset).toContain('[ "$before" != "$after" ]');
expect(reset).toContain('[ "$rows" != "0" ]');
expect(reset).toContain('\"siblingDatabasesUnchanged\":true');
});
test("status ready=true 且远端退出 0 时成功", () => {
expect(monitorStatusSucceeded(0, { ok: true, ready: true, schemaReady: true, empty: true, businessRows: 0 })).toBe(true);
});
test("status ready=false 时失败,即使远端误返回退出 0", () => {
expect(monitorStatusSucceeded(0, { ok: false, ready: false, schemaReady: true, empty: false, businessRows: 1 })).toBe(false);
expect(monitorStatusSucceeded(0, { ok: true, ready: false, schemaReady: false, empty: false, businessRows: null })).toBe(false);
expect(monitorStatusSucceeded(1, { ok: false, ready: false })).toBe(false);
});
});