|
| 1 | +import { createMemoryHistory } from "@tanstack/react-router"; |
| 2 | +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 3 | +import { MessageId, ThreadId } from "@t3tools/contracts"; |
| 4 | +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; |
| 5 | +import { |
| 6 | + collectAssistantCitationComments, |
| 7 | + serializeAssistantCitation, |
| 8 | +} from "@t3tools/shared/assistantCitations"; |
| 9 | +import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; |
| 10 | +import type * as Context from "effect/Context"; |
| 11 | +import { expect, it, vi } from "vite-plus/test"; |
| 12 | +import { page, userEvent } from "vite-plus/test/browser"; |
| 13 | + |
| 14 | +import { AppRoot } from "~/AppRoot"; |
| 15 | +// The route loads ChatView lazily; importing it here keeps its first transform out of the timeout. |
| 16 | +import "~/components/ChatView"; |
| 17 | +import { |
| 18 | + COMPOSER_MENTION_DRAG_TYPE, |
| 19 | + composerMentionFromTreePath, |
| 20 | +} from "~/components/chat/composerMentionDrag"; |
| 21 | +import { useComposerDraftStore } from "~/composerDraftStore"; |
| 22 | +import { __setClientSettingsForTests } from "~/hooks/useSettings"; |
| 23 | +import { getRouter } from "~/router"; |
| 24 | +import { |
| 25 | + ENVIRONMENT_ID, |
| 26 | + installFakeEnvironment, |
| 27 | + threadWithHistory, |
| 28 | + type FakeDriver, |
| 29 | +} from "../../test/fakeEnvironment"; |
| 30 | +import { render } from "../../test/render"; |
| 31 | + |
| 32 | +// The socket is the only fake layer; see test/appScenarios.renders.tsx. The app keeps its |
| 33 | +// connection for the whole file, so the threads served here are this file's only fixture. |
| 34 | +vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => { |
| 35 | + const [actual, { fakeDriver }, Layer] = await Promise.all([ |
| 36 | + importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(), |
| 37 | + import("../../test/fakeEnvironment"), |
| 38 | + import("effect/Layer"), |
| 39 | + ]); |
| 40 | + return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) }; |
| 41 | +}); |
| 42 | + |
| 43 | +vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({ |
| 44 | + ...(await original()), |
| 45 | + trackRpcRequestSent: () => {}, |
| 46 | +})); |
| 47 | + |
| 48 | +it("a citation comment's @ menu and drops put their chips in the comment", async () => { |
| 49 | + using _environment = installFakeEnvironment([ |
| 50 | + threadWithHistory("alpha", 1), |
| 51 | + threadWithHistory("beta", 1), |
| 52 | + ]); |
| 53 | + __setClientSettingsForTests({ |
| 54 | + ...DEFAULT_CLIENT_SETTINGS, |
| 55 | + onboardingCompletedAt: "2026-01-01T00:00:00.000Z", |
| 56 | + }); |
| 57 | + localStorage.removeItem("t3code:citation-comment-submit:v1"); |
| 58 | + const alpha = ThreadId.make("alpha"); |
| 59 | + const citation = { |
| 60 | + version: 1 as const, |
| 61 | + environmentId: ENVIRONMENT_ID, |
| 62 | + threadId: alpha, |
| 63 | + messageId: MessageId.make("alpha-assistant-0"), |
| 64 | + text: "Answer 0", |
| 65 | + start: 0, |
| 66 | + end: 8, |
| 67 | + prefix: "", |
| 68 | + suffix: "", |
| 69 | + }; |
| 70 | + const threadRef = scopeThreadRef(ENVIRONMENT_ID, alpha); |
| 71 | + useComposerDraftStore |
| 72 | + .getState() |
| 73 | + .setPrompt(threadRef, `See ${serializeAssistantCitation(citation)} `); |
| 74 | + |
| 75 | + const router = getRouter(createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/alpha`] })); |
| 76 | + await router.load(); |
| 77 | + await using _app = await render(<AppRoot router={router} />, { |
| 78 | + // AppRoot brings its own router. |
| 79 | + router: false, |
| 80 | + }); |
| 81 | + await page.getByLabelText("Add comment to citation").click(); |
| 82 | + const comment = page.getByLabelText("Comment on selected text"); |
| 83 | + await expect.element(comment).toHaveFocus(); |
| 84 | + await userEvent.keyboard("@beta"); |
| 85 | + // The fake server never answers a file search, so the menu waits on Files. |
| 86 | + await page.getByRole("tab", { name: "Chats" }).click(); |
| 87 | + await expect.element(page.getByRole("option", { name: /Thread beta/ })).toBeVisible(); |
| 88 | + // Enter picks from the menu while it is open, and saves the comment once it closed. |
| 89 | + await userEvent.keyboard("{Enter}"); |
| 90 | + await expect.element(comment.getByText("Thread beta")).toBeVisible(); |
| 91 | + // The chat's own drop target would put it in the prompt. |
| 92 | + const dropped = new DataTransfer(); |
| 93 | + const png = Uint8Array.from( |
| 94 | + atob( |
| 95 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", |
| 96 | + ), |
| 97 | + (char) => char.charCodeAt(0), |
| 98 | + ); |
| 99 | + dropped.items.add(new File([png], "dot.png", { type: "image/png" })); |
| 100 | + for (const type of ["dragenter", "dragover", "drop"]) { |
| 101 | + comment |
| 102 | + .element() |
| 103 | + .dispatchEvent( |
| 104 | + new DragEvent(type, { bubbles: true, cancelable: true, dataTransfer: dropped }), |
| 105 | + ); |
| 106 | + } |
| 107 | + await expect.element(comment.getByText("dot.png")).toBeVisible(); |
| 108 | + const mention = new DataTransfer(); |
| 109 | + mention.setData(COMPOSER_MENTION_DRAG_TYPE, composerMentionFromTreePath("src/tree.ts") ?? ""); |
| 110 | + for (const type of ["dragenter", "dragover", "drop"]) { |
| 111 | + comment |
| 112 | + .element() |
| 113 | + .dispatchEvent( |
| 114 | + new DragEvent(type, { bubbles: true, cancelable: true, dataTransfer: mention }), |
| 115 | + ); |
| 116 | + } |
| 117 | + await expect.element(comment.getByText("tree.ts")).toBeVisible(); |
| 118 | + await userEvent.keyboard("{Enter}"); |
| 119 | + |
| 120 | + const prompt = useComposerDraftStore.getState().getComposerDraft(threadRef)?.prompt ?? ""; |
| 121 | + const [saved] = collectAssistantCitationComments(prompt); |
| 122 | + expect(saved).toContain( |
| 123 | + threadContextRecord(scopeThreadRef(ENVIRONMENT_ID, ThreadId.make("beta")), "").contextId, |
| 124 | + ); |
| 125 | + expect(useComposerDraftStore.getState().getComposerDraft(threadRef)?.images).toEqual([ |
| 126 | + expect.objectContaining({ name: "dot.png" }), |
| 127 | + ]); |
| 128 | + // The chips went into the comment, not beside the citation in the prompt. |
| 129 | + expect(prompt.startsWith("See [")).toBe(true); |
| 130 | + expect(prompt.endsWith(") ")).toBe(true); |
| 131 | +}); |