@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 111 lines 4.42 KB
Raw
1 import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
2 import type { AnyRouter } from "@tanstack/react-router";
3 import { describe, expect, it, vi } from "vite-plus/test";
4 import { userEvent } from "vite-plus/test/browser";
5
6 import { render } from "../../../test/render";
7 import {
8 assistantCitationFromLocation,
9 assistantCitationNavigation,
10 } from "../../lib/assistantCitationNavigation";
11 import {
12 startMainAppLocationTracking,
13 useCloseAppModal,
14 useNavigateToMainApp,
15 } from "./mainAppLocation";
16
17 const THREAD_PATH = "/environment-one/thread-one";
18
19 const 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
31 function 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.
47 async 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
54 describe("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 expect(go).not.toHaveBeenCalled();
66 });
67
68 it("returns to the thread without re-opening the quote source it was opened at", async () => {
69 await using screen = await render(<ReturnControls />, { router: THREAD_PATH });
70 const router = screen.router!;
71 using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
72
73 const returnLocation = async (from: string, control: string) => {
74 await settled(router, router.navigate(assistantCitationNavigation(citation, null)));
75 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
76 await settled(router, router.navigate({ href: from }));
77 await userEvent.click(screen.getByRole("button", { name: control }));
78 await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
79 return router.state.location.href;
80 };
81
82 const closingUsage = await returnLocation("/usage", "Close modal");
83 const sidebarBack = await returnLocation("/pull-requests", "Back to app");
84
85 expect({ closingUsage, sidebarBack }).toEqual({
86 closingUsage: THREAD_PATH,
87 sidebarBack: THREAD_PATH,
88 });
89 });
90
91 it("leaves no stop on Settings for history back after it closes", async () => {
92 const otherThreadPath = "/environment-one/thread-two";
93 await using screen = await render(<ReturnControls />, { router: THREAD_PATH });
94 const router = screen.router!;
95 using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
96
97 await settled(router, router.navigate({ href: otherThreadPath }));
98 await settled(router, router.navigate({ href: "/settings/general" }));
99 await userEvent.click(screen.getByRole("button", { name: "Close modal" }));
100 await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
101
102 router.history.forward();
103 await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
104 router.history.back();
105 await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
106 router.history.forward();
107 await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
108 router.history.forward();
109 await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
110 });
111 });