test(telegram): cover draft preview overflow

This commit is contained in:
Ayaan Zaidi
2026-05-28 13:02:34 +00:00
parent 8e3be0a705
commit a048cbc4f0

View File

@@ -399,7 +399,42 @@ describe("createTelegramDraftStream", () => {
});
});
it("continues in a new message when rendered preview crosses maxChars", async () => {
it("keeps non-final overflow in one editable preview", async () => {
const api = createMockDraftApi();
const onSupersededPreview = vi.fn();
const stream = createDraftStream(api, { maxChars: 20, onSupersededPreview });
stream.update("Hello world");
await stream.flush();
stream.update("Hello world foo bar baz qux");
await stream.flush();
expect(api.sendMessage).toHaveBeenCalledTimes(1);
expect(api.sendMessage).toHaveBeenNthCalledWith(1, 123, "Hello world", undefined);
expect(api.editMessageText).toHaveBeenCalledWith(123, 17, "Hello world foo bar");
expect(onSupersededPreview).not.toHaveBeenCalled();
expect(stream.lastDeliveredText?.()).toBe("Hello world foo bar");
});
it("does not retain non-final overflow preview pages", async () => {
const api = createMockDraftApi();
const onSupersededPreview = vi.fn();
const stream = createDraftStream(api, {
maxChars: 20,
onSupersededPreview,
});
stream.update("Hello world");
await stream.flush();
stream.update("Hello world foo bar baz qux");
await stream.flush();
expect(api.sendMessage).toHaveBeenCalledTimes(1);
expect(api.editMessageText).toHaveBeenCalledWith(123, 17, "Hello world foo bar");
expect(onSupersededPreview).not.toHaveBeenCalled();
});
it("continues in a new message when a final rendered preview crosses maxChars", async () => {
const api = createMockDraftApi();
api.sendMessage
.mockResolvedValueOnce({ message_id: 17 })
@@ -409,29 +444,53 @@ describe("createTelegramDraftStream", () => {
stream.update("Hello world");
await stream.flush();
stream.update("Hello world foo bar baz qux");
await stream.flush();
await stream.stop();
expect(api.sendMessage).toHaveBeenCalledTimes(2);
expect(api.sendMessage).toHaveBeenNthCalledWith(1, 123, "Hello world", undefined);
expect(api.sendMessage).toHaveBeenNthCalledWith(2, 123, "foo bar baz qux", undefined);
});
it("splits a first oversized rendered preview into chained messages", async () => {
it("clamps a first oversized non-final preview", async () => {
const api = createMockDraftApi();
api.sendMessage
.mockResolvedValueOnce({ message_id: 17 })
.mockResolvedValueOnce({ message_id: 42 });
const stream = createDraftStream(api, { maxChars: 10 });
stream.update("1234567890ABCDEFGHIJ");
await stream.flush();
expect(api.sendMessage).toHaveBeenCalledTimes(1);
expect(api.sendMessage).toHaveBeenNthCalledWith(1, 123, "1234567890", undefined);
expect(stream.lastDeliveredText?.()).toBe("1234567890");
});
it("finalizes overflow that was hidden by a clamped non-final preview", async () => {
const api = createMockDraftApi();
api.sendMessage
.mockResolvedValueOnce({ message_id: 17 })
.mockResolvedValueOnce({ message_id: 42 });
const onSupersededPreview = vi.fn();
const stream = createDraftStream(api, {
maxChars: 10,
onSupersededPreview,
});
stream.update("1234567890ABCDEFGHIJ");
await stream.flush();
await stream.stop();
expect(api.sendMessage).toHaveBeenCalledTimes(2);
expect(api.sendMessage).toHaveBeenNthCalledWith(1, 123, "1234567890", undefined);
expect(api.sendMessage).toHaveBeenNthCalledWith(2, 123, "ABCDEFGHIJ", undefined);
expect(stream.lastDeliveredText?.()).toBe("1234567890ABCDEFGHIJ");
expect(onSupersededPreview).toHaveBeenCalledWith(
expect.objectContaining({
messageId: 17,
retain: true,
}),
);
});
it("retains overflow preview pages", async () => {
it("retains final overflow preview pages", async () => {
const api = createMockDraftApi();
api.sendMessage
.mockResolvedValueOnce({ message_id: 17 })
@@ -445,7 +504,7 @@ describe("createTelegramDraftStream", () => {
stream.update("Hello world");
await stream.flush();
stream.update("Hello world foo bar baz qux");
await stream.flush();
await stream.stop();
expect(onSupersededPreview).toHaveBeenCalledTimes(1);
const [supersededPreview] = onSupersededPreview.mock.calls.at(0) ?? [];