Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AssistantCitationChip.test.tsx

150 lines4.8 KB

1import {
2 ASSISTANT_CITATION_MAX_COMMENT_LENGTH,
3 EnvironmentId,
4 MessageId,
5 ThreadId,
6} from "@t3tools/contracts";
7import { act, useState, type ReactNode } from "react";
8import { create, type ReactTestRenderer } from "react-test-renderer";
9import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
10​
11import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
12​
13const mocks = vi.hoisted(() => ({ observeSource: vi.fn(), dispose: vi.fn() }));
14vi.mock("./AssistantCitationSource", () => ({
15 observeAssistantCitationCommentSource: mocks.observeSource,
16}));
17vi.mock("@tanstack/react-router", () => ({
18 useNavigate: () => vi.fn(),
19 Link: ({ children }: { children: ReactNode }) => <a>{children}</a>,
20}));
21vi.mock("../ui/popover", () => ({
22 Popover: ({ children }: { children: ReactNode }) => <>{children}</>,
23 PopoverTrigger: ({ children }: { children: ReactNode }) => <button>{children}</button>,
24 PopoverPopup: ({ children }: { children: ReactNode }) => <>{children}</>,
25}));
26vi.mock("../ui/button", () => ({
27 Button: (props: React.ComponentProps<"button">) => <button {...props} />,
28}));
29​
30import { PopoverPopup } from "../ui/popover";
31import { AssistantCitationChip } from "./AssistantCitationChip";
32​
33const citation = {
34 version: 1 as const,
35 environmentId: EnvironmentId.make("environment"),
36 threadId: ThreadId.make("thread"),
37 messageId: MessageId.make("source"),
38 text: "hello",
39 start: 0,
40 end: 5,
41 prefix: "",
42 suffix: "",
43};
44const sourceAnchor = {
45 source: {},
46 range: {},
47 viewport: {},
48} as AssistantCitationSourceAnchor;
49​
50let renderer: ReactTestRenderer;
51​
52function mount(onSave = vi.fn(() => true)) {
53 function Composer() {
54 const [open, setOpen] = useState(true);
55 return (
56 <AssistantCitationChip
57 citation={citation}
58 composer
59 commentEditor={{ open, sourceAnchor, onOpenChange: setOpen, onSave }}
60 />
61 );
62 }
63 act(() => {
64 renderer = create(<Composer />);
65 });
66 return onSave;
68​
69function typeComment(value: string) {
70 act(() => renderer.root.findByType("textarea").props.onChange({ currentTarget: { value } }));
72​
73function removeSource() {
74 const { onUnavailable } = mocks.observeSource.mock.lastCall![0];
75 act(() => onUnavailable());
77​
78function clickButton(label: string) {
79 act(() =>
80 renderer.root
81 .findAllByType("button")
82 .find((button) => button.props.children === label)!
83 .props.onClick(),
84 );
86​
87beforeEach(() => {
88 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
89 mocks.observeSource.mockReset().mockReturnValue(mocks.dispose);
90 mocks.dispose.mockClear();
91});
92​
93afterEach(() => {
94 act(() => renderer?.unmount());
95 vi.unstubAllGlobals();
96});
97​
98describe("citation comment source disappearance", () => {
99 it("preserves an over-length draft at the composer until it can be shortened and saved", () => {
100 const onSave = mount();
101 const draft = "x".repeat(ASSISTANT_CITATION_MAX_COMMENT_LENGTH + 1);
102 typeComment(draft);
103 removeSource();
104​
105 expect(renderer.root.findByType("textarea").props.value).toBe(draft);
106 expect(renderer.root.findByType("textarea").props["aria-invalid"]).toBe(true);
107 expect(renderer.root.findByType(PopoverPopup).props.anchor).toBeUndefined();
108 expect(renderer.root.findByType(PopoverPopup).props.side).toBe("top");
109 expect(onSave).not.toHaveBeenCalled();
110 expect(mocks.dispose).toHaveBeenCalled();
111 expect(mocks.observeSource).toHaveBeenCalledTimes(1);
112​
113 typeComment("shortened comment");
114 clickButton("Save");
115 expect(onSave).toHaveBeenCalledWith("shortened comment");
116 expect(renderer.root.findAllByType("textarea")).toHaveLength(0);
117 });
118​
119 it("preserves a rejected save and allows a later retry", () => {
120 const onSave = mount(vi.fn(() => false));
121 typeComment("keep this draft");
122 removeSource();
123​
124 expect(onSave).toHaveBeenCalledWith("keep this draft");
125 expect(renderer.root.findByType("textarea").props.value).toBe("keep this draft");
126 expect(renderer.root.findByType(PopoverPopup).props.anchor).toBeUndefined();
127 onSave.mockReturnValue(true);
128 clickButton("Save");
129 expect(onSave).toHaveBeenLastCalledWith("keep this draft");
130 expect(renderer.root.findAllByType("textarea")).toHaveLength(0);
131 });
132​
133 it("still allows explicit cancellation after source disappearance", () => {
134 const onSave = mount(vi.fn(() => false));
135 typeComment("discard this draft");
136 removeSource();
137 onSave.mockClear();
138 clickButton("Cancel");
139 expect(onSave).not.toHaveBeenCalled();
140 expect(renderer.root.findAllByType("textarea")).toHaveLength(0);
141 });
142​
143 it("saves and closes when the source disappears with a valid draft", () => {
144 const onSave = mount();
145 typeComment("saved comment");
146 removeSource();
147 expect(onSave).toHaveBeenCalledWith("saved comment");
148 expect(renderer.root.findAllByType("textarea")).toHaveLength(0);
149 });
150});