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,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();
});
});