Tests: stabilize Matrix-related shared suites

This commit is contained in:
Gustavo Madeira Santana
2026-03-28 01:52:37 -04:00
parent 29b6e27c9e
commit b253ca70ef
5 changed files with 50 additions and 19 deletions

View File

@@ -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) =>

View File

@@ -75,16 +75,10 @@ function installSkillCommandTestMocks(registerMock: SkillCommandMockRegistrar) {
}));
}
const registerSkillCommandMock: SkillCommandMockRegistrar = (modulePath, factory) => {
vi.mock(modulePath, factory as Parameters<typeof vi.mock>[1]);
};
const registerDynamicSkillCommandMock: SkillCommandMockRegistrar = (modulePath, factory) => {
vi.doMock(modulePath, factory as Parameters<typeof vi.doMock>[1]);
};
installSkillCommandTestMocks(registerSkillCommandMock);
async function loadFreshSkillCommandsModuleForTest() {
vi.resetModules();
installSkillCommandTestMocks(registerDynamicSkillCommandMock);

View File

@@ -12,34 +12,59 @@ vi.mock("../config/config.js", async (importOriginal) => ({
vi.mock("../channels/plugins/index.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../channels/plugins/index.js")>();
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();

View File

@@ -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();

View File

@@ -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();
});