test: speed up zalouser directory tests

This commit is contained in:
Peter Steinberger
2026-04-07 15:40:10 +01:00
parent 91b3446098
commit c2995bc470
3 changed files with 113 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest"; import { beforeEach, describe, expect, it } from "vitest";
import "./accounts.test-mocks.js"; import "./accounts.test-mocks.js";
import { zalouserPlugin } from "./channel.js"; import { listZalouserDirectoryGroupMembers } from "./directory.js";
import { createZalouserRuntimeEnv } from "./test-helpers.js"; import { createZalouserRuntimeEnv } from "./test-helpers.js";
import "./zalo-js.test-mocks.js"; import "./zalo-js.test-mocks.js";
import { listZaloGroupMembersMock } from "./zalo-js.test-mocks.js"; import { listZaloGroupMembersMock } from "./zalo-js.test-mocks.js";
@@ -8,36 +8,67 @@ import { listZaloGroupMembersMock } from "./zalo-js.test-mocks.js";
const runtimeStub = createZalouserRuntimeEnv(); const runtimeStub = createZalouserRuntimeEnv();
describe("zalouser directory group members", () => { describe("zalouser directory group members", () => {
it("accepts prefixed group ids from directory groups list output", async () => { beforeEach(() => {
await zalouserPlugin.directory!.listGroupMembers!({ listZaloGroupMembersMock.mockClear();
cfg: {}, });
accountId: "default",
groupId: "group:1471383327500481391",
runtime: runtimeStub,
});
expect(listZaloGroupMembersMock).toHaveBeenCalledWith("default", "1471383327500481391"); it("accepts prefixed group ids from directory groups list output", async () => {
await listZalouserDirectoryGroupMembers(
{
cfg: {},
accountId: "default",
groupId: "group:1471383327500481391",
},
{
listZaloGroupMembers: async (profile, groupId) =>
await listZaloGroupMembersMock(profile, groupId, runtimeStub),
},
);
expect(listZaloGroupMembersMock).toHaveBeenLastCalledWith(
"default",
"1471383327500481391",
runtimeStub,
);
}); });
it("keeps backward compatibility for raw group ids", async () => { it("keeps backward compatibility for raw group ids", async () => {
await zalouserPlugin.directory!.listGroupMembers!({ await listZalouserDirectoryGroupMembers(
cfg: {}, {
accountId: "default", cfg: {},
groupId: "1471383327500481391", accountId: "default",
runtime: runtimeStub, groupId: "1471383327500481391",
}); },
{
listZaloGroupMembers: async (profile, groupId) =>
await listZaloGroupMembersMock(profile, groupId, runtimeStub),
},
);
expect(listZaloGroupMembersMock).toHaveBeenCalledWith("default", "1471383327500481391"); expect(listZaloGroupMembersMock).toHaveBeenLastCalledWith(
"default",
"1471383327500481391",
runtimeStub,
);
}); });
it("accepts provider-native g- group ids without stripping the prefix", async () => { it("accepts provider-native g- group ids without stripping the prefix", async () => {
await zalouserPlugin.directory!.listGroupMembers!({ await listZalouserDirectoryGroupMembers(
cfg: {}, {
accountId: "default", cfg: {},
groupId: "g-1471383327500481391", accountId: "default",
runtime: runtimeStub, groupId: "g-1471383327500481391",
}); },
{
listZaloGroupMembers: async (profile, groupId) =>
await listZaloGroupMembersMock(profile, groupId, runtimeStub),
},
);
expect(listZaloGroupMembersMock).toHaveBeenCalledWith("default", "g-1471383327500481391"); expect(listZaloGroupMembersMock).toHaveBeenLastCalledWith(
"default",
"g-1471383327500481391",
runtimeStub,
);
}); });
}); });

View File

@@ -37,6 +37,7 @@ import {
normalizeAccountId, normalizeAccountId,
sendPayloadWithChunkedTextAndMedia, sendPayloadWithChunkedTextAndMedia,
} from "./channel-api.js"; } from "./channel-api.js";
import { listZalouserDirectoryGroupMembers } from "./directory.js";
import { buildZalouserGroupCandidates, findZalouserGroupEntry } from "./group-policy.js"; import { buildZalouserGroupCandidates, findZalouserGroupEntry } from "./group-policy.js";
import { resolveZalouserReactionMessageIds } from "./message-sid.js"; import { resolveZalouserReactionMessageIds } from "./message-sid.js";
import type { ZalouserProbeResult } from "./probe.js"; import type { ZalouserProbeResult } from "./probe.js";
@@ -44,7 +45,6 @@ import { writeQrDataUrlToTempFile } from "./qr-temp-file.js";
import { getZalouserRuntime } from "./runtime.js"; import { getZalouserRuntime } from "./runtime.js";
import { import {
normalizeZalouserTarget, normalizeZalouserTarget,
parseZalouserDirectoryGroupId,
parseZalouserOutboundTarget, parseZalouserOutboundTarget,
resolveZalouserOutboundSessionRoute, resolveZalouserOutboundSessionRoute,
} from "./session-route.js"; } from "./session-route.js";
@@ -319,18 +319,10 @@ export const zalouserPlugin: ChannelPlugin<ResolvedZalouserAccount, ZalouserProb
}, },
listGroupMembers: async ({ cfg, accountId, groupId, limit }) => { listGroupMembers: async ({ cfg, accountId, groupId, limit }) => {
const { listZaloGroupMembers } = await loadZalouserChannelRuntime(); const { listZaloGroupMembers } = await loadZalouserChannelRuntime();
const account = resolveZalouserAccountSync({ cfg: cfg, accountId }); return await listZalouserDirectoryGroupMembers(
const normalizedGroupId = parseZalouserDirectoryGroupId(groupId); { cfg, accountId, groupId, limit },
const members = await listZaloGroupMembers(account.profile, normalizedGroupId); { listZaloGroupMembers },
const rows = members.map((member) =>
mapUser({
id: member.userId,
name: member.displayName,
avatarUrl: member.avatar ?? null,
raw: member,
}),
); );
return typeof limit === "number" && limit > 0 ? rows.slice(0, limit) : rows;
}, },
}, },
resolver: { resolver: {

View File

@@ -0,0 +1,54 @@
import { resolveZalouserAccountSync } from "./accounts.js";
import type { ChannelDirectoryEntry, OpenClawConfig } from "./channel-api.js";
import { parseZalouserDirectoryGroupId } from "./session-route.js";
type ZalouserDirectoryDeps = {
listZaloGroupMembers: (
profile: string,
groupId: string,
) => Promise<
Array<{
userId: string;
displayName?: string | null;
avatar?: string | null;
}>
>;
};
function mapUser(params: {
id: string;
name?: string | null;
avatarUrl?: string | null;
raw?: unknown;
}): ChannelDirectoryEntry {
return {
kind: "user",
id: params.id,
name: params.name ?? undefined,
avatarUrl: params.avatarUrl ?? undefined,
raw: params.raw,
};
}
export async function listZalouserDirectoryGroupMembers(
params: {
cfg: OpenClawConfig;
accountId?: string;
groupId: string;
limit?: number;
},
deps: ZalouserDirectoryDeps,
) {
const account = resolveZalouserAccountSync({ cfg: params.cfg, accountId: params.accountId });
const normalizedGroupId = parseZalouserDirectoryGroupId(params.groupId);
const members = await deps.listZaloGroupMembers(account.profile, normalizedGroupId);
const rows = members.map((member) =>
mapUser({
id: member.userId,
name: member.displayName,
avatarUrl: member.avatar ?? null,
raw: member,
}),
);
return typeof params.limit === "number" && params.limit > 0 ? rows.slice(0, params.limit) : rows;
}