test: speed up stream and bash tool tests

This commit is contained in:
Peter Steinberger
2026-04-08 00:08:44 +01:00
parent d2fa1de7ba
commit f738297bda
5 changed files with 46 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
import type { Model } from "@mariozechner/pi-ai";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { SYSTEM_PROMPT_CACHE_BOUNDARY } from "./system-prompt-cache-boundary.js";
const hoisted = vi.hoisted(() => {
@@ -31,29 +31,29 @@ vi.mock("@anthropic-ai/vertex-sdk", () => ({
}),
}));
vi.mock("../plugin-sdk/anthropic-vertex.js", () => ({
resolveAnthropicVertexProjectId: (env: NodeJS.ProcessEnv = process.env) =>
env.ANTHROPIC_VERTEX_PROJECT_ID || env.GOOGLE_CLOUD_PROJECT || env.GOOGLE_CLOUD_PROJECT_ID,
resolveAnthropicVertexClientRegion: (params?: { baseUrl?: string; env?: NodeJS.ProcessEnv }) => {
const baseUrl = params?.baseUrl?.trim();
if (baseUrl) {
try {
const host = new URL(baseUrl).hostname;
const match = /^([a-z0-9-]+)-aiplatform\.googleapis\.com$/u.exec(host);
if (match?.[1]) {
return match[1];
}
} catch {
// noop; test seam only
}
}
return params?.env?.GOOGLE_CLOUD_LOCATION || params?.env?.CLOUD_ML_REGION || "global";
},
}));
let createAnthropicVertexStreamFn: typeof import("./anthropic-vertex-stream.js").createAnthropicVertexStreamFn;
let createAnthropicVertexStreamFnForModel: typeof import("./anthropic-vertex-stream.js").createAnthropicVertexStreamFnForModel;
async function loadFreshAnthropicVertexStreamModuleForTest() {
vi.resetModules();
vi.doMock("@mariozechner/pi-ai", async () => {
const original =
await vi.importActual<typeof import("@mariozechner/pi-ai")>("@mariozechner/pi-ai");
return {
...original,
streamAnthropic: (model: unknown, context: unknown, options: unknown) =>
hoisted.streamAnthropicMock(model, context, options),
};
});
vi.doMock("@anthropic-ai/vertex-sdk", () => ({
AnthropicVertex: vi.fn(function MockAnthropicVertex(options: unknown) {
hoisted.anthropicVertexCtorMock(options);
return { options };
}),
}));
return await import("./anthropic-vertex-stream.js");
}
function makeModel(params: { id: string; maxTokens?: number }): Model<"anthropic-messages"> {
return {
id: params.id,
@@ -64,16 +64,16 @@ function makeModel(params: { id: string; maxTokens?: number }): Model<"anthropic
}
describe("createAnthropicVertexStreamFn", () => {
beforeAll(async () => {
({ createAnthropicVertexStreamFn, createAnthropicVertexStreamFnForModel } =
await import("./anthropic-vertex-stream.js"));
});
beforeEach(() => {
hoisted.streamAnthropicMock.mockClear();
hoisted.anthropicVertexCtorMock.mockClear();
});
beforeEach(async () => {
({ createAnthropicVertexStreamFn, createAnthropicVertexStreamFnForModel } =
await loadFreshAnthropicVertexStreamModuleForTest());
});
it("omits projectId when ADC credentials are used without an explicit project", () => {
const streamFn = createAnthropicVertexStreamFn(undefined, "global");
@@ -323,11 +323,6 @@ describe("createAnthropicVertexStreamFnForModel", () => {
hoisted.anthropicVertexCtorMock.mockClear();
});
beforeEach(async () => {
({ createAnthropicVertexStreamFn, createAnthropicVertexStreamFnForModel } =
await loadFreshAnthropicVertexStreamModuleForTest());
});
it("derives project and region from the model and env", () => {
const streamFn = createAnthropicVertexStreamFnForModel(
{ baseUrl: "https://europe-west4-aiplatform.googleapis.com" },

View File

@@ -1,4 +1,4 @@
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, expect, test, vi } from "vitest";
import { killProcessTree } from "../process/kill-tree.js";
const BACKGROUND_HOLD_CMD = 'node -e "setTimeout(() => {}, 5000)"';
@@ -23,13 +23,16 @@ const createTestExecTool = (
defaults?: Parameters<typeof createExecTool>[0],
): ReturnType<typeof createExecTool> => createExecTool({ ...TEST_EXEC_DEFAULTS, ...defaults });
beforeEach(async () => {
vi.resetModules();
beforeAll(async () => {
({ createExecTool } = await import("./bash-tools.exec.js"));
({ getFinishedSession, getSession, resetProcessRegistryForTests } =
await import("./bash-process-registry.js"));
});
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
resetProcessRegistryForTests();
});

View File

@@ -1,4 +1,4 @@
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, expect, test, vi } from "vitest";
let createExecTool: typeof import("./bash-tools.exec.js").createExecTool;
let resetProcessRegistryForTests: typeof import("./bash-process-registry.js").resetProcessRegistryForTests;
@@ -10,12 +10,15 @@ vi.mock("@lydell/node-pty", () => ({
spawn: (...args: unknown[]) => ptySpawnMock(...args),
}));
beforeEach(async () => {
vi.resetModules();
beforeAll(async () => {
({ createExecTool } = await import("./bash-tools.exec.js"));
({ resetProcessRegistryForTests } = await import("./bash-process-registry.js"));
});
beforeEach(() => {
ptySpawnMock.mockReset();
});
afterEach(() => {
resetProcessRegistryForTests();
vi.clearAllMocks();

View File

@@ -1,4 +1,4 @@
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, expect, test, vi } from "vitest";
let createExecTool: typeof import("./bash-tools.exec.js").createExecTool;
let listRunningSessions: typeof import("./bash-process-registry.js").listRunningSessions;
let resetProcessRegistryForTests: typeof import("./bash-process-registry.js").resetProcessRegistryForTests;
@@ -22,13 +22,16 @@ vi.mock("../process/supervisor/index.js", () => ({
getProcessSupervisor: () => makeSupervisor(),
}));
beforeEach(async () => {
vi.resetModules();
beforeAll(async () => {
({ createExecTool } = await import("./bash-tools.exec.js"));
({ listRunningSessions, resetProcessRegistryForTests } =
await import("./bash-process-registry.js"));
});
beforeEach(() => {
supervisorSpawnMock.mockReset();
});
afterEach(() => {
resetProcessRegistryForTests();
vi.clearAllMocks();

View File

@@ -1,4 +1,4 @@
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { afterEach, beforeAll, expect, test, vi } from "vitest";
let createExecTool: typeof import("./bash-tools.exec.js").createExecTool;
let resetProcessRegistryForTests: typeof import("./bash-process-registry.js").resetProcessRegistryForTests;
@@ -10,8 +10,7 @@ vi.mock("@lydell/node-pty", () => ({
},
}));
beforeEach(async () => {
vi.resetModules();
beforeAll(async () => {
({ createExecTool } = await import("./bash-tools.exec.js"));
({ resetProcessRegistryForTests } = await import("./bash-process-registry.js"));
});