Files
openclaw/extensions/slack/src/send.upload.test.ts
2026-05-10 14:38:38 +01:00

441 lines
14 KiB
TypeScript

import type { WebClient } from "@slack/web-api";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { installSlackBlockTestMocks } from "./blocks.test-helpers.js";
import {
clearSlackThreadParticipationCache,
hasSlackThreadParticipation,
} from "./sent-thread-cache.js";
// --- Module mocks (must precede dynamic import) ---
installSlackBlockTestMocks();
const loadOutboundMediaFromUrlMock = vi.hoisted(() =>
vi.fn(async (_mediaUrl: string, _options?: unknown) => ({
buffer: Buffer.from("fake-image"),
contentType: "image/png",
kind: "image",
fileName: "screenshot.png",
})),
);
const fetchWithSsrFGuard = vi.fn(
async (params: { url: string; init?: RequestInit }) =>
({
response: await fetch(params.url, params.init),
finalUrl: params.url,
release: async () => {},
}) as const,
);
vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ({
fetchWithSsrFGuard: (...args: unknown[]) =>
fetchWithSsrFGuard(...(args as [params: { url: string; init?: RequestInit }])),
}));
vi.mock("openclaw/plugin-sdk/fetch-runtime", async () => {
const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/fetch-runtime")>(
"openclaw/plugin-sdk/fetch-runtime",
);
return {
...actual,
withTrustedEnvProxyGuardedFetchMode: (params: Record<string, unknown>) => ({
...params,
mode: "trusted_env_proxy",
}),
};
});
vi.mock("./runtime-api.js", async () => {
const actual = await vi.importActual<typeof import("./runtime-api.js")>("./runtime-api.js");
const mockedLoadOutboundMediaFromUrl =
loadOutboundMediaFromUrlMock as unknown as typeof actual.loadOutboundMediaFromUrl;
return {
...actual,
loadOutboundMediaFromUrl: (...args: Parameters<typeof actual.loadOutboundMediaFromUrl>) =>
mockedLoadOutboundMediaFromUrl(...args),
};
});
let sendMessageSlack: typeof import("./send.js").sendMessageSlack;
let clearSlackDmChannelCache: typeof import("./send.js").clearSlackDmChannelCache;
let clearSlackSendQueuesForTest: typeof import("./send.js").clearSlackSendQueuesForTest;
({ sendMessageSlack, clearSlackDmChannelCache, clearSlackSendQueuesForTest } =
await import("./send.js"));
const SLACK_TEST_CFG = { channels: { slack: { botToken: "xoxb-test" } } };
type UploadTestClient = WebClient & {
conversations: { open: ReturnType<typeof vi.fn> };
chat: { postMessage: ReturnType<typeof vi.fn> };
files: {
getUploadURLExternal: ReturnType<typeof vi.fn>;
completeUploadExternal: ReturnType<typeof vi.fn>;
};
};
type MockCalls = {
mock: { calls: unknown[][] };
};
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
function requireRecord(value: unknown, label: string): Record<string, unknown> {
expect(isRecord(value), `${label} should be an object`).toBe(true);
if (!isRecord(value)) {
throw new Error(`${label} should be an object`);
}
return value;
}
function requireArray(value: unknown, label: string): unknown[] {
expect(Array.isArray(value), `${label} should be an array`).toBe(true);
if (!Array.isArray(value)) {
throw new Error(`${label} should be an array`);
}
return value;
}
function expectFields(record: Record<string, unknown>, expected: Record<string, unknown>) {
for (const [key, value] of Object.entries(expected)) {
expect(record[key], key).toEqual(value);
}
}
function expectCallFirstArg(
mock: MockCalls,
callNumber: number,
expected: Record<string, unknown>,
label = "mock first argument",
): Record<string, unknown> {
expect(mock.mock.calls.length).toBeGreaterThanOrEqual(callNumber);
const [firstArg] = mock.mock.calls[callNumber - 1] ?? [];
const record = requireRecord(firstArg, label);
expectFields(record, expected);
return record;
}
function expectOnlyCallFirstArg(
mock: MockCalls,
expected: Record<string, unknown>,
label?: string,
): Record<string, unknown> {
expect(mock.mock.calls).toHaveLength(1);
return expectCallFirstArg(mock, 1, expected, label);
}
function expectCompletedUpload(params: {
client: UploadTestClient;
expected: Record<string, unknown>;
file?: Record<string, unknown>;
}) {
const payload = expectOnlyCallFirstArg(
params.client.files.completeUploadExternal,
params.expected,
"complete upload payload",
);
if (params.file) {
const [file] = requireArray(payload.files, "complete upload files");
expectFields(requireRecord(file, "complete upload file"), params.file);
}
return payload;
}
function createUploadTestClient(): UploadTestClient {
return {
conversations: {
open: vi.fn(async () => ({ channel: { id: "D99RESOLVED" } })),
},
chat: {
postMessage: vi.fn(async () => ({ ts: "171234.567" })),
},
files: {
getUploadURLExternal: vi.fn(async () => ({
ok: true,
upload_url: "https://uploads.slack.test/upload",
file_id: "F001",
})),
completeUploadExternal: vi.fn(async () => ({ ok: true })),
},
} as unknown as UploadTestClient;
}
describe("sendMessageSlack file upload with user IDs", () => {
const originalFetch = globalThis.fetch;
beforeEach(() => {
globalThis.fetch = vi.fn(
async () => new Response("ok", { status: 200 }),
) as unknown as typeof fetch;
fetchWithSsrFGuard.mockClear();
loadOutboundMediaFromUrlMock.mockClear();
clearSlackDmChannelCache();
clearSlackSendQueuesForTest();
clearSlackThreadParticipationCache();
});
afterEach(() => {
globalThis.fetch = originalFetch;
vi.restoreAllMocks();
});
it("resolves bare user ID to DM channel before completing upload", async () => {
const client = createUploadTestClient();
// Bare user ID — parseSlackTarget classifies this as kind="channel"
await sendMessageSlack("U2ZH3MFSR", "screenshot", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/screenshot.png",
});
// Should call conversations.open to resolve user ID → DM channel
expect(client.conversations.open).toHaveBeenCalledWith({
users: "U2ZH3MFSR",
});
expectCompletedUpload({
client,
expected: { channel_id: "D99RESOLVED" },
file: { id: "F001", title: "screenshot.png" },
});
});
it("resolves prefixed user ID to DM channel before completing upload", async () => {
const client = createUploadTestClient();
await sendMessageSlack("user:UABC123", "image", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/photo.png",
});
expect(client.conversations.open).toHaveBeenCalledWith({
users: "UABC123",
});
expectCompletedUpload({ client, expected: { channel_id: "D99RESOLVED" } });
});
it("posts text-only user-target DMs directly without conversations.open", async () => {
const client = createUploadTestClient();
client.conversations.open.mockRejectedValueOnce(new Error("missing_scope"));
await sendMessageSlack("user:UABC123", "first", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
});
await sendMessageSlack("user:UABC123", "second", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
});
expect(client.conversations.open).not.toHaveBeenCalled();
expect(client.chat.postMessage).toHaveBeenCalledTimes(2);
expectCallFirstArg(client.chat.postMessage, 2, {
channel: "UABC123",
text: "second",
});
});
it("serializes concurrent sends to the same Slack target", async () => {
const client = createUploadTestClient();
let resolveFirst: (() => void) | undefined;
client.chat.postMessage.mockImplementation(async (payload: { text?: string }) => {
if (payload.text === "first") {
await new Promise<void>((resolve) => {
resolveFirst = resolve;
});
return { ts: "1.000" };
}
return { ts: "2.000" };
});
const first = sendMessageSlack("channel:C123CHAN", "first", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
});
await vi.waitFor(() => expect(client.chat.postMessage).toHaveBeenCalledTimes(1));
const second = sendMessageSlack("channel:C123CHAN", "second", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
});
await Promise.resolve();
expect(client.chat.postMessage).toHaveBeenCalledTimes(1);
if (!resolveFirst) {
throw new Error("Expected first Slack send release callback to be initialized");
}
resolveFirst();
const firstResult = await first;
expectFields(requireRecord(firstResult, "first send result"), {
channelId: "C123CHAN",
messageId: "1.000",
});
expectFields(requireRecord(firstResult.receipt, "first receipt"), {
primaryPlatformMessageId: "1.000",
platformMessageIds: ["1.000"],
});
const secondResult = await second;
expectFields(requireRecord(secondResult, "second send result"), {
channelId: "C123CHAN",
messageId: "2.000",
});
expectFields(requireRecord(secondResult.receipt, "second receipt"), {
primaryPlatformMessageId: "2.000",
platformMessageIds: ["2.000"],
});
expectCallFirstArg(client.chat.postMessage, 2, { text: "second" });
});
it("scopes DM channel resolution cache by token identity", async () => {
const client = createUploadTestClient();
await sendMessageSlack("user:UABC123", "first", {
token: "xoxb-test-a",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/first.png",
});
await sendMessageSlack("user:UABC123", "second", {
token: "xoxb-test-b",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/second.png",
});
expect(client.conversations.open).toHaveBeenCalledTimes(2);
});
it("sends file directly to channel without conversations.open", async () => {
const client = createUploadTestClient();
const result = await sendMessageSlack("channel:C123CHAN", "chart", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/chart.png",
});
expect(client.conversations.open).not.toHaveBeenCalled();
expectCompletedUpload({ client, expected: { channel_id: "C123CHAN" } });
expectFields(requireRecord(result.receipt, "receipt"), {
primaryPlatformMessageId: "F001",
platformMessageIds: ["F001"],
});
const [part] = requireArray(result.receipt.parts, "receipt parts");
const partRecord = requireRecord(part, "receipt part");
expectFields(partRecord, {
platformMessageId: "F001",
kind: "media",
});
expectFields(requireRecord(partRecord.raw, "receipt raw"), {
channel: "slack",
channelId: "C123CHAN",
});
});
it("resolves mention-style user ID before file upload", async () => {
const client = createUploadTestClient();
await sendMessageSlack("<@U777TEST>", "report", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/report.png",
});
expect(client.conversations.open).toHaveBeenCalledWith({
users: "U777TEST",
});
expectCompletedUpload({ client, expected: { channel_id: "D99RESOLVED" } });
});
it("uploads bytes to the presigned URL and completes with thread+caption", async () => {
const client = createUploadTestClient();
const result = await sendMessageSlack("channel:C123CHAN", "caption", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/threaded.png",
threadTs: "171.222",
});
expect(client.files.getUploadURLExternal).toHaveBeenCalledWith({
filename: "screenshot.png",
length: Buffer.from("fake-image").length,
});
const fetchCalls = (globalThis.fetch as unknown as MockCalls).mock.calls;
expect(fetchCalls).toHaveLength(1);
const [fetchUrl, fetchInit] = fetchCalls[0] ?? [];
expect(fetchUrl).toBe("https://uploads.slack.test/upload");
expectFields(requireRecord(fetchInit, "fetch init"), { method: "POST" });
expectOnlyCallFirstArg(fetchWithSsrFGuard, {
url: "https://uploads.slack.test/upload",
mode: "trusted_env_proxy",
auditContext: "slack-upload-file",
});
expectCompletedUpload({
client,
expected: {
channel_id: "C123CHAN",
initial_comment: "caption",
thread_ts: "171.222",
},
});
expect(hasSlackThreadParticipation("default", "C123CHAN", "171.222")).toBe(true);
expect(result.receipt.threadId).toBe("171.222");
});
it("uses explicit upload filename and title overrides when provided", async () => {
const client = createUploadTestClient();
await sendMessageSlack("channel:C123CHAN", "caption", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/threaded.png",
uploadFileName: "custom-name.bin",
uploadTitle: "Custom Title",
});
expect(client.files.getUploadURLExternal).toHaveBeenCalledWith({
filename: "custom-name.bin",
length: Buffer.from("fake-image").length,
});
expectCompletedUpload({
client,
expected: {},
file: { id: "F001", title: "Custom Title" },
});
});
it("uses uploadFileName as the title fallback when uploadTitle is omitted", async () => {
const client = createUploadTestClient();
await sendMessageSlack("channel:C123CHAN", "caption", {
token: "xoxb-test",
cfg: SLACK_TEST_CFG,
client,
mediaUrl: "/tmp/threaded.png",
uploadFileName: "custom-name.bin",
});
expect(client.files.getUploadURLExternal).toHaveBeenCalledWith({
filename: "custom-name.bin",
length: Buffer.from("fake-image").length,
});
expectCompletedUpload({
client,
expected: {},
file: { id: "F001", title: "custom-name.bin" },
});
});
});