Open History to see the last commit
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("leaves live-thread and project-selection links unchanged", () => {
95 const state = { routes: [{ name: "Thread", params: { threadId: "thread-1" } }] };
96 expect(restoreDraftLinkState(state, "/threads/env-1/thread-1")).toBe(state);
97 expect(restoreDraftLinkState(state, "/new")).toBe(state);
98 expect(restoreDraftLinkState(undefined, "/new/draft")).toBeUndefined();
99 });
100});
101
102describe("nested link routes", () => {
103 const settingsLink = {
104 routes: [
105 { name: "Home" },
106 {
107 name: "SettingsSheet",
108 state: {
109 routes: [
110 { name: "Settings" },
111 { name: "SettingsEnvironmentNew", params: { pairingUrl: "x", autoConnect: "1" } },
112 ],
113 },
114 },
115 ],
116 };
117
118 it("finds a route the link opens inside a nested navigator", () => {
119 expect(findLinkedRoute(settingsLink, ["SettingsEnvironmentNew"])?.params).toEqual({
120 pairingUrl: "x",
121 autoConnect: "1",
122 });
123 expect(findLinkedRoute(settingsLink, ["ConnectionsNew"])).toBeUndefined();
124 });
125
126 it("rewrites a nested route without touching the rest of the state", () => {
127 const next = updateLinkedRoute(settingsLink, ["SettingsEnvironmentNew"], (route) => ({
128 ...route,
129 params: { pairingUrl: "x" },
130 }));
131 expect(findLinkedRoute(next, ["SettingsEnvironmentNew"])?.params).toEqual({ pairingUrl: "x" });
132 expect(next?.routes[0]).toBe(settingsLink.routes[0]);
133 expect(updateLinkedRoute(settingsLink, ["ConnectionsNew"], (route) => route)).toBe(
134 settingsLink,
135 );
136 });
137});
138
139describe("findStackRoute", () => {
140 const stack = (key: string, routes: NavigationState["routes"]) =>
141 ({
142 key,
143 index: routes.length - 1,
144 routeNames: routes.map((route) => route.name),
145 routes,
146 stale: false,
147 type: "stack",
148 }) as NavigationState;
149
150 it("finds a nested screen with the navigator that holds it", () => {
151 const root = stack("root", [
152 { key: "home", name: "Home" },
153 {
154 key: "settings",
155 name: "Settings",
156 state: stack("settings-stack", [{ key: "new-task", name: "NewTaskProject" }]),
157 },
158 ]);
159 expect(findStackRoute(root, "NewTaskProject")).toEqual({
160 navigatorKey: "settings-stack",
161 routeKey: "new-task",
162 focused: true,
163 });
164 });
165
166 it("reports whether the screen is its navigator's focused route", () => {
167 const covered = stack("root", [
168 { key: "new-task", name: "NewTaskProject" },
169 { key: "add-project", name: "AddProject" },
170 ]);
171 expect(findStackRoute(covered, "NewTaskProject")?.focused).toBe(false);
172 expect(findStackRoute(covered, "AddProject")?.focused).toBe(true);
173 });
174
175 it("prefers the top-most screen and reports a missing one", () => {
176 const root = stack("root", [
177 { key: "first", name: "NewTaskProject" },
178 { key: "second", name: "NewTaskProject" },
179 ]);
180 expect(findStackRoute(root, "NewTaskProject")?.routeKey).toBe("second");
181 expect(findStackRoute(root, "Thread")).toBeUndefined();
182 });
183});