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

main
ts 176 lines 5.75 KB
Raw
1 import { describe, expect, it, vi } from "vite-plus/test";
2 import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
3
4 vi.mock("react-native", () => ({ Alert: { alert: vi.fn() } }));
5
6 vi.mock("../../state/queries", () => ({
7 useComposerPathSearch: () => ({ entries: [], isPending: false }),
8 useComposerPullRequestSearch: () => ({ entries: [], isPending: false, error: null }),
9 }));
10 vi.mock("../../state/use-composer-drafts", () => ({
11 getComposerDraftSnapshot: vi.fn(),
12 setComposerDraftContext: vi.fn(),
13 useComposerDraftSelection: vi.fn(),
14 }));
15 vi.mock("../../lib/uuid", () => ({ uuidv4: () => "context-id" }));
16 vi.mock("../../state/server", () => ({
17 serverEnvironment: {
18 providersValueAtom: vi.fn(),
19 refreshProviders: Symbol("refreshProviders"),
20 },
21 }));
22
23 import type { ModelOption } from "../../lib/modelOptions";
24 import {
25 buildComposerModelItems,
26 buildComposerSlashCommandItems,
27 resolveComposerCommandSelection,
28 resolveComposerMenuSelection,
29 } from "./use-composer-command-menu";
30
31 describe("resolveComposerMenuSelection", () => {
32 it("prefers the caret an insertion placed in the current draft text", () => {
33 expect(
34 resolveComposerMenuSelection({
35 draftMessage: "hello @chip ",
36 ownerKey: "draft-a",
37 inserted: { start: 12, end: 12 },
38 local: { ownerKey: "draft-a", start: 2, end: 2 },
39 }),
40 ).toEqual({ start: 12, end: 12 });
41 });
42
43 it("clamps the composer's own caret when the draft shrinks", () => {
44 expect(
45 resolveComposerMenuSelection({
46 draftMessage: "hi",
47 ownerKey: "draft-a",
48 inserted: null,
49 local: { ownerKey: "draft-a", start: 5, end: 9 },
50 }),
51 ).toEqual({ start: 2, end: 2 });
52 });
53
54 it("starts at the end of the draft after switching owners", () => {
55 expect(
56 resolveComposerMenuSelection({
57 draftMessage: "other draft",
58 ownerKey: "draft-b",
59 inserted: null,
60 local: { ownerKey: "draft-a", start: 1, end: 1 },
61 }),
62 ).toEqual({ start: 11, end: 11 });
63 });
64 });
65
66 describe("mobile slash commands", () => {
67 it("keeps a provider's native /plan", () => {
68 const items = buildComposerSlashCommandItems({
69 query: "pl",
70 atMessageStart: true,
71 hasThread: true,
72 selectedProviderStatus: {
73 driver: ProviderDriverKind.make("antigravity"),
74 slashCommands: [{ name: "plan", description: "Plan with Antigravity" }],
75 },
76 });
77
78 expect(items).toHaveLength(1);
79 expect(items[0]?.type).toBe("provider-slash-command");
80 const item = items[0];
81 if (!item) throw new Error("Expected the native plan command");
82 expect(
83 resolveComposerCommandSelection({
84 draftMessage: "/pl",
85 trigger: { rangeStart: 0, rangeEnd: 3 },
86 item,
87 }),
88 ).toEqual({ text: "/plan ", cursor: 6 });
89 });
90
91 it("offers T3's /clear in place of the provider's, only at the message start", () => {
92 const claude = {
93 driver: ProviderDriverKind.make("claudeAgent"),
94 slashCommands: [{ name: "clear", description: "Clear conversation history" }],
95 };
96 const build = (offersFreshSession: boolean, atMessageStart = true) =>
97 buildComposerSlashCommandItems({
98 query: "cle",
99 atMessageStart,
100 hasThread: true,
101 offersFreshSession,
102 selectedProviderStatus: claude,
103 }).map((item) => item.id);
104 expect(build(true)).toEqual(["cmd:clear"]);
105 expect(build(false)).toEqual(["pcmd:clear"]);
106 expect(build(true, false)).toEqual([]);
107 });
108 });
109
110 describe("mobile model mentions", () => {
111 const provider = (
112 instanceId: string,
113 overrides: Partial<ServerProvider> = {},
114 ): ServerProvider => ({
115 instanceId: ProviderInstanceId.make(instanceId),
116 driver: ProviderDriverKind.make("codex"),
117 enabled: true,
118 installed: true,
119 version: null,
120 status: "ready",
121 auth: { status: "authenticated" },
122 checkedAt: "2026-01-01T00:00:00.000Z",
123 models: [],
124 slashCommands: [],
125 skills: [],
126 ...overrides,
127 });
128 const option = (key: string, overrides: Partial<ModelOption> = {}): ModelOption => {
129 const [instanceId = "", model = ""] = key.split(":");
130 return {
131 key,
132 label: "GPT-6.1",
133 subtitle: "",
134 providerKey: instanceId,
135 providerLabel: "Traex",
136 providerDriver: "codex",
137 isDefault: false,
138 isLegacy: false,
139 capabilities: null,
140 selection: { instanceId: ProviderInstanceId.make(instanceId), model },
141 ...overrides,
142 };
143 };
144
145 it("offers current, visible models labelled with their provider", () => {
146 const items = buildComposerModelItems(
147 [
148 option("traex:gpt-6.1"),
149 option("opencode:traex/gpt-6.1", {
150 subtitle: "Traex",
151 providerLabel: "OpenCode",
152 providerDriver: "opencode",
153 }),
154 option("hidden:gpt-6.1", { isHidden: true }),
155 option("gone:gpt-6.1", { isUnavailable: true }),
156 option("old:gpt-5", { label: "GPT-5", isLegacy: true }),
157 ],
158 "gpt",
159 ["traex", "opencode", "hidden", "gone", "old"].map((id) => provider(id)),
160 );
161 expect(items.map((item) => [item.id, item.description]).toSorted()).toEqual([
162 ["model:opencode:traex/gpt-6.1", "OpenCode · Traex"],
163 ["model:traex:gpt-6.1", "Traex"],
164 ]);
165 });
166
167 it.each([
168 ["missing", []],
169 ["not installed", [provider("traex", { installed: false })]],
170 ["signed out", [provider("traex", { auth: { status: "unauthenticated" } })]],
171 ["not ready", [provider("traex", { status: "error" })]],
172 ["unavailable", [provider("traex", { availability: "unavailable" })]],
173 ] as const)("does not suggest a preserved selection whose provider is %s", (_, providers) => {
174 expect(buildComposerModelItems([option("traex:gpt-6.1")], "gpt", providers)).toEqual([]);
175 });
176 });