1
0

fix: 安全性与代码质量加固(异常保护、外键级联、竞态修复、优雅关机)

This commit is contained in:
2026-05-11 14:24:12 +08:00
parent 35ba56888b
commit 0ee10b47c9
17 changed files with 132 additions and 33 deletions

View File

@@ -254,4 +254,33 @@ describe("API 路由", () => {
const asset = await fetchHandler(new Request("http://localhost/assets/app.js"));
expect(asset.status).toBe(200);
});
test("损坏的 failure JSON 返回 null 而不崩溃", async () => {
const targets = store.getTargets();
const t1Id = targets[0]!.id;
store.insertCheckResult({
targetId: t1Id,
timestamp: "2025-06-01T00:00:00.000Z",
matched: false,
durationMs: 100,
statusDetail: "200 OK",
failure: { kind: "error", phase: "body", path: "$", message: "test" },
});
(store as unknown as { db: { prepare: (sql: string) => { run: (...args: unknown[]) => void } } }).db
.prepare("UPDATE check_results SET failure = ? WHERE target_id = ? AND timestamp = ?")
.run("{invalid json!!!", t1Id, "2025-06-01T00:00:00.000Z");
const from = "2025-06-01T00:00:00.000Z";
const to = "2025-06-01T23:59:59.999Z";
const response = await fetchHandler(
new Request(`http://localhost/api/targets/${t1Id}/history?from=${from}&to=${to}`),
);
const body = (await response.json()) as HistoryResponse;
expect(response.status).toBe(200);
expect(body.items).toHaveLength(1);
expect(body.items[0]!.failure).toBeNull();
});
});