diff --git a/extensions/zalouser/src/accounts.test.ts b/extensions/zalouser/src/accounts.test.ts new file mode 100644 index 00000000000..f1ce6509358 --- /dev/null +++ b/extensions/zalouser/src/accounts.test.ts @@ -0,0 +1,214 @@ +import type { OpenClawConfig } from "openclaw/plugin-sdk"; +import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/account-id"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { + getZcaUserInfo, + listEnabledZalouserAccounts, + listZalouserAccountIds, + resolveDefaultZalouserAccountId, + resolveZalouserAccount, + resolveZalouserAccountSync, +} from "./accounts.js"; +import { checkZaloAuthenticated, getZaloUserInfo } from "./zalo-js.js"; + +vi.mock("./zalo-js.js", () => ({ + checkZaloAuthenticated: vi.fn(), + getZaloUserInfo: vi.fn(), +})); + +const mockCheckAuthenticated = vi.mocked(checkZaloAuthenticated); +const mockGetUserInfo = vi.mocked(getZaloUserInfo); + +function asConfig(value: unknown): OpenClawConfig { + return value as OpenClawConfig; +} + +describe("zalouser account resolution", () => { + beforeEach(() => { + mockCheckAuthenticated.mockReset(); + mockGetUserInfo.mockReset(); + delete process.env.ZALOUSER_PROFILE; + delete process.env.ZCA_PROFILE; + }); + + it("returns default account id when no accounts are configured", () => { + expect(listZalouserAccountIds(asConfig({}))).toEqual([DEFAULT_ACCOUNT_ID]); + }); + + it("returns sorted configured account ids", () => { + const cfg = asConfig({ + channels: { + zalouser: { + accounts: { + work: {}, + personal: {}, + default: {}, + }, + }, + }, + }); + + expect(listZalouserAccountIds(cfg)).toEqual(["default", "personal", "work"]); + }); + + it("uses configured defaultAccount when present", () => { + const cfg = asConfig({ + channels: { + zalouser: { + defaultAccount: "work", + accounts: { + default: {}, + work: {}, + }, + }, + }, + }); + + expect(resolveDefaultZalouserAccountId(cfg)).toBe("work"); + }); + + it("falls back to default account when configured defaultAccount is missing", () => { + const cfg = asConfig({ + channels: { + zalouser: { + defaultAccount: "missing", + accounts: { + default: {}, + work: {}, + }, + }, + }, + }); + + expect(resolveDefaultZalouserAccountId(cfg)).toBe("default"); + }); + + it("falls back to first sorted configured account when default is absent", () => { + const cfg = asConfig({ + channels: { + zalouser: { + accounts: { + zzz: {}, + aaa: {}, + }, + }, + }, + }); + + expect(resolveDefaultZalouserAccountId(cfg)).toBe("aaa"); + }); + + it("resolves sync account by merging base + account config", () => { + const cfg = asConfig({ + channels: { + zalouser: { + enabled: true, + dmPolicy: "pairing", + accounts: { + work: { + enabled: false, + name: "Work", + dmPolicy: "allowlist", + allowFrom: ["123"], + }, + }, + }, + }, + }); + + const resolved = resolveZalouserAccountSync({ cfg, accountId: "work" }); + expect(resolved.accountId).toBe("work"); + expect(resolved.enabled).toBe(false); + expect(resolved.name).toBe("Work"); + expect(resolved.config.dmPolicy).toBe("allowlist"); + expect(resolved.config.allowFrom).toEqual(["123"]); + }); + + it("resolves profile precedence correctly", () => { + const cfg = asConfig({ + channels: { + zalouser: { + accounts: { + work: {}, + }, + }, + }, + }); + + process.env.ZALOUSER_PROFILE = "zalo-env"; + expect(resolveZalouserAccountSync({ cfg, accountId: "work" }).profile).toBe("zalo-env"); + + delete process.env.ZALOUSER_PROFILE; + process.env.ZCA_PROFILE = "zca-env"; + expect(resolveZalouserAccountSync({ cfg, accountId: "work" }).profile).toBe("zca-env"); + + delete process.env.ZCA_PROFILE; + expect(resolveZalouserAccountSync({ cfg, accountId: "work" }).profile).toBe("work"); + }); + + it("uses explicit profile from config over env fallback", () => { + process.env.ZALOUSER_PROFILE = "env-profile"; + const cfg = asConfig({ + channels: { + zalouser: { + accounts: { + work: { + profile: "explicit-profile", + }, + }, + }, + }, + }); + + expect(resolveZalouserAccountSync({ cfg, accountId: "work" }).profile).toBe("explicit-profile"); + }); + + it("checks authentication during async account resolution", async () => { + mockCheckAuthenticated.mockResolvedValueOnce(true); + const cfg = asConfig({ + channels: { + zalouser: { + accounts: { + default: {}, + }, + }, + }, + }); + + const resolved = await resolveZalouserAccount({ cfg, accountId: "default" }); + expect(mockCheckAuthenticated).toHaveBeenCalledWith("default"); + expect(resolved.authenticated).toBe(true); + }); + + it("filters disabled accounts when listing enabled accounts", async () => { + mockCheckAuthenticated.mockResolvedValue(true); + const cfg = asConfig({ + channels: { + zalouser: { + accounts: { + default: { enabled: true }, + work: { enabled: false }, + }, + }, + }, + }); + + const accounts = await listEnabledZalouserAccounts(cfg); + expect(accounts.map((account) => account.accountId)).toEqual(["default"]); + }); + + it("maps account info helper from zalo-js", async () => { + mockGetUserInfo.mockResolvedValueOnce({ + userId: "123", + displayName: "Alice", + avatar: "https://example.com/avatar.png", + }); + expect(await getZcaUserInfo("default")).toEqual({ + userId: "123", + displayName: "Alice", + }); + + mockGetUserInfo.mockResolvedValueOnce(null); + expect(await getZcaUserInfo("default")).toBeNull(); + }); +}); diff --git a/extensions/zalouser/src/channel.test.ts b/extensions/zalouser/src/channel.test.ts index 65b759b226e..726577dda29 100644 --- a/extensions/zalouser/src/channel.test.ts +++ b/extensions/zalouser/src/channel.test.ts @@ -16,3 +16,51 @@ describe("zalouser outbound chunker", () => { expect(chunks.every((c) => c.length <= limit)).toBe(true); }); }); + +describe("zalouser channel policies", () => { + it("resolves group tool policy by explicit group id", () => { + const resolveToolPolicy = zalouserPlugin.groups?.resolveToolPolicy; + expect(resolveToolPolicy).toBeTypeOf("function"); + if (!resolveToolPolicy) { + return; + } + const policy = resolveToolPolicy({ + cfg: { + channels: { + zalouser: { + groups: { + "123": { tools: { allow: ["search"] } }, + }, + }, + }, + }, + accountId: "default", + groupId: "123", + groupChannel: "123", + }); + expect(policy).toEqual({ allow: ["search"] }); + }); + + it("falls back to wildcard group policy", () => { + const resolveToolPolicy = zalouserPlugin.groups?.resolveToolPolicy; + expect(resolveToolPolicy).toBeTypeOf("function"); + if (!resolveToolPolicy) { + return; + } + const policy = resolveToolPolicy({ + cfg: { + channels: { + zalouser: { + groups: { + "*": { tools: { deny: ["system.run"] } }, + }, + }, + }, + }, + accountId: "default", + groupId: "missing", + groupChannel: "missing", + }); + expect(policy).toEqual({ deny: ["system.run"] }); + }); +}); diff --git a/extensions/zalouser/src/probe.test.ts b/extensions/zalouser/src/probe.test.ts new file mode 100644 index 00000000000..64217a39264 --- /dev/null +++ b/extensions/zalouser/src/probe.test.ts @@ -0,0 +1,60 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { probeZalouser } from "./probe.js"; +import { getZaloUserInfo } from "./zalo-js.js"; + +vi.mock("./zalo-js.js", () => ({ + getZaloUserInfo: vi.fn(), +})); + +const mockGetUserInfo = vi.mocked(getZaloUserInfo); + +describe("probeZalouser", () => { + beforeEach(() => { + mockGetUserInfo.mockReset(); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it("returns ok=true with user when authenticated", async () => { + mockGetUserInfo.mockResolvedValueOnce({ + userId: "123", + displayName: "Alice", + }); + + await expect(probeZalouser("default")).resolves.toEqual({ + ok: true, + user: { userId: "123", displayName: "Alice" }, + }); + }); + + it("returns not authenticated when no user info is returned", async () => { + mockGetUserInfo.mockResolvedValueOnce(null); + await expect(probeZalouser("default")).resolves.toEqual({ + ok: false, + error: "Not authenticated", + }); + }); + + it("returns error when user lookup throws", async () => { + mockGetUserInfo.mockRejectedValueOnce(new Error("network down")); + await expect(probeZalouser("default")).resolves.toEqual({ + ok: false, + error: "network down", + }); + }); + + it("times out when lookup takes too long", async () => { + vi.useFakeTimers(); + mockGetUserInfo.mockReturnValueOnce(new Promise(() => undefined)); + + const pending = probeZalouser("default", 10); + await vi.advanceTimersByTimeAsync(1000); + + await expect(pending).resolves.toEqual({ + ok: false, + error: "Not authenticated", + }); + }); +}); diff --git a/extensions/zalouser/src/send.test.ts b/extensions/zalouser/src/send.test.ts index abca9fd50ed..4a379365559 100644 --- a/extensions/zalouser/src/send.test.ts +++ b/extensions/zalouser/src/send.test.ts @@ -1,156 +1,65 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { - sendImageZalouser, - sendLinkZalouser, - sendMessageZalouser, - type ZalouserSendResult, -} from "./send.js"; -import { runZca } from "./zca.js"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { sendImageZalouser, sendLinkZalouser, sendMessageZalouser } from "./send.js"; +import { sendZaloLink, sendZaloTextMessage } from "./zalo-js.js"; -vi.mock("./zca.js", () => ({ - runZca: vi.fn(), +vi.mock("./zalo-js.js", () => ({ + sendZaloTextMessage: vi.fn(), + sendZaloLink: vi.fn(), })); -const mockRunZca = vi.mocked(runZca); -const originalZcaProfile = process.env.ZCA_PROFILE; - -function okResult(stdout = "message_id: msg-1") { - return { - ok: true, - stdout, - stderr: "", - exitCode: 0, - }; -} - -function failResult(stderr = "") { - return { - ok: false, - stdout: "", - stderr, - exitCode: 1, - }; -} +const mockSendText = vi.mocked(sendZaloTextMessage); +const mockSendLink = vi.mocked(sendZaloLink); describe("zalouser send helpers", () => { beforeEach(() => { - mockRunZca.mockReset(); - delete process.env.ZCA_PROFILE; + mockSendText.mockReset(); + mockSendLink.mockReset(); }); - afterEach(() => { - if (originalZcaProfile) { - process.env.ZCA_PROFILE = originalZcaProfile; - return; - } - delete process.env.ZCA_PROFILE; - }); + it("delegates text send to JS transport", async () => { + mockSendText.mockResolvedValueOnce({ ok: true, messageId: "mid-1" }); - it("returns validation error when thread id is missing", async () => { - const result = await sendMessageZalouser("", "hello"); - expect(result).toEqual({ - ok: false, - error: "No threadId provided", - } satisfies ZalouserSendResult); - expect(mockRunZca).not.toHaveBeenCalled(); - }); - - it("builds text send command with truncation and group flag", async () => { - mockRunZca.mockResolvedValueOnce(okResult("message id: mid-123")); - - const result = await sendMessageZalouser(" thread-1 ", "x".repeat(2200), { - profile: "profile-a", + const result = await sendMessageZalouser("thread-1", "hello", { + profile: "default", isGroup: true, }); - expect(mockRunZca).toHaveBeenCalledWith(["msg", "send", "thread-1", "x".repeat(2000), "-g"], { - profile: "profile-a", + expect(mockSendText).toHaveBeenCalledWith("thread-1", "hello", { + profile: "default", + isGroup: true, }); - expect(result).toEqual({ ok: true, messageId: "mid-123" }); + expect(result).toEqual({ ok: true, messageId: "mid-1" }); }); - it("routes media sends from sendMessage and keeps text as caption", async () => { - mockRunZca.mockResolvedValueOnce(okResult()); + it("maps image helper to media send", async () => { + mockSendText.mockResolvedValueOnce({ ok: true, messageId: "mid-2" }); - await sendMessageZalouser("thread-2", "media caption", { - profile: "profile-b", - mediaUrl: "https://cdn.example.com/video.mp4", + await sendImageZalouser("thread-2", "https://example.com/a.png", { + profile: "p2", + caption: "cap", + isGroup: false, + }); + + expect(mockSendText).toHaveBeenCalledWith("thread-2", "cap", { + profile: "p2", + caption: "cap", + isGroup: false, + mediaUrl: "https://example.com/a.png", + }); + }); + + it("delegates link helper to JS transport", async () => { + mockSendLink.mockResolvedValueOnce({ ok: false, error: "boom" }); + + const result = await sendLinkZalouser("thread-3", "https://openclaw.ai", { + profile: "p3", isGroup: true, }); - expect(mockRunZca).toHaveBeenCalledWith( - [ - "msg", - "video", - "thread-2", - "-u", - "https://cdn.example.com/video.mp4", - "-m", - "media caption", - "-g", - ], - { profile: "profile-b" }, - ); - }); - - it("maps audio media to voice command", async () => { - mockRunZca.mockResolvedValueOnce(okResult()); - - await sendMessageZalouser("thread-3", "", { - profile: "profile-c", - mediaUrl: "https://cdn.example.com/clip.mp3", - }); - - expect(mockRunZca).toHaveBeenCalledWith( - ["msg", "voice", "thread-3", "-u", "https://cdn.example.com/clip.mp3"], - { profile: "profile-c" }, - ); - }); - - it("builds image command with caption and returns fallback error", async () => { - mockRunZca.mockResolvedValueOnce(failResult("")); - - const result = await sendImageZalouser("thread-4", " https://cdn.example.com/img.png ", { - profile: "profile-d", - caption: "caption text", + expect(mockSendLink).toHaveBeenCalledWith("thread-3", "https://openclaw.ai", { + profile: "p3", isGroup: true, }); - - expect(mockRunZca).toHaveBeenCalledWith( - [ - "msg", - "image", - "thread-4", - "-u", - "https://cdn.example.com/img.png", - "-m", - "caption text", - "-g", - ], - { profile: "profile-d" }, - ); - expect(result).toEqual({ ok: false, error: "Failed to send image" }); - }); - - it("uses env profile fallback and builds link command", async () => { - process.env.ZCA_PROFILE = "env-profile"; - mockRunZca.mockResolvedValueOnce(okResult("abc123")); - - const result = await sendLinkZalouser("thread-5", " https://openclaw.ai ", { isGroup: true }); - - expect(mockRunZca).toHaveBeenCalledWith( - ["msg", "link", "thread-5", "https://openclaw.ai", "-g"], - { profile: "env-profile" }, - ); - expect(result).toEqual({ ok: true, messageId: "abc123" }); - }); - - it("returns caught command errors", async () => { - mockRunZca.mockRejectedValueOnce(new Error("zca unavailable")); - - await expect(sendLinkZalouser("thread-6", "https://openclaw.ai")).resolves.toEqual({ - ok: false, - error: "zca unavailable", - }); + expect(result).toEqual({ ok: false, error: "boom" }); }); }); diff --git a/extensions/zalouser/src/status-issues.test.ts b/extensions/zalouser/src/status-issues.test.ts index b84d15d6f25..73f7277b2b9 100644 --- a/extensions/zalouser/src/status-issues.test.ts +++ b/extensions/zalouser/src/status-issues.test.ts @@ -2,20 +2,6 @@ import { describe, expect, it } from "vitest"; import { collectZalouserStatusIssues } from "./status-issues.js"; describe("collectZalouserStatusIssues", () => { - it("flags missing zca when configured is false", () => { - const issues = collectZalouserStatusIssues([ - { - accountId: "default", - enabled: true, - configured: false, - lastError: "zca CLI not found in PATH", - }, - ]); - expect(issues).toHaveLength(1); - expect(issues[0]?.kind).toBe("runtime"); - expect(issues[0]?.message).toMatch(/zca CLI not found/i); - }); - it("flags missing auth when configured is false", () => { const issues = collectZalouserStatusIssues([ { @@ -49,7 +35,7 @@ describe("collectZalouserStatusIssues", () => { accountId: "default", enabled: false, configured: false, - lastError: "zca CLI not found in PATH", + lastError: "not authenticated", }, ]); expect(issues).toHaveLength(0); diff --git a/extensions/zalouser/src/tool.test.ts b/extensions/zalouser/src/tool.test.ts new file mode 100644 index 00000000000..77e27a6280d --- /dev/null +++ b/extensions/zalouser/src/tool.test.ts @@ -0,0 +1,148 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { sendImageZalouser, sendLinkZalouser, sendMessageZalouser } from "./send.js"; +import { executeZalouserTool } from "./tool.js"; +import { + checkZaloAuthenticated, + getZaloUserInfo, + listZaloFriendsMatching, + listZaloGroupsMatching, +} from "./zalo-js.js"; + +vi.mock("./send.js", () => ({ + sendMessageZalouser: vi.fn(), + sendImageZalouser: vi.fn(), + sendLinkZalouser: vi.fn(), +})); + +vi.mock("./zalo-js.js", () => ({ + checkZaloAuthenticated: vi.fn(), + getZaloUserInfo: vi.fn(), + listZaloFriendsMatching: vi.fn(), + listZaloGroupsMatching: vi.fn(), +})); + +const mockSendMessage = vi.mocked(sendMessageZalouser); +const mockSendImage = vi.mocked(sendImageZalouser); +const mockSendLink = vi.mocked(sendLinkZalouser); +const mockCheckAuth = vi.mocked(checkZaloAuthenticated); +const mockGetUserInfo = vi.mocked(getZaloUserInfo); +const mockListFriends = vi.mocked(listZaloFriendsMatching); +const mockListGroups = vi.mocked(listZaloGroupsMatching); + +function extractDetails(result: Awaited>): unknown { + const text = result.content[0]?.text ?? "{}"; + return JSON.parse(text) as unknown; +} + +describe("executeZalouserTool", () => { + beforeEach(() => { + mockSendMessage.mockReset(); + mockSendImage.mockReset(); + mockSendLink.mockReset(); + mockCheckAuth.mockReset(); + mockGetUserInfo.mockReset(); + mockListFriends.mockReset(); + mockListGroups.mockReset(); + }); + + it("returns error when send action is missing required fields", async () => { + const result = await executeZalouserTool("tool-1", { action: "send" }); + expect(extractDetails(result)).toEqual({ + error: "threadId and message required for send action", + }); + }); + + it("sends text message for send action", async () => { + mockSendMessage.mockResolvedValueOnce({ ok: true, messageId: "m-1" }); + const result = await executeZalouserTool("tool-1", { + action: "send", + threadId: "t-1", + message: "hello", + profile: "work", + isGroup: true, + }); + expect(mockSendMessage).toHaveBeenCalledWith("t-1", "hello", { + profile: "work", + isGroup: true, + }); + expect(extractDetails(result)).toEqual({ success: true, messageId: "m-1" }); + }); + + it("returns tool error when send action fails", async () => { + mockSendMessage.mockResolvedValueOnce({ ok: false, error: "blocked" }); + const result = await executeZalouserTool("tool-1", { + action: "send", + threadId: "t-1", + message: "hello", + }); + expect(extractDetails(result)).toEqual({ error: "blocked" }); + }); + + it("routes image and link actions to correct helpers", async () => { + mockSendImage.mockResolvedValueOnce({ ok: true, messageId: "img-1" }); + const imageResult = await executeZalouserTool("tool-1", { + action: "image", + threadId: "g-1", + url: "https://example.com/image.jpg", + message: "caption", + isGroup: true, + }); + expect(mockSendImage).toHaveBeenCalledWith("g-1", "https://example.com/image.jpg", { + profile: undefined, + caption: "caption", + isGroup: true, + }); + expect(extractDetails(imageResult)).toEqual({ success: true, messageId: "img-1" }); + + mockSendLink.mockResolvedValueOnce({ ok: true, messageId: "lnk-1" }); + const linkResult = await executeZalouserTool("tool-1", { + action: "link", + threadId: "t-2", + url: "https://openclaw.ai", + message: "read this", + }); + expect(mockSendLink).toHaveBeenCalledWith("t-2", "https://openclaw.ai", { + profile: undefined, + caption: "read this", + isGroup: undefined, + }); + expect(extractDetails(linkResult)).toEqual({ success: true, messageId: "lnk-1" }); + }); + + it("returns friends/groups lists", async () => { + mockListFriends.mockResolvedValueOnce([{ userId: "1", displayName: "Alice" }]); + mockListGroups.mockResolvedValueOnce([{ groupId: "2", name: "Work" }]); + + const friends = await executeZalouserTool("tool-1", { + action: "friends", + profile: "work", + query: "ali", + }); + expect(mockListFriends).toHaveBeenCalledWith("work", "ali"); + expect(extractDetails(friends)).toEqual([{ userId: "1", displayName: "Alice" }]); + + const groups = await executeZalouserTool("tool-1", { + action: "groups", + profile: "work", + query: "wrk", + }); + expect(mockListGroups).toHaveBeenCalledWith("work", "wrk"); + expect(extractDetails(groups)).toEqual([{ groupId: "2", name: "Work" }]); + }); + + it("reports me + status actions", async () => { + mockGetUserInfo.mockResolvedValueOnce({ userId: "7", displayName: "Me" }); + mockCheckAuth.mockResolvedValueOnce(true); + + const me = await executeZalouserTool("tool-1", { action: "me", profile: "work" }); + expect(mockGetUserInfo).toHaveBeenCalledWith("work"); + expect(extractDetails(me)).toEqual({ userId: "7", displayName: "Me" }); + + const status = await executeZalouserTool("tool-1", { action: "status", profile: "work" }); + expect(mockCheckAuth).toHaveBeenCalledWith("work"); + expect(extractDetails(status)).toEqual({ + authenticated: true, + output: "authenticated", + }); + }); +});