test: stabilize vitest mocks and harness typing

This commit is contained in:
Peter Steinberger
2026-02-14 20:44:46 +01:00
parent e4d63818f5
commit 5b7a33272a
14 changed files with 80 additions and 39 deletions

View File

@@ -1,5 +1,5 @@
import { join } from "node:path";
import { afterEach, vi } from "vitest";
import { afterEach, type MockInstance, vi } from "vitest";
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
const piEmbeddedMocks = vi.hoisted(() => ({
@@ -11,19 +11,19 @@ const piEmbeddedMocks = vi.hoisted(() => ({
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
}));
export function getAbortEmbeddedPiRunMock() {
export function getAbortEmbeddedPiRunMock(): MockInstance {
return piEmbeddedMocks.abortEmbeddedPiRun;
}
export function getCompactEmbeddedPiSessionMock() {
export function getCompactEmbeddedPiSessionMock(): MockInstance {
return piEmbeddedMocks.compactEmbeddedPiSession;
}
export function getRunEmbeddedPiAgentMock() {
export function getRunEmbeddedPiAgentMock(): MockInstance {
return piEmbeddedMocks.runEmbeddedPiAgent;
}
export function getQueueEmbeddedPiMessageMock() {
export function getQueueEmbeddedPiMessageMock(): MockInstance {
return piEmbeddedMocks.queueEmbeddedPiMessage;
}
@@ -49,7 +49,7 @@ const providerUsageMocks = vi.hoisted(() => ({
resolveUsageProviderId: vi.fn((provider: string) => provider.split("/")[0]),
}));
export function getProviderUsageMocks() {
export function getProviderUsageMocks(): Record<string, MockInstance> {
return providerUsageMocks;
}
@@ -77,7 +77,7 @@ const modelCatalogMocks = vi.hoisted(() => ({
resetModelCatalogCacheForTest: vi.fn(),
}));
export function getModelCatalogMocks() {
export function getModelCatalogMocks(): Record<string, MockInstance> {
return modelCatalogMocks;
}
@@ -89,7 +89,7 @@ const webSessionMocks = vi.hoisted(() => ({
readWebSelfId: vi.fn().mockReturnValue({ e164: "+1999" }),
}));
export function getWebSessionMocks() {
export function getWebSessionMocks(): Record<string, MockInstance> {
return webSessionMocks;
}

View File

@@ -1,4 +1,4 @@
import { beforeEach, vi } from "vitest";
import { beforeEach, type MockInstance, vi } from "vitest";
import type { SessionEntry } from "../../config/sessions.js";
import type { TypingMode } from "../../config/types.js";
import type { TemplateContext } from "../templating.js";
@@ -10,7 +10,7 @@ const state = vi.hoisted(() => ({
runEmbeddedPiAgentMock: vi.fn(),
}));
export function getRunEmbeddedPiAgentMock() {
export function getRunEmbeddedPiAgentMock(): MockInstance {
return state.runEmbeddedPiAgentMock;
}

View File

@@ -2,7 +2,6 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { runReplyAgent } from "./agent-runner.js";
import {
createBaseRun,
getRunEmbeddedPiAgentMock,
@@ -13,6 +12,7 @@ import { DEFAULT_MEMORY_FLUSH_PROMPT } from "./memory-flush.js";
describe("runReplyAgent memory flush", () => {
it("increments compaction count when flush compaction completes", async () => {
const { runReplyAgent } = await import("./agent-runner.js");
const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();
runEmbeddedPiAgentMock.mockReset();
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-flush-"));

View File

@@ -1,8 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { runReplyAgent } from "./agent-runner.js";
import { beforeAll, describe, expect, it } from "vitest";
import {
createBaseRun,
getRunEmbeddedPiAgentMock,
@@ -11,6 +10,12 @@ import {
} from "./agent-runner.memory-flush.test-harness.js";
import { DEFAULT_MEMORY_FLUSH_PROMPT } from "./memory-flush.js";
let runReplyAgent: typeof import("./agent-runner.js").runReplyAgent;
beforeAll(async () => {
({ runReplyAgent } = await import("./agent-runner.js"));
});
describe("runReplyAgent memory flush", () => {
it("runs a memory flush turn and updates session metadata", async () => {
const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();

View File

@@ -2,7 +2,6 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { runReplyAgent } from "./agent-runner.js";
import {
createBaseRun,
getRunCliAgentMock,
@@ -13,6 +12,7 @@ import {
describe("runReplyAgent memory flush", () => {
it("skips memory flush for CLI providers", async () => {
const { runReplyAgent } = await import("./agent-runner.js");
const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();
const runCliAgentMock = getRunCliAgentMock();
runEmbeddedPiAgentMock.mockReset();

View File

@@ -1,8 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { runReplyAgent } from "./agent-runner.js";
import { beforeAll, describe, expect, it } from "vitest";
import {
createBaseRun,
getRunEmbeddedPiAgentMock,
@@ -10,6 +9,12 @@ import {
type EmbeddedRunParams,
} from "./agent-runner.memory-flush.test-harness.js";
let runReplyAgent: typeof import("./agent-runner.js").runReplyAgent;
beforeAll(async () => {
({ runReplyAgent } = await import("./agent-runner.js"));
});
describe("runReplyAgent memory flush", () => {
it("skips memory flush when the sandbox workspace is read-only", async () => {
const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();

View File

@@ -1,8 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { runReplyAgent } from "./agent-runner.js";
import { beforeAll, describe, expect, it } from "vitest";
import {
createBaseRun,
getRunEmbeddedPiAgentMock,
@@ -11,6 +10,12 @@ import {
} from "./agent-runner.memory-flush.test-harness.js";
import { DEFAULT_MEMORY_FLUSH_PROMPT } from "./memory-flush.js";
let runReplyAgent: typeof import("./agent-runner.js").runReplyAgent;
beforeAll(async () => {
({ runReplyAgent } = await import("./agent-runner.js"));
});
describe("runReplyAgent memory flush", () => {
it("uses configured prompts for memory flush runs", async () => {
const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();

View File

@@ -1,6 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { vi } from "vitest";
import { type MockInstance, vi } from "vitest";
import type { TemplateContext } from "../templating.js";
import type { FollowupRun, QueueSettings } from "./queue.js";
import { createMockTypingController } from "./test-helpers.js";
@@ -16,11 +16,11 @@ const state = vi.hoisted(() => ({
runCliAgentMock: vi.fn(),
}));
export function getRunEmbeddedPiAgentMock() {
export function getRunEmbeddedPiAgentMock(): MockInstance {
return state.runEmbeddedPiAgentMock;
}
export function getRunCliAgentMock() {
export function getRunCliAgentMock(): MockInstance {
return state.runCliAgentMock;
}