fix: honor zalouser default runtime account

This commit is contained in:
Tak Hoffman
2026-04-03 13:18:02 -05:00
parent 72b8025107
commit 045010a2a5
2 changed files with 110 additions and 6 deletions

View File

@@ -1,9 +1,18 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import "./zalo-js.test-mocks.js";
import {
listZaloFriendsMatchingMock,
startZaloQrLoginMock,
waitForZaloQrLoginMock,
} from "./zalo-js.test-mocks.js";
import { zalouserPlugin } from "./channel.js";
import { setZalouserRuntime } from "./runtime.js";
import { sendMessageZalouser, sendReactionZalouser } from "./send.js";
vi.mock("./qr-temp-file.js", () => ({
writeQrDataUrlToTempFile: vi.fn(async () => null),
}));
vi.mock("./send.js", async () => {
const actual = (await vi.importActual("./send.js")) as Record<string, unknown>;
return {
@@ -266,3 +275,96 @@ describe("zalouser channel policies", () => {
expect(actions?.describeMessageTool?.({ cfg, accountId: "work" })?.actions).toEqual(["react"]);
});
});
describe("zalouser account resolution", () => {
beforeEach(() => {
listZaloFriendsMatchingMock.mockReset();
startZaloQrLoginMock.mockReset();
waitForZaloQrLoginMock.mockReset();
});
it("uses the configured default account for omitted target lookup", async () => {
const resolveTargets = zalouserPlugin.resolver?.resolveTargets;
if (!resolveTargets) {
throw new Error("zalouser resolver.resolveTargets unavailable");
}
listZaloFriendsMatchingMock.mockResolvedValue([
{ userId: "42", displayName: "Work User" } as never,
]);
const result = await resolveTargets({
cfg: {
channels: {
zalouser: {
defaultAccount: "work",
accounts: {
work: {
profile: "work-profile",
},
},
},
},
} as never,
inputs: ["Work User"],
kind: "user",
runtime: {},
});
expect(listZaloFriendsMatchingMock).toHaveBeenCalledWith("work-profile", "Work User");
expect(result).toEqual([
expect.objectContaining({
input: "Work User",
resolved: true,
id: "42",
name: "Work User",
}),
]);
});
it("uses the configured default account for omitted qr login", async () => {
const login = zalouserPlugin.auth?.login;
if (!login) {
throw new Error("zalouser auth.login unavailable");
}
startZaloQrLoginMock.mockResolvedValue({
message: "qr ready",
qrDataUrl: "data:image/png;base64,abc",
} as never);
waitForZaloQrLoginMock.mockResolvedValue({
connected: true,
userId: "u-1",
displayName: "Work User",
} as never);
const runtime = {
log: vi.fn(),
};
await login({
cfg: {
channels: {
zalouser: {
defaultAccount: "work",
accounts: {
work: {
profile: "work-profile",
},
},
},
},
} as never,
runtime,
});
expect(startZaloQrLoginMock).toHaveBeenCalledWith({
profile: "work-profile",
timeoutMs: 35_000,
});
expect(waitForZaloQrLoginMock).toHaveBeenCalledWith({
profile: "work-profile",
timeoutMs: 180_000,
});
});
});

View File

@@ -180,10 +180,12 @@ const resolveZalouserDmPolicy = createScopedDmSecurityResolver<ResolvedZalouserA
});
const zalouserMessageActions: ChannelMessageActionAdapter = {
describeMessageTool: ({ cfg }) => {
const accounts = listZalouserAccountIds(cfg)
.map((accountId) => resolveZalouserAccountSync({ cfg, accountId }))
.filter((account) => account.enabled);
describeMessageTool: ({ cfg, accountId }) => {
const accounts = accountId
? [resolveZalouserAccountSync({ cfg, accountId })].filter((account) => account.enabled)
: listZalouserAccountIds(cfg)
.map((resolvedAccountId) => resolveZalouserAccountSync({ cfg, accountId: resolvedAccountId }))
.filter((account) => account.enabled);
if (accounts.length === 0) {
return null;
}
@@ -347,7 +349,7 @@ export const zalouserPlugin: ChannelPlugin<ResolvedZalouserAccount, ZalouserProb
try {
const account = resolveZalouserAccountSync({
cfg: cfg,
accountId: accountId ?? DEFAULT_ACCOUNT_ID,
accountId: accountId ?? resolveDefaultZalouserAccountId(cfg),
});
if (kind === "user") {
const friends = await listZaloFriendsMatching(account.profile, trimmed);
@@ -384,7 +386,7 @@ export const zalouserPlugin: ChannelPlugin<ResolvedZalouserAccount, ZalouserProb
login: async ({ cfg, accountId, runtime }) => {
const account = resolveZalouserAccountSync({
cfg: cfg,
accountId: accountId ?? DEFAULT_ACCOUNT_ID,
accountId: accountId ?? resolveDefaultZalouserAccountId(cfg),
});
runtime.log(