mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-14 02:31:24 +00:00
test: speed up zalouser directory tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import "./accounts.test-mocks.js";
|
||||
import { zalouserPlugin } from "./channel.js";
|
||||
import { listZalouserDirectoryGroupMembers } from "./directory.js";
|
||||
import { createZalouserRuntimeEnv } from "./test-helpers.js";
|
||||
import "./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();
|
||||
|
||||
describe("zalouser directory group members", () => {
|
||||
it("accepts prefixed group ids from directory groups list output", async () => {
|
||||
await zalouserPlugin.directory!.listGroupMembers!({
|
||||
cfg: {},
|
||||
accountId: "default",
|
||||
groupId: "group:1471383327500481391",
|
||||
runtime: runtimeStub,
|
||||
});
|
||||
beforeEach(() => {
|
||||
listZaloGroupMembersMock.mockClear();
|
||||
});
|
||||
|
||||
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 () => {
|
||||
await zalouserPlugin.directory!.listGroupMembers!({
|
||||
cfg: {},
|
||||
accountId: "default",
|
||||
groupId: "1471383327500481391",
|
||||
runtime: runtimeStub,
|
||||
});
|
||||
await listZalouserDirectoryGroupMembers(
|
||||
{
|
||||
cfg: {},
|
||||
accountId: "default",
|
||||
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 () => {
|
||||
await zalouserPlugin.directory!.listGroupMembers!({
|
||||
cfg: {},
|
||||
accountId: "default",
|
||||
groupId: "g-1471383327500481391",
|
||||
runtime: runtimeStub,
|
||||
});
|
||||
await listZalouserDirectoryGroupMembers(
|
||||
{
|
||||
cfg: {},
|
||||
accountId: "default",
|
||||
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,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -37,6 +37,7 @@ import {
|
||||
normalizeAccountId,
|
||||
sendPayloadWithChunkedTextAndMedia,
|
||||
} from "./channel-api.js";
|
||||
import { listZalouserDirectoryGroupMembers } from "./directory.js";
|
||||
import { buildZalouserGroupCandidates, findZalouserGroupEntry } from "./group-policy.js";
|
||||
import { resolveZalouserReactionMessageIds } from "./message-sid.js";
|
||||
import type { ZalouserProbeResult } from "./probe.js";
|
||||
@@ -44,7 +45,6 @@ import { writeQrDataUrlToTempFile } from "./qr-temp-file.js";
|
||||
import { getZalouserRuntime } from "./runtime.js";
|
||||
import {
|
||||
normalizeZalouserTarget,
|
||||
parseZalouserDirectoryGroupId,
|
||||
parseZalouserOutboundTarget,
|
||||
resolveZalouserOutboundSessionRoute,
|
||||
} from "./session-route.js";
|
||||
@@ -319,18 +319,10 @@ export const zalouserPlugin: ChannelPlugin<ResolvedZalouserAccount, ZalouserProb
|
||||
},
|
||||
listGroupMembers: async ({ cfg, accountId, groupId, limit }) => {
|
||||
const { listZaloGroupMembers } = await loadZalouserChannelRuntime();
|
||||
const account = resolveZalouserAccountSync({ cfg: cfg, accountId });
|
||||
const normalizedGroupId = parseZalouserDirectoryGroupId(groupId);
|
||||
const members = await listZaloGroupMembers(account.profile, normalizedGroupId);
|
||||
const rows = members.map((member) =>
|
||||
mapUser({
|
||||
id: member.userId,
|
||||
name: member.displayName,
|
||||
avatarUrl: member.avatar ?? null,
|
||||
raw: member,
|
||||
}),
|
||||
return await listZalouserDirectoryGroupMembers(
|
||||
{ cfg, accountId, groupId, limit },
|
||||
{ listZaloGroupMembers },
|
||||
);
|
||||
return typeof limit === "number" && limit > 0 ? rows.slice(0, limit) : rows;
|
||||
},
|
||||
},
|
||||
resolver: {
|
||||
|
||||
54
extensions/zalouser/src/directory.ts
Normal file
54
extensions/zalouser/src/directory.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user