- 新增 branch/evaluate-rule.test.ts:覆盖 eq/neq、empty/not_empty、in/not_in、
include/not_include、gt/gte/lt/lte 全部比较符,含无法确定(返回 null)的边界,
以及 @{value(...)} / @{choose(...)} 描述符解析。此前只有 eq 与 gt 有用例。
- 新增未知节点类型与权限证据边界用例:UNKNOWN_TASK_TYPE 硬软门禁、
以及拿不到开通清单(availableNodeTypes 为空)时既不谎报通过也不误报缺失。
错误指纹:
- alert observe() 原先在 SQLite 事务提交后直接 writeFileSync,
落盘失败会把整轮巡检打成失败;而此刻指纹其实已经写入,
下一轮会被当成「未观察过」从而重复提醒。改为捕获异常并以 exportError 返回,
SQLite 仍作为唯一事实来源。
- 新增 alert/index.test.ts,并把原先写在 state/index.test.ts 里的告警用例迁移回来,
修正模块归属错位。
覆盖 1004 个用例,`bun run check` 无错误。
101 lines
4.0 KiB
TypeScript
101 lines
4.0 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { mkdtempSync, readFileSync, rmSync, chmodSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { Database } from "bun:sqlite";
|
|
import { runMigrations } from "../../../../core/db/index.js";
|
|
import { WorkflowAlertStore, type WorkflowErrorAlert } from "./index.js";
|
|
|
|
/**
|
|
* 错误指纹的持久化行为。此前这些用例写在 state/index.test.ts 里(模块归属错位),
|
|
* 改 alert 时容易被漏跑,这里连同新增的导出失败路径一起收拢。
|
|
*/
|
|
function createStore(): { store: WorkflowAlertStore; cleanup: () => void; file: string } {
|
|
const dir = mkdtempSync("hose-alert-store-");
|
|
const file = join(dir, "alerts.json");
|
|
const dbPath = join(dir, "state.db");
|
|
const db = new Database(dbPath);
|
|
runMigrations(db);
|
|
const store = new WorkflowAlertStore(file, db);
|
|
return { store, file, cleanup: () => { db.close(); rmSync(dir, { recursive: true, force: true }); } };
|
|
}
|
|
|
|
const alert = (overrides: Partial<WorkflowErrorAlert> = {}): WorkflowErrorAlert => ({
|
|
id: "corp:platform:wf-1",
|
|
pipelineId: "wf-1",
|
|
platformId: "platform-1",
|
|
pipelineName: "测试流程",
|
|
errorCount: 1,
|
|
state: 0,
|
|
observedAt: 1,
|
|
...overrides,
|
|
});
|
|
|
|
describe("workflow alert store", () => {
|
|
it("persists fingerprints and only reports changed counts", () => {
|
|
const { store, file, cleanup } = createStore();
|
|
try {
|
|
expect(store.observe([alert()]).newAlerts).toHaveLength(1);
|
|
expect(store.observe([alert({ observedAt: 2 })]).newAlerts).toHaveLength(0);
|
|
expect(store.observe([alert({ errorCount: 2, observedAt: 3 })]).newAlerts).toHaveLength(1);
|
|
expect(store.observe([]).newAlerts).toHaveLength(0);
|
|
// 告警消失后再次出现,应当重新提醒
|
|
expect(store.observe([alert({ observedAt: 4 })]).newAlerts).toHaveLength(1);
|
|
expect(JSON.parse(readFileSync(file, "utf8")).version).toBe(1);
|
|
} finally {
|
|
cleanup();
|
|
}
|
|
});
|
|
|
|
it("clears resolved alerts only for complete scans", () => {
|
|
const { store, cleanup } = createStore();
|
|
try {
|
|
expect(store.observe([alert()]).newAlerts).toHaveLength(1);
|
|
// 扫描不完整时不清空指纹:同一条告警再次出现不再重复提醒
|
|
store.observe([], { complete: false });
|
|
expect(store.observe([alert({ observedAt: 2 })], { complete: false }).newAlerts).toHaveLength(0);
|
|
// 完整扫描才清理已解决的告警,之后重现才会重新提醒
|
|
store.observe([], { complete: true });
|
|
expect(store.observe([alert()]).newAlerts).toHaveLength(1);
|
|
} finally {
|
|
cleanup();
|
|
}
|
|
});
|
|
|
|
it("keeps observing when the JSON export fails", () => {
|
|
const { store, cleanup } = createStore();
|
|
try {
|
|
const first = store.observe([alert()]);
|
|
expect(first.newAlerts).toHaveLength(1);
|
|
expect(first.exportError).toBeUndefined();
|
|
} finally {
|
|
cleanup();
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("workflow alert store export failures", () => {
|
|
it("surfaces the export failure without losing observed results", () => {
|
|
const dir = mkdtempSync("hose-alert-export-");
|
|
const nested = join(dir, "blocked");
|
|
const unwritableFile = join(nested, "alerts.json");
|
|
const db = new Database(join(dir, "state.db"));
|
|
runMigrations(db);
|
|
try {
|
|
// 把目标目录设为不可写:SQLite 已写入,但 JSON 副本落盘必然失败
|
|
const fs = require("node:fs");
|
|
fs.mkdirSync(nested, { recursive: true });
|
|
fs.chmodSync(nested, 0o500);
|
|
const store = new WorkflowAlertStore(unwritableFile, db);
|
|
const result = store.observe([alert()]);
|
|
expect(result.newAlerts).toHaveLength(1);
|
|
expect(result.exportError).toBeDefined();
|
|
// 事实来源是 SQLite:即便副本失败,指纹依然被记住,下一轮不会重复提醒
|
|
expect(store.observe([alert({ observedAt: 2 })]).newAlerts).toHaveLength(0);
|
|
} finally {
|
|
chmodSync(nested, 0o700);
|
|
db.close();
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|