|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { render } from "lit"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { waitForFast } from "../../test-helpers/wait-for.ts"; |
| 6 | +import { NewSessionAttachmentDraft } from "./attachment-draft.ts"; |
| 7 | +import { renderNewSessionDraftComposer } from "./composer.ts"; |
| 8 | +import { NewSessionModelControl } from "./model-control.ts"; |
| 9 | + |
| 10 | +const attachmentDrafts: NewSessionAttachmentDraft[] = []; |
| 11 | + |
| 12 | +function renderComposer(overrides: { submitting?: boolean; messageLocked?: boolean } = {}) { |
| 13 | + const container = document.createElement("div"); |
| 14 | + const attachmentDraft = new NewSessionAttachmentDraft(() => undefined); |
| 15 | + attachmentDrafts.push(attachmentDraft); |
| 16 | + render( |
| 17 | + renderNewSessionDraftComposer({ |
| 18 | + agentId: "main", |
| 19 | + attachmentDraft, |
| 20 | + canSubmit: true, |
| 21 | + context: undefined, |
| 22 | + isCatalogTarget: true, |
| 23 | + message: "", |
| 24 | + modelControl: new NewSessionModelControl(() => undefined), |
| 25 | + requiresModifier: false, |
| 26 | + submitting: overrides.submitting ?? false, |
| 27 | + messageLocked: overrides.messageLocked, |
| 28 | + onInput: () => undefined, |
| 29 | + onSubmit: () => undefined, |
| 30 | + }), |
| 31 | + container, |
| 32 | + ); |
| 33 | + const composer = container.querySelector<HTMLElement>(".new-session-page__composer"); |
| 34 | + if (!composer) { |
| 35 | + throw new Error("Expected new-session composer"); |
| 36 | + } |
| 37 | + return { attachmentDraft, composer }; |
| 38 | +} |
| 39 | + |
| 40 | +function createDragEvent(type: string, files: File[] = [], types = ["Files"]): Event { |
| 41 | + const event = new Event(type, { bubbles: true, cancelable: true }); |
| 42 | + Object.defineProperty(event, "dataTransfer", { |
| 43 | + value: { files, types }, |
| 44 | + }); |
| 45 | + return event; |
| 46 | +} |
| 47 | + |
| 48 | +afterEach(() => { |
| 49 | + for (const attachmentDraft of attachmentDrafts) { |
| 50 | + attachmentDraft.reset({ release: true }); |
| 51 | + } |
| 52 | + attachmentDrafts.length = 0; |
| 53 | + vi.restoreAllMocks(); |
| 54 | +}); |
| 55 | + |
| 56 | +describe("new-session composer attachment drops", () => { |
| 57 | + it("adds a dropped file through the shared attachment handling", async () => { |
| 58 | + const { attachmentDraft, composer } = renderComposer(); |
| 59 | + const replace = vi.spyOn(attachmentDraft, "replace"); |
| 60 | + const file = new File(["image"], "pic.png", { type: "image/png" }); |
| 61 | + |
| 62 | + composer.dispatchEvent(createDragEvent("drop", [file])); |
| 63 | + |
| 64 | + await waitForFast(() => expect(replace).toHaveBeenCalledOnce()); |
| 65 | + expect(replace).toHaveBeenCalledWith([ |
| 66 | + expect.objectContaining({ |
| 67 | + fileName: "pic.png", |
| 68 | + mimeType: "image/png", |
| 69 | + sizeBytes: file.size, |
| 70 | + }), |
| 71 | + ]); |
| 72 | + expect(attachmentDraft.attachments).toHaveLength(1); |
| 73 | + expect(attachmentDraft.attachments[0]).toMatchObject({ |
| 74 | + fileName: "pic.png", |
| 75 | + mimeType: "image/png", |
| 76 | + sizeBytes: file.size, |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("keeps the drop affordance balanced across nested drag targets", () => { |
| 81 | + const { composer } = renderComposer(); |
| 82 | + |
| 83 | + composer.dispatchEvent(createDragEvent("dragenter")); |
| 84 | + expect(composer.hasAttribute("data-attachment-drop-active")).toBe(true); |
| 85 | + |
| 86 | + composer.dispatchEvent(createDragEvent("dragenter")); |
| 87 | + composer.dispatchEvent(createDragEvent("dragleave")); |
| 88 | + expect(composer.hasAttribute("data-attachment-drop-active")).toBe(true); |
| 89 | + |
| 90 | + composer.dispatchEvent(createDragEvent("dragleave")); |
| 91 | + expect(composer.hasAttribute("data-attachment-drop-active")).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it("keeps non-file drops native inside the textarea and cancels them elsewhere", () => { |
| 95 | + const { attachmentDraft, composer } = renderComposer(); |
| 96 | + const replace = vi.spyOn(attachmentDraft, "replace"); |
| 97 | + const textarea = composer.querySelector<HTMLTextAreaElement>("textarea"); |
| 98 | + if (!textarea) { |
| 99 | + throw new Error("Expected composer textarea"); |
| 100 | + } |
| 101 | + |
| 102 | + const dragenter = createDragEvent("dragenter", [], ["text/plain"]); |
| 103 | + composer.dispatchEvent(dragenter); |
| 104 | + expect(composer.hasAttribute("data-attachment-drop-active")).toBe(false); |
| 105 | + |
| 106 | + const textareaDrop = createDragEvent("drop", [], ["text/plain"]); |
| 107 | + textarea.dispatchEvent(textareaDrop); |
| 108 | + expect(textareaDrop.defaultPrevented).toBe(false); |
| 109 | + |
| 110 | + const shellDrop = createDragEvent("drop", [], ["text/uri-list"]); |
| 111 | + composer.dispatchEvent(shellDrop); |
| 112 | + expect(shellDrop.defaultPrevented).toBe(true); |
| 113 | + expect(replace).not.toHaveBeenCalled(); |
| 114 | + |
| 115 | + const checkbox = document.createElement("input"); |
| 116 | + checkbox.type = "checkbox"; |
| 117 | + composer.append(checkbox); |
| 118 | + const checkboxDrop = createDragEvent("drop", [], ["text/uri-list"]); |
| 119 | + checkbox.dispatchEvent(checkboxDrop); |
| 120 | + expect(checkboxDrop.defaultPrevented).toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it.each([ |
| 124 | + { submitting: true, messageLocked: false }, |
| 125 | + { submitting: false, messageLocked: true }, |
| 126 | + ])("ignores drops while the composer is disabled", (disabled) => { |
| 127 | + const { attachmentDraft, composer } = renderComposer(disabled); |
| 128 | + const replace = vi.spyOn(attachmentDraft, "replace"); |
| 129 | + const readAsDataUrl = vi.spyOn(FileReader.prototype, "readAsDataURL"); |
| 130 | + const file = new File(["image"], "pic.png", { type: "image/png" }); |
| 131 | + |
| 132 | + composer.dispatchEvent(createDragEvent("dragenter")); |
| 133 | + composer.dispatchEvent(createDragEvent("drop", [file])); |
| 134 | + |
| 135 | + expect(composer.hasAttribute("data-attachment-drop-active")).toBe(false); |
| 136 | + expect(readAsDataUrl).not.toHaveBeenCalled(); |
| 137 | + expect(replace).not.toHaveBeenCalled(); |
| 138 | + expect(attachmentDraft.attachments).toEqual([]); |
| 139 | + |
| 140 | + const textarea = composer.querySelector<HTMLTextAreaElement>("textarea"); |
| 141 | + if (!textarea) { |
| 142 | + throw new Error("Expected composer textarea"); |
| 143 | + } |
| 144 | + expect(textarea.disabled).toBe(true); |
| 145 | + const disabledTextareaDrop = createDragEvent("drop", [], ["text/uri-list"]); |
| 146 | + textarea.dispatchEvent(disabledTextareaDrop); |
| 147 | + expect(disabledTextareaDrop.defaultPrevented).toBe(true); |
| 148 | + }); |
| 149 | +}); |
0 commit comments