






















@@ -399,7 +399,42 @@ describe("createTelegramDraftStream", () => {
399399});
400400});
401401402-it("continues in a new message when rendered preview crosses maxChars", async () => {
402+it("keeps non-final overflow in one editable preview", async () => {
403+const api = createMockDraftApi();
404+const onSupersededPreview = vi.fn();
405+const stream = createDraftStream(api, { maxChars: 20, onSupersededPreview });
406+407+stream.update("Hello world");
408+await stream.flush();
409+stream.update("Hello world foo bar baz qux");
410+await stream.flush();
411+412+expect(api.sendMessage).toHaveBeenCalledTimes(1);
413+expect(api.sendMessage).toHaveBeenNthCalledWith(1, 123, "Hello world", undefined);
414+expect(api.editMessageText).toHaveBeenCalledWith(123, 17, "Hello world foo bar");
415+expect(onSupersededPreview).not.toHaveBeenCalled();
416+expect(stream.lastDeliveredText?.()).toBe("Hello world foo bar");
417+});
418+419+it("does not retain non-final overflow preview pages", async () => {
420+const api = createMockDraftApi();
421+const onSupersededPreview = vi.fn();
422+const stream = createDraftStream(api, {
423+maxChars: 20,
424+ onSupersededPreview,
425+});
426+427+stream.update("Hello world");
428+await stream.flush();
429+stream.update("Hello world foo bar baz qux");
430+await stream.flush();
431+432+expect(api.sendMessage).toHaveBeenCalledTimes(1);
433+expect(api.editMessageText).toHaveBeenCalledWith(123, 17, "Hello world foo bar");
434+expect(onSupersededPreview).not.toHaveBeenCalled();
435+});
436+437+it("continues in a new message when a final rendered preview crosses maxChars", async () => {
403438const api = createMockDraftApi();
404439api.sendMessage
405440.mockResolvedValueOnce({ message_id: 17 })
@@ -409,29 +444,53 @@ describe("createTelegramDraftStream", () => {
409444stream.update("Hello world");
410445await stream.flush();
411446stream.update("Hello world foo bar baz qux");
412-await stream.flush();
447+await stream.stop();
413448414449expect(api.sendMessage).toHaveBeenCalledTimes(2);
415450expect(api.sendMessage).toHaveBeenNthCalledWith(1, 123, "Hello world", undefined);
416451expect(api.sendMessage).toHaveBeenNthCalledWith(2, 123, "foo bar baz qux", undefined);
417452});
418453419-it("splits a first oversized rendered preview into chained messages", async () => {
454+it("clamps a first oversized non-final preview", async () => {
455+const api = createMockDraftApi();
456+const stream = createDraftStream(api, { maxChars: 10 });
457+458+stream.update("1234567890ABCDEFGHIJ");
459+await stream.flush();
460+461+expect(api.sendMessage).toHaveBeenCalledTimes(1);
462+expect(api.sendMessage).toHaveBeenNthCalledWith(1, 123, "1234567890", undefined);
463+expect(stream.lastDeliveredText?.()).toBe("1234567890");
464+});
465+466+it("finalizes overflow that was hidden by a clamped non-final preview", async () => {
420467const api = createMockDraftApi();
421468api.sendMessage
422469.mockResolvedValueOnce({ message_id: 17 })
423470.mockResolvedValueOnce({ message_id: 42 });
424-const stream = createDraftStream(api, { maxChars: 10 });
471+const onSupersededPreview = vi.fn();
472+const stream = createDraftStream(api, {
473+maxChars: 10,
474+ onSupersededPreview,
475+});
425476426477stream.update("1234567890ABCDEFGHIJ");
427478await stream.flush();
479+await stream.stop();
428480429481expect(api.sendMessage).toHaveBeenCalledTimes(2);
430482expect(api.sendMessage).toHaveBeenNthCalledWith(1, 123, "1234567890", undefined);
431483expect(api.sendMessage).toHaveBeenNthCalledWith(2, 123, "ABCDEFGHIJ", undefined);
484+expect(stream.lastDeliveredText?.()).toBe("1234567890ABCDEFGHIJ");
485+expect(onSupersededPreview).toHaveBeenCalledWith(
486+expect.objectContaining({
487+messageId: 17,
488+retain: true,
489+}),
490+);
432491});
433492434-it("retains overflow preview pages", async () => {
493+it("retains final overflow preview pages", async () => {
435494const api = createMockDraftApi();
436495api.sendMessage
437496.mockResolvedValueOnce({ message_id: 17 })
@@ -445,7 +504,7 @@ describe("createTelegramDraftStream", () => {
445504stream.update("Hello world");
446505await stream.flush();
447506stream.update("Hello world foo bar baz qux");
448-await stream.flush();
507+await stream.stop();
449508450509expect(onSupersededPreview).toHaveBeenCalledTimes(1);
451510const [supersededPreview] = onSupersededPreview.mock.calls.at(0) ?? [];
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。