mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 03:07:27 +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 { 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 {
|
||||||
const getActiveMatrixClientMock = vi.fn();
|
loadConfigMock,
|
||||||
const createMatrixClientMock = vi.fn();
|
getMatrixRuntimeMock,
|
||||||
const isBunRuntimeMock = vi.fn(() => false);
|
getActiveMatrixClientMock,
|
||||||
const resolveMatrixAuthMock = vi.fn();
|
createMatrixClientMock,
|
||||||
const resolveMatrixAuthContextMock = vi.fn();
|
isBunRuntimeMock,
|
||||||
|
resolveMatrixAuthMock,
|
||||||
|
resolveMatrixAuthContextMock,
|
||||||
|
} = matrixClientResolverMocks;
|
||||||
|
|
||||||
vi.mock("../../runtime.js", () => ({
|
vi.mock("../../runtime.js", () => ({
|
||||||
getMatrixRuntime: () => ({
|
getMatrixRuntime: () => getMatrixRuntimeMock(),
|
||||||
config: {
|
|
||||||
loadConfig: loadConfigMock,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("../active-client.js", () => ({
|
vi.mock("../active-client.js", () => ({
|
||||||
@@ -29,44 +32,10 @@ vi.mock("../client.js", () => ({
|
|||||||
|
|
||||||
let resolveActionClient: typeof import("./client.js").resolveActionClient;
|
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", () => {
|
describe("resolveActionClient", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
vi.clearAllMocks();
|
primeMatrixClientResolverMocks();
|
||||||
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());
|
|
||||||
|
|
||||||
({ resolveActionClient } = await import("./client.js"));
|
({ 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 { 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 {
|
||||||
const createMatrixClientMock = vi.fn();
|
getMatrixRuntimeMock,
|
||||||
const isBunRuntimeMock = vi.fn(() => false);
|
getActiveMatrixClientMock,
|
||||||
const resolveMatrixAuthMock = vi.fn();
|
createMatrixClientMock,
|
||||||
const resolveMatrixAuthContextMock = vi.fn();
|
isBunRuntimeMock,
|
||||||
const getMatrixRuntimeMock = vi.fn();
|
resolveMatrixAuthMock,
|
||||||
|
resolveMatrixAuthContextMock,
|
||||||
|
} = matrixClientResolverMocks;
|
||||||
|
|
||||||
vi.mock("../active-client.js", () => ({
|
vi.mock("../active-client.js", () => ({
|
||||||
getActiveMatrixClient: (...args: unknown[]) => getActiveMatrixClientMock(...args),
|
getActiveMatrixClient: (...args: unknown[]) => getActiveMatrixClientMock(...args),
|
||||||
@@ -25,39 +31,12 @@ vi.mock("../../runtime.js", () => ({
|
|||||||
|
|
||||||
let resolveMatrixClient: typeof import("./client.js").resolveMatrixClient;
|
let resolveMatrixClient: typeof import("./client.js").resolveMatrixClient;
|
||||||
|
|
||||||
function createMockMatrixClient(): MatrixClient {
|
|
||||||
return {
|
|
||||||
prepareForOneOff: vi.fn(async () => undefined),
|
|
||||||
} as unknown as MatrixClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("resolveMatrixClient", () => {
|
describe("resolveMatrixClient", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
vi.clearAllMocks();
|
primeMatrixClientResolverMocks({
|
||||||
getActiveMatrixClientMock.mockReturnValue(null);
|
|
||||||
isBunRuntimeMock.mockReturnValue(false);
|
|
||||||
getMatrixRuntimeMock.mockReturnValue({
|
|
||||||
config: {
|
|
||||||
loadConfig: () => ({}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
resolveMatrixAuthContextMock.mockReturnValue({
|
|
||||||
cfg: {},
|
|
||||||
env: process.env,
|
|
||||||
accountId: "default",
|
|
||||||
resolved: {},
|
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"));
|
({ resolveMatrixClient } = await import("./client.js"));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user