test(slack): dedupe block and draft stream test fixtures

This commit is contained in:
Peter Steinberger
2026-02-18 12:57:51 +00:00
parent 3daf730fcc
commit 7bca5f5400
4 changed files with 138 additions and 144 deletions

View File

@@ -1,36 +1,12 @@
import type { WebClient } from "@slack/web-api";
import { describe, expect, it, vi } from "vitest";
vi.mock("../config/config.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("./accounts.js", () => ({
resolveSlackAccount: () => ({
accountId: "default",
botToken: "xoxb-test",
botTokenSource: "config",
config: {},
}),
}));
import { describe, expect, it } from "vitest";
import { createSlackEditTestClient, installSlackBlockTestMocks } from "./blocks.test-helpers.js";
installSlackBlockTestMocks();
const { editSlackMessage } = await import("./actions.js");
function createClient() {
return {
chat: {
update: vi.fn(async () => ({ ok: true })),
},
} as unknown as WebClient & {
chat: {
update: ReturnType<typeof vi.fn>;
};
};
}
describe("editSlackMessage blocks", () => {
it("updates with valid blocks", async () => {
const client = createClient();
const client = createSlackEditTestClient();
await editSlackMessage("C123", "171234.567", "", {
token: "xoxb-test",
@@ -49,7 +25,7 @@ describe("editSlackMessage blocks", () => {
});
it("uses image block text as edit fallback", async () => {
const client = createClient();
const client = createSlackEditTestClient();
await editSlackMessage("C123", "171234.567", "", {
token: "xoxb-test",
@@ -65,7 +41,7 @@ describe("editSlackMessage blocks", () => {
});
it("uses video block title as edit fallback", async () => {
const client = createClient();
const client = createSlackEditTestClient();
await editSlackMessage("C123", "171234.567", "", {
token: "xoxb-test",
@@ -89,7 +65,7 @@ describe("editSlackMessage blocks", () => {
});
it("uses generic file fallback text for file blocks", async () => {
const client = createClient();
const client = createSlackEditTestClient();
await editSlackMessage("C123", "171234.567", "", {
token: "xoxb-test",
@@ -105,7 +81,7 @@ describe("editSlackMessage blocks", () => {
});
it("rejects empty blocks arrays", async () => {
const client = createClient();
const client = createSlackEditTestClient();
await expect(
editSlackMessage("C123", "171234.567", "updated", {
@@ -119,7 +95,7 @@ describe("editSlackMessage blocks", () => {
});
it("rejects blocks missing a type", async () => {
const client = createClient();
const client = createSlackEditTestClient();
await expect(
editSlackMessage("C123", "171234.567", "updated", {
@@ -131,4 +107,19 @@ describe("editSlackMessage blocks", () => {
expect(client.chat.update).not.toHaveBeenCalled();
});
it("rejects blocks arrays above Slack max count", async () => {
const client = createSlackEditTestClient();
const blocks = Array.from({ length: 51 }, () => ({ type: "divider" }));
await expect(
editSlackMessage("C123", "171234.567", "updated", {
token: "xoxb-test",
client,
blocks,
}),
).rejects.toThrow(/cannot exceed 50 items/i);
expect(client.chat.update).not.toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,51 @@
import type { WebClient } from "@slack/web-api";
import { vi } from "vitest";
export type SlackEditTestClient = WebClient & {
chat: {
update: ReturnType<typeof vi.fn>;
};
};
export type SlackSendTestClient = WebClient & {
conversations: {
open: ReturnType<typeof vi.fn>;
};
chat: {
postMessage: ReturnType<typeof vi.fn>;
};
};
export function installSlackBlockTestMocks() {
vi.mock("../config/config.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("./accounts.js", () => ({
resolveSlackAccount: () => ({
accountId: "default",
botToken: "xoxb-test",
botTokenSource: "config",
config: {},
}),
}));
}
export function createSlackEditTestClient(): SlackEditTestClient {
return {
chat: {
update: vi.fn(async () => ({ ok: true })),
},
} as unknown as SlackEditTestClient;
}
export function createSlackSendTestClient(): SlackSendTestClient {
return {
conversations: {
open: vi.fn(async () => ({ channel: { id: "D123" } })),
},
chat: {
postMessage: vi.fn(async () => ({ ts: "171234.567" })),
},
} as unknown as SlackSendTestClient;
}

View File

@@ -1,20 +1,40 @@
import { describe, expect, it, vi } from "vitest";
import { createSlackDraftStream } from "./draft-stream.js";
describe("createSlackDraftStream", () => {
it("sends the first update and edits subsequent updates", async () => {
const send = vi.fn(async () => ({
function createDraftStreamHarness(
params: {
maxChars?: number;
send?: ReturnType<typeof vi.fn>;
edit?: ReturnType<typeof vi.fn>;
remove?: ReturnType<typeof vi.fn>;
warn?: ReturnType<typeof vi.fn>;
} = {},
) {
const send =
params.send ??
vi.fn(async () => ({
channelId: "C123",
messageId: "111.222",
}));
const edit = vi.fn(async () => {});
const stream = createSlackDraftStream({
target: "channel:C123",
token: "xoxb-test",
throttleMs: 250,
send,
edit,
});
const edit = params.edit ?? vi.fn(async () => {});
const remove = params.remove ?? vi.fn(async () => {});
const warn = params.warn ?? vi.fn();
const stream = createSlackDraftStream({
target: "channel:C123",
token: "xoxb-test",
throttleMs: 250,
maxChars: params.maxChars,
send,
edit,
remove,
warn,
});
return { stream, send, edit, remove, warn };
}
describe("createSlackDraftStream", () => {
it("sends the first update and edits subsequent updates", async () => {
const { stream, send, edit } = createDraftStreamHarness();
stream.update("hello");
await stream.flush();
@@ -30,18 +50,7 @@ describe("createSlackDraftStream", () => {
});
it("does not send duplicate text", async () => {
const send = vi.fn(async () => ({
channelId: "C123",
messageId: "111.222",
}));
const edit = vi.fn(async () => {});
const stream = createSlackDraftStream({
target: "channel:C123",
token: "xoxb-test",
throttleMs: 250,
send,
edit,
});
const { stream, send, edit } = createDraftStreamHarness();
stream.update("same");
await stream.flush();
@@ -57,14 +66,7 @@ describe("createSlackDraftStream", () => {
.fn()
.mockResolvedValueOnce({ channelId: "C123", messageId: "111.222" })
.mockResolvedValueOnce({ channelId: "C123", messageId: "333.444" });
const edit = vi.fn(async () => {});
const stream = createSlackDraftStream({
target: "channel:C123",
token: "xoxb-test",
throttleMs: 250,
send,
edit,
});
const { stream, edit } = createDraftStreamHarness({ send });
stream.update("first");
await stream.flush();
@@ -78,21 +80,7 @@ describe("createSlackDraftStream", () => {
});
it("stops when text exceeds max chars", async () => {
const send = vi.fn(async () => ({
channelId: "C123",
messageId: "111.222",
}));
const edit = vi.fn(async () => {});
const warn = vi.fn();
const stream = createSlackDraftStream({
target: "channel:C123",
token: "xoxb-test",
maxChars: 5,
throttleMs: 250,
send,
edit,
warn,
});
const { stream, send, edit, warn } = createDraftStreamHarness({ maxChars: 5 });
stream.update("123456");
await stream.flush();
@@ -105,20 +93,7 @@ describe("createSlackDraftStream", () => {
});
it("clear removes preview message when one exists", async () => {
const send = vi.fn(async () => ({
channelId: "C123",
messageId: "111.222",
}));
const edit = vi.fn(async () => {});
const remove = vi.fn(async () => {});
const stream = createSlackDraftStream({
target: "channel:C123",
token: "xoxb-test",
throttleMs: 250,
send,
edit,
remove,
});
const { stream, remove } = createDraftStreamHarness();
stream.update("hello");
await stream.flush();
@@ -134,23 +109,26 @@ describe("createSlackDraftStream", () => {
});
it("clear is a no-op when no preview message exists", async () => {
const send = vi.fn(async () => ({
channelId: "C123",
messageId: "111.222",
}));
const edit = vi.fn(async () => {});
const remove = vi.fn(async () => {});
const stream = createSlackDraftStream({
target: "channel:C123",
token: "xoxb-test",
throttleMs: 250,
send,
edit,
remove,
});
const { stream, remove } = createDraftStreamHarness();
await stream.clear();
expect(remove).not.toHaveBeenCalled();
});
it("clear warns when cleanup fails", async () => {
const remove = vi.fn(async () => {
throw new Error("cleanup failed");
});
const warn = vi.fn();
const { stream } = createDraftStreamHarness({ remove, warn });
stream.update("hello");
await stream.flush();
await stream.clear();
expect(warn).toHaveBeenCalledWith("slack stream preview cleanup failed: cleanup failed");
expect(stream.messageId()).toBeUndefined();
expect(stream.channelId()).toBeUndefined();
});
});

View File

@@ -1,38 +1,12 @@
import type { WebClient } from "@slack/web-api";
import { describe, expect, it, vi } from "vitest";
vi.mock("../config/config.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("./accounts.js", () => ({
resolveSlackAccount: () => ({
accountId: "default",
botToken: "xoxb-test",
botTokenSource: "config",
config: {},
}),
}));
import { describe, expect, it } from "vitest";
import { createSlackSendTestClient, installSlackBlockTestMocks } from "./blocks.test-helpers.js";
installSlackBlockTestMocks();
const { sendMessageSlack } = await import("./send.js");
function createClient() {
return {
conversations: {
open: vi.fn(async () => ({ channel: { id: "D123" } })),
},
chat: {
postMessage: vi.fn(async () => ({ ts: "171234.567" })),
},
} as unknown as WebClient & {
conversations: { open: ReturnType<typeof vi.fn> };
chat: { postMessage: ReturnType<typeof vi.fn> };
};
}
describe("sendMessageSlack blocks", () => {
it("posts blocks with fallback text when message is empty", async () => {
const client = createClient();
const client = createSlackSendTestClient();
const result = await sendMessageSlack("channel:C123", "", {
token: "xoxb-test",
client,
@@ -51,7 +25,7 @@ describe("sendMessageSlack blocks", () => {
});
it("derives fallback text from image blocks", async () => {
const client = createClient();
const client = createSlackSendTestClient();
await sendMessageSlack("channel:C123", "", {
token: "xoxb-test",
client,
@@ -66,7 +40,7 @@ describe("sendMessageSlack blocks", () => {
});
it("derives fallback text from video blocks", async () => {
const client = createClient();
const client = createSlackSendTestClient();
await sendMessageSlack("channel:C123", "", {
token: "xoxb-test",
client,
@@ -89,7 +63,7 @@ describe("sendMessageSlack blocks", () => {
});
it("derives fallback text from file blocks", async () => {
const client = createClient();
const client = createSlackSendTestClient();
await sendMessageSlack("channel:C123", "", {
token: "xoxb-test",
client,
@@ -104,7 +78,7 @@ describe("sendMessageSlack blocks", () => {
});
it("rejects blocks combined with mediaUrl", async () => {
const client = createClient();
const client = createSlackSendTestClient();
await expect(
sendMessageSlack("channel:C123", "hi", {
token: "xoxb-test",
@@ -117,7 +91,7 @@ describe("sendMessageSlack blocks", () => {
});
it("rejects empty blocks arrays from runtime callers", async () => {
const client = createClient();
const client = createSlackSendTestClient();
await expect(
sendMessageSlack("channel:C123", "hi", {
token: "xoxb-test",
@@ -129,7 +103,7 @@ describe("sendMessageSlack blocks", () => {
});
it("rejects blocks arrays above Slack max count", async () => {
const client = createClient();
const client = createSlackSendTestClient();
const blocks = Array.from({ length: 51 }, () => ({ type: "divider" }));
await expect(
sendMessageSlack("channel:C123", "hi", {
@@ -142,7 +116,7 @@ describe("sendMessageSlack blocks", () => {
});
it("rejects blocks missing type from runtime callers", async () => {
const client = createClient();
const client = createSlackSendTestClient();
await expect(
sendMessageSlack("channel:C123", "hi", {
token: "xoxb-test",