The best way to control Claude Code, Codex and more from any computer or phone

main
ts 108 lines 4.01 KB
Raw
1 import { ThreadId } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3
4 import {
5 resolveMobileSideThreadPresentation,
6 resolveSideThreadOpenTarget,
7 sideThreadComposer,
8 sideThreadKindOf,
9 sideThreadOpenIntentOf,
10 } from "./sideThread.ts";
11
12 const parentThreadId = ThreadId.make("parent");
13 const lineage = (
14 overrides: Partial<{
15 parentThreadId: ThreadId | null;
16 relationshipToParent: "fork" | "subagent" | null;
17 sideChat: boolean;
18 }>,
19 ) => ({
20 lineage: {
21 rootThreadId: parentThreadId,
22 parentThreadId,
23 relationshipToParent: null,
24 forkedFrom: null,
25 ...overrides,
26 },
27 });
28
29 describe("sideThreadKindOf", () => {
30 it.each([
31 ["a side chat", lineage({ relationshipToParent: "fork", sideChat: true }), "side-chat"],
32 ["an app subagent", { ...lineage({ relationshipToParent: "subagent" }) }, "subagent"],
33 [
34 "a provider's own subagent",
35 { ...lineage({ relationshipToParent: "subagent" }), creationSource: "provider" as const },
36 "provider-subagent",
37 ],
38 ["a fork", lineage({ relationshipToParent: "fork" }), null],
39 ["a top-level thread", lineage({ parentThreadId: null }), null],
40 // The flag alone does not make a tab: a side chat is beside some thread.
41 ["a side chat with no parent", lineage({ parentThreadId: null, sideChat: true }), null],
42 ])("classifies %s", (_name, thread, kind) => {
43 expect(sideThreadKindOf(thread as Parameters<typeof sideThreadKindOf>[0])).toBe(kind);
44 });
45
46 it("treats a thread that is not loaded as an ordinary one", () => {
47 expect(sideThreadKindOf(null)).toBeNull();
48 });
49 });
50
51 describe("sideThreadComposer", () => {
52 it.each([
53 ["side-chat", "composer"],
54 ["subagent", "composer"],
55 ["provider-subagent", "read-only"],
56 ["linked", "read-only-connect"],
57 ] as const)("gives %s the %s", (kind, composer) => {
58 expect(sideThreadComposer(kind)).toBe(composer);
59 });
60 });
61
62 describe("resolveSideThreadOpenTarget", () => {
63 it.each([
64 // preference, intent, panelAvailable, target
65 ["panel", "primary", true, "panel"],
66 ["panel", "alternate", true, "main"],
67 ["main", "primary", true, "main"],
68 ["main", "alternate", true, "panel"],
69 // The context menu names a place, whatever the preference.
70 ["panel", "main", true, "main"],
71 ["main", "panel", true, "panel"],
72 // With no panel to open in, every way of asking lands on the main view.
73 ["panel", "primary", false, "main"],
74 ["main", "alternate", false, "main"],
75 ["main", "panel", false, "main"],
76 ] as const)(
77 "prefers %s, asked %s, panel %s: opens in %s",
78 (preference, intent, panelAvailable, target) => {
79 expect(resolveSideThreadOpenTarget({ preference, intent, panelAvailable })).toBe(target);
80 },
81 );
82 });
83
84 describe("sideThreadOpenIntentOf", () => {
85 const plain = { metaKey: false, ctrlKey: false, button: 0 };
86
87 it("takes the platform's modifier and the middle button as the other place", () => {
88 expect(sideThreadOpenIntentOf(plain, true)).toBe("primary");
89 expect(sideThreadOpenIntentOf({ ...plain, metaKey: true }, true)).toBe("alternate");
90 expect(sideThreadOpenIntentOf({ ...plain, ctrlKey: true }, false)).toBe("alternate");
91 expect(sideThreadOpenIntentOf({ ...plain, button: 1 }, false)).toBe("alternate");
92 // A key press has no button.
93 expect(sideThreadOpenIntentOf({ metaKey: true, ctrlKey: false }, true)).toBe("alternate");
94 });
95
96 it("ignores the other platform's modifier", () => {
97 // Ctrl-click on macOS is the context menu, and the Windows key is not Ctrl.
98 expect(sideThreadOpenIntentOf({ ...plain, ctrlKey: true }, true)).toBe("primary");
99 expect(sideThreadOpenIntentOf({ ...plain, metaKey: true }, false)).toBe("primary");
100 });
101 });
102
103 describe("resolveMobileSideThreadPresentation", () => {
104 it("uses the inspector wherever the layout has one, else the full screen", () => {
105 expect(resolveMobileSideThreadPresentation({ inspectorAvailable: true })).toBe("inspector");
106 expect(resolveMobileSideThreadPresentation({ inspectorAvailable: false })).toBe("full-screen");
107 });
108 });