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

appLinking.test.ts

193 lines6.5 KB

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