Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
280 lines
9.0 KiB
TypeScript
280 lines
9.0 KiB
TypeScript
// Line tests cover auto reply delivery plugin behavior.
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import type { LineAutoReplyDeps } from "./auto-reply-delivery.js";
|
|
import { deliverLineAutoReply } from "./auto-reply-delivery.js";
|
|
import { sendLineReplyChunks } from "./reply-chunks.js";
|
|
import { createLineSendReceipt } from "./send-receipt.js";
|
|
|
|
const createFlexMessage = (altText: string, contents: unknown) => ({
|
|
type: "flex" as const,
|
|
altText,
|
|
contents,
|
|
});
|
|
|
|
const createImageMessage = (url: string) => ({
|
|
type: "image" as const,
|
|
originalContentUrl: url,
|
|
previewImageUrl: url,
|
|
});
|
|
|
|
const createLocationMessage = (location: {
|
|
title: string;
|
|
address: string;
|
|
latitude: number;
|
|
longitude: number;
|
|
}) => ({
|
|
type: "location" as const,
|
|
...location,
|
|
});
|
|
|
|
describe("deliverLineAutoReply", () => {
|
|
const LINE_TEST_CFG = { channels: { line: { accounts: { acc: {} } } } };
|
|
const baseDeliveryParams = {
|
|
cfg: LINE_TEST_CFG,
|
|
to: "line:user:1",
|
|
replyToken: "token",
|
|
replyTokenUsed: false,
|
|
accountId: "acc",
|
|
textLimit: 5000,
|
|
};
|
|
|
|
function createDeps(overrides?: Partial<LineAutoReplyDeps>) {
|
|
const replyMessageLine = vi.fn(async () => ({}));
|
|
const pushMessageLine = vi.fn(async () => ({}));
|
|
const pushTextMessageWithQuickReplies = vi.fn(async () => ({}));
|
|
const createTextMessageWithQuickReplies = vi.fn((text: string) => ({
|
|
type: "text" as const,
|
|
text,
|
|
}));
|
|
const createQuickReplyItems = vi.fn((labels: string[]) => ({ items: labels }));
|
|
const pushMessagesLine = vi.fn(async () => ({
|
|
messageId: "push",
|
|
chatId: "u1",
|
|
receipt: createLineSendReceipt({ messageId: "push", chatId: "u1", kind: "text" }),
|
|
}));
|
|
|
|
const deps: LineAutoReplyDeps = {
|
|
buildTemplateMessageFromPayload: () => null,
|
|
processLineMessage: (text) => ({ text, flexMessages: [] }),
|
|
chunkMarkdownText: (text) => [text],
|
|
sendLineReplyChunks,
|
|
replyMessageLine,
|
|
pushMessageLine,
|
|
pushTextMessageWithQuickReplies,
|
|
createTextMessageWithQuickReplies,
|
|
createQuickReplyItems: createQuickReplyItems as LineAutoReplyDeps["createQuickReplyItems"],
|
|
pushMessagesLine,
|
|
createFlexMessage: createFlexMessage as LineAutoReplyDeps["createFlexMessage"],
|
|
createImageMessage,
|
|
createLocationMessage,
|
|
...overrides,
|
|
};
|
|
|
|
return {
|
|
deps,
|
|
replyMessageLine,
|
|
pushMessageLine,
|
|
pushTextMessageWithQuickReplies,
|
|
createTextMessageWithQuickReplies,
|
|
createQuickReplyItems,
|
|
pushMessagesLine,
|
|
};
|
|
}
|
|
|
|
it("uses reply token for text before sending rich messages", async () => {
|
|
const lineData = {
|
|
flexMessage: { altText: "Card", contents: { type: "bubble" } },
|
|
};
|
|
const { deps, replyMessageLine, pushMessagesLine, createQuickReplyItems } = createDeps();
|
|
|
|
const result = await deliverLineAutoReply({
|
|
...baseDeliveryParams,
|
|
payload: { text: "hello", channelData: { line: lineData } },
|
|
lineData,
|
|
deps,
|
|
});
|
|
|
|
expect(result.replyTokenUsed).toBe(true);
|
|
expect(replyMessageLine).toHaveBeenCalledTimes(1);
|
|
expect(replyMessageLine).toHaveBeenCalledWith("token", [{ type: "text", text: "hello" }], {
|
|
cfg: LINE_TEST_CFG,
|
|
accountId: "acc",
|
|
});
|
|
expect(pushMessagesLine).toHaveBeenCalledTimes(1);
|
|
expect(pushMessagesLine).toHaveBeenCalledWith(
|
|
"line:user:1",
|
|
[createFlexMessage("Card", { type: "bubble" })],
|
|
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
);
|
|
expect(createQuickReplyItems).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("truncates flex altText on a surrogate boundary", async () => {
|
|
// The emoji's surrogate pair straddles LINE's 400-char altText cap; a raw
|
|
// slice used to send a lone high surrogate to the LINE API.
|
|
const lineData = {
|
|
flexMessage: { altText: `${"a".repeat(399)}😀 overflow`, contents: { type: "bubble" } },
|
|
};
|
|
const createFlexMessageSpy = vi.fn(createFlexMessage);
|
|
const { deps } = createDeps({
|
|
createFlexMessage: createFlexMessageSpy as LineAutoReplyDeps["createFlexMessage"],
|
|
});
|
|
|
|
await deliverLineAutoReply({
|
|
...baseDeliveryParams,
|
|
payload: { text: "hello", channelData: { line: lineData } },
|
|
lineData,
|
|
deps,
|
|
});
|
|
|
|
const sentAltText = createFlexMessageSpy.mock.calls[0]?.[0] ?? "";
|
|
expect(sentAltText.length).toBeLessThanOrEqual(400);
|
|
expect(
|
|
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(sentAltText),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("uses reply token for rich-only payloads", async () => {
|
|
const lineData = {
|
|
flexMessage: { altText: "Card", contents: { type: "bubble" } },
|
|
quickReplies: ["A"],
|
|
};
|
|
const { deps, replyMessageLine, pushMessagesLine, createQuickReplyItems } = createDeps({
|
|
processLineMessage: () => ({ text: "", flexMessages: [] }),
|
|
chunkMarkdownText: () => [],
|
|
sendLineReplyChunks: vi.fn(async () => ({ replyTokenUsed: false })),
|
|
});
|
|
|
|
const result = await deliverLineAutoReply({
|
|
...baseDeliveryParams,
|
|
payload: { channelData: { line: lineData } },
|
|
lineData,
|
|
deps,
|
|
});
|
|
|
|
expect(result.replyTokenUsed).toBe(true);
|
|
expect(replyMessageLine).toHaveBeenCalledTimes(1);
|
|
expect(replyMessageLine).toHaveBeenCalledWith(
|
|
"token",
|
|
[
|
|
{
|
|
...createFlexMessage("Card", { type: "bubble" }),
|
|
quickReply: { items: ["A"] },
|
|
},
|
|
],
|
|
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
);
|
|
expect(pushMessagesLine).not.toHaveBeenCalled();
|
|
expect(createQuickReplyItems).toHaveBeenCalledWith(["A"]);
|
|
});
|
|
|
|
it("uses fallback text for quick-reply-only payloads", async () => {
|
|
const createTextMessageWithQuickReplies = vi.fn((text: string, _quickReplies: string[]) => ({
|
|
type: "text" as const,
|
|
text,
|
|
quickReply: { items: ["A", "B"] },
|
|
}));
|
|
const lineData = {
|
|
quickReplies: ["A", "B"],
|
|
};
|
|
const { deps, replyMessageLine, pushMessagesLine } = createDeps({
|
|
createTextMessageWithQuickReplies:
|
|
createTextMessageWithQuickReplies as LineAutoReplyDeps["createTextMessageWithQuickReplies"],
|
|
});
|
|
|
|
const result = await deliverLineAutoReply({
|
|
...baseDeliveryParams,
|
|
payload: { text: "", channelData: { line: lineData } },
|
|
lineData,
|
|
deps,
|
|
});
|
|
|
|
expect(result.replyTokenUsed).toBe(true);
|
|
expect(replyMessageLine).toHaveBeenCalledWith(
|
|
"token",
|
|
[
|
|
{
|
|
type: "text",
|
|
text: "Options:\n- A\n- B",
|
|
quickReply: { items: ["A", "B"] },
|
|
},
|
|
],
|
|
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
);
|
|
expect(pushMessagesLine).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("sends rich messages before quick-reply text so quick replies remain visible", async () => {
|
|
const createTextMessageWithQuickReplies = vi.fn((text: string, _quickReplies: string[]) => ({
|
|
type: "text" as const,
|
|
text,
|
|
quickReply: { items: ["A"] },
|
|
}));
|
|
|
|
const lineData = {
|
|
flexMessage: { altText: "Card", contents: { type: "bubble" } },
|
|
quickReplies: ["A"],
|
|
};
|
|
const { deps, pushMessagesLine, replyMessageLine } = createDeps({
|
|
createTextMessageWithQuickReplies:
|
|
createTextMessageWithQuickReplies as LineAutoReplyDeps["createTextMessageWithQuickReplies"],
|
|
});
|
|
|
|
await deliverLineAutoReply({
|
|
...baseDeliveryParams,
|
|
payload: { text: "hello", channelData: { line: lineData } },
|
|
lineData,
|
|
deps,
|
|
});
|
|
|
|
expect(pushMessagesLine).toHaveBeenCalledWith(
|
|
"line:user:1",
|
|
[createFlexMessage("Card", { type: "bubble" })],
|
|
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
);
|
|
expect(replyMessageLine).toHaveBeenCalledWith(
|
|
"token",
|
|
[
|
|
{
|
|
type: "text",
|
|
text: "hello",
|
|
quickReply: { items: ["A"] },
|
|
},
|
|
],
|
|
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
);
|
|
const pushOrder = pushMessagesLine.mock.invocationCallOrder[0];
|
|
const replyOrder = replyMessageLine.mock.invocationCallOrder[0];
|
|
expect(pushOrder).toBeLessThan(replyOrder);
|
|
});
|
|
|
|
it("falls back to push when reply token delivery fails", async () => {
|
|
const lineData = {
|
|
flexMessage: { altText: "Card", contents: { type: "bubble" } },
|
|
};
|
|
const failingReplyMessageLine = vi.fn(async () => {
|
|
throw new Error("reply failed");
|
|
});
|
|
const { deps, pushMessagesLine } = createDeps({
|
|
processLineMessage: () => ({ text: "", flexMessages: [] }),
|
|
chunkMarkdownText: () => [],
|
|
replyMessageLine: failingReplyMessageLine as LineAutoReplyDeps["replyMessageLine"],
|
|
});
|
|
|
|
const result = await deliverLineAutoReply({
|
|
...baseDeliveryParams,
|
|
payload: { channelData: { line: lineData } },
|
|
lineData,
|
|
deps,
|
|
});
|
|
|
|
expect(result.replyTokenUsed).toBe(true);
|
|
expect(failingReplyMessageLine).toHaveBeenCalledTimes(1);
|
|
expect(pushMessagesLine).toHaveBeenCalledWith(
|
|
"line:user:1",
|
|
[createFlexMessage("Card", { type: "bubble" })],
|
|
{ cfg: LINE_TEST_CFG, accountId: "acc" },
|
|
);
|
|
});
|
|
});
|