Repository navigation
fix(web): an unsaved citation comment survives a question or approval #19350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
apps/web/src/components/ComposerPromptEditorTiptap.citationDraft.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // @vitest-environment jsdom | ||
|
|
||
| import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts"; | ||
| import { | ||
| serializeAssistantCitation, | ||
| withAssistantCitationComment, | ||
| } from "@t3tools/shared/assistantCitations"; | ||
| import { act, createRef, useState, type ReactNode } from "react"; | ||
| import { createRoot, type Root } from "react-dom/client"; | ||
| import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection"; | ||
|
|
||
| vi.mock("./chat/AssistantCitationSource", () => ({ | ||
| observeAssistantCitationCommentSource: () => () => {}, | ||
| })); | ||
| vi.mock("@tanstack/react-router", () => ({ | ||
| useNavigate: () => () => {}, | ||
| Link: ({ children }: { children: ReactNode }) => <a>{children}</a>, | ||
| })); | ||
| // Keep the real chip and comment editor while replacing floating layers. | ||
| vi.mock("./ui/popover", () => ({ | ||
| Popover: ({ children }: { children: ReactNode }) => <>{children}</>, | ||
| PopoverTrigger: ({ "aria-label": label }: { "aria-label": string }) => ( | ||
| <button aria-label={label} /> | ||
| ), | ||
| PopoverPopup: ({ children }: { children: ReactNode }) => <>{children}</>, | ||
| PopoverClose: ({ children }: { children: ReactNode }) => <>{children}</>, | ||
| })); | ||
|
|
||
| import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor"; | ||
|
|
||
| const citation = { | ||
| version: 1 as const, | ||
| environmentId: EnvironmentId.make("environment"), | ||
| threadId: ThreadId.make("thread"), | ||
| messageId: MessageId.make("source"), | ||
| text: "hello", | ||
| start: 0, | ||
| end: 5, | ||
| prefix: "", | ||
| suffix: "", | ||
| }; | ||
| const promptBefore = `See ${serializeAssistantCitation(citation)} `; | ||
|
|
||
| let root: Root; | ||
| let container: HTMLDivElement; | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); | ||
| container = document.createElement("div"); | ||
| document.body.append(container); | ||
| root = createRoot(container); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await act(async () => root.unmount()); | ||
| container.remove(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| // Mirrors ChatComposer: a pending question shows its answer in the same editor. | ||
| function Composer({ | ||
| question, | ||
| editorRef, | ||
| }: { | ||
| question: boolean; | ||
| editorRef: React.RefObject<ComposerPromptEditorHandle | null>; | ||
| }) { | ||
| const [prompt, setPrompt] = useState(promptBefore); | ||
| const [answer, setAnswer] = useState(""); | ||
| return ( | ||
| <> | ||
| <output data-prompt={prompt} data-answer={answer} /> | ||
| <ComposerPromptEditor | ||
| editorRef={editorRef} | ||
| value={question ? answer : prompt} | ||
| cursor={0} | ||
| contextRecords={new Map()} | ||
| skills={[]} | ||
| disabled={false} | ||
| placeholder="" | ||
| onChange={(next) => (question ? setAnswer(next) : setPrompt(next))} | ||
| onCitationDraftSave={(replacedValue, savedValue) => { | ||
| if (replacedValue === prompt) setPrompt(savedValue); | ||
| }} | ||
| onPaste={() => {}} | ||
| /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| it("keeps an unsaved citation comment when a question takes the composer", async () => { | ||
| const editorRef = createRef<ComposerPromptEditorHandle>(); | ||
| const drafts = () => container.querySelector("output")?.dataset; | ||
|
|
||
| await act(async () => root.render(<Composer question={false} editorRef={editorRef} />)); | ||
| await act(async () => | ||
| editorRef.current?.requestCitationComment({ | ||
| previousValue: "See ", | ||
| value: promptBefore, | ||
| citationStart: "See ".length, | ||
| sourceAnchor: {} as AssistantCitationSourceAnchor, | ||
| insertedSpaces: { before: false, after: true }, | ||
| }), | ||
| ); | ||
| const textarea = container.querySelector("textarea"); | ||
| if (!textarea) throw new Error("Comment editor did not open"); | ||
| await act(async () => { | ||
| const setValue = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")?.set; | ||
| setValue?.call(textarea, "check this"); | ||
| textarea.dispatchEvent(new Event("input", { bubbles: true })); | ||
| }); | ||
|
|
||
| await act(async () => root.render(<Composer question editorRef={editorRef} />)); | ||
| expect(drafts()?.answer).toBe(""); | ||
| await act(async () => root.render(<Composer question={false} editorRef={editorRef} />)); | ||
|
|
||
| expect(drafts()?.prompt).toBe( | ||
| `See ${serializeAssistantCitation(withAssistantCitationComment(citation, "check this"))} `, | ||
| ); | ||
| expect(container.querySelector('[aria-label="Edit citation comment"]')).not.toBeNull(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Save the draft before a literal-mode remount.
A pending question with
initialAnswerchangesliteralText.ComposerPromptEditorTiptapthen remounts its inner editor. The layout effect that callssaveCitationDraftdoes not run in the unmounted editor. The citation comment is lost instead of being restored to the prompt.Persist the draft during teardown before the remount, or avoid remounting while a citation comment is open. Add a test that switches to
literalText.🤖 Prompt for AI Agents