Open History to see the last commit
1import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
2import type { AnyRouter } from "@tanstack/react-router";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { userEvent } from "vite-plus/test/browser";
5
6import { render } from "../../../test/render";
7import {
8 assistantCitationFromLocation,
9 assistantCitationNavigation,
10} from "../../lib/assistantCitationNavigation";
11import {
12 startMainAppLocationTracking,
13 useCloseAppModal,
14 useNavigateToMainApp,
15} from "./mainAppLocation";
16
17const THREAD_PATH = "/environment-one/thread-one";
18
19const citation: AssistantCitation = {
20 version: 1,
21 environmentId: EnvironmentId.make("environment-one"),
22 threadId: ThreadId.make("thread-one"),
23 messageId: MessageId.make("assistant-one"),
24 text: "quoted reply",
25 start: 0,
26 end: 12,
27 prefix: "",
28 suffix: "",
29};
30
31function ReturnControls() {
32 const closeAppModal = useCloseAppModal();
33 const navigateToMainApp = useNavigateToMainApp();
34 return (
35 <>
36 <button type="button" onClick={() => void closeAppModal()}>
37 Close modal
38 </button>
39 <button type="button" onClick={() => void navigateToMainApp()}>
40 Back to app
41 </button>
42 </>
43 );
44}
45
46// The tracker records on onResolved, which the router emits only once React commits the page.
47async function settled(router: AnyRouter, navigation: Promise<void>) {
48 await navigation;
49 await vi.waitFor(() =>
50 expect(router.state.resolvedLocation?.href).toBe(router.state.location.href),
51 );
52}
53
54describe("main app return location", () => {
55 it("returns to the thread without re-opening the quote source it was opened at", async () => {
56 await using screen = await render(<ReturnControls />, { router: THREAD_PATH });
57 const router = screen.router!;
58 using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
59
60 const returnLocation = async (from: string, control: string) => {
61 await settled(router, router.navigate(assistantCitationNavigation(citation, null)));
62 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
63 await settled(router, router.navigate({ href: from }));
64 await userEvent.click(screen.getByRole("button", { name: control }));
65 await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
66 return router.state.location.href;
67 };
68
69 const closingUsage = await returnLocation("/usage", "Close modal");
70 const sidebarBack = await returnLocation("/pull-requests", "Back to app");
71
72 expect({ closingUsage, sidebarBack }).toEqual({
73 closingUsage: THREAD_PATH,
74 sidebarBack: THREAD_PATH,
75 });
76 });
77});