mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-16 04:20:44 +00:00
Matrix: dedupe resolver test harness
This commit is contained in:
@@ -1,19 +1,22 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { MatrixClient } from "../sdk.js";
|
||||
import {
|
||||
createMockMatrixClient,
|
||||
matrixClientResolverMocks,
|
||||
primeMatrixClientResolverMocks,
|
||||
} from "../client-resolver.test-helpers.js";
|
||||
|
||||
const loadConfigMock = vi.fn(() => ({}));
|
||||
const getActiveMatrixClientMock = vi.fn();
|
||||
const createMatrixClientMock = vi.fn();
|
||||
const isBunRuntimeMock = vi.fn(() => false);
|
||||
const resolveMatrixAuthMock = vi.fn();
|
||||
const resolveMatrixAuthContextMock = vi.fn();
|
||||
const {
|
||||
loadConfigMock,
|
||||
getMatrixRuntimeMock,
|
||||
getActiveMatrixClientMock,
|
||||
createMatrixClientMock,
|
||||
isBunRuntimeMock,
|
||||
resolveMatrixAuthMock,
|
||||
resolveMatrixAuthContextMock,
|
||||
} = matrixClientResolverMocks;
|
||||
|
||||
vi.mock("../../runtime.js", () => ({
|
||||
getMatrixRuntime: () => ({
|
||||
config: {
|
||||
loadConfig: loadConfigMock,
|
||||
},
|
||||
}),
|
||||
getMatrixRuntime: () => getMatrixRuntimeMock(),
|
||||
}));
|
||||
|
||||
vi.mock("../active-client.js", () => ({
|
||||
@@ -29,44 +32,10 @@ vi.mock("../client.js", () => ({
|
||||
|
||||
let resolveActionClient: typeof import("./client.js").resolveActionClient;
|
||||
|
||||
function createMockMatrixClient(): MatrixClient {
|
||||
return {
|
||||
prepareForOneOff: vi.fn(async () => undefined),
|
||||
start: vi.fn(async () => undefined),
|
||||
} as unknown as MatrixClient;
|
||||
}
|
||||
|
||||
describe("resolveActionClient", () => {
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
vi.clearAllMocks();
|
||||
getActiveMatrixClientMock.mockReturnValue(null);
|
||||
isBunRuntimeMock.mockReturnValue(false);
|
||||
resolveMatrixAuthMock.mockResolvedValue({
|
||||
accountId: "default",
|
||||
homeserver: "https://matrix.example.org",
|
||||
userId: "@bot:example.org",
|
||||
accessToken: "token",
|
||||
password: undefined,
|
||||
deviceId: "DEVICE123",
|
||||
encryption: false,
|
||||
});
|
||||
resolveMatrixAuthContextMock.mockImplementation(
|
||||
({ cfg, accountId }: { cfg: unknown; accountId?: string | null }) => ({
|
||||
cfg,
|
||||
env: process.env,
|
||||
accountId: accountId ?? "default",
|
||||
resolved: {
|
||||
homeserver: "https://matrix.example.org",
|
||||
userId: "@bot:example.org",
|
||||
accessToken: "token",
|
||||
password: undefined,
|
||||
deviceId: "DEVICE123",
|
||||
encryption: false,
|
||||
},
|
||||
}),
|
||||
);
|
||||
createMatrixClientMock.mockResolvedValue(createMockMatrixClient());
|
||||
primeMatrixClientResolverMocks();
|
||||
|
||||
({ resolveActionClient } = await import("./client.js"));
|
||||
});
|
||||
|
||||
92
extensions/matrix/src/matrix/client-resolver.test-helpers.ts
Normal file
92
extensions/matrix/src/matrix/client-resolver.test-helpers.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { vi } from "vitest";
|
||||
import type { MatrixClient } from "./sdk.js";
|
||||
|
||||
export const matrixClientResolverMocks = {
|
||||
loadConfigMock: vi.fn(() => ({})),
|
||||
getMatrixRuntimeMock: vi.fn(),
|
||||
getActiveMatrixClientMock: vi.fn(),
|
||||
createMatrixClientMock: vi.fn(),
|
||||
isBunRuntimeMock: vi.fn(() => false),
|
||||
resolveMatrixAuthMock: vi.fn(),
|
||||
resolveMatrixAuthContextMock: vi.fn(),
|
||||
};
|
||||
|
||||
export function createMockMatrixClient(): MatrixClient {
|
||||
return {
|
||||
prepareForOneOff: vi.fn(async () => undefined),
|
||||
start: vi.fn(async () => undefined),
|
||||
} as unknown as MatrixClient;
|
||||
}
|
||||
|
||||
export function primeMatrixClientResolverMocks(params?: {
|
||||
cfg?: unknown;
|
||||
accountId?: string;
|
||||
resolved?: Record<string, unknown>;
|
||||
auth?: Record<string, unknown>;
|
||||
client?: MatrixClient;
|
||||
}): MatrixClient {
|
||||
const {
|
||||
loadConfigMock,
|
||||
getMatrixRuntimeMock,
|
||||
getActiveMatrixClientMock,
|
||||
createMatrixClientMock,
|
||||
isBunRuntimeMock,
|
||||
resolveMatrixAuthMock,
|
||||
resolveMatrixAuthContextMock,
|
||||
} = matrixClientResolverMocks;
|
||||
|
||||
const cfg = params?.cfg ?? {};
|
||||
const accountId = params?.accountId ?? "default";
|
||||
const defaultResolved = {
|
||||
homeserver: "https://matrix.example.org",
|
||||
userId: "@bot:example.org",
|
||||
accessToken: "token",
|
||||
password: undefined,
|
||||
deviceId: "DEVICE123",
|
||||
encryption: false,
|
||||
};
|
||||
const defaultAuth = {
|
||||
accountId,
|
||||
homeserver: "https://matrix.example.org",
|
||||
userId: "@bot:example.org",
|
||||
accessToken: "token",
|
||||
password: undefined,
|
||||
deviceId: "DEVICE123",
|
||||
encryption: false,
|
||||
};
|
||||
const client = params?.client ?? createMockMatrixClient();
|
||||
|
||||
vi.clearAllMocks();
|
||||
loadConfigMock.mockReturnValue(cfg);
|
||||
getMatrixRuntimeMock.mockReturnValue({
|
||||
config: {
|
||||
loadConfig: loadConfigMock,
|
||||
},
|
||||
});
|
||||
getActiveMatrixClientMock.mockReturnValue(null);
|
||||
isBunRuntimeMock.mockReturnValue(false);
|
||||
resolveMatrixAuthContextMock.mockImplementation(
|
||||
({
|
||||
cfg: explicitCfg,
|
||||
accountId: explicitAccountId,
|
||||
}: {
|
||||
cfg: unknown;
|
||||
accountId?: string | null;
|
||||
}) => ({
|
||||
cfg: explicitCfg,
|
||||
env: process.env,
|
||||
accountId: explicitAccountId ?? accountId,
|
||||
resolved: {
|
||||
...defaultResolved,
|
||||
...params?.resolved,
|
||||
},
|
||||
}),
|
||||
);
|
||||
resolveMatrixAuthMock.mockResolvedValue({
|
||||
...defaultAuth,
|
||||
...params?.auth,
|
||||
});
|
||||
createMatrixClientMock.mockResolvedValue(client);
|
||||
|
||||
return client;
|
||||
}
|
||||
@@ -1,12 +1,18 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { MatrixClient } from "../sdk.js";
|
||||
import {
|
||||
createMockMatrixClient,
|
||||
matrixClientResolverMocks,
|
||||
primeMatrixClientResolverMocks,
|
||||
} from "../client-resolver.test-helpers.js";
|
||||
|
||||
const getActiveMatrixClientMock = vi.fn();
|
||||
const createMatrixClientMock = vi.fn();
|
||||
const isBunRuntimeMock = vi.fn(() => false);
|
||||
const resolveMatrixAuthMock = vi.fn();
|
||||
const resolveMatrixAuthContextMock = vi.fn();
|
||||
const getMatrixRuntimeMock = vi.fn();
|
||||
const {
|
||||
getMatrixRuntimeMock,
|
||||
getActiveMatrixClientMock,
|
||||
createMatrixClientMock,
|
||||
isBunRuntimeMock,
|
||||
resolveMatrixAuthMock,
|
||||
resolveMatrixAuthContextMock,
|
||||
} = matrixClientResolverMocks;
|
||||
|
||||
vi.mock("../active-client.js", () => ({
|
||||
getActiveMatrixClient: (...args: unknown[]) => getActiveMatrixClientMock(...args),
|
||||
@@ -25,39 +31,12 @@ vi.mock("../../runtime.js", () => ({
|
||||
|
||||
let resolveMatrixClient: typeof import("./client.js").resolveMatrixClient;
|
||||
|
||||
function createMockMatrixClient(): MatrixClient {
|
||||
return {
|
||||
prepareForOneOff: vi.fn(async () => undefined),
|
||||
} as unknown as MatrixClient;
|
||||
}
|
||||
|
||||
describe("resolveMatrixClient", () => {
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
vi.clearAllMocks();
|
||||
getActiveMatrixClientMock.mockReturnValue(null);
|
||||
isBunRuntimeMock.mockReturnValue(false);
|
||||
getMatrixRuntimeMock.mockReturnValue({
|
||||
config: {
|
||||
loadConfig: () => ({}),
|
||||
},
|
||||
});
|
||||
resolveMatrixAuthContextMock.mockReturnValue({
|
||||
cfg: {},
|
||||
env: process.env,
|
||||
accountId: "default",
|
||||
primeMatrixClientResolverMocks({
|
||||
resolved: {},
|
||||
});
|
||||
resolveMatrixAuthMock.mockResolvedValue({
|
||||
accountId: "default",
|
||||
homeserver: "https://matrix.example.org",
|
||||
userId: "@bot:example.org",
|
||||
accessToken: "token",
|
||||
password: undefined,
|
||||
deviceId: "DEVICE123",
|
||||
encryption: false,
|
||||
});
|
||||
createMatrixClientMock.mockResolvedValue(createMockMatrixClient());
|
||||
|
||||
({ resolveMatrixClient } = await import("./client.js"));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user