Reorganize repo structure and standards references

This commit is contained in:
david committed 2026-06-29 05:03:42 +02:00
1 parent 3cb93a81be
commit be2054274d
5885 files changed
+2588 -195

No files matched your search

@@ -0,0 +1,129 @@
import { describe, expect, test, vi } from "vitest";
import { createCrashTracker, type CrashTrackerStore } from "./crash-tracker.js";
function makeMemoryStore(): CrashTrackerStore {
const map = new Map<string, number>();
return {
async incr(k, by = 1) {
const next = (map.get(k) ?? 0) + by;
map.set(k, next);
return next;
},
async expire() {
return true;
},
async get(k) {
const v = map.get(k);
return v === undefined ? null : String(v);
},
async set(k, v) {
map.set(k, Number(v));
return true;
},
async del(k) {
return map.delete(k) ? 1 : 0;
},
};
}
describe("createCrashTracker", () => {
test("records failures and reports running count", async () => {
const store = makeMemoryStore();
const disablePlugin = vi.fn();
const tracker = createCrashTracker({ store, disablePlugin, options: { threshold: 5 } });
for (let i = 0; i < 4; i += 1) {
const r = await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
expect(r.autoDisabled).toBe(false);
}
expect(await tracker.getFailureCount({ pluginId: "p", tenantId: "t" })).toBe(4);
expect(disablePlugin).not.toHaveBeenCalled();
});
test("auto-disables when threshold is crossed", async () => {
const store = makeMemoryStore();
const disablePlugin = vi.fn().mockResolvedValue(undefined);
const notify = vi.fn().mockResolvedValue(undefined);
const tracker = createCrashTracker({ store, disablePlugin, notify, options: { threshold: 3 } });
await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
const result = await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
expect(result.autoDisabled).toBe(true);
expect(disablePlugin).toHaveBeenCalledOnce();
expect(notify).toHaveBeenCalledOnce();
// Counter should reset so a re-enable doesn't trip again.
expect(await tracker.getFailureCount({ pluginId: "p", tenantId: "t" })).toBe(0);
});
test("passes failure correlation through auto-disable hooks", async () => {
const store = makeMemoryStore();
const disablePlugin = vi.fn().mockResolvedValue(undefined);
const notify = vi.fn().mockResolvedValue(undefined);
const tracker = createCrashTracker({ store, disablePlugin, notify, options: { threshold: 1 } });
await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap", correlationId: "inv-1" });
expect(disablePlugin).toHaveBeenCalledWith({
pluginId: "p",
tenantId: "t",
failureCount: 1,
reason: "trap",
correlationId: "inv-1",
});
expect(notify).toHaveBeenCalledWith({
pluginId: "p",
tenantId: "t",
failureCount: 1,
reason: "trap",
correlationId: "inv-1",
});
});
test("uses deterministic fallback correlation when invocation id is missing", async () => {
const store = makeMemoryStore();
const disablePlugin = vi.fn().mockResolvedValue(undefined);
const tracker = createCrashTracker({ store, disablePlugin, options: { threshold: 2 } });
await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
expect(disablePlugin).toHaveBeenCalledWith({
pluginId: "p",
tenantId: "t",
failureCount: 2,
reason: "trap",
correlationId: "missing-invocation:2",
});
});
test("recordSuccess decrements failure count toward zero", async () => {
const store = makeMemoryStore();
const tracker = createCrashTracker({
store,
disablePlugin: vi.fn(),
options: { threshold: 5 },
});
await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
expect((await tracker.recordSuccess({ pluginId: "p", tenantId: "t" })).failureCount).toBe(1);
expect((await tracker.recordSuccess({ pluginId: "p", tenantId: "t" })).failureCount).toBe(0);
// Already-zero counter stays at zero.
expect((await tracker.recordSuccess({ pluginId: "p", tenantId: "t" })).failureCount).toBe(0);
});
test("keeps the crash counter when auto-disable fails", async () => {
const store = makeMemoryStore();
const disablePlugin = vi.fn().mockRejectedValue(new Error("db down"));
const notify = vi.fn().mockResolvedValue(undefined);
const tracker = createCrashTracker({ store, disablePlugin, notify, options: { threshold: 1 } });
const result = await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
expect(result).toEqual({ failureCount: 1, autoDisabled: false });
expect(notify).not.toHaveBeenCalled();
expect(await tracker.getFailureCount({ pluginId: "p", tenantId: "t" })).toBe(1);
});
test("getFailureCount returns 0 for unknown keys", async () => {
const store = makeMemoryStore();
const tracker = createCrashTracker({ store, disablePlugin: vi.fn() });
expect(await tracker.getFailureCount({ pluginId: "x", tenantId: "y" })).toBe(0);
});
});