Files
pi-auto-reinject/test/diag.test.ts
T
grayhook 84535def76 Phase 15: extend debug diag with compaction source and delivery branch
Log sourceInferred, isIdle, deliveryBranch on session_compact and
mid_turn_deliver after steer for B-003 troubleshooting.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-18 23:01:47 +07:00

97 lines
2.2 KiB
TypeScript

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();
});
});