Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/plugin-runtime/src/crash-tracker.coverage.test.ts
T
2026-07-08 16:15:37 +02:00

134 lines
4.7 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import { createCrashTracker, type CrashTrackerStore } from "./crash-tracker.js";
/**
* Coverage for crash-tracker logging + seam-failure branches not hit by
* crash-tracker.test.ts: the warn/info logger calls on the auto-disable
* path, the error logs when the disable / notify seams reject (Error and
* non-Error), and the non-numeric-counter fallbacks.
*/
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;
},
};
}
function makeLogger() {
return {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
}
describe("createCrashTracker — logging + seam failures", () => {
test("logs warn-per-failure and info on auto-disable", async () => {
const logger = makeLogger();
const tracker = createCrashTracker({
store: makeMemoryStore(),
disablePlugin: vi.fn().mockResolvedValue(undefined),
notify: vi.fn().mockResolvedValue(undefined),
options: { threshold: 1, logger },
});
const result = await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
expect(result).toEqual({ failureCount: 1, autoDisabled: true });
expect(logger.warn).toHaveBeenCalledWith(
expect.objectContaining({ pluginId: "p", failureCount: 1, reason: "trap" }),
"plugin.crash.recorded"
);
expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({ pluginId: "p", failureCount: 1 }),
"plugin.crash.auto_disabled"
);
});
test("logs an error and stays enabled when the disable seam rejects with an Error", async () => {
const logger = makeLogger();
const tracker = createCrashTracker({
store: makeMemoryStore(),
disablePlugin: vi.fn().mockRejectedValue(new Error("db unreachable")),
notify: vi.fn().mockResolvedValue(undefined),
options: { threshold: 1, logger },
});
const result = await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
expect(result).toEqual({ failureCount: 1, autoDisabled: false });
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({ pluginId: "p", tenantId: "t", error: "db unreachable" }),
"plugin.crash.disable_failed"
);
});
test("stringifies a non-Error rejection from the disable seam", async () => {
const logger = makeLogger();
const tracker = createCrashTracker({
store: makeMemoryStore(),
// eslint-disable-next-line @typescript-eslint/only-throw-error, prefer-promise-reject-errors
disablePlugin: vi.fn().mockRejectedValue("string failure"),
options: { threshold: 1, logger },
});
const result = await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
expect(result.autoDisabled).toBe(false);
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({ error: "string failure" }),
"plugin.crash.disable_failed"
);
});
test("logs notify_failed but still auto-disables when the notify seam rejects", async () => {
const logger = makeLogger();
const disablePlugin = vi.fn().mockResolvedValue(undefined);
const tracker = createCrashTracker({
store: makeMemoryStore(),
disablePlugin,
notify: vi.fn().mockRejectedValue(new Error("notify channel down")),
options: { threshold: 1, logger },
});
const result = await tracker.recordFailure({ pluginId: "p", tenantId: "t", reason: "trap" });
// Disable succeeded → still auto-disabled even though notify failed.
expect(result).toEqual({ failureCount: 1, autoDisabled: true });
expect(disablePlugin).toHaveBeenCalledOnce();
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({ error: "notify channel down" }),
"plugin.crash.notify_failed"
);
});
test("treats a non-numeric stored counter as zero", async () => {
const garbageStore: CrashTrackerStore = {
incr: async () => 1,
expire: async () => true,
get: async () => "not-a-number",
set: async () => true,
del: async () => 1,
};
const tracker = createCrashTracker({ store: garbageStore, disablePlugin: vi.fn() });
expect(await tracker.getFailureCount({ pluginId: "p", tenantId: "t" })).toBe(0);
expect((await tracker.recordSuccess({ pluginId: "p", tenantId: "t" })).failureCount).toBe(0);
});
});