import { describe, expect, it, vi } from "vitest"; import { buildReinjectDiagSnapshot, notifyReinjectDiag } from "../src/diag.js"; import { createDefaultSettings } from "../src/settings.js"; import { createInitialState } from "../src/state.js"; describe("buildReinjectDiagSnapshot", () => { it("collects tracked, kept, registered, planned, and pending", () => { const state = createInitialState(); state.skills.push({ name: "alpha", filePath: "/skills/alpha/SKILL.md", baseDir: "/skills/alpha", firstSeenAt: 1, lastSeenAt: 1, sources: ["slash"], }); state.pendingReinject = ["alpha"]; expect( buildReinjectDiagSnapshot( state, [{ name: "beta" }], new Set(["gamma"]), ["alpha"], { compactionSource: "auto", sourceInferred: true, deliveryBranch: "steer", isIdle: false, }, ), ).toEqual({ tracked: ["alpha"], kept: ["gamma"], registered: ["beta"], planned: ["alpha"], pending: ["alpha"], compactionSource: "auto", sourceInferred: true, deliveryBranch: "steer", isIdle: false, }); }); }); describe("notifyReinjectDiag", () => { it("no-ops when debug is off", () => { const notify = vi.fn(); notifyReinjectDiag( { hasUI: true, ui: { notify }, } as never, createDefaultSettings(), "session_compact", { tracked: [], kept: [], registered: [], planned: [], pending: [], }, ); expect(notify).not.toHaveBeenCalled(); }); it("notifies with JSON snapshot when debug is on", () => { const notify = vi.fn(); const stderrSpy = vi.spyOn(console, "error").mockImplementation(() => {}); const settings = { ...createDefaultSettings(), debug: true }; const snapshot = { tracked: ["a"], kept: [], registered: [], planned: ["a"], pending: ["a"], }; notifyReinjectDiag( { hasUI: true, ui: { notify }, } as never, settings, "before_agent_start", snapshot, ); expect(stderrSpy).toHaveBeenCalledWith( `skill-reinject [before_agent_start]: ${JSON.stringify(snapshot)}`, ); expect(notify).toHaveBeenCalledWith( `skill-reinject [before_agent_start]: ${JSON.stringify(snapshot)}`, "info", ); stderrSpy.mockRestore(); }); });