The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { |
| 3 | EnvironmentId, |
| 4 | makeProviderSettingsSchema, |
| 5 | SourceControlProviderKind, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { UNKNOWN_SOURCE_CONTROL_CLIENT } from "@t3tools/client-runtime/source-control-clients"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { decodeMsg } from "gt-react"; |
| 10 | |
| 11 | import { |
| 12 | filterAvailableSettingsSearchItems, |
| 13 | getSettingsSearchTargetScope, |
| 14 | getThreadAutoSettlementSearchAvailability, |
| 15 | isSettingsSearchScopeAvailable, |
| 16 | movedSettingsPath, |
| 17 | searchSettings, |
| 18 | SETTINGS_NAV_GROUPS, |
| 19 | SETTINGS_SEARCH_ITEMS, |
| 20 | sourceControlHostSearchItems, |
| 21 | sourceControlHostSettingsSearchId, |
| 22 | type SettingsSearchItem, |
| 23 | } from "./settingsSearch"; |
| 24 | |
| 25 | vi.mock("~/env", async (importOriginal) => ({ |
| 26 | ...(await importOriginal<typeof import("~/env")>()), |
| 27 | isSecretsFeatureEnabled: false, |
| 28 | })); |
| 29 | |
| 30 | const ITEMS: ReadonlyArray<SettingsSearchItem> = [ |
| 31 | { |
| 32 | id: "word-wrap", |
| 33 | title: "Word wrap", |
| 34 | to: "/settings/general", |
| 35 | searchTerms: ["long lines in code previews"], |
| 36 | }, |
| 37 | { |
| 38 | id: "network-access", |
| 39 | title: "Network access", |
| 40 | to: "/settings/access", |
| 41 | searchTerms: ["remote pairing backend"], |
| 42 | }, |
| 43 | { |
| 44 | id: "providers", |
| 45 | title: "Providers", |
| 46 | to: "/settings/providers", |
| 47 | searchTerms: ["claude codex agents"], |
| 48 | }, |
| 49 | { |
| 50 | id: "provider-updates", |
| 51 | title: "Update checks", |
| 52 | to: "/settings/general", |
| 53 | }, |
| 54 | { |
| 55 | id: "automatic-updates", |
| 56 | title: "Automatic updates", |
| 57 | to: "/settings/general", |
| 58 | }, |
| 59 | ]; |
| 60 | |
| 61 | describe("searchSettings", () => { |
| 62 | it("matches titles, sections, and remembered setting details", () => { |
| 63 | expect(searchSettings("word", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]); |
| 64 | expect(searchSettings("network", ITEMS).map((item) => item.id)).toEqual(["network-access"]); |
| 65 | expect(searchSettings("sharing", ITEMS).map((item) => item.id)).toEqual(["network-access"]); |
| 66 | expect(searchSettings("claude", ITEMS).map((item) => item.id)).toEqual(["providers"]); |
| 67 | expect(searchSettings("long lines", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]); |
| 68 | const spanish = (message: string) => (message === "Word wrap" ? "Ajuste de línea" : message); |
| 69 | expect(searchSettings("ajuste", ITEMS, spanish).map((item) => item.id)).toEqual(["word-wrap"]); |
| 70 | for (const query of ["fallback", "fallback agent", "fallback model"]) { |
| 71 | expect(searchSettings(query)[0]).toMatchObject({ |
| 72 | id: "fallback-model", |
| 73 | to: "/settings/threads", |
| 74 | scope: "environment-defaults", |
| 75 | }); |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | it("hides the Secrets page from nav and search while the secrets feature is disabled", () => { |
| 80 | expect(SETTINGS_NAV_GROUPS.flatMap((group) => group.paths)).not.toContain("/settings/secrets"); |
| 81 | expect(searchSettings("1password").map((item) => item.to)).not.toContain("/settings/secrets"); |
| 82 | }); |
| 83 | |
| 84 | it("finds where subagents and side chats open by any of its names", () => { |
| 85 | for (const query of ["side panel", "subagent", "side chat", "linked thread", "middle click"]) { |
| 86 | expect(searchSettings(query).map((item) => item.id)).toContain("side-thread-open-target"); |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | it("finds the Scheduled tasks page", () => { |
| 91 | expect(searchSettings("scheduled tasks")[0]).toMatchObject({ |
| 92 | id: "scheduled-tasks", |
| 93 | to: "/settings/scheduled-tasks", |
| 94 | }); |
| 95 | }); |
| 96 | |
| 97 | it("matches normalized title substrings", () => { |
| 98 | expect(searchSettings(" WORD WRAP ", ITEMS).map((item) => item.id)).toEqual(["word-wrap"]); |
| 99 | expect(searchSettings("glass").map((item) => item.id)).toEqual(["setting-glass-opacity"]); |
| 100 | expect(searchSettings("panel animations").map((item) => item.id)).toEqual(["panel-animations"]); |
| 101 | expect(searchSettings("thè\u{1ab0}mes")[0]?.id).toBe("theme"); |
| 102 | const localeLowerCase = vi.spyOn(String.prototype, "toLocaleLowerCase").mockReturnValue("gıt"); |
| 103 | try { |
| 104 | expect(searchSettings("GIT")[0]?.id).toBe("git-fetch-interval"); |
| 105 | expect(localeLowerCase).not.toHaveBeenCalled(); |
| 106 | } finally { |
| 107 | localeLowerCase.mockRestore(); |
| 108 | } |
| 109 | expect(searchSettings("xyzzy")).toEqual([]); |
| 110 | }); |
| 111 | |
| 112 | it("matches query words across fields and ranks the strongest result first", () => { |
| 113 | expect(searchSettings("pairing remote", ITEMS).map((item) => item.id)).toEqual([ |
| 114 | "network-access", |
| 115 | ]); |
| 116 | expect( |
| 117 | searchSettings("remote pairing") |
| 118 | .slice(0, 2) |
| 119 | .map((item) => item.id), |
| 120 | ).toEqual(["network-access", "connections-environment"]); |
| 121 | }); |
| 122 | |
| 123 | it("hides desktop-only settings from browser search", () => { |
| 124 | expect(SETTINGS_SEARCH_ITEMS.some((item) => item.id === "quit-confirmation")).toBe(true); |
| 125 | expect(searchSettings("hold to quit")).toEqual([]); |
| 126 | expect(searchSettings("wsl")).toEqual([]); |
| 127 | expect(searchSettings("update track")).toEqual([]); |
| 128 | }); |
| 129 | |
| 130 | it("hides settings whose controls are unavailable", () => { |
| 131 | const available = filterAvailableSettingsSearchItems({ |
| 132 | hasCloudPublicConfig: false, |
| 133 | hasEnvironment: false, |
| 134 | hasProviderSettingsEnvironment: false, |
| 135 | canManageLocalBackend: false, |
| 136 | isWslSettingsRowVisible: false, |
| 137 | hasThreadAutoSettlement: false, |
| 138 | }); |
| 139 | |
| 140 | const gatedIds = new Set<string>([ |
| 141 | "follow-change-request-templates", |
| 142 | "git-fetch-interval", |
| 143 | "network-access", |
| 144 | "publish-agent-activity", |
| 145 | "provider-health-check-interval", |
| 146 | "source-control-writer-model", |
| 147 | "review-guide-model", |
| 148 | "review-guide-instructions", |
| 149 | "source-control-writing-style", |
| 150 | "t3-connect", |
| 151 | "hold-webhooks-while-offline", |
| 152 | "tailscale-https", |
| 153 | "wsl-backend", |
| 154 | "auto-settle-inactive-threads", |
| 155 | "auto-settle-merged-threads", |
| 156 | "days-before-auto-settle", |
| 157 | "linked-environments", |
| 158 | ]); |
| 159 | expect(available.map((item) => item.id).filter((id) => gatedIds.has(id))).toEqual([]); |
| 160 | }); |
| 161 | |
| 162 | it("finds linked environments on a remote host without local maintenance access", () => { |
| 163 | const availability = { |
| 164 | hasCloudPublicConfig: false, |
| 165 | hasEnvironment: true, |
| 166 | hasProviderSettingsEnvironment: true, |
| 167 | canManageLocalBackend: false, |
| 168 | localEnvironmentDisabled: true, |
| 169 | isWslSettingsRowVisible: false, |
| 170 | hasThreadAutoSettlement: false, |
| 171 | }; |
| 172 | const results = (hasPeerLinkHost: boolean) => |
| 173 | searchSettings( |
| 174 | "linked environments", |
| 175 | filterAvailableSettingsSearchItems({ ...availability, hasPeerLinkHost }), |
| 176 | ); |
| 177 | expect(results(false).map((item) => item.id)).not.toContain("linked-environments"); |
| 178 | expect(results(true)).toContainEqual( |
| 179 | expect.objectContaining({ |
| 180 | id: "linked-environments", |
| 181 | to: "/settings/connections", |
| 182 | targetId: "linked-environments", |
| 183 | }), |
| 184 | ); |
| 185 | }); |
| 186 | |
| 187 | it("finds the Welcome page only where a primary computer can open it", () => { |
| 188 | const availability = { |
| 189 | hasCloudPublicConfig: true, |
| 190 | hasEnvironment: true, |
| 191 | hasProviderSettingsEnvironment: true, |
| 192 | canManageLocalBackend: false, |
| 193 | isWslSettingsRowVisible: false, |
| 194 | hasThreadAutoSettlement: false, |
| 195 | }; |
| 196 | const ids = (hasPrimaryEnvironment: boolean) => |
| 197 | searchSettings( |
| 198 | "welcome page", |
| 199 | filterAvailableSettingsSearchItems({ ...availability, hasPrimaryEnvironment }), |
| 200 | ).map((item) => item.id); |
| 201 | // Hosted web reaches remote computers but has no primary one to set up. |
| 202 | expect(ids(false)).not.toContain("welcome-page"); |
| 203 | expect(ids(true)).toContain("welcome-page"); |
| 204 | }); |
| 205 | |
| 206 | it("finds keybinding commands by label, command id, and default key", () => { |
| 207 | expect(searchSettings("toggle sidebar")[0]?.id).toBe("keybinding-sidebar.toggle"); |
| 208 | expect(searchSettings("sidebar.toggle")[0]?.id).toBe("keybinding-sidebar.toggle"); |
| 209 | expect(searchSettings("mod+b")[0]?.id).toBe("keybinding-sidebar.toggle"); |
| 210 | expect(searchSettings("copy link")[0]).toMatchObject({ |
| 211 | id: "keybinding-thread.copyReference", |
| 212 | to: "/settings/keybindings", |
| 213 | }); |
| 214 | }); |
| 215 | |
| 216 | it("sends commands without a default binding to the section", () => { |
| 217 | expect(searchSettings("thread.stop")[0]).toMatchObject({ |
| 218 | id: "keybinding-thread.stop", |
| 219 | targetId: "keybindings", |
| 220 | }); |
| 221 | expect(searchSettings("sidebar.toggle")[0]?.targetId).toBeUndefined(); |
| 222 | }); |
| 223 | |
| 224 | it("finds rows by the titles shown on their pages", () => { |
| 225 | for (const [title, id] of [ |
| 226 | ["Automatic worktree cleanup", "storage-worktrees"], |
| 227 | ["Delete settled worktrees", "storage-worktrees"], |
| 228 | ["Delete old browser artifacts", "storage-artifacts"], |
| 229 | ["Delete old rotated logs", "storage-artifacts"], |
| 230 | ["Custom search URL", "browser-search-engine"], |
| 231 | ["Native sessions", "providers"], |
| 232 | ["CODEX_HOME path", "providers"], |
| 233 | ["Shadow home path", "providers"], |
| 234 | ] as const) { |
| 235 | expect(searchSettings(title)[0]?.id, title).toBe(id); |
| 236 | } |
| 237 | }); |
| 238 | |
| 239 | it("keeps catalog result ids unique", () => { |
| 240 | const ids = SETTINGS_SEARCH_ITEMS.map((item) => item.id); |
| 241 | expect(new Set(ids).size).toBe(ids.length); |
| 242 | }); |
| 243 | }); |
| 244 | |
| 245 | describe("settings search targets", () => { |
| 246 | it("requires an environment for the resolved telemetry export destination", () => { |
| 247 | const result = searchSettings("OpenTelemetry export").find( |
| 248 | (item) => item.id === "telemetry-export", |
| 249 | )!; |
| 250 | const target = getSettingsSearchTargetScope(result.targetId ?? result.id)!; |
| 251 | expect(isSettingsSearchScopeAvailable(target.scope, "all")).toBe(false); |
| 252 | expect(isSettingsSearchScopeAvailable(target.scope, "project")).toBe(false); |
| 253 | expect(isSettingsSearchScopeAvailable(target.scope, "environment")).toBe(true); |
| 254 | }); |
| 255 | |
| 256 | it("keeps environment-wide settings out of project scopes", () => { |
| 257 | const updates = getSettingsSearchTargetScope("provider-update-checks")!; |
| 258 | expect(updates.scope).toBe("environment-defaults"); |
| 259 | expect(isSettingsSearchScopeAvailable(updates.scope, "environment")).toBe(true); |
| 260 | expect(isSettingsSearchScopeAvailable(updates.scope, "all")).toBe(true); |
| 261 | expect(isSettingsSearchScopeAvailable(updates.scope, "project")).toBe(false); |
| 262 | const fallback = getSettingsSearchTargetScope("fallback-model")!; |
| 263 | expect(fallback.scope).toBe("environment-defaults"); |
| 264 | expect(isSettingsSearchScopeAvailable(fallback.scope, "environment")).toBe(true); |
| 265 | expect(isSettingsSearchScopeAvailable(fallback.scope, "all")).toBe(true); |
| 266 | expect(isSettingsSearchScopeAvailable(fallback.scope, "project")).toBe(false); |
| 267 | for (const id of ["response-streaming", "speech-project-dictionary"]) { |
| 268 | expect(getSettingsSearchTargetScope(id)?.scope).toBe("project-defaults"); |
| 269 | } |
| 270 | for (const scope of ["all", "environment", "project"] as const) { |
| 271 | expect(isSettingsSearchScopeAvailable("project-defaults", scope)).toBe(true); |
| 272 | } |
| 273 | for (const id of ["tool-call-visibility", "boring-sidebar"]) { |
| 274 | expect(getSettingsSearchTargetScope(id)!.scope).toBeNull(); |
| 275 | } |
| 276 | }); |
| 277 | }); |
| 278 | |
| 279 | describe("auto-settlement search availability", () => { |
| 280 | function environment(id: string, { connected = true, loaded = true } = {}) { |
| 281 | return { |
| 282 | environmentId: EnvironmentId.make(id), |
| 283 | connection: { phase: connected ? ("connected" as const) : ("offline" as const) }, |
| 284 | serverConfig: loaded ? { environment: { capabilities: {} } } : null, |
| 285 | }; |
| 286 | } |
| 287 | |
| 288 | const capable = environment("capable"); |
| 289 | const offline = environment("offline", { connected: false }); |
| 290 | const loading = environment("loading", { loaded: false }); |
| 291 | const environments = [capable, offline, loading]; |
| 292 | |
| 293 | it("keeps results discoverable when one connected environment supports them", () => { |
| 294 | const availability = getThreadAutoSettlementSearchAvailability(environments); |
| 295 | expect(availability.eligibleEnvironmentIds).toEqual([capable.environmentId]); |
| 296 | const items = filterAvailableSettingsSearchItems({ |
| 297 | hasCloudPublicConfig: false, |
| 298 | hasEnvironment: true, |
| 299 | hasProviderSettingsEnvironment: true, |
| 300 | canManageLocalBackend: false, |
| 301 | isWslSettingsRowVisible: false, |
| 302 | hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0, |
| 303 | }); |
| 304 | expect(searchSettings("auto-settle", items).map((item) => item.id)).toEqual([ |
| 305 | "auto-settle-inactive-threads", |
| 306 | "auto-settle-merged-threads", |
| 307 | "days-before-auto-settle", |
| 308 | "keep-threads-with-pull-requests", |
| 309 | ]); |
| 310 | }); |
| 311 | |
| 312 | it.each([offline, loading])( |
| 313 | "does not render the target on $environmentId or silently fall back", |
| 314 | (selected) => { |
| 315 | expect( |
| 316 | getThreadAutoSettlementSearchAvailability(environments, { |
| 317 | kind: "environment", |
| 318 | environmentIds: [selected.environmentId], |
| 319 | }), |
| 320 | ).toEqual({ eligibleEnvironmentIds: [capable.environmentId], isTargetAvailable: false }); |
| 321 | }, |
| 322 | ); |
| 323 | |
| 324 | it("ignores offline and unloaded targets when all connected targets support the setting", () => { |
| 325 | const selected = [capable, offline, loading]; |
| 326 | expect( |
| 327 | getThreadAutoSettlementSearchAvailability(selected, { |
| 328 | kind: "all", |
| 329 | environmentIds: selected.map((entry) => entry.environmentId), |
| 330 | }).isTargetAvailable, |
| 331 | ).toBe(true); |
| 332 | }); |
| 333 | }); |
| 334 | |
| 335 | describe("source control host search items", () => { |
| 336 | const definition = { |
| 337 | ...UNKNOWN_SOURCE_CONTROL_CLIENT, |
| 338 | kind: SourceControlProviderKind.make("example"), |
| 339 | label: "Example", |
| 340 | searchKeywords: ["sample"], |
| 341 | settings: makeProviderSettingsSchema({ apiToken: Schema.String }), |
| 342 | }; |
| 343 | |
| 344 | it("lists a host by its label, its keywords, and its settings fields", () => { |
| 345 | const items = sourceControlHostSearchItems([UNKNOWN_SOURCE_CONTROL_CLIENT, definition]); |
| 346 | expect(items.map((item) => decodeMsg(item.title))).toEqual(["Example credentials"]); |
| 347 | expect(searchSettings("sample apitoken", items).map((item) => item.id)).toEqual([ |
| 348 | sourceControlHostSettingsSearchId("example"), |
| 349 | ]); |
| 350 | }); |
| 351 | |
| 352 | it("lists a host with credentials by its accounts, token and CLI", () => { |
| 353 | const items = sourceControlHostSearchItems([ |
| 354 | { |
| 355 | ...definition, |
| 356 | credentials: { |
| 357 | cliName: "ex", |
| 358 | cliLabel: "Example CLI", |
| 359 | loginCommand: "ex login", |
| 360 | tokenEnvVar: "EX_TOKEN", |
| 361 | tokenAccess: "access", |
| 362 | newTokenUrl: (host) => `https://${host}/tokens`, |
| 363 | }, |
| 364 | }, |
| 365 | ]); |
| 366 | expect(items.map((item) => decodeMsg(item.title))).toEqual(["Example accounts and token"]); |
| 367 | expect(searchSettings("ex switch accounts", items)).toHaveLength(1); |
| 368 | }); |
| 369 | |
| 370 | it("finds the built-in hosts by their own words", () => { |
| 371 | expect(searchSettings("codeberg").map((item) => item.id)).toContain("source-control"); |
| 372 | expect(searchSettings("ghes").map((item) => item.id)).toContain( |
| 373 | sourceControlHostSettingsSearchId("github"), |
| 374 | ); |
| 375 | }); |
| 376 | }); |
| 377 | |
| 378 | describe("movedSettingsPath", () => { |
| 379 | it("sends old links to the page their row moved to", () => { |
| 380 | expect(movedSettingsPath("/settings/general", "text-generation-model")).toBe( |
| 381 | "/settings/threads", |
| 382 | ); |
| 383 | expect(movedSettingsPath("/settings/connections", "authorized-clients")).toBe( |
| 384 | "/settings/access", |
| 385 | ); |
| 386 | expect(movedSettingsPath("/settings/source-control", "worktree-branch-naming")).toBe( |
| 387 | "/settings/worktrees", |
| 388 | ); |
| 389 | expect(movedSettingsPath("/settings/appearance", "diff-color-scheme")).toBe("/settings/diffs"); |
| 390 | expect(movedSettingsPath("/settings/connections", "network-access")).toBe("/settings/access"); |
| 391 | expect(movedSettingsPath("/settings/storage", "storage-worktrees")).toBe("/settings/worktrees"); |
| 392 | expect(movedSettingsPath("/settings/storage", "")).toBe("/settings/maintenance"); |
| 393 | expect(movedSettingsPath("/settings/integrations", "devices")).toBe("/settings/devices"); |
| 394 | expect(movedSettingsPath("/settings/beta", "")).toBe("/settings/labs"); |
| 395 | }); |
| 396 | |
| 397 | it("leaves current pages alone", () => { |
| 398 | expect(movedSettingsPath("/settings/general", "time-format")).toBeNull(); |
| 399 | expect(movedSettingsPath("/settings/projects", "project-defaults")).toBeNull(); |
| 400 | expect(movedSettingsPath("/settings/connections", "remote-environments")).toBeNull(); |
| 401 | expect(movedSettingsPath("/settings/appearance", "theme")).toBeNull(); |
| 402 | }); |
| 403 | }); |