File size: 5,347 Bytes
f778c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | // Boot.md hook tests cover boot file discovery and injected startup context.
import path from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { InternalHookEvent } from "../../internal-hooks.js";
const runBootOnce = vi.fn();
const listAgentIds = vi.fn();
const resolveAgentWorkspaceDir = vi.fn();
const logWarn = vi.fn();
const logDebug = vi.fn();
const MAIN_WORKSPACE_DIR = path.join(path.sep, "ws", "main");
const OPS_WORKSPACE_DIR = path.join(path.sep, "ws", "ops");
function createMockLogger() {
return {
warn: logWarn,
debug: logDebug,
child: vi.fn(() => createMockLogger()),
};
}
vi.mock("../../../gateway/boot.js", () => ({ runBootOnce }));
vi.mock("../../../agents/agent-scope.js", () => ({
listAgentIds,
resolveAgentWorkspaceDir,
}));
vi.mock("../../../logging/subsystem.js", () => ({
createSubsystemLogger: () => createMockLogger(),
}));
const { default: runBootChecklist } = await import("./handler.js");
function makeEvent(overrides?: Partial<InternalHookEvent>): InternalHookEvent {
return {
type: "gateway",
action: "startup",
sessionKey: "test",
context: {},
timestamp: new Date(),
messages: [],
...overrides,
};
}
function expectBootCall(
index: number,
expected: { cfg: unknown; workspaceDir: string; agentId: string },
) {
const params = runBootOnce.mock.calls[index]?.[0] as
| { cfg?: unknown; workspaceDir?: unknown; agentId?: unknown }
| undefined;
if (!params) {
throw new Error(`missing boot call ${index}`);
}
expect(params.cfg).toBe(expected.cfg);
expect(params.workspaceDir).toBe(expected.workspaceDir);
expect(params.agentId).toBe(expected.agentId);
}
describe("boot-md handler", () => {
function setupTwoAgentBootConfig() {
const cfg = { agents: { list: [{ id: "main" }, { id: "ops" }] } };
listAgentIds.mockReturnValue(["main", "ops"]);
resolveAgentWorkspaceDir.mockImplementation((_cfg: unknown, id: string) =>
id === "main" ? MAIN_WORKSPACE_DIR : OPS_WORKSPACE_DIR,
);
return cfg;
}
function setupSingleMainAgentBootConfig(cfg: unknown) {
listAgentIds.mockReturnValue(["main"]);
resolveAgentWorkspaceDir.mockReturnValue(MAIN_WORKSPACE_DIR);
return cfg;
}
beforeEach(() => {
vi.clearAllMocks();
});
it("skips non-gateway events", async () => {
await runBootChecklist(makeEvent({ type: "command", action: "new" }));
expect(runBootOnce).not.toHaveBeenCalled();
});
it("skips non-startup actions", async () => {
await runBootChecklist(makeEvent({ action: "shutdown" }));
expect(runBootOnce).not.toHaveBeenCalled();
});
it("skips when cfg is missing from context", async () => {
await runBootChecklist(makeEvent({ context: { workspaceDir: "/tmp" } }));
expect(runBootOnce).not.toHaveBeenCalled();
});
it("runs boot for each agent", async () => {
const cfg = setupTwoAgentBootConfig();
runBootOnce.mockResolvedValue({ status: "ran" });
await runBootChecklist(makeEvent({ context: { cfg } }));
expect(listAgentIds).toHaveBeenCalledWith(cfg);
expect(runBootOnce).toHaveBeenCalledTimes(2);
expectBootCall(0, { cfg, workspaceDir: MAIN_WORKSPACE_DIR, agentId: "main" });
expectBootCall(1, { cfg, workspaceDir: OPS_WORKSPACE_DIR, agentId: "ops" });
});
it("runs boot for single default agent when no agents configured", async () => {
const cfg = setupSingleMainAgentBootConfig({});
runBootOnce.mockResolvedValue({ status: "skipped", reason: "missing" });
await runBootChecklist(makeEvent({ context: { cfg } }));
expect(runBootOnce).toHaveBeenCalledTimes(1);
expectBootCall(0, { cfg, workspaceDir: MAIN_WORKSPACE_DIR, agentId: "main" });
});
it("logs warning details when a per-agent boot run fails", async () => {
const cfg = setupTwoAgentBootConfig();
runBootOnce
.mockResolvedValueOnce({ status: "ran" })
.mockResolvedValueOnce({ status: "failed", reason: "agent failed" });
await runBootChecklist(makeEvent({ context: { cfg } }));
expect(logWarn).toHaveBeenCalledTimes(1);
expect(logWarn).toHaveBeenCalledWith("startup task failed", {
source: "boot-md",
agentId: "ops",
workspaceDir: OPS_WORKSPACE_DIR,
reason: "agent failed",
});
});
it("logs debug details when a per-agent boot run is skipped", async () => {
const cfg = setupSingleMainAgentBootConfig({ agents: { list: [{ id: "main" }] } });
runBootOnce.mockResolvedValue({ status: "skipped", reason: "missing" });
await runBootChecklist(makeEvent({ context: { cfg } }));
expect(logDebug).toHaveBeenCalledWith("startup task skipped", {
source: "boot-md",
agentId: "main",
workspaceDir: MAIN_WORKSPACE_DIR,
reason: "missing",
});
});
it("deduplicates agents sharing the same workspaceDir (#74072)", async () => {
const cfg = { agents: { list: [{ id: "main" }, { id: "alias" }] } };
listAgentIds.mockReturnValue(["main", "alias"]);
resolveAgentWorkspaceDir.mockReturnValue(MAIN_WORKSPACE_DIR);
runBootOnce.mockResolvedValue({ status: "ran" });
await runBootChecklist(makeEvent({ context: { cfg } }));
expect(runBootOnce).toHaveBeenCalledTimes(1);
expectBootCall(0, { cfg, workspaceDir: MAIN_WORKSPACE_DIR, agentId: "main" });
});
});
|