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("closes Usage without leaving the current document when the app started with history", async () => {
56 await using screen = await render(<ReturnControls />, { router: "/" });
57 const router = screen.router!;
58 await settled(router, router.navigate({ href: THREAD_PATH }));
59 using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
60 using go = vi.spyOn(router.history, "go");
61
62 await settled(router, router.navigate({ href: "/usage" }));
63 await userEvent.click(screen.getByRole("button", { name: "Close modal" }));
64 await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
65 // Only the thread's own entry, never the entry before this document.
66 expect(go.mock.calls).toEqual([[-1]]);
67 });
68
69 it("closes Settings on the document's first entry without adding a back stop", async () => {
70 await using screen = await render(<ReturnControls />, { router: "/" });
71 const router = screen.router!;
72 await settled(router, router.navigate({ href: THREAD_PATH }));
73 using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
74
75 await settled(router, router.navigate({ href: "/settings/general" }));
76 await userEvent.click(screen.getByRole("button", { name: "Close modal" }));
77 await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
78
79 router.history.forward();
80 await vi.waitFor(() => expect(router.history.location.pathname).toBe(THREAD_PATH));
81 await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
82 router.history.back();
83 await vi.waitFor(() => expect(router.state.location.pathname).toBe("/"));
84 });
85
86 it("returns to the thread without re-opening the quote source it was opened at", async () => {
87 await using screen = await render(<ReturnControls />, { router: THREAD_PATH });
88 const router = screen.router!;
89 using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
90
91 const returnLocation = async (from: string, control: string) => {
92 await settled(router, router.navigate(assistantCitationNavigation(citation, null)));
93 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
94 await settled(router, router.navigate({ href: from }));
95 await userEvent.click(screen.getByRole("button", { name: control }));
96 await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
97 return router.state.location.href;
98 };
99
100 const closingUsage = await returnLocation("/usage", "Close modal");
101 const sidebarBack = await returnLocation("/pull-requests", "Back to app");
102
103 expect({ closingUsage, sidebarBack }).toEqual({
104 closingUsage: THREAD_PATH,
105 sidebarBack: THREAD_PATH,
106 });
107 });
108
109 it("leaves no stop on Settings for history back after it closes", async () => {
110 const otherThreadPath = "/environment-one/thread-two";
111 await using screen = await render(<ReturnControls />, { router: THREAD_PATH });
112 const router = screen.router!;
113 using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
114
115 await settled(router, router.navigate({ href: otherThreadPath }));
116 await settled(router, router.navigate({ href: "/settings/general" }));
117 await userEvent.click(screen.getByRole("button", { name: "Close modal" }));
118 await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
119
120 router.history.forward();
121 await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
122 router.history.back();
123 await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
124 router.history.forward();
125 await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
126 router.history.forward();
127 await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
128 });
129});