diff --git a/extensions/discord/src/directory-contract.test.ts b/extensions/discord/src/directory-contract.test.ts index c8e61dccfbc..f56a1b4e3fd 100644 --- a/extensions/discord/src/directory-contract.test.ts +++ b/extensions/discord/src/directory-contract.test.ts @@ -1,10 +1,7 @@ -import type { - BaseProbeResult, - BaseTokenResolution, - ChannelDirectoryEntry, -} from "openclaw/plugin-sdk/channel-contract"; +import type { BaseProbeResult, BaseTokenResolution } from "openclaw/plugin-sdk/channel-contract"; import type { OpenClawConfig } from "openclaw/plugin-sdk/testing"; import { describe, expect, expectTypeOf, it } from "vitest"; +import { expectDirectoryIds } from "../../../test/helpers/channels/directory-ids.js"; import { listDiscordDirectoryGroupsFromConfig, listDiscordDirectoryPeersFromConfig, @@ -12,43 +9,6 @@ import { import type { DiscordProbe } from "./probe.js"; import type { DiscordTokenResolution } from "./token.js"; -type DirectoryListFn = (params: { - cfg: OpenClawConfig; - accountId?: string; - query?: string | null; - limit?: number | null; -}) => Promise; - -async function listDirectoryEntriesWithDefaults(listFn: DirectoryListFn, cfg: OpenClawConfig) { - return await listFn({ - cfg, - accountId: "default", - query: null, - limit: null, - }); -} - -async function expectDirectoryIds( - listFn: DirectoryListFn, - cfg: OpenClawConfig, - expected: string[], - options?: { sorted?: boolean }, -) { - const entries = await listDirectoryEntriesWithDefaults(listFn, cfg); - const ids = entries.map((entry) => entry.id); - expect(options?.sorted ? sortDirectoryIds(ids) : ids).toEqual( - options?.sorted ? sortDirectoryIds(expected) : expected, - ); -} - -function compareDirectoryIds(left: string, right: string) { - return left < right ? -1 : left > right ? 1 : 0; -} - -function sortDirectoryIds(values: string[]) { - return values.toSorted(compareDirectoryIds); -} - describe("Discord directory contract", () => { it("keeps public probe and token resolution aligned with base contracts", () => { expectTypeOf().toMatchTypeOf(); diff --git a/extensions/slack/src/directory-contract.test.ts b/extensions/slack/src/directory-contract.test.ts index 47a08752b1f..8311eae93ff 100644 --- a/extensions/slack/src/directory-contract.test.ts +++ b/extensions/slack/src/directory-contract.test.ts @@ -1,49 +1,13 @@ -import type { BaseProbeResult, ChannelDirectoryEntry } from "openclaw/plugin-sdk/channel-contract"; +import type { BaseProbeResult } from "openclaw/plugin-sdk/channel-contract"; import type { OpenClawConfig } from "openclaw/plugin-sdk/testing"; import { describe, expect, expectTypeOf, it } from "vitest"; +import { expectDirectoryIds } from "../../../test/helpers/channels/directory-ids.js"; import { listSlackDirectoryGroupsFromConfig, listSlackDirectoryPeersFromConfig, } from "../directory-contract-api.js"; import type { SlackProbe } from "./probe.js"; -type DirectoryListFn = (params: { - cfg: OpenClawConfig; - accountId?: string; - query?: string | null; - limit?: number | null; -}) => Promise; - -async function listDirectoryEntriesWithDefaults(listFn: DirectoryListFn, cfg: OpenClawConfig) { - return await listFn({ - cfg, - accountId: "default", - query: null, - limit: null, - }); -} - -async function expectDirectoryIds( - listFn: DirectoryListFn, - cfg: OpenClawConfig, - expected: string[], - options?: { sorted?: boolean }, -) { - const entries = await listDirectoryEntriesWithDefaults(listFn, cfg); - const ids = entries.map((entry) => entry.id); - expect(options?.sorted ? sortDirectoryIds(ids) : ids).toEqual( - options?.sorted ? sortDirectoryIds(expected) : expected, - ); -} - -function compareDirectoryIds(left: string, right: string) { - return left < right ? -1 : left > right ? 1 : 0; -} - -function sortDirectoryIds(values: string[]) { - return values.toSorted(compareDirectoryIds); -} - describe("Slack directory contract", () => { it("keeps public probe aligned with base contract", () => { expectTypeOf().toMatchTypeOf(); diff --git a/extensions/telegram/src/directory-contract.test.ts b/extensions/telegram/src/directory-contract.test.ts index 5cc78ecb1ff..e06d9456236 100644 --- a/extensions/telegram/src/directory-contract.test.ts +++ b/extensions/telegram/src/directory-contract.test.ts @@ -1,10 +1,7 @@ -import type { - BaseProbeResult, - BaseTokenResolution, - ChannelDirectoryEntry, -} from "openclaw/plugin-sdk/channel-contract"; +import type { BaseProbeResult, BaseTokenResolution } from "openclaw/plugin-sdk/channel-contract"; import { type OpenClawConfig, withEnvAsync } from "openclaw/plugin-sdk/testing"; import { describe, expect, expectTypeOf, it } from "vitest"; +import { expectDirectoryIds } from "../../../test/helpers/channels/directory-ids.js"; import { listTelegramDirectoryGroupsFromConfig, listTelegramDirectoryPeersFromConfig, @@ -12,43 +9,6 @@ import { import type { TelegramProbe } from "./probe.js"; import type { TelegramTokenResolution } from "./token.js"; -type DirectoryListFn = (params: { - cfg: OpenClawConfig; - accountId?: string; - query?: string | null; - limit?: number | null; -}) => Promise; - -async function listDirectoryEntriesWithDefaults(listFn: DirectoryListFn, cfg: OpenClawConfig) { - return await listFn({ - cfg, - accountId: "default", - query: null, - limit: null, - }); -} - -async function expectDirectoryIds( - listFn: DirectoryListFn, - cfg: OpenClawConfig, - expected: string[], - options?: { sorted?: boolean }, -) { - const entries = await listDirectoryEntriesWithDefaults(listFn, cfg); - const ids = entries.map((entry) => entry.id); - expect(options?.sorted ? sortDirectoryIds(ids) : ids).toEqual( - options?.sorted ? sortDirectoryIds(expected) : expected, - ); -} - -function compareDirectoryIds(left: string, right: string) { - return left < right ? -1 : left > right ? 1 : 0; -} - -function sortDirectoryIds(values: string[]) { - return values.toSorted(compareDirectoryIds); -} - describe("Telegram directory contract", () => { it("keeps public probe and token resolution aligned with base contracts", () => { expectTypeOf().toMatchTypeOf(); diff --git a/test/helpers/channels/directory-ids.ts b/test/helpers/channels/directory-ids.ts new file mode 100644 index 00000000000..19227fb4a97 --- /dev/null +++ b/test/helpers/channels/directory-ids.ts @@ -0,0 +1,36 @@ +import type { ChannelDirectoryEntry } from "openclaw/plugin-sdk/channel-contract"; +import type { OpenClawConfig } from "openclaw/plugin-sdk/testing"; +import { expect } from "vitest"; + +export type DirectoryListFn = (params: { + cfg: OpenClawConfig; + accountId?: string; + query?: string | null; + limit?: number | null; +}) => Promise; + +export async function expectDirectoryIds( + listFn: DirectoryListFn, + cfg: OpenClawConfig, + expected: string[], + options?: { sorted?: boolean }, +) { + const entries = await listFn({ + cfg, + accountId: "default", + query: null, + limit: null, + }); + const ids = entries.map((entry) => entry.id); + expect(options?.sorted ? sortDirectoryIds(ids) : ids).toEqual( + options?.sorted ? sortDirectoryIds(expected) : expected, + ); +} + +function compareDirectoryIds(left: string, right: string) { + return left < right ? -1 : left > right ? 1 : 0; +} + +function sortDirectoryIds(values: string[]) { + return values.toSorted(compareDirectoryIds); +}