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

assistantCitationNavigation.test.ts

214 lines8.4 KB

1import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
2import {
3 createMemoryHistory,
4 createRootRoute,
5 createRoute,
6 createRouter,
7} from "@tanstack/react-router";
8import { scopeThreadRef } from "@t3tools/client-runtime/environment";
9import { describe, expect, it } from "vite-plus/test";
10​
11import { selectActiveRightPanelSurface, useRightPanelStore } from "../rightPanelStore";
12import {
13 assistantCitationFromLocation,
14 assistantCitationNavigation,
15 revealSideChatCitation,
16 selectAssistantCitationHref,
17 selectAssistantCitationKey,
18} from "./assistantCitationNavigation";
19​
20const citation: AssistantCitation = {
21 version: 1,
22 environmentId: EnvironmentId.make("environment-one"),
23 threadId: ThreadId.make("thread-one"),
24 messageId: MessageId.make("assistant-one"),
25 text: "first line\n\tsecond line (with a #hash & spaces) 🚀",
26 start: 12,
27 end: 59,
28 prefix: "Before:\n",
29 suffix: "\nAfter.",
30};
31​
32function createCitationRouter(initialEntry = "/environment-one/thread-one") {
33 const root = createRootRoute();
34 const thread = createRoute({ getParentRoute: () => root, path: "/$environmentId/$threadId" });
35 return createRouter({
36 routeTree: root.addChildren([thread]),
37 history: createMemoryHistory({ initialEntries: [initialEntry] }),
38 });
40​
41describe("assistant citation navigation", () => {
42 it("preserves encoded quote whitespace through router navigation and reload", async () => {
43 const router = createCitationRouter();
44 await router.navigate(assistantCitationNavigation(citation, null));
45​
46 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
47 expect(selectAssistantCitationHref(router.state.location)).toBe(router.state.location.href);
48 expect(selectAssistantCitationKey(router.state.location)).toBe(
49 router.state.location.state.assistantCitationActivation,
50 );
51 let href = router.history.location.href;
52 for (let reload = 0; reload < 3; reload++) {
53 const reloaded = createCitationRouter(href);
54 await reloaded.load();
55 // A reload loses the activation, so the history key stands in.
56 expect(selectAssistantCitationKey(reloaded.state.location)).toBe(
57 reloaded.state.location.state.__TSR_key ?? null,
58 );
59 await reloaded.navigate({
60 to: "/$environmentId/$threadId",
61 params: { environmentId: citation.environmentId, threadId: citation.threadId },
62 hash: true,
63 replace: true,
64 });
65 href = reloaded.history.location.href;
66 expect(assistantCitationFromLocation(href)).toEqual(citation);
67 }
68 });
69​
70 it("reactivates an identical citation instead of deduplicating its navigation", async () => {
71 const router = createCitationRouter();
72 await router.navigate(assistantCitationNavigation(citation, null));
73 const previous = router.state.location;
74 await router.navigate(assistantCitationNavigation(citation, null));
75​
76 expect(router.state.location.href).toBe(previous.href);
77 expect(router.state.location.state.assistantCitationActivation).not.toBe(
78 previous.state.assistantCitationActivation,
79 );
80 expect(selectAssistantCitationHref(router.state.location)).toBe(previous.href);
81 expect(selectAssistantCitationKey(router.state.location)).not.toBe(
82 selectAssistantCitationKey(previous),
83 );
84 expect(router.history.length).toBe(3);
85 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
86 });
87​
88 it("selects nothing from a location that holds no quote", async () => {
89 const router = createCitationRouter();
90 await router.load();
91 const select = () => [
92 selectAssistantCitationHref(router.state.location),
93 selectAssistantCitationKey(router.state.location),
94 ];
95 expect(select()).toEqual([null, null]);
96 // Each navigation gets a new history key; without a quote it must not reach a reader.
97 for (const href of [
98 "/environment-one/thread-two",
99 "/environment-one/thread-two#section",
100 "/environment-one/thread-two#assistant-citation",
101 "/environment-one/thread-two?note=%23assistant-citation%3Dx",
102 ]) {
103 await router.navigate({ href });
104 expect(router.state.location.state.__TSR_key).toBeDefined();
105 expect(select()).toEqual([null, null]);
106 }
107 });
108​
109 it("opens a side chat citation as a tab of its parent thread", async () => {
110 resetRightPanels();
111 const router = createCitationRouter("/environment-one/thread-parent");
112 await router.navigate(assistantCitationNavigation(sideChatCitation, sideChatShell));
113 expect(router.state.location.pathname).toBe("/environment-one/thread-parent");
114 await revealAtRoute(router);
115​
116 expectSideChatRevealedOnParent(router);
117 });
118​
119 it("opens the side chat tab when arriving on the parent with its citation", async () => {
120 resetRightPanels();
121 const linked = createCitationRouter("/environment-one/thread-parent");
122 await linked.navigate(assistantCitationNavigation(sideChatCitation, sideChatShell));
123 const router = createCitationRouter(linked.history.location.href);
124 await router.load();
125 await revealAtRoute(router);
126​
127 expectSideChatRevealedOnParent(router);
128 });
129​
130 it("moves a citation activated before its side chat shell loaded onto the parent", async () => {
131 resetRightPanels();
132 const router = createCitationRouter("/environment-one/thread-parent");
133 await router.navigate(assistantCitationNavigation(sideChatCitation, null));
134 const activation = router.state.location.state.assistantCitationActivation;
135 expect(router.state.location.pathname).toBe("/environment-one/side-chat-one");
136​
137 await revealAtRoute(router);
138​
139 expectSideChatRevealedOnParent(router);
140 expect(router.state.location.state.assistantCitationActivation).toBe(activation);
141 expect(router.history.length).toBe(2);
142 });
143​
144 it("keeps quotes from forks on their own thread", () => {
145 const forkShell = { lineage: { ...sideChatShell.lineage, sideChat: false } };
146​
147 expect(assistantCitationNavigation(sideChatCitation, forkShell).params.threadId).toBe(
148 sideChatCitation.threadId,
149 );
150 });
151​
152 it("leaves an unrelated route alone", async () => {
153 resetRightPanels();
154 const router = createCitationRouter("/environment-one/thread-other");
155 await router.navigate({
156 ...assistantCitationNavigation(sideChatCitation, null),
157 params: { environmentId: citation.environmentId, threadId: ThreadId.make("thread-other") },
158 });
159 await revealAtRoute(router);
160​
161 expect(router.state.location.pathname).toBe("/environment-one/thread-other");
162 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
163 });
164​
165 it("ignores a reveal for a side chat the location no longer quotes", async () => {
166 resetRightPanels();
167 const router = createCitationRouter("/environment-one/thread-parent");
168 await router.navigate(assistantCitationNavigation(sideChatCitation, sideChatShell));
169 await revealAtRoute(router, ThreadId.make("side-chat-two"));
170​
171 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
172 });
173});
174​
175const parentThreadId = ThreadId.make("thread-parent");
176const sideChatCitation = { ...citation, threadId: ThreadId.make("side-chat-one") };
177const sideChatShell = {
178 lineage: {
179 parentThreadId,
180 relationshipToParent: "fork" as const,
181 rootThreadId: parentThreadId,
182 sideChat: true,
183 },
184};
185​
186function resetRightPanels() {
187 useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} });
189​
190/** What the route-level ChatView does once the side chat shell is known. */
191function revealAtRoute(
192 router: ReturnType<typeof createCitationRouter>,
193 sideChatThreadId = sideChatCitation.threadId,
194) {
195 const { pathname, state } = router.state.location;
196 return revealSideChatCitation(
197 router,
198 scopeThreadRef(citation.environmentId, ThreadId.make(pathname.split("/").at(-1) ?? "")),
199 sideChatThreadId,
200 parentThreadId,
201 state.assistantCitationActivation ?? state.__TSR_key ?? "",
202 );
204​
205function expectSideChatRevealedOnParent(router: ReturnType<typeof createCitationRouter>) {
206 expect(router.state.location.pathname).toBe("/environment-one/thread-parent");
207 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(sideChatCitation);
208 expect(
209 selectActiveRightPanelSurface(
210 useRightPanelStore.getState().byThreadKey,
211 scopeThreadRef(citation.environmentId, parentThreadId),
212 ),
213 ).toMatchObject({ kind: "thread", threadId: sideChatCitation.threadId });