From b253ca70ef676b6bf33376263d1545e24b0b71f8 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Sat, 28 Mar 2026 01:52:37 -0400 Subject: [PATCH] Tests: stabilize Matrix-related shared suites --- .../commands-subagents/action-agents.test.ts | 10 ++++- src/auto-reply/skill-commands.test.ts | 6 --- src/commands/agents.bind.commands.test.ts | 39 +++++++++++++++---- .../agents.bind.matrix.integration.test.ts | 7 +++- .../shared/allowlist-policy-repair.test.ts | 7 +++- 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/auto-reply/reply/commands-subagents/action-agents.test.ts b/src/auto-reply/reply/commands-subagents/action-agents.test.ts index fc50940c0df..0b7a076a5f4 100644 --- a/src/auto-reply/reply/commands-subagents/action-agents.test.ts +++ b/src/auto-reply/reply/commands-subagents/action-agents.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, vi } from "vitest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; const { listBySessionMock } = vi.hoisted(() => ({ listBySessionMock: vi.fn(), @@ -10,9 +10,15 @@ vi.mock("../../../infra/outbound/session-binding-service.js", () => ({ }), })); -import { handleSubagentsAgentsAction } from "./action-agents.js"; +let handleSubagentsAgentsAction: typeof import("./action-agents.js").handleSubagentsAgentsAction; describe("handleSubagentsAgentsAction", () => { + beforeEach(async () => { + vi.resetModules(); + ({ handleSubagentsAgentsAction } = await import("./action-agents.js")); + listBySessionMock.mockReset(); + }); + it("dedupes stale bound rows for the same child session", () => { const childSessionKey = "agent:main:subagent:worker"; listBySessionMock.mockImplementation((sessionKey: string) => diff --git a/src/auto-reply/skill-commands.test.ts b/src/auto-reply/skill-commands.test.ts index b8a84bfe718..0c31f2a59d6 100644 --- a/src/auto-reply/skill-commands.test.ts +++ b/src/auto-reply/skill-commands.test.ts @@ -75,16 +75,10 @@ function installSkillCommandTestMocks(registerMock: SkillCommandMockRegistrar) { })); } -const registerSkillCommandMock: SkillCommandMockRegistrar = (modulePath, factory) => { - vi.mock(modulePath, factory as Parameters[1]); -}; - const registerDynamicSkillCommandMock: SkillCommandMockRegistrar = (modulePath, factory) => { vi.doMock(modulePath, factory as Parameters[1]); }; -installSkillCommandTestMocks(registerSkillCommandMock); - async function loadFreshSkillCommandsModuleForTest() { vi.resetModules(); installSkillCommandTestMocks(registerDynamicSkillCommandMock); diff --git a/src/commands/agents.bind.commands.test.ts b/src/commands/agents.bind.commands.test.ts index 0b55adb2cdd..b87246c7026 100644 --- a/src/commands/agents.bind.commands.test.ts +++ b/src/commands/agents.bind.commands.test.ts @@ -12,34 +12,59 @@ vi.mock("../config/config.js", async (importOriginal) => ({ vi.mock("../channels/plugins/index.js", async (importOriginal) => { const actual = await importOriginal(); + const knownChannels = new Set(["discord", "matrix", "telegram"]); + const createPlugin = (id: "discord" | "matrix" | "telegram") => ({ + id, + meta: { + id, + label: id, + selectionLabel: id, + docsPath: `/channels/${id}`, + blurb: `${id} test plugin`, + }, + capabilities: {}, + config: { + listAccountIds: () => [], + }, + }); return { ...actual, getChannelPlugin: (channel: string) => { - if (channel === "matrix") { + const normalized = channel.trim().toLowerCase(); + if (!knownChannels.has(normalized)) { + return actual.getChannelPlugin(channel); + } + if (normalized === "matrix") { return { - id: "matrix", + ...createPlugin("matrix"), setup: { resolveBindingAccountId: ({ agentId }: { agentId: string }) => agentId.toLowerCase(), }, }; } - return actual.getChannelPlugin(channel); + return createPlugin(normalized as "discord" | "telegram"); }, normalizeChannelId: (channel: string) => { - if (channel.trim().toLowerCase() === "matrix") { - return "matrix"; + const normalized = channel.trim().toLowerCase(); + if (knownChannels.has(normalized)) { + return normalized; } return actual.normalizeChannelId(channel); }, }; }); -import { agentsBindCommand, agentsBindingsCommand, agentsUnbindCommand } from "./agents.js"; +let agentsBindCommand: typeof import("./agents.js").agentsBindCommand; +let agentsBindingsCommand: typeof import("./agents.js").agentsBindingsCommand; +let agentsUnbindCommand: typeof import("./agents.js").agentsUnbindCommand; const runtime = createTestRuntime(); describe("agents bind/unbind commands", () => { - beforeEach(() => { + beforeEach(async () => { + vi.resetModules(); + ({ agentsBindCommand, agentsBindingsCommand, agentsUnbindCommand } = + await import("./agents.js")); readConfigFileSnapshotMock.mockClear(); writeConfigFileMock.mockClear(); runtime.log.mockClear(); diff --git a/src/commands/agents.bind.matrix.integration.test.ts b/src/commands/agents.bind.matrix.integration.test.ts index e9f82a8dc69..f4041980a31 100644 --- a/src/commands/agents.bind.matrix.integration.test.ts +++ b/src/commands/agents.bind.matrix.integration.test.ts @@ -1,7 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { setActivePluginRegistry } from "../plugins/runtime.js"; import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js"; -import { agentsBindCommand } from "./agents.js"; import { setDefaultChannelPluginRegistryForTests } from "./channel-test-helpers.js"; import { baseConfigSnapshot, createTestRuntime } from "./test-runtime-config-helpers.js"; @@ -28,10 +27,14 @@ vi.mock("../config/config.js", async (importOriginal) => ({ writeConfigFile: writeConfigFileMock, })); +let agentsBindCommand: typeof import("./agents.js").agentsBindCommand; + describe("agents bind matrix integration", () => { const runtime = createTestRuntime(); - beforeEach(() => { + beforeEach(async () => { + vi.resetModules(); + ({ agentsBindCommand } = await import("./agents.js")); readConfigFileSnapshotMock.mockClear(); writeConfigFileMock.mockClear(); runtime.log.mockClear(); diff --git a/src/commands/doctor/shared/allowlist-policy-repair.test.ts b/src/commands/doctor/shared/allowlist-policy-repair.test.ts index aac58e9914d..2d60982b7ea 100644 --- a/src/commands/doctor/shared/allowlist-policy-repair.test.ts +++ b/src/commands/doctor/shared/allowlist-policy-repair.test.ts @@ -1,5 +1,4 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; -import { maybeRepairAllowlistPolicyAllowFrom } from "./allowlist-policy-repair.js"; const { readChannelAllowFromStoreMock } = vi.hoisted(() => ({ readChannelAllowFromStoreMock: vi.fn(), @@ -9,8 +8,12 @@ vi.mock("../../../pairing/pairing-store.js", () => ({ readChannelAllowFromStore: readChannelAllowFromStoreMock, })); +let maybeRepairAllowlistPolicyAllowFrom: typeof import("./allowlist-policy-repair.js").maybeRepairAllowlistPolicyAllowFrom; + describe("doctor allowlist-policy repair", () => { - beforeEach(() => { + beforeEach(async () => { + vi.resetModules(); + ({ maybeRepairAllowlistPolicyAllowFrom } = await import("./allowlist-policy-repair.js")); readChannelAllowFromStoreMock.mockReset(); });