Fix type-test harness issues from session routing and mock typing

This commit is contained in:
Vincent Koc
2026-03-02 00:02:55 -08:00
parent 0b8142f706
commit e6cf0bce5e
6 changed files with 146 additions and 51 deletions

View File

@@ -1,12 +1,18 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it, type Mock, vi } from "vitest";
import { saveExecApprovals } from "../infra/exec-approvals.js";
import type { ExecHostResponse } from "../infra/exec-host.js";
import { handleSystemRunInvoke, formatSystemRunAllowlistMissMessage } from "./invoke-system-run.js";
import type { HandleSystemRunInvokeOptions } from "./invoke-system-run.js";
type MockedRunCommand = Mock<HandleSystemRunInvokeOptions["runCommand"]>;
type MockedRunViaMacAppExecHost = Mock<HandleSystemRunInvokeOptions["runViaMacAppExecHost"]>;
type MockedSendInvokeResult = Mock<HandleSystemRunInvokeOptions["sendInvokeResult"]>;
type MockedSendExecFinishedEvent = Mock<HandleSystemRunInvokeOptions["sendExecFinishedEvent"]>;
type MockedSendNodeEvent = Mock<HandleSystemRunInvokeOptions["sendNodeEvent"]>;
describe("formatSystemRunAllowlistMissMessage", () => {
it("returns legacy allowlist miss message by default", () => {
expect(formatSystemRunAllowlistMissMessage()).toBe("SYSTEM_RUN_DENIED: allowlist miss");
@@ -35,7 +41,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => {
}
function expectInvokeOk(
sendInvokeResult: ReturnType<typeof vi.fn>,
sendInvokeResult: MockedSendInvokeResult,
params?: { payloadContains?: string },
) {
expect(sendInvokeResult).toHaveBeenCalledWith(
@@ -49,7 +55,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => {
}
function expectInvokeErrorMessage(
sendInvokeResult: ReturnType<typeof vi.fn>,
sendInvokeResult: MockedSendInvokeResult,
params: { message: string; exact?: boolean },
) {
expect(sendInvokeResult).toHaveBeenCalledWith(
@@ -63,8 +69,8 @@ describe("handleSystemRunInvoke mac app exec host routing", () => {
}
function expectApprovalRequiredDenied(params: {
sendNodeEvent: ReturnType<typeof vi.fn>;
sendInvokeResult: ReturnType<typeof vi.fn>;
sendNodeEvent: MockedSendNodeEvent;
sendInvokeResult: MockedSendInvokeResult;
}) {
expect(params.sendNodeEvent).toHaveBeenCalledWith(
expect.anything(),
@@ -126,7 +132,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => {
}
function expectCommandPinnedToCanonicalPath(params: {
runCommand: ReturnType<typeof vi.fn>;
runCommand: MockedRunCommand;
expected: string;
commandTail: string[];
cwd?: string;
@@ -153,24 +159,44 @@ describe("handleSystemRunInvoke mac app exec host routing", () => {
sendExecFinishedEvent?: HandleSystemRunInvokeOptions["sendExecFinishedEvent"];
sendNodeEvent?: HandleSystemRunInvokeOptions["sendNodeEvent"];
skillBinsCurrent?: () => Promise<Array<{ name: string; resolvedPath: string }>>;
}) {
const runCommand =
params.runCommand ??
(vi.fn(async (_command: string[], _cwd?: string, _env?: Record<string, string>) =>
createLocalRunResult(),
) as HandleSystemRunInvokeOptions["runCommand"]);
const runViaMacAppExecHost =
params.runViaMacAppExecHost ??
(vi.fn(async () => params.runViaResponse ?? null) as HandleSystemRunInvokeOptions["runViaMacAppExecHost"]);
const sendInvokeResult =
params.sendInvokeResult ??
(vi.fn(async () => {}) as HandleSystemRunInvokeOptions["sendInvokeResult"]);
const sendExecFinishedEvent =
params.sendExecFinishedEvent ??
(vi.fn(async () => {}) as HandleSystemRunInvokeOptions["sendExecFinishedEvent"]);
const sendNodeEvent =
params.sendNodeEvent ??
(vi.fn(async () => {}) as HandleSystemRunInvokeOptions["sendNodeEvent"]);
}): Promise<{
runCommand: MockedRunCommand;
runViaMacAppExecHost: MockedRunViaMacAppExecHost;
sendInvokeResult: MockedSendInvokeResult;
sendNodeEvent: MockedSendNodeEvent;
sendExecFinishedEvent: MockedSendExecFinishedEvent;
}> {
const runCommand: MockedRunCommand = vi.fn<HandleSystemRunInvokeOptions["runCommand"]>(
async () => createLocalRunResult(),
);
const runViaMacAppExecHost: MockedRunViaMacAppExecHost = vi.fn<
HandleSystemRunInvokeOptions["runViaMacAppExecHost"]
>(async () => params.runViaResponse ?? null);
const sendInvokeResult: MockedSendInvokeResult = vi.fn<
HandleSystemRunInvokeOptions["sendInvokeResult"]
>(async () => {});
const sendNodeEvent: MockedSendNodeEvent = vi.fn<HandleSystemRunInvokeOptions["sendNodeEvent"]>(
async () => {},
);
const sendExecFinishedEvent: MockedSendExecFinishedEvent = vi.fn<
HandleSystemRunInvokeOptions["sendExecFinishedEvent"]
>(async () => {});
if (params.runCommand !== undefined) {
runCommand.mockImplementation(params.runCommand);
}
if (params.runViaMacAppExecHost !== undefined) {
runViaMacAppExecHost.mockImplementation(params.runViaMacAppExecHost);
}
if (params.sendInvokeResult !== undefined) {
sendInvokeResult.mockImplementation(params.sendInvokeResult);
}
if (params.sendNodeEvent !== undefined) {
sendNodeEvent.mockImplementation(params.sendNodeEvent);
}
if (params.sendExecFinishedEvent !== undefined) {
sendExecFinishedEvent.mockImplementation(params.sendExecFinishedEvent);
}
await handleSystemRunInvoke({
client: {} as never,
@@ -198,7 +224,13 @@ describe("handleSystemRunInvoke mac app exec host routing", () => {
preferMacAppExecHost: params.preferMacAppExecHost,
});
return { runCommand, runViaMacAppExecHost, sendInvokeResult, sendExecFinishedEvent };
return {
runCommand,
runViaMacAppExecHost,
sendInvokeResult,
sendNodeEvent,
sendExecFinishedEvent,
};
}
it("uses local execution by default when mac app exec host preference is disabled", async () => {