mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
test: stabilize vitest mocks and harness typing
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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-"));
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { monitorIMessageProvider } from "./monitor.js";
|
||||
import { beforeAll, describe, expect, it } from "vitest";
|
||||
import {
|
||||
flush,
|
||||
getCloseResolve,
|
||||
@@ -15,6 +14,12 @@ import {
|
||||
|
||||
installMonitorIMessageProviderTestHooks();
|
||||
|
||||
let monitorIMessageProvider: typeof import("./monitor.js").monitorIMessageProvider;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ monitorIMessageProvider } = await import("./monitor.js"));
|
||||
});
|
||||
|
||||
const replyMock = getReplyMock();
|
||||
const sendMock = getSendMock();
|
||||
const upsertPairingRequestMock = getUpsertPairingRequestMock();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { beforeEach, vi } from "vitest";
|
||||
import { beforeEach, type MockInstance, vi } from "vitest";
|
||||
|
||||
type NotificationHandler = (msg: { method: string; params?: unknown }) => void;
|
||||
|
||||
@@ -15,31 +15,31 @@ const state = vi.hoisted(() => ({
|
||||
closeResolve: undefined as (() => void) | undefined,
|
||||
}));
|
||||
|
||||
export function getRequestMock() {
|
||||
export function getRequestMock(): MockInstance {
|
||||
return state.requestMock;
|
||||
}
|
||||
|
||||
export function getStopMock() {
|
||||
export function getStopMock(): MockInstance {
|
||||
return state.stopMock;
|
||||
}
|
||||
|
||||
export function getSendMock() {
|
||||
export function getSendMock(): MockInstance {
|
||||
return state.sendMock;
|
||||
}
|
||||
|
||||
export function getReplyMock() {
|
||||
export function getReplyMock(): MockInstance {
|
||||
return state.replyMock;
|
||||
}
|
||||
|
||||
export function getUpdateLastRouteMock() {
|
||||
export function getUpdateLastRouteMock(): MockInstance {
|
||||
return state.updateLastRouteMock;
|
||||
}
|
||||
|
||||
export function getReadAllowFromStoreMock() {
|
||||
export function getReadAllowFromStoreMock(): MockInstance {
|
||||
return state.readAllowFromStoreMock;
|
||||
}
|
||||
|
||||
export function getUpsertPairingRequestMock() {
|
||||
export function getUpsertPairingRequestMock(): MockInstance {
|
||||
return state.upsertPairingRequestMock;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { monitorIMessageProvider } from "./monitor.js";
|
||||
import { beforeAll, describe, expect, it } from "vitest";
|
||||
import {
|
||||
flush,
|
||||
getCloseResolve,
|
||||
@@ -14,6 +13,12 @@ import {
|
||||
|
||||
installMonitorIMessageProviderTestHooks();
|
||||
|
||||
let monitorIMessageProvider: typeof import("./monitor.js").monitorIMessageProvider;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ monitorIMessageProvider } = await import("./monitor.js"));
|
||||
});
|
||||
|
||||
const replyMock = getReplyMock();
|
||||
const requestMock = getRequestMock();
|
||||
const stopMock = getStopMock();
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { monitorWebChannel } from "./auto-reply.js";
|
||||
import { beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
installWebAutoReplyTestHomeHooks,
|
||||
installWebAutoReplyUnitTestHooks,
|
||||
@@ -13,6 +12,12 @@ import {
|
||||
|
||||
installWebAutoReplyTestHomeHooks();
|
||||
|
||||
let monitorWebChannel: typeof import("./auto-reply.js").monitorWebChannel;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ monitorWebChannel } = await import("./auto-reply.js"));
|
||||
});
|
||||
|
||||
describe("web auto-reply", () => {
|
||||
installWebAutoReplyUnitTestHooks();
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import crypto from "node:crypto";
|
||||
import fs from "node:fs/promises";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { expectInboundContextContract } from "../../test/helpers/inbound-contract.js";
|
||||
import { setLoggerOverride } from "../logging.js";
|
||||
import { monitorWebChannel, SILENT_REPLY_TOKEN } from "./auto-reply.js";
|
||||
import {
|
||||
installWebAutoReplyTestHomeHooks,
|
||||
installWebAutoReplyUnitTestHooks,
|
||||
@@ -14,6 +13,13 @@ import {
|
||||
|
||||
installWebAutoReplyTestHomeHooks();
|
||||
|
||||
let monitorWebChannel: typeof import("./auto-reply.js").monitorWebChannel;
|
||||
let SILENT_REPLY_TOKEN: typeof import("./auto-reply.js").SILENT_REPLY_TOKEN;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ monitorWebChannel, SILENT_REPLY_TOKEN } = await import("./auto-reply.js"));
|
||||
});
|
||||
|
||||
describe("web auto-reply", () => {
|
||||
installWebAutoReplyUnitTestHooks();
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { monitorWebChannel } from "./auto-reply.js";
|
||||
import { beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
installWebAutoReplyTestHomeHooks,
|
||||
installWebAutoReplyUnitTestHooks,
|
||||
@@ -9,6 +8,12 @@ import {
|
||||
|
||||
installWebAutoReplyTestHomeHooks();
|
||||
|
||||
let monitorWebChannel: typeof import("./auto-reply.js").monitorWebChannel;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ monitorWebChannel } = await import("./auto-reply.js"));
|
||||
});
|
||||
|
||||
describe("web auto-reply", () => {
|
||||
installWebAutoReplyUnitTestHooks();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user