diff --git a/extensions/zalouser/src/accounts.test-mocks.ts b/extensions/zalouser/src/accounts.test-mocks.ts index 0206095d0fc..9e8e1f14de3 100644 --- a/extensions/zalouser/src/accounts.test-mocks.ts +++ b/extensions/zalouser/src/accounts.test-mocks.ts @@ -1,10 +1,14 @@ import { vi } from "vitest"; import { createDefaultResolvedZalouserAccount } from "./test-helpers.js"; -vi.mock("./accounts.js", async (importOriginal) => { - const actual = (await importOriginal()) as Record; +vi.mock("./accounts.js", () => { return { - ...actual, + listZalouserAccountIds: () => ["default"], + resolveDefaultZalouserAccountId: () => "default", resolveZalouserAccountSync: () => createDefaultResolvedZalouserAccount(), + resolveZalouserAccount: async () => createDefaultResolvedZalouserAccount(), + listEnabledZalouserAccounts: async () => [createDefaultResolvedZalouserAccount()], + getZcaUserInfo: async () => null, + checkZcaAuthenticated: async () => false, }; }); diff --git a/extensions/zalouser/src/channel.directory.test.ts b/extensions/zalouser/src/channel.directory.test.ts index 1736118bc0e..8beb8a8d623 100644 --- a/extensions/zalouser/src/channel.directory.test.ts +++ b/extensions/zalouser/src/channel.directory.test.ts @@ -1,18 +1,9 @@ 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 () => [])); - -vi.mock("./zalo-js.js", async (importOriginal) => { - const actual = (await importOriginal()) as Record; - return { - ...actual, - listZaloGroupMembers: listZaloGroupMembersMock, - }; -}); - +import "./zalo-js.test-mocks.js"; import { zalouserPlugin } from "./channel.js"; +import { createZalouserRuntimeEnv } from "./test-helpers.js"; +import { listZaloGroupMembersMock } from "./zalo-js.test-mocks.js"; const runtimeStub = createZalouserRuntimeEnv(); diff --git a/extensions/zalouser/src/channel.sendpayload.test.ts b/extensions/zalouser/src/channel.sendpayload.test.ts index 207707a5bd8..5054fd53941 100644 --- a/extensions/zalouser/src/channel.sendpayload.test.ts +++ b/extensions/zalouser/src/channel.sendpayload.test.ts @@ -1,6 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import { primeChannelOutboundSendMock } from "../../../src/channels/plugins/contracts/suites.js"; import "./accounts.test-mocks.js"; +import "./zalo-js.test-mocks.js"; import type { ReplyPayload } from "../runtime-api.js"; import { zalouserPlugin } from "./channel.js"; import { setZalouserRuntime } from "./runtime.js"; diff --git a/extensions/zalouser/src/channel.setup.test.ts b/extensions/zalouser/src/channel.setup.test.ts index 552a45c882e..75aebe5e6be 100644 --- a/extensions/zalouser/src/channel.setup.test.ts +++ b/extensions/zalouser/src/channel.setup.test.ts @@ -4,6 +4,7 @@ import path from "node:path"; import { describe, expect, it } from "vitest"; import { buildChannelSetupWizardAdapterFromSetupWizard } from "../../../src/channels/plugins/setup-wizard.js"; import { withEnvAsync } from "../../../test/helpers/extensions/env.js"; +import "./zalo-js.test-mocks.js"; import { zalouserSetupPlugin } from "./channel.setup.js"; const zalouserSetupAdapter = buildChannelSetupWizardAdapterFromSetupWizard({ diff --git a/extensions/zalouser/src/channel.test.ts b/extensions/zalouser/src/channel.test.ts index 23ef1809e25..012b970810a 100644 --- a/extensions/zalouser/src/channel.test.ts +++ b/extensions/zalouser/src/channel.test.ts @@ -1,4 +1,5 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; +import "./zalo-js.test-mocks.js"; import { zalouserPlugin } from "./channel.js"; import { setZalouserRuntime } from "./runtime.js"; import { sendMessageZalouser, sendReactionZalouser } from "./send.js"; diff --git a/extensions/zalouser/src/monitor.account-scope.test.ts b/extensions/zalouser/src/monitor.account-scope.test.ts index 5119d57f69b..69f77c4b2d5 100644 --- a/extensions/zalouser/src/monitor.account-scope.test.ts +++ b/extensions/zalouser/src/monitor.account-scope.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it, vi } from "vitest"; import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "../runtime-api.js"; import "./monitor.send-mocks.js"; +import "./zalo-js.test-mocks.js"; import { __testing } from "./monitor.js"; import { sendMessageZalouserMock } from "./monitor.send-mocks.js"; import { setZalouserRuntime } from "./runtime.js"; diff --git a/extensions/zalouser/src/monitor.group-gating.test.ts b/extensions/zalouser/src/monitor.group-gating.test.ts index bc21914417f..7f6eac47487 100644 --- a/extensions/zalouser/src/monitor.group-gating.test.ts +++ b/extensions/zalouser/src/monitor.group-gating.test.ts @@ -1,6 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "../runtime-api.js"; import "./monitor.send-mocks.js"; +import "./zalo-js.test-mocks.js"; import { resolveZalouserAccountSync } from "./accounts.js"; import { __testing } from "./monitor.js"; import { diff --git a/extensions/zalouser/src/reaction.ts b/extensions/zalouser/src/reaction.ts index 0579df86ce5..5739fe1cd50 100644 --- a/extensions/zalouser/src/reaction.ts +++ b/extensions/zalouser/src/reaction.ts @@ -1,4 +1,4 @@ -import { Reactions } from "./zca-client.js"; +import { Reactions } from "./zca-constants.js"; const REACTION_ALIAS_MAP = new Map([ ["like", Reactions.LIKE], diff --git a/extensions/zalouser/src/send.test.ts b/extensions/zalouser/src/send.test.ts index cc920e6be7e..ecbaff5982d 100644 --- a/extensions/zalouser/src/send.test.ts +++ b/extensions/zalouser/src/send.test.ts @@ -17,7 +17,7 @@ import { sendZaloTextMessage, sendZaloTypingEvent, } from "./zalo-js.js"; -import { TextStyle } from "./zca-client.js"; +import { TextStyle } from "./zca-constants.js"; vi.mock("./zalo-js.js", () => ({ sendZaloTextMessage: vi.fn(), diff --git a/extensions/zalouser/src/send.ts b/extensions/zalouser/src/send.ts index 55ff17df636..b730c1a1a96 100644 --- a/extensions/zalouser/src/send.ts +++ b/extensions/zalouser/src/send.ts @@ -8,7 +8,7 @@ import { sendZaloTextMessage, sendZaloTypingEvent, } from "./zalo-js.js"; -import { TextStyle } from "./zca-client.js"; +import { TextStyle } from "./zca-constants.js"; export type ZalouserSendOptions = ZaloSendOptions; export type ZalouserSendResult = ZaloSendResult; diff --git a/extensions/zalouser/src/setup-surface.test.ts b/extensions/zalouser/src/setup-surface.test.ts index e04590b9dba..14030a60936 100644 --- a/extensions/zalouser/src/setup-surface.test.ts +++ b/extensions/zalouser/src/setup-surface.test.ts @@ -3,30 +3,7 @@ import { buildChannelSetupWizardAdapterFromSetupWizard } from "../../../src/chan import { createRuntimeEnv } from "../../../test/helpers/extensions/runtime-env.js"; import { createTestWizardPrompter } from "../../../test/helpers/extensions/setup-wizard.js"; import type { OpenClawConfig } from "../runtime-api.js"; - -vi.mock("./zalo-js.js", async (importOriginal) => { - const actual = await importOriginal(); - return { - ...actual, - checkZaloAuthenticated: vi.fn(async () => false), - logoutZaloProfile: vi.fn(async () => {}), - startZaloQrLogin: vi.fn(async () => ({ - message: "qr pending", - qrDataUrl: undefined, - })), - waitForZaloQrLogin: vi.fn(async () => ({ - connected: false, - message: "login pending", - })), - resolveZaloAllowFromEntries: vi.fn(async ({ entries }: { entries: string[] }) => - entries.map((entry) => ({ input: entry, resolved: true, id: entry, note: undefined })), - ), - resolveZaloGroupsByEntries: vi.fn(async ({ entries }: { entries: string[] }) => - entries.map((entry) => ({ input: entry, resolved: true, id: entry, note: undefined })), - ), - }; -}); - +import "./zalo-js.test-mocks.js"; import { zalouserPlugin } from "./channel.js"; const zalouserConfigureAdapter = buildChannelSetupWizardAdapterFromSetupWizard({ diff --git a/extensions/zalouser/src/text-styles.test.ts b/extensions/zalouser/src/text-styles.test.ts index 01e6c2da86b..b2540f74bb6 100644 --- a/extensions/zalouser/src/text-styles.test.ts +++ b/extensions/zalouser/src/text-styles.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vitest"; import { parseZalouserTextStyles } from "./text-styles.js"; -import { TextStyle } from "./zca-client.js"; +import { TextStyle } from "./zca-constants.js"; describe("parseZalouserTextStyles", () => { it("renders inline markdown emphasis as Zalo style ranges", () => { diff --git a/extensions/zalouser/src/text-styles.ts b/extensions/zalouser/src/text-styles.ts index cdfe8b492b5..f352c5d239e 100644 --- a/extensions/zalouser/src/text-styles.ts +++ b/extensions/zalouser/src/text-styles.ts @@ -1,4 +1,4 @@ -import { TextStyle, type Style } from "./zca-client.js"; +import { TextStyle, type Style } from "./zca-constants.js"; type InlineStyle = (typeof TextStyle)[keyof typeof TextStyle]; diff --git a/extensions/zalouser/src/types.ts b/extensions/zalouser/src/types.ts index 08dc2fd8d12..aaf9b9b44b7 100644 --- a/extensions/zalouser/src/types.ts +++ b/extensions/zalouser/src/types.ts @@ -1,4 +1,4 @@ -import type { Style } from "./zca-client.js"; +import type { Style } from "./zca-constants.js"; export type ZcaFriend = { userId: string; diff --git a/extensions/zalouser/src/zalo-js.test-mocks.ts b/extensions/zalouser/src/zalo-js.test-mocks.ts new file mode 100644 index 00000000000..2b9853a26d7 --- /dev/null +++ b/extensions/zalouser/src/zalo-js.test-mocks.ts @@ -0,0 +1,60 @@ +import { vi } from "vitest"; + +const zaloJsMocks = vi.hoisted(() => ({ + checkZaloAuthenticatedMock: vi.fn(async () => false), + getZaloUserInfoMock: vi.fn(async () => null), + listZaloFriendsMock: vi.fn(async () => []), + listZaloFriendsMatchingMock: vi.fn(async () => []), + listZaloGroupMembersMock: vi.fn(async () => []), + listZaloGroupsMock: vi.fn(async () => []), + listZaloGroupsMatchingMock: vi.fn(async () => []), + logoutZaloProfileMock: vi.fn(async () => {}), + resolveZaloAllowFromEntriesMock: vi.fn(async ({ entries }: { entries: string[] }) => + entries.map((entry) => ({ input: entry, resolved: true, id: entry, note: undefined })), + ), + resolveZaloGroupContextMock: vi.fn(async () => null), + resolveZaloGroupsByEntriesMock: vi.fn(async ({ entries }: { entries: string[] }) => + entries.map((entry) => ({ input: entry, resolved: true, id: entry, note: undefined })), + ), + startZaloListenerMock: vi.fn(async () => ({ stop: vi.fn() })), + startZaloQrLoginMock: vi.fn(async () => ({ + message: "qr pending", + qrDataUrl: undefined, + })), + waitForZaloQrLoginMock: vi.fn(async () => ({ + connected: false, + message: "login pending", + })), +})); + +export const checkZaloAuthenticatedMock = zaloJsMocks.checkZaloAuthenticatedMock; +export const getZaloUserInfoMock = zaloJsMocks.getZaloUserInfoMock; +export const listZaloFriendsMock = zaloJsMocks.listZaloFriendsMock; +export const listZaloFriendsMatchingMock = zaloJsMocks.listZaloFriendsMatchingMock; +export const listZaloGroupMembersMock = zaloJsMocks.listZaloGroupMembersMock; +export const listZaloGroupsMock = zaloJsMocks.listZaloGroupsMock; +export const listZaloGroupsMatchingMock = zaloJsMocks.listZaloGroupsMatchingMock; +export const logoutZaloProfileMock = zaloJsMocks.logoutZaloProfileMock; +export const resolveZaloAllowFromEntriesMock = zaloJsMocks.resolveZaloAllowFromEntriesMock; +export const resolveZaloGroupContextMock = zaloJsMocks.resolveZaloGroupContextMock; +export const resolveZaloGroupsByEntriesMock = zaloJsMocks.resolveZaloGroupsByEntriesMock; +export const startZaloListenerMock = zaloJsMocks.startZaloListenerMock; +export const startZaloQrLoginMock = zaloJsMocks.startZaloQrLoginMock; +export const waitForZaloQrLoginMock = zaloJsMocks.waitForZaloQrLoginMock; + +vi.mock("./zalo-js.js", () => ({ + checkZaloAuthenticated: checkZaloAuthenticatedMock, + getZaloUserInfo: getZaloUserInfoMock, + listZaloFriends: listZaloFriendsMock, + listZaloFriendsMatching: listZaloFriendsMatchingMock, + listZaloGroupMembers: listZaloGroupMembersMock, + listZaloGroups: listZaloGroupsMock, + listZaloGroupsMatching: listZaloGroupsMatchingMock, + logoutZaloProfile: logoutZaloProfileMock, + resolveZaloAllowFromEntries: resolveZaloAllowFromEntriesMock, + resolveZaloGroupContext: resolveZaloGroupContextMock, + resolveZaloGroupsByEntries: resolveZaloGroupsByEntriesMock, + startZaloListener: startZaloListenerMock, + startZaloQrLogin: startZaloQrLoginMock, + waitForZaloQrLogin: waitForZaloQrLoginMock, +})); diff --git a/extensions/zalouser/src/zalo-js.ts b/extensions/zalouser/src/zalo-js.ts index 3d1a146ea9f..e8e6c3659f6 100644 --- a/extensions/zalouser/src/zalo-js.ts +++ b/extensions/zalouser/src/zalo-js.ts @@ -19,17 +19,16 @@ import type { ZcaUserInfo, } from "./types.js"; import { - LoginQRCallbackEventType, TextStyle, - ThreadType, - Zalo, type API, type Credentials, type GroupInfo, type LoginQRCallbackEvent, type Message, type User, + Zalo, } from "./zca-client.js"; +import { LoginQRCallbackEventType, ThreadType } from "./zca-constants.js"; const API_LOGIN_TIMEOUT_MS = 20_000; const QR_LOGIN_TTL_MS = 3 * 60_000; diff --git a/extensions/zalouser/src/zca-client.ts b/extensions/zalouser/src/zca-client.ts index f7bc1a358b3..bae0472fc09 100644 --- a/extensions/zalouser/src/zca-client.ts +++ b/extensions/zalouser/src/zca-client.ts @@ -1,67 +1,17 @@ import * as zcaJsRuntime from "zca-js"; +import { + LoginQRCallbackEventType, + Reactions, + TextStyle, + ThreadType, + type Style, +} from "./zca-constants.js"; const zcaJs = zcaJsRuntime as unknown as { - ThreadType: unknown; - LoginQRCallbackEventType: unknown; - Reactions: unknown; Zalo: unknown; }; - -export const ThreadType = zcaJs.ThreadType as { - User: 0; - Group: 1; -}; - -export const LoginQRCallbackEventType = zcaJs.LoginQRCallbackEventType as { - QRCodeGenerated: 0; - QRCodeExpired: 1; - QRCodeScanned: 2; - QRCodeDeclined: 3; - GotLoginInfo: 4; -}; - -export const Reactions = zcaJs.Reactions as Record & { - HEART: string; - LIKE: string; - HAHA: string; - WOW: string; - CRY: string; - ANGRY: string; - NONE: string; -}; - -// Mirror zca-js sendMessage style constants locally because the package root -// typing surface does not consistently expose TextStyle/Style to tsgo. -export const TextStyle = { - Bold: "b", - Italic: "i", - Underline: "u", - StrikeThrough: "s", - Red: "c_db342e", - Orange: "c_f27806", - Yellow: "c_f7b503", - Green: "c_15a85f", - Small: "f_13", - Big: "f_18", - UnorderedList: "lst_1", - OrderedList: "lst_2", - Indent: "ind_$", -} as const; - -type TextStyleValue = (typeof TextStyle)[keyof typeof TextStyle]; - -export type Style = - | { - start: number; - len: number; - st: Exclude; - } - | { - start: number; - len: number; - st: typeof TextStyle.Indent; - indentSize?: number; - }; +export { LoginQRCallbackEventType, Reactions, TextStyle, ThreadType }; +export type { Style }; export type Credentials = { imei: string; diff --git a/extensions/zalouser/src/zca-constants.ts b/extensions/zalouser/src/zca-constants.ts new file mode 100644 index 00000000000..ec906427e34 --- /dev/null +++ b/extensions/zalouser/src/zca-constants.ts @@ -0,0 +1,55 @@ +export const ThreadType = { + User: 0, + Group: 1, +} as const; + +export const LoginQRCallbackEventType = { + QRCodeGenerated: 0, + QRCodeExpired: 1, + QRCodeScanned: 2, + QRCodeDeclined: 3, + GotLoginInfo: 4, +} as const; + +export const Reactions = { + HEART: "/-heart", + LIKE: "/-strong", + HAHA: ":>", + WOW: ":o", + CRY: ":-((", + ANGRY: ":-h", + NONE: "", +} as const; + +// Mirror zca-js sendMessage style constants locally because the package root +// typing surface does not consistently expose TextStyle/Style to tsgo. +export const TextStyle = { + Bold: "b", + Italic: "i", + Underline: "u", + StrikeThrough: "s", + Red: "c_db342e", + Orange: "c_f27806", + Yellow: "c_f7b503", + Green: "c_15a85f", + Small: "f_13", + Big: "f_18", + UnorderedList: "lst_1", + OrderedList: "lst_2", + Indent: "ind_$", +} as const; + +type TextStyleValue = (typeof TextStyle)[keyof typeof TextStyle]; + +export type Style = + | { + start: number; + len: number; + st: Exclude; + } + | { + start: number; + len: number; + st: typeof TextStyle.Indent; + indentSize?: number; + };