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

linkedThreadView.test.ts

103 lines3.4 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5import { linkedThreadSurface } from "../linkedThreadSurface";
6import { selectThreadRightPanelState, useRightPanelStore } from "../rightPanelStore";
7import { useConnectLinkedEnvironment } from "./linkedThreadView";
8​
9const pairing = vi.hoisted(() => ({
10 navigate: vi.fn(),
11 finish: vi.fn(),
12}));
13​
14vi.mock("@effect/atom-react", () => ({
15 useAtom: () => [{ phase: "idle" }, vi.fn()],
16 useAtomSet: () => vi.fn(),
17 useAtomValue: () => true,
18}));
19vi.mock("@tanstack/react-router", () => ({
20 useNavigate: () => pairing.navigate,
21 useRouter: () => ({ state: { location: { href: "/env-1/thread-A" } } }),
22}));
23vi.mock("../connection/onboarding", () => ({
24 connectPairing: "connect",
25 reachesEnvironment: "reaches",
26}));
27vi.mock("./peerLinks", () => ({
28 peerLinkEnvironment: { pairClient: { permissionAtom: vi.fn() } },
29}));
30vi.mock("./use-atom-command", () => ({
31 useAtomCommand: (command: unknown) => {
32 if (command === "connect") return pairing.finish;
33 if (command === "reaches") return async () => ({ _tag: "Success", value: true });
34 return async () => ({
35 _tag: "Success",
36 value: {
37 type: "credential",
38 label: "Remote",
39 urls: ["http://127.0.0.1:1234"],
40 credential: "fixture",
41 },
42 });
43 },
44}));
45vi.mock("./environments", () => ({}));
46vi.mock("./query", () => ({}));
47vi.mock("../lib/sideChatPane", () => ({}));
48​
49const ref = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-A"));
50​
51describe("useConnectLinkedEnvironment", () => {
52 it.each([
53 {
54 choice: "switches tabs",
55 act: () => useRightPanelStore.getState().activateSurface(ref, "diff"),
56 navigations: 0,
57 isOpen: true,
58 },
59 {
60 choice: "hides the panel",
61 act: () => useRightPanelStore.getState().close(ref),
62 navigations: 0,
63 isOpen: false,
64 },
65 { choice: "keeps the linked tab selected", navigations: 1, isOpen: true },
66 ])(
67 "respects the panel when the user $choice during pairing",
68 async ({ act, navigations, isOpen }) => {
69 const surface = linkedThreadSurface({
70 viaEnvironmentId: ref.environmentId,
71 environmentId: EnvironmentId.make("env-2"),
72 threadId: ThreadId.make("remote"),
73 label: "Remote",
74 title: null,
75 });
76 useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} });
77 const store = useRightPanelStore.getState();
78 store.open(ref, "diff");
79 store.openLinkedThread(ref, surface);
80 const requested = Promise.withResolvers<void>();
81 const response = Promise.withResolvers<{ _tag: "Success" }>();
82 pairing.navigate.mockClear();
83 pairing.finish.mockImplementationOnce(() => {
84 requested.resolve();
85 return response.promise;
86 });
87​
88 const result = useConnectLinkedEnvironment(ref, surface).connect();
89 await requested.promise;
90 act?.();
91 response.resolve({ _tag: "Success" });
92​
93 expect(await result).toEqual({ type: "connected" });
94 expect(pairing.navigate).toHaveBeenCalledTimes(navigations);
95 expect(
96 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, ref),
97 ).toMatchObject({
98 activeSurfaceId: "diff",
99 isOpen,
100 });
101 },
102 );
103});