Files
nanobot/webui/src/tests/channel-locale-registry.test.ts
T
Carmelo Campos b7cb5da4b5 feat(channels): add WhatsApp Agent Platform channel
Adds `whatsapp_agent`, an additive channel that connects nanobot to the
official WhatsApp Agent Platform API (https://api.whatsapp.com/agent/v1).
It authenticates with a per-agent API token and long-polls `GET /updates`
instead of linking a device, so it is the officially supported interface
rather than the Neonize linked-device protocol used by the existing
`whatsapp` channel.

The existing `whatsapp` channel is unchanged; both can run at once under
different names.

Design notes:
- The poll cursor, dedup window, and creator identifier live in one
  atomically replaced state file. A message id is recorded before it is
  handled, so a crash re-reads the batch without answering twice, and the
  cursor is only accepted after the batch is processed.
- The first poll omits `offset`, which resolves the head and never replays
  up to 30 days of history into an auto-replying agent.
- A changed token resets the cursor, because `next_offset` is only valid
  for the agent that issued it.
- Each API method is paced below its own rolling 60-second limit, and
  progress/tool-hint messages default to off because they spend the shared
  12-per-minute send budget.
- Retries are limited to confirmed non-delivery (`429`, `503`/`131016`).
  A `500` or read timeout is ambiguous, so it is not retried unless
  `retryAmbiguousSends` is set; a duplicate is worse than a missed reply.
- Inbound media is downloaded through the shared SSRF guard, size-capped,
  and verified against the reported digest. Outbound media is validated
  before upload.
- Reactions are receive-only, matching the API. The token is never logged
  or persisted; state stores only a SHA-256 fingerprint.

Docs compare both WhatsApp channels, including the lack of end-to-end
encryption, country availability, and rate limits.
2026-10-18 14:49:49 +08:00

123 lines
4.1 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { channelFieldMessageKey } from "@/channel-plugins/i18n";
import { registeredChannelLocales } from "@/channel-plugins/locale-registry";
import { registeredChannelUiContributions } from "@/channel-plugins/registry";
import { supportedLocales } from "@/i18n/config";
const expectedChannels = [
"dingtalk",
"discord",
"email",
"feishu",
"linear",
"matrix",
"mattermost",
"mochat",
"msteams",
"napcat",
"qq",
"signal",
"slack",
"telegram",
"websocket",
"wecom",
"weixin",
"whatsapp",
"whatsapp_agent",
];
function flatten(value: unknown, prefix = ""): Map<string, string> {
const entries = new Map<string, string>();
if (typeof value === "string") {
entries.set(prefix, value);
return entries;
}
if (!value || typeof value !== "object") return entries;
for (const [key, child] of Object.entries(value)) {
if (!prefix && key === "displayName") continue;
const childPrefix = prefix ? `${prefix}.${key}` : key;
for (const [childKey, text] of flatten(child, childPrefix)) {
entries.set(childKey, text);
}
}
return entries;
}
function interpolationKeys(value: string): string[] {
return [...value.matchAll(/{{\s*([\w.-]+)\s*}}/g)]
.map((match) => match[1])
.sort();
}
describe("channel locale registry", () => {
it("loads every supported locale from every built-in channel package", () => {
const registrations = registeredChannelLocales();
expect([...registrations.keys()].sort()).toEqual(expectedChannels);
for (const [channel, locales] of registrations) {
expect([...locales.keys()].sort()).toEqual(
supportedLocales.map(({ code }) => code).sort(),
);
const english = flatten(locales.get("en"));
for (const [locale, messages] of locales) {
const translated = flatten(messages);
expect([...translated.keys()].sort(), `${channel}/${locale} message keys`).toEqual(
[...english.keys()].sort(),
);
for (const [key, source] of english) {
expect(
interpolationKeys(translated.get(key) ?? ""),
`${channel}/${locale}:${key} interpolation keys`,
).toEqual(interpolationKeys(source));
}
}
}
});
it("keeps structural UI definitions aligned with English locale keys", () => {
const locales = registeredChannelLocales();
for (const { channel, contribution } of registeredChannelUiContributions()) {
const messages = locales.get(channel)?.get("en");
expect(messages, `${channel} English messages`).toBeDefined();
const setup = contribution.presentation.setup;
for (const field of [...(setup?.fields ?? []), ...(setup?.manualFields ?? [])]) {
const messageKey = channelFieldMessageKey(channel, field.key);
expect(messages?.setup.fields?.[messageKey], `${channel} field ${messageKey}`).toBeDefined();
}
for (const action of setup?.actions ?? []) {
expect(messages?.setup.actions?.[action.id], `${channel} action ${action.id}`).toBeTypeOf("string");
}
for (const preset of setup?.presets ?? []) {
expect(messages?.setup.presets?.[preset.id], `${channel} preset ${preset.id}`).toBeTypeOf("string");
}
}
});
});
describe("channel locale registry concurrency", () => {
it("registers every locale loaded concurrently for the same channel", async () => {
// A fresh module instance starts with an empty registry, which is the state
// startup reaches when the stored locale differs from the fallback locale.
vi.resetModules();
const registry = await import("@/channel-plugins/locale-registry");
await Promise.all([
registry.channelLocaleResources("en"),
registry.channelLocaleResources("zh-CN"),
]);
const registrations = registry.registeredChannelLocales();
expect([...registrations.keys()].sort()).toEqual(expectedChannels);
for (const [channel, locales] of registrations) {
expect([...locales.keys()].sort(), `${channel} concurrent locales`).toEqual([
"en",
"zh-CN",
]);
}
});
});