test: share zalouser test helpers

This commit is contained in:
Peter Steinberger 2026-03-13 22:51:26 +00:00
parent 66979bcc2f
commit d7aa3cc1c3
6 changed files with 45 additions and 56 deletions

View File

@ -0,0 +1,10 @@
import { vi } from "vitest";
import { createDefaultResolvedZalouserAccount } from "./test-helpers.js";
vi.mock("./accounts.js", async (importOriginal) => {
const actual = (await importOriginal()) as Record<string, unknown>;
return {
...actual,
resolveZalouserAccountSync: () => createDefaultResolvedZalouserAccount(),
};
});

View File

@ -1,5 +1,6 @@
import type { RuntimeEnv } from "openclaw/plugin-sdk/zalouser";
import { describe, expect, it, vi } from "vitest";
import "./accounts.test-mocks.js";
import { createZalouserRuntimeEnv } from "./test-helpers.js";
const listZaloGroupMembersMock = vi.hoisted(() => vi.fn(async () => []));
@ -11,30 +12,9 @@ vi.mock("./zalo-js.js", async (importOriginal) => {
};
});
vi.mock("./accounts.js", async (importOriginal) => {
const actual = (await importOriginal()) as Record<string, unknown>;
return {
...actual,
resolveZalouserAccountSync: () => ({
accountId: "default",
profile: "default",
name: "test",
enabled: true,
authenticated: true,
config: {},
}),
};
});
import { zalouserPlugin } from "./channel.js";
const runtimeStub: RuntimeEnv = {
log: vi.fn(),
error: vi.fn(),
exit: ((code: number): never => {
throw new Error(`exit ${code}`);
}) as RuntimeEnv["exit"],
};
const runtimeStub = createZalouserRuntimeEnv();
describe("zalouser directory group members", () => {
it("accepts prefixed group ids from directory groups list output", async () => {

View File

@ -1,5 +1,6 @@
import type { ReplyPayload } from "openclaw/plugin-sdk/zalouser";
import { beforeEach, describe, expect, it, vi } from "vitest";
import "./accounts.test-mocks.js";
import {
installSendPayloadContractSuite,
primeSendMock,
@ -12,20 +13,6 @@ vi.mock("./send.js", () => ({
sendReactionZalouser: vi.fn().mockResolvedValue({ ok: true }),
}));
vi.mock("./accounts.js", async (importOriginal) => {
const actual = (await importOriginal()) as Record<string, unknown>;
return {
...actual,
resolveZalouserAccountSync: () => ({
accountId: "default",
profile: "default",
name: "test",
enabled: true,
config: {},
}),
};
});
function baseCtx(payload: ReplyPayload) {
return {
cfg: {},

View File

@ -4,6 +4,7 @@ import "./monitor.send-mocks.js";
import { __testing } from "./monitor.js";
import { sendMessageZalouserMock } from "./monitor.send-mocks.js";
import { setZalouserRuntime } from "./runtime.js";
import { createZalouserRuntimeEnv } from "./test-helpers.js";
import type { ResolvedZalouserAccount, ZaloInboundMessage } from "./types.js";
describe("zalouser monitor pairing account scoping", () => {
@ -80,19 +81,11 @@ describe("zalouser monitor pairing account scoping", () => {
raw: { source: "test" },
};
const runtime: RuntimeEnv = {
log: vi.fn(),
error: vi.fn(),
exit: ((code: number): never => {
throw new Error(`exit ${code}`);
}) as RuntimeEnv["exit"],
};
await __testing.processMessage({
message,
account,
config,
runtime,
runtime: createZalouserRuntimeEnv(),
});
expect(readAllowFromStore).toHaveBeenCalledWith(

View File

@ -9,6 +9,7 @@ import {
sendTypingZalouserMock,
} from "./monitor.send-mocks.js";
import { setZalouserRuntime } from "./runtime.js";
import { createZalouserRuntimeEnv } from "./test-helpers.js";
import type { ResolvedZalouserAccount, ZaloInboundMessage } from "./types.js";
function createAccount(): ResolvedZalouserAccount {
@ -39,15 +40,7 @@ function createConfig(): OpenClawConfig {
};
}
function createRuntimeEnv(): RuntimeEnv {
return {
log: vi.fn(),
error: vi.fn(),
exit: ((code: number): never => {
throw new Error(`exit ${code}`);
}) as RuntimeEnv["exit"],
};
}
const createRuntimeEnv = () => createZalouserRuntimeEnv();
function installRuntime(params: {
commandAuthorized?: boolean;
@ -269,7 +262,7 @@ describe("zalouser monitor group mention gating", () => {
message: params.message,
account: params.account ?? createAccount(),
config: createConfig(),
runtime: createRuntimeEnv(),
runtime: createZalouserRuntimeEnv(),
historyState: params.historyState,
});
}

View File

@ -0,0 +1,26 @@
import type { RuntimeEnv } from "openclaw/plugin-sdk/zalouser";
import type { ResolvedZalouserAccount } from "./types.js";
export function createZalouserRuntimeEnv(): RuntimeEnv {
return {
log: () => {},
error: () => {},
exit: ((code: number): never => {
throw new Error(`exit ${code}`);
}) as RuntimeEnv["exit"],
};
}
export function createDefaultResolvedZalouserAccount(
overrides: Partial<ResolvedZalouserAccount> = {},
): ResolvedZalouserAccount {
return {
accountId: "default",
profile: "default",
name: "test",
enabled: true,
authenticated: true,
config: {},
...overrides,
};
}