Skip to content

Commit 9105b37

Browse files
committed
test: harden no-isolate test module resets
1 parent 771a78c commit 9105b37

22 files changed

Lines changed: 126 additions & 100 deletions

‎src/acp/control-plane/manager.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ vi.mock("../runtime/registry.js", async (importOriginal) => {
3333
};
3434
});
3535

36-
const { AcpSessionManager } = await import("./manager.js");
37-
const { AcpRuntimeError } = await import("../runtime/errors.js");
36+
let AcpSessionManager: typeof import("./manager.js").AcpSessionManager;
37+
let AcpRuntimeError: typeof import("../runtime/errors.js").AcpRuntimeError;
3838

3939
const baseCfg = {
4040
acp: {
@@ -149,7 +149,10 @@ function extractRuntimeOptionsFromUpserts(): Array<AcpSessionRuntimeOptions | un
149149
}
150150

151151
describe("AcpSessionManager", () => {
152-
beforeEach(() => {
152+
beforeEach(async () => {
153+
vi.resetModules();
154+
({ AcpSessionManager } = await import("./manager.js"));
155+
({ AcpRuntimeError } = await import("../runtime/errors.js"));
153156
vi.useRealTimers();
154157
hoisted.listAcpSessionEntriesMock.mockReset().mockResolvedValue([]);
155158
hoisted.readAcpSessionEntryMock.mockReset();

‎src/acp/persistent-bindings.test.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import { parseFeishuConversationId } from "../../extensions/feishu/src/conversation-id.js";
33
import { resolveAgentWorkspaceDir } from "../agents/agent-scope.js";
44
import type { ChannelConfiguredBindingProvider, ChannelPlugin } from "../channels/plugins/types.js";
55
import type { OpenClawConfig } from "../config/config.js";
66
import { setActivePluginRegistry } from "../plugins/runtime.js";
77
import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js";
88
import { parseTelegramTopicConversation } from "./conversation-id.js";
9-
import * as persistentBindingsResolveModule from "./persistent-bindings.resolve.js";
109
import { buildConfiguredAcpSessionKey } from "./persistent-bindings.types.js";
1110
const managerMocks = vi.hoisted(() => ({
1211
resolveSession: vi.fn(),
@@ -43,6 +42,10 @@ let lifecycleBindingsModule: Pick<
4342
typeof import("./persistent-bindings.lifecycle.js"),
4443
"ensureConfiguredAcpBindingSession" | "resetAcpSessionInPlace"
4544
>;
45+
let persistentBindingsResolveModule: Pick<
46+
typeof import("./persistent-bindings.resolve.js"),
47+
"resolveConfiguredAcpBindingRecord" | "resolveConfiguredAcpBindingSpecBySessionKey"
48+
>;
4649

4750
type ConfiguredBinding = NonNullable<OpenClawConfig["bindings"]>[number];
4851
type BindingRecordInput = Parameters<
@@ -308,7 +311,10 @@ function mockReadySession(params: {
308311
return sessionKey;
309312
}
310313

311-
beforeEach(() => {
314+
beforeEach(async () => {
315+
vi.resetModules();
316+
persistentBindingsResolveModule = await import("./persistent-bindings.resolve.js");
317+
lifecycleBindingsModule = await import("./persistent-bindings.lifecycle.js");
312318
persistentBindings = {
313319
resolveConfiguredAcpBindingRecord:
314320
persistentBindingsResolveModule.resolveConfiguredAcpBindingRecord,
@@ -346,10 +352,6 @@ beforeEach(() => {
346352
sessionMetaMocks.readAcpSessionEntry.mockReset().mockReturnValue(undefined);
347353
});
348354

349-
beforeAll(async () => {
350-
lifecycleBindingsModule = await import("./persistent-bindings.lifecycle.js");
351-
});
352-
353355
describe("resolveConfiguredAcpBindingRecord", () => {
354356
it("resolves discord channel ACP binding from top-level typed bindings", () => {
355357
const cfg = createCfgWithBindings([

‎src/acp/runtime/session-meta.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ vi.mock("../../config/sessions.js", async () => {
2222
};
2323
});
2424

25-
const { listAcpSessionEntries } = await import("./session-meta.js");
25+
let listAcpSessionEntries: typeof import("./session-meta.js").listAcpSessionEntries;
2626

2727
describe("listAcpSessionEntries", () => {
28-
beforeEach(() => {
28+
beforeEach(async () => {
29+
vi.resetModules();
30+
({ listAcpSessionEntries } = await import("./session-meta.js"));
2931
vi.clearAllMocks();
3032
});
3133

‎src/channels/plugins/actions/actions.test.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import type { OpenClawConfig } from "../../../config/config.js";
33
import type { ChannelMessageActionAdapter } from "../types.js";
44

@@ -199,15 +199,13 @@ async function expectSlackSendRejected(params: Record<string, unknown>, error: R
199199
expect(handleSlackAction).not.toHaveBeenCalled();
200200
}
201201

202-
beforeAll(async () => {
202+
beforeEach(async () => {
203+
vi.resetModules();
203204
({ discordMessageActions } = await import("../../../../extensions/discord/runtime-api.js"));
204205
({ handleDiscordMessageAction } = await import("./discord/handle-action.js"));
205206
({ telegramMessageActions } = await import("../../../../extensions/telegram/runtime-api.js"));
206207
({ signalMessageActions } = await import("../../../../extensions/signal/src/message-actions.js"));
207208
({ createSlackActions } = await import("../../../../extensions/slack/src/channel-actions.js"));
208-
});
209-
210-
beforeEach(() => {
211209
vi.clearAllMocks();
212210
});
213211

‎src/cli/command-secret-gateway.test.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import type { OpenClawConfig } from "../config/config.js";
33

44
const callGateway = vi.fn();
@@ -18,11 +18,9 @@ vi.mock("../utils/message-channel.js", () => ({
1818

1919
let resolveCommandSecretRefsViaGateway: typeof import("./command-secret-gateway.js").resolveCommandSecretRefsViaGateway;
2020

21-
beforeAll(async () => {
21+
beforeEach(async () => {
22+
vi.resetModules();
2223
({ resolveCommandSecretRefsViaGateway } = await import("./command-secret-gateway.js"));
23-
});
24-
25-
beforeEach(() => {
2624
callGateway.mockReset();
2725
});
2826

‎src/config/config.web-search-provider.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2-
import { validateConfigObjectWithPlugins } from "./config.js";
32
import { buildWebSearchProviderConfig } from "./test-helpers.js";
43

54
vi.mock("../runtime.js", () => ({
@@ -121,8 +120,16 @@ vi.mock("../plugins/web-search-providers.js", () => {
121120
};
122121
});
123122

124-
const { __testing } = await import("../agents/tools/web-search.js");
125-
const { resolveSearchProvider } = __testing;
123+
let validateConfigObjectWithPlugins: typeof import("./config.js").validateConfigObjectWithPlugins;
124+
let resolveSearchProvider: typeof import("../agents/tools/web-search.js").__testing.resolveSearchProvider;
125+
126+
beforeEach(async () => {
127+
vi.resetModules();
128+
({ validateConfigObjectWithPlugins } = await import("./config.js"));
129+
({
130+
__testing: { resolveSearchProvider },
131+
} = await import("../agents/tools/web-search.js"));
132+
});
126133

127134
function pluginWebSearchApiKey(
128135
config: Record<string, unknown> | undefined,

‎src/config/sessions/store.pruning.integration.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ vi.mock("../config.js", () => ({
1010
loadConfig: vi.fn().mockReturnValue({}),
1111
}));
1212

13-
import { loadConfig } from "../config.js";
14-
import { clearSessionStoreCacheForTest, loadSessionStore, saveSessionStore } from "./store.js";
13+
let loadConfig: typeof import("../config.js").loadConfig;
14+
let clearSessionStoreCacheForTest: typeof import("./store.js").clearSessionStoreCacheForTest;
15+
let loadSessionStore: typeof import("./store.js").loadSessionStore;
16+
let saveSessionStore: typeof import("./store.js").saveSessionStore;
1517

1618
let mockLoadConfig: ReturnType<typeof vi.fn>;
1719

@@ -79,6 +81,10 @@ describe("Integration: saveSessionStore with pruning", () => {
7981
});
8082

8183
beforeEach(async () => {
84+
vi.resetModules();
85+
({ loadConfig } = await import("../config.js"));
86+
({ clearSessionStoreCacheForTest, loadSessionStore, saveSessionStore } =
87+
await import("./store.js"));
8288
mockLoadConfig = vi.mocked(loadConfig) as ReturnType<typeof vi.fn>;
8389
testDir = await createCaseDir("pruning-integ");
8490
storePath = path.join(testDir, "sessions.json");

‎src/infra/outbound/agent-delivery.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ vi.mock("./targets.js", () => ({
7070
}));
7171

7272
import type { OpenClawConfig } from "../../config/config.js";
73-
import { resolveAgentDeliveryPlan, resolveAgentOutboundTarget } from "./agent-delivery.js";
73+
let resolveAgentDeliveryPlan: typeof import("./agent-delivery.js").resolveAgentDeliveryPlan;
74+
let resolveAgentOutboundTarget: typeof import("./agent-delivery.js").resolveAgentOutboundTarget;
7475

75-
beforeEach(() => {
76+
beforeEach(async () => {
77+
vi.resetModules();
78+
({ resolveAgentDeliveryPlan, resolveAgentOutboundTarget } = await import("./agent-delivery.js"));
7679
mocks.resolveOutboundTarget.mockClear();
7780
mocks.resolveSessionDeliveryTarget.mockClear();
7881
});

‎src/infra/outbound/deliver.lifecycle.test.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22
import type { OpenClawConfig } from "../../config/config.js";
33
import { setActivePluginRegistry } from "../../plugins/runtime.js";
44
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
@@ -77,11 +77,9 @@ function expectSuccessfulWhatsAppInternalHookPayload(
7777
}
7878

7979
describe("deliverOutboundPayloads lifecycle", () => {
80-
beforeAll(async () => {
80+
beforeEach(async () => {
81+
vi.resetModules();
8182
({ deliverOutboundPayloads } = await import("./deliver.js"));
82-
});
83-
84-
beforeEach(() => {
8583
resetDeliverTestState();
8684
resetDeliverTestMocks({ includeSessionMocks: true });
8785
});

‎src/infra/outbound/deliver.test.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import path from "node:path";
2-
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import { markdownToSignalTextChunks } from "../../../extensions/signal/src/format.js";
44
import {
55
signalOutbound,
66
telegramOutbound,
77
whatsappOutbound,
88
} from "../../../test/channel-outbounds.js";
99
import type { OpenClawConfig } from "../../config/config.js";
10-
import { STATE_DIR } from "../../config/paths.js";
1110
import { setActivePluginRegistry } from "../../plugins/runtime.js";
1211
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
1312
import { withEnvAsync } from "../../test-utils/env.js";
@@ -201,11 +200,9 @@ function expectSuccessfulWhatsAppInternalHookPayload(
201200
}
202201

203202
describe("deliverOutboundPayloads", () => {
204-
beforeAll(async () => {
203+
beforeEach(async () => {
204+
vi.resetModules();
205205
({ deliverOutboundPayloads, normalizeOutboundPayloads } = await import("./deliver.js"));
206-
});
207-
208-
beforeEach(() => {
209206
setActivePluginRegistry(defaultRegistry);
210207
mocks.appendAssistantMessageToSessionTranscript.mockClear();
211208
hookMocks.runner.hasHooks.mockClear();
@@ -454,14 +451,19 @@ describe("deliverOutboundPayloads", () => {
454451
payload: { text: "hi", mediaUrl: "file:///tmp/f.png" },
455452
});
456453

454+
const sendOpts = sendTelegram.mock.calls[0]?.[2] as { mediaLocalRoots?: string[] } | undefined;
457455
expect(sendTelegram).toHaveBeenCalledWith(
458456
"123",
459457
"hi",
460458
expect.objectContaining({
461459
mediaUrl: "file:///tmp/f.png",
462-
mediaLocalRoots: expect.arrayContaining([path.join(STATE_DIR, "workspace-work")]),
463460
}),
464461
);
462+
expect(
463+
sendOpts?.mediaLocalRoots?.some((root) =>
464+
root.endsWith(path.join(".openclaw", "workspace-work")),
465+
),
466+
).toBe(true);
465467
});
466468

467469
it("includes OpenClaw tmp root in telegram mediaLocalRoots", async () => {

0 commit comments

Comments
 (0)