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

appLinking.test.ts

227 lines7.6 KB

1import * as NodeModule from "node:module";
2import { describe, expect, it } from "vite-plus/test";
3​
4import type { NavigationState } from "@react-navigation/native";
5import {
6 FILE_PATH_LINK_PARAM,
7 findLinkedRoute,
8 findStackRoute,
9 restoreDraftLinkState,
10 shouldHandleAppLink,
11 updateLinkedRoute,
12} from "./appLinking";
13​
14describe("shouldHandleAppLink", () => {
15 it.each(["t3code://", "t3code:///", "t3code-dev://", "t3code-preview://"])(
16 "ignores scheme-only URL %s",
17 (url) => {
18 expect(shouldHandleAppLink(url, true)).toBe(false);
19 },
20 );
21​
22 it.each([
23 "t3code://threads/env-1/thread-1",
24 "t3code-dev://threads/env-1/thread-1/files/packages/expo-development-client.ts",
25 "t3code://pair?pairingUrl=x",
26 "t3code-dev://settings/usage?tab=limits",
27 "https://app.t3.codes/threads/env/thread?prompt=Fix%20expo-development-client%20and%20t3code://expo-sharing",
28 ])("handles path-bearing URL %s", (url) => {
29 expect(shouldHandleAppLink(url, true)).toBe(true);
30 });
31​
32 it.each(["t3code://expo-development-client/?url=x", "t3code://expo-sharing/anything"])(
33 "ignores lifecycle URL %s",
34 (url) => {
35 expect(shouldHandleAppLink(url, true)).toBe(false);
36 },
37 );
38});
39​
40describe("file deep links", () => {
41 const { getStateFromPath } = NodeModule.createRequire(
42 NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
43 )("@react-navigation/core") as {
44 readonly getStateFromPath: typeof import("@react-navigation/native").getStateFromPath;
45 };
46 const screens = {
47 ThreadFiles: "threads/:environmentId/:threadId/files",
48 ThreadFile: `threads/:environmentId/:threadId/files/${FILE_PATH_LINK_PARAM}`,
49 NotFound: "*",
50 };
51​
52 it.each(["package.json", "packages/expo-development-client.ts", "a%20b/c.ts"])(
53 "opens the file %s",
54 (path) => {
55 expect(getStateFromPath(`threads/env-1/thread-1/files/${path}`, { screens })?.routes).toEqual(
56 [
57 expect.objectContaining({
58 name: "ThreadFile",
59 params: {
60 environmentId: "env-1",
61 threadId: "thread-1",
62 path: decodeURIComponent(path),
63 },
64 }),
65 ],
66 );
67 },
68 );
69});
70​
71describe("cloud-only deep links", () => {
72 it.each([
73 "t3code://connect-onboarding",
74 "t3code-dev://connect-onboarding/?source=x",
75 "t3code://settings/waitlist",
76 "t3code://settings/auth",
77 "exp://192.168.1.2:8081/--/settings/auth",
78 ])("ignores %s without cloud config", (url) => {
79 expect(shouldHandleAppLink(url, false)).toBe(false);
80 expect(shouldHandleAppLink(url, true)).toBe(true);
81 });
82​
83 it.each([
84 "t3code://settings",
85 "t3code://settings/authors",
86 "t3code://pair?next=settings/auth",
87 "t3code://threads/env/thread/files/scripts/connect-onboarding",
88 "t3code://new/draft/files/settings/auth",
89 "exp://192.168.1.2:8081/--/threads/env/thread/files/settings/waitlist",
90 ])("still handles %s without cloud config", (url) => {
91 expect(shouldHandleAppLink(url, false)).toBe(true);
92 });
93});
94​
95describe("draft deep links", () => {
96 it("opens the old draft URL on the thread screen with its draft identity", () => {
97 const draft = { projectId: "project-1", environmentId: "env-1", draftId: "draft-1" };
98 expect(
99 restoreDraftLinkState(
100 { routes: [{ name: "Home" }, { name: "Thread", params: draft }] },
101 "/new/draft?draftId=draft-1",
102 ),
103 ).toEqual({
104 index: 1,
105 routes: [{ name: "Home" }, { name: "Thread", params: { draft } }],
106 });
107 });
108​
109 it.each([
110 ["environment", "NewTaskEnvironment"],
111 ["branch", "NewTaskBranch"],
112 ["repositories", "NewTaskRepositories"],
113 ["files", "NewTaskFiles"],
114 ["files/src/index.ts", "NewTaskFile"],
115 ["attachments/attachment-1", "NewTaskAttachment"],
116 ["settings", "NewTaskThreadSettings"],
117 ])("restores a draft beneath its %s screen", (path, name) => {
118 const params = { environmentId: "env-1", projectId: "project-1" };
119 const target = { name, params };
120 expect(
121 restoreDraftLinkState({ routes: [{ name: "Home" }, target] }, `/new/draft/${path}`),
122 ).toEqual({
123 index: 2,
124 routes: [{ name: "Home" }, { name: "Thread", params: { draft: params } }, target],
125 });
126 });
127​
128 it.each(["/new/draft", "/new/draft/branch"])("drops the checkout a %s link names", (path) => {
129 const project = { environmentId: "env-1", projectId: "project-1" };
130 const linked = { ...project, branch: "other", worktreePath: "/elsewhere" };
131 const name = path === "/new/draft" ? "Thread" : "NewTaskBranch";
132 expect(restoreDraftLinkState({ routes: [{ name, params: linked }] }, path)?.routes[0]).toEqual({
133 name: "Thread",
134 params: { draft: project },
135 });
136 });
137​
138 it("leaves live-thread and project-selection links unchanged", () => {
139 const state = { routes: [{ name: "Thread", params: { threadId: "thread-1" } }] };
140 expect(restoreDraftLinkState(state, "/threads/env-1/thread-1")).toBe(state);
141 expect(restoreDraftLinkState(state, "/new")).toBe(state);
142 expect(restoreDraftLinkState(undefined, "/new/draft")).toBeUndefined();
143 });
144});
145​
146describe("nested link routes", () => {
147 const settingsLink = {
148 routes: [
149 { name: "Home" },
150 {
151 name: "SettingsSheet",
152 state: {
153 routes: [
154 { name: "Settings" },
155 { name: "SettingsEnvironmentNew", params: { pairingUrl: "x", autoConnect: "1" } },
156 ],
157 },
158 },
159 ],
160 };
161​
162 it("finds a route the link opens inside a nested navigator", () => {
163 expect(findLinkedRoute(settingsLink, ["SettingsEnvironmentNew"])?.params).toEqual({
164 pairingUrl: "x",
165 autoConnect: "1",
166 });
167 expect(findLinkedRoute(settingsLink, ["ConnectionsNew"])).toBeUndefined();
168 });
169​
170 it("rewrites a nested route without touching the rest of the state", () => {
171 const next = updateLinkedRoute(settingsLink, ["SettingsEnvironmentNew"], (route) => ({
172 ...route,
173 params: { pairingUrl: "x" },
174 }));
175 expect(findLinkedRoute(next, ["SettingsEnvironmentNew"])?.params).toEqual({ pairingUrl: "x" });
176 expect(next?.routes[0]).toBe(settingsLink.routes[0]);
177 expect(updateLinkedRoute(settingsLink, ["ConnectionsNew"], (route) => route)).toBe(
178 settingsLink,
179 );
180 });
181});
182​
183describe("findStackRoute", () => {
184 const stack = (key: string, routes: NavigationState["routes"]) =>
185 ({
186 key,
187 index: routes.length - 1,
188 routeNames: routes.map((route) => route.name),
189 routes,
190 stale: false,
191 type: "stack",
192 }) as NavigationState;
193​
194 it("finds a nested screen with the navigator that holds it", () => {
195 const root = stack("root", [
196 { key: "home", name: "Home" },
197 {
198 key: "settings",
199 name: "Settings",
200 state: stack("settings-stack", [{ key: "new-task", name: "NewTaskProject" }]),
201 },
202 ]);
203 expect(findStackRoute(root, "NewTaskProject")).toEqual({
204 navigatorKey: "settings-stack",
205 routeKey: "new-task",
206 focused: true,
207 });
208 });
209​
210 it("reports whether the screen is its navigator's focused route", () => {
211 const covered = stack("root", [
212 { key: "new-task", name: "NewTaskProject" },
213 { key: "add-project", name: "AddProject" },
214 ]);
215 expect(findStackRoute(covered, "NewTaskProject")?.focused).toBe(false);
216 expect(findStackRoute(covered, "AddProject")?.focused).toBe(true);
217 });
218​
219 it("prefers the top-most screen and reports a missing one", () => {
220 const root = stack("root", [
221 { key: "first", name: "NewTaskProject" },
222 { key: "second", name: "NewTaskProject" },
223 ]);
224 expect(findStackRoute(root, "NewTaskProject")?.routeKey).toBe("second");
225 expect(findStackRoute(root, "Thread")).toBeUndefined();
226 });
227});