@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 190 lines 6.55 KB
Raw
1 import { EnvironmentId, GuestShareId, ThreadId, type ThreadGuestShare } from "@t3tools/contracts";
2 import * as DateTime from "effect/DateTime";
3 import { act, type ReactNode } from "react";
4 import { createRoot } from "react-dom/client";
5 import { expect, it, vi } from "vite-plus/test";
6
7 const { Slot, sharing, created } = vi.hoisted(() => ({
8 Slot: ({ children }: { children?: ReactNode }) => children,
9 created: {
10 share: undefined as ThreadGuestShare | null | undefined,
11 updatedAt: "1970-01-01T00:00:00.000Z",
12 command: vi.fn(),
13 },
14 sharing: { messages: [] as Array<{ status: "pending" | "dismissed" }> },
15 }));
16
17 vi.mock("react-native", () => ({
18 Platform: { OS: "ios" },
19 ScrollView: Slot,
20 View: Slot,
21 Share: {},
22 }));
23 vi.mock("@react-navigation/native", () => ({
24 useNavigation: () => ({ goBack: () => {} }),
25 usePreventRemove: () => {},
26 }));
27 vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) }));
28 vi.mock("@effect/atom-react", () => ({
29 useAtomValue: (atom: { value: unknown }, select?: (value: unknown) => unknown) =>
30 select
31 ? select(atom.value)
32 : atom.value === "thread"
33 ? { projection: { thread: { updatedAt: DateTime.makeUnsafe(created.updatedAt) } } }
34 : atom.value,
35 }));
36 vi.mock("../../state/server", () => ({
37 serverEnvironment: { createGuestShare: { permissionAtom: () => ({ value: true }) } },
38 environmentServerConfigsAtom: {},
39 }));
40 vi.mock("../../state/threads", () => ({
41 environmentThreadDetails: {
42 threadAtom: () => ({ value: "thread" }),
43 statusAtom: () => ({ value: "ready" }),
44 errorAtom: () => ({ value: null }),
45 },
46 }));
47 vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => created.command }));
48 vi.mock("./ThreadGuestShareBar", () => ({
49 useThreadGuestShare: () =>
50 created.share !== undefined
51 ? created.share
52 : {
53 guestName: null,
54 expiresAt: "1970-01-01T00:00:01.000Z",
55 messages: sharing.messages,
56 },
57 }));
58 vi.mock("../../components/ui/AppText", () => ({ AppText: Slot }));
59 vi.mock("../../components/ui/Button", () => ({
60 Button: ({
61 label,
62 disabled,
63 onPress,
64 }: {
65 label: string;
66 disabled?: boolean;
67 onPress?: () => void;
68 }) => (
69 <button disabled={disabled} onClick={onPress}>
70 {label}
71 </button>
72 ),
73 }));
74 vi.mock("../../components/ui/AppSymbol", () => ({ SymbolView: () => null }));
75 vi.mock("../../components/ui/AppBar", () => ({ AppBar: () => null }));
76 vi.mock("../../components/ui/SheetHeader", () => ({ SheetHeader: () => null }));
77 vi.mock("../../components/ui/ScreenContent", () => ({ ScreenContent: Slot }));
78 vi.mock("../../components/ui/Dialog", () => ({}));
79 vi.mock("../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
80 vi.mock("../../lib/copyTextWithHaptic", () => ({ tryCopyTextWithHaptic: () => true }));
81
82 import { ThreadGuestShareSheet } from "./ThreadGuestShareSheet";
83
84 it("stops treating a retained guest link as active when it expires", async () => {
85 sharing.messages = [];
86 created.share = undefined;
87 vi.useFakeTimers();
88 vi.setSystemTime(0);
89 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
90 const container = document.createElement("div");
91 const root = createRoot(container);
92 const renderSheet = () =>
93 root.render(
94 <ThreadGuestShareSheet
95 route={{
96 params: {
97 environmentId: EnvironmentId.make("test"),
98 threadId: ThreadId.make("thread"),
99 },
100 }}
101 />,
102 );
103 try {
104 await act(renderSheet);
105 expect(container.textContent).toContain("A guest link is active.");
106 expect(container.querySelector("button")?.textContent).toBe("Create new link");
107
108 await act(() => vi.advanceTimersByTimeAsync(1000));
109
110 expect(container.textContent).not.toContain("A guest link is active.");
111 expect(container.querySelector("button")?.textContent).toBe("Create link");
112 sharing.messages = [{ status: "pending" }];
113 await act(renderSheet);
114 expect(container.querySelector("button")?.disabled).toBe(true);
115 expect(container.textContent).toContain("Send or dismiss pending guest messages");
116
117 sharing.messages = [{ status: "dismissed" }];
118 await act(renderSheet);
119 expect(container.querySelector("button")?.disabled).toBe(false);
120 } finally {
121 act(() => root.unmount());
122 vi.useRealTimers();
123 vi.unstubAllGlobals();
124 }
125 });
126
127 it.each(["expired", "stopped", "replaced", "redeemed"] as const)(
128 "hides a created link after it is %s, while keeping it through projection catch-up",
129 async (change) => {
130 created.share = undefined;
131 created.updatedAt = "1970-01-01T00:00:00.000Z";
132 sharing.messages = [];
133 const share: ThreadGuestShare = {
134 id: GuestShareId.make("created"),
135 host: "env.t3.test",
136 createdAt: "1970-01-01T00:00:00.100Z",
137 expiresAt: "1970-01-01T00:00:01.000Z",
138 guestName: null,
139 messages: [],
140 };
141 const url = "https://env.t3.test/guest#invite";
142 created.command.mockResolvedValue({ _tag: "Success", value: { url, share } });
143 vi.useFakeTimers();
144 vi.setSystemTime(0);
145 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
146 const container = document.createElement("div");
147 const root = createRoot(container);
148 const renderSheet = () =>
149 root.render(
150 <ThreadGuestShareSheet
151 route={{
152 params: {
153 environmentId: EnvironmentId.make("test"),
154 threadId: ThreadId.make("thread"),
155 },
156 }}
157 />,
158 );
159 try {
160 await act(renderSheet);
161 await act(async () => container.querySelector("button")!.click());
162 expect(container.textContent).toContain(url);
163 created.share = share;
164 created.updatedAt = share.createdAt;
165 await act(renderSheet);
166 expect(container.textContent).toContain(url);
167 expect(container.textContent).toContain("Copy");
168 if (change === "expired") {
169 await act(() => vi.advanceTimersByTimeAsync(1000));
170 } else {
171 created.updatedAt = "1970-01-01T00:00:00.200Z";
172 created.share =
173 change === "redeemed"
174 ? { ...share, guestName: "Sam" }
175 : { ...share, id: GuestShareId.make("other") };
176 if (change === "stopped") created.share = null;
177 await act(renderSheet);
178 }
179 expect(container.textContent).not.toContain(url);
180 expect(container.textContent).not.toContain("Copy");
181 expect(container.textContent).not.toContain("Share");
182 } finally {
183 act(() => root.unmount());
184 vi.useRealTimers();
185 vi.unstubAllGlobals();
186 created.command.mockReset();
187 created.share = undefined;
188 }
189 },
190 );