|
| 1 | +// @vitest-environment node |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { ChatAttachment } from "../../lib/chat/chat-types.ts"; |
| 4 | +import { createSessionCapability } from "../../lib/sessions/index.ts"; |
| 5 | +import { |
| 6 | + getChatAttachmentDataUrl, |
| 7 | + registerChatAttachmentPayload, |
| 8 | + releaseChatAttachmentPayloads, |
| 9 | +} from "./attachment-payload-store.ts"; |
| 10 | +import type { ChatHost } from "./chat-send-contract.ts"; |
| 11 | +import { handleSendChat } from "./chat-send-submit.ts"; |
| 12 | + |
| 13 | +const attachmentsToRelease: ChatAttachment[] = []; |
| 14 | +const attachmentDataUrl = "data:application/pdf;base64,JVBERi0xLjQK"; |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + releaseChatAttachmentPayloads(attachmentsToRelease); |
| 18 | + attachmentsToRelease.length = 0; |
| 19 | +}); |
| 20 | + |
| 21 | +function createStagedAttachment(id: string): ChatAttachment { |
| 22 | + const file = new File(["%PDF-1.4\n"], "brief.pdf", { type: "application/pdf" }); |
| 23 | + const attachment = registerChatAttachmentPayload({ |
| 24 | + attachment: { |
| 25 | + id, |
| 26 | + mimeType: "application/pdf", |
| 27 | + fileName: "brief.pdf", |
| 28 | + sizeBytes: file.size, |
| 29 | + }, |
| 30 | + dataUrl: attachmentDataUrl, |
| 31 | + file, |
| 32 | + }); |
| 33 | + attachmentsToRelease.push(attachment); |
| 34 | + return attachment; |
| 35 | +} |
| 36 | + |
| 37 | +function createImmediateCommandHost( |
| 38 | + command: string, |
| 39 | + attachment: ChatAttachment, |
| 40 | + overrides: Partial<ChatHost> = {}, |
| 41 | +): ChatHost { |
| 42 | + const host = { |
| 43 | + sessions: createSessionCapability({ |
| 44 | + snapshot: { client: null, phase: "reconnecting", hello: null }, |
| 45 | + subscribe: () => () => undefined, |
| 46 | + subscribeEvents: () => () => undefined, |
| 47 | + }), |
| 48 | + client: null, |
| 49 | + connected: true, |
| 50 | + sessionKey: "agent:main", |
| 51 | + chatLoading: false, |
| 52 | + chatMessage: command, |
| 53 | + chatMessages: [], |
| 54 | + chatLocalInputHistoryBySession: {}, |
| 55 | + chatInputHistorySessionKey: null, |
| 56 | + chatInputHistoryItems: null, |
| 57 | + chatInputHistoryIndex: -1, |
| 58 | + chatDraftBeforeHistory: null, |
| 59 | + chatAttachments: [attachment], |
| 60 | + chatQueue: [], |
| 61 | + chatRunId: null, |
| 62 | + chatSending: false, |
| 63 | + chatStream: null, |
| 64 | + chatModelCatalog: [], |
| 65 | + hello: null, |
| 66 | + refreshSessionsAfterChat: new Map(), |
| 67 | + ...overrides, |
| 68 | + } satisfies Partial<ChatHost>; |
| 69 | + return host as ChatHost; |
| 70 | +} |
| 71 | + |
| 72 | +describe("handleSendChat immediate local commands", () => { |
| 73 | + it.each(["/export-session", "/export"])( |
| 74 | + "preserves staged attachments while %s exports the chat", |
| 75 | + async (command) => { |
| 76 | + const attachment = createStagedAttachment("export-att"); |
| 77 | + const exportCurrentChat = vi.fn(); |
| 78 | + const host = createImmediateCommandHost(command, attachment, { exportCurrentChat }); |
| 79 | + |
| 80 | + await handleSendChat(host); |
| 81 | + |
| 82 | + expect(exportCurrentChat).toHaveBeenCalledOnce(); |
| 83 | + expect(host.chatMessage).toBe(""); |
| 84 | + expect(host.chatAttachments).toEqual([attachment]); |
| 85 | + expect(getChatAttachmentDataUrl(attachment)).toBe(attachmentDataUrl); |
| 86 | + expect(host.chatQueue).toStrictEqual([]); |
| 87 | + }, |
| 88 | + ); |
| 89 | + |
| 90 | + it("does not duplicate staged attachments into both old and new session composers", async () => { |
| 91 | + const attachment = createStagedAttachment("new-session-att"); |
| 92 | + const attachmentsBySession = new Map<string, ChatAttachment[]>(); |
| 93 | + const host = createImmediateCommandHost("/new", attachment); |
| 94 | + host.createChatSession = vi.fn(async () => { |
| 95 | + const previousSessionKey = host.sessionKey; |
| 96 | + const nextSessionKey = "agent:main:new"; |
| 97 | + // Session creation captures the next composer before route switching |
| 98 | + // decides whether the old session's attachment needs a memory fallback. |
| 99 | + const createdSessionAttachments = [...host.chatAttachments]; |
| 100 | + attachmentsBySession.set(previousSessionKey, [...host.chatAttachments]); |
| 101 | + host.sessionKey = nextSessionKey; |
| 102 | + host.chatAttachments = createdSessionAttachments; |
| 103 | + attachmentsBySession.set(nextSessionKey, [...host.chatAttachments]); |
| 104 | + return true; |
| 105 | + }); |
| 106 | + |
| 107 | + await handleSendChat(host); |
| 108 | + |
| 109 | + expect(host.createChatSession).toHaveBeenCalledOnce(); |
| 110 | + expect(attachmentsBySession.get("agent:main")).toStrictEqual([]); |
| 111 | + expect(attachmentsBySession.get("agent:main:new")).toStrictEqual([]); |
| 112 | + expect(host.chatAttachments).toStrictEqual([]); |
| 113 | + }); |
| 114 | + |
| 115 | + it("restores staged attachments when creating a new session is cancelled", async () => { |
| 116 | + const attachment = createStagedAttachment("cancelled-new-session-att"); |
| 117 | + const createChatSession = vi.fn(async () => false); |
| 118 | + const host = createImmediateCommandHost("/new", attachment, { createChatSession }); |
| 119 | + |
| 120 | + await handleSendChat(host); |
| 121 | + |
| 122 | + expect(createChatSession).toHaveBeenCalledOnce(); |
| 123 | + expect(host.chatMessage).toBe("/new"); |
| 124 | + expect(host.chatAttachments).toHaveLength(1); |
| 125 | + expect(host.chatAttachments[0]).toMatchObject(attachment); |
| 126 | + expect(getChatAttachmentDataUrl(host.chatAttachments[0]!)).toBe(attachmentDataUrl); |
| 127 | + }); |
| 128 | +}); |
0 commit comments