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

appShortcuts.test.ts

98 lines3.5 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { NavigationState } from "@react-navigation/native";
3​
4import type { RecentThreadShortcut } from "../../persistence/imperative";
5import {
6 activeThreadRef,
7 buildShortcutActions,
8 shortcutHref,
9 withRecentThreadShortcut,
10} from "./appShortcuts";
11​
12function navState(route: { name: string; params?: unknown }): NavigationState {
13 return { index: 0, routes: [route] } as unknown as NavigationState;
15​
16function thread(suffix: string, title = `Thread ${suffix}`): RecentThreadShortcut {
17 return { environmentId: `env-${suffix}`, threadId: `thread-${suffix}`, title };
19​
20describe("withRecentThreadShortcut", () => {
21 it("moves a reopened thread to the front without duplicating it", () => {
22 const next = withRecentThreadShortcut([thread("a"), thread("b")], thread("b"));
23 expect(next.map((entry) => entry.threadId)).toEqual(["thread-b", "thread-a"]);
24 });
25​
26 it("keeps the known title when a reopen records an empty one", () => {
27 const current = [thread("a", "Fix the build")];
28 const next = withRecentThreadShortcut(current, thread("a", ""));
29 expect(next).toBe(current);
30 });
31});
32​
33describe("buildShortcutActions", () => {
34 it("deep-links threads with encoded route params", () => {
35 const actions = buildShortcutActions([
36 { environmentId: "env 1", threadId: "thread/2", title: "Spaced out" },
37 ]);
38 expect(actions[1]?.params?.href).toBe("/threads/env%201/thread%2F2");
39 expect(actions[1]?.title).toBe("Spaced out");
40 });
41});
42​
43describe("shortcutHref", () => {
44 it("accepts exactly the destinations shortcuts can produce", () => {
45 expect(shortcutHref({ id: "x", title: "x", params: { href: "/new" } })).toBe("/new");
46 expect(shortcutHref({ id: "x", title: "x", params: { href: "/threads/env-1/thread-2" } })).toBe(
47 "/threads/env-1/thread-2",
48 );
49 expect(
50 shortcutHref({ id: "x", title: "x", params: { href: "/threads/env%201/thread%2F2" } }),
51 ).toBe("/threads/env%201/thread%2F2");
52 });
53​
54 it("rejects everything else", () => {
55 for (const href of [
56 "https://evil.example",
57 "//evil.example",
58 "/settings",
59 "/threads/only-one-segment",
60 "/threads/a/b/c",
61 "/threads//x",
62 "/threads/a/b?x=1",
63 "/threads/a/b#frag",
64 "/new/extra",
65 ]) {
66 expect(shortcutHref({ id: "x", title: "x", params: { href } })).toBe(null);
67 }
68 expect(shortcutHref({ id: "x", title: "x", params: { href: 3 } })).toBe(null);
69 expect(shortcutHref({ id: "x", title: "x" })).toBe(null);
70 });
71});
72​
73describe("activeThreadRef", () => {
74 it("returns null instead of throwing for malformed params", () => {
75 const malformed: unknown[] = [
76 undefined,
77 {},
78 { environmentId: "env-1" },
79 { environmentId: "", threadId: "thread-2" },
80 { environmentId: " ", threadId: "thread-2" },
81 { environmentId: "env-1", threadId: " " },
82 { environmentId: 42, threadId: "thread-2" },
83 { environmentId: { nested: true }, threadId: "thread-2" },
84 { environmentId: [], threadId: [] },
85 ];
86 for (const params of malformed) {
87 expect(activeThreadRef(navState({ name: "Thread", params }))).toBe(null);
88 }
89 });
90});
91​
92describe("launcher shortcut ids", () => {
93 it("cannot collide across different env/thread pairs", () => {
94 const a = buildShortcutActions([{ environmentId: "a-b", threadId: "c", title: "x" }]);
95 const b = buildShortcutActions([{ environmentId: "a", threadId: "b-c", title: "x" }]);
96 expect(a[1]?.id).not.toBe(b[1]?.id);
97 });
98});