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 { EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 5 | import { buildHomeProjectScopes, type HomeProjectScope } from "../home/homeThreadList"; |
| 6 | import { |
| 7 | getProjectScopeSelectionTarget, |
| 8 | isSameRepository, |
| 9 | newTaskDestination, |
| 10 | newTaskFilteredProject, |
| 11 | resolveDraftProjectSelection, |
| 12 | resolveEnvironmentProjectMatch, |
| 13 | resolveNewTaskScratchOption, |
| 14 | } from "./new-task-project-selection"; |
| 15 | |
| 16 | function makeProject( |
| 17 | id: string, |
| 18 | environmentId = "environment", |
| 19 | options: { |
| 20 | readonly title?: string; |
| 21 | readonly workspaceRoot?: string; |
| 22 | readonly repositoryKey?: string; |
| 23 | } = {}, |
| 24 | ): EnvironmentProject { |
| 25 | return { |
| 26 | environmentId: EnvironmentId.make(environmentId), |
| 27 | id: ProjectId.make(id), |
| 28 | title: options.title ?? id, |
| 29 | workspaceRoot: options.workspaceRoot ?? `/work/${id}`, |
| 30 | repositoryIdentity: options.repositoryKey |
| 31 | ? { |
| 32 | canonicalKey: options.repositoryKey, |
| 33 | locator: { |
| 34 | source: "git-remote", |
| 35 | remoteName: "origin", |
| 36 | remoteUrl: `https://${options.repositoryKey}.git`, |
| 37 | }, |
| 38 | } |
| 39 | : null, |
| 40 | defaultModelSelection: null, |
| 41 | scripts: [], |
| 42 | createdAt: "2026-07-01T00:00:00.000Z", |
| 43 | updatedAt: "2026-07-01T00:00:00.000Z", |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | function makeScope(projects: ReadonlyArray<EnvironmentProject>): HomeProjectScope { |
| 48 | return { |
| 49 | key: "github.com/t3tools/t3code", |
| 50 | title: "T3 Code", |
| 51 | representative: projects[0]!, |
| 52 | projects, |
| 53 | projectRefs: projects.map((project) => ({ |
| 54 | environmentId: project.environmentId, |
| 55 | projectId: project.id, |
| 56 | })), |
| 57 | pinned: false, |
| 58 | archived: false, |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | describe("getProjectScopeSelectionTarget", () => { |
| 63 | it("chooses an open copy of a repository over an archived copy on the preferred machine", () => { |
| 64 | const repo = { repositoryKey: "github.com/t3tools/t3code" }; |
| 65 | const archived = { |
| 66 | ...makeProject("archived-copy", "mac", repo), |
| 67 | archivedAt: "2026-07-02T00:00:00.000Z", |
| 68 | }; |
| 69 | const open = makeProject("open-copy", "server", repo); |
| 70 | const [scope] = buildHomeProjectScopes({ |
| 71 | projects: [archived, open], |
| 72 | environmentId: null, |
| 73 | projectGroupingMode: "repository", |
| 74 | }); |
| 75 | if (!scope || scope.archived) throw new Error("Expected a pickable repository group"); |
| 76 | |
| 77 | expect( |
| 78 | getProjectScopeSelectionTarget( |
| 79 | scope, |
| 80 | archived.environmentId, |
| 81 | new Set([archived.environmentId, open.environmentId]), |
| 82 | ).id, |
| 83 | ).toBe(open.id); |
| 84 | }); |
| 85 | |
| 86 | it("prefers a connected copy of a project that spans environments", () => { |
| 87 | const mac = makeProject("t3code", "mac"); |
| 88 | const server = makeProject("t3code", "server"); |
| 89 | const target = (connected: ReadonlyArray<string>, preferred: string | null = null) => |
| 90 | getProjectScopeSelectionTarget( |
| 91 | makeScope([mac, server]), |
| 92 | preferred === null ? null : EnvironmentId.make(preferred), |
| 93 | new Set(connected.map((id) => EnvironmentId.make(id))), |
| 94 | ); |
| 95 | expect(target(["server"])).toBe(server); |
| 96 | expect(target(["server"], "mac")).toBe(server); |
| 97 | expect(target(["mac", "server"], "server")).toBe(server); |
| 98 | expect(target(["mac", "server"])).toBe(mac); |
| 99 | // With nothing connected, the usual order still applies. |
| 100 | expect(target([], "server")).toBe(server); |
| 101 | expect(target([])).toBe(mac); |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | describe("newTaskFilteredProject", () => { |
| 106 | it("never starts in an archived copy of the filtered project", () => { |
| 107 | const mac = { ...makeProject("t3code", "mac"), archivedAt: "2026-01-01T00:00:00.000Z" }; |
| 108 | const server = makeProject("t3code", "server"); |
| 109 | expect( |
| 110 | newTaskFilteredProject( |
| 111 | makeScope([mac, server]), |
| 112 | null, |
| 113 | new Set([mac.environmentId, server.environmentId]), |
| 114 | { environmentId: "mac", projectId: "t3code" }, |
| 115 | ), |
| 116 | ).toEqual({ environmentId: "server", projectId: "t3code" }); |
| 117 | }); |
| 118 | |
| 119 | it("prefers the remembered copy of the filtered project", () => { |
| 120 | const mac = makeProject("t3code", "mac"); |
| 121 | const server = makeProject("t3code", "server"); |
| 122 | const connected = new Set([mac.environmentId, server.environmentId]); |
| 123 | const filtered = (last?: { readonly environmentId: string; readonly projectId: string }) => |
| 124 | newTaskFilteredProject(makeScope([mac, server]), null, connected, last); |
| 125 | expect(filtered({ environmentId: "server", projectId: "t3code" })).toEqual({ |
| 126 | environmentId: "server", |
| 127 | projectId: "t3code", |
| 128 | }); |
| 129 | expect(filtered({ environmentId: "server", projectId: "other" })).toEqual({ |
| 130 | environmentId: "mac", |
| 131 | projectId: "t3code", |
| 132 | }); |
| 133 | }); |
| 134 | }); |
| 135 | |
| 136 | describe("resolveEnvironmentProjectMatch", () => { |
| 137 | it("follows the same repository onto the target machine", () => { |
| 138 | const selected = makeProject("t3code", "mac", { repositoryKey: "github.com/t3tools/t3code" }); |
| 139 | const target = [ |
| 140 | makeProject("other", "server", { repositoryKey: "github.com/t3tools/other" }), |
| 141 | makeProject("t3code-clone", "server", { repositoryKey: "github.com/t3tools/t3code" }), |
| 142 | ]; |
| 143 | expect(resolveEnvironmentProjectMatch(target, selected)).toBe(target[1]); |
| 144 | expect(isSameRepository(target[1]!, selected)).toBe(true); |
| 145 | }); |
| 146 | |
| 147 | it("falls back to workspace basename, then title, for unindexed projects", () => { |
| 148 | const selected = makeProject("t3code", "mac", { workspaceRoot: "/Users/me/t3code" }); |
| 149 | const byBasename = [ |
| 150 | makeProject("other", "server"), |
| 151 | makeProject("srv", "server", { workspaceRoot: "/home/me/t3code" }), |
| 152 | ]; |
| 153 | expect(resolveEnvironmentProjectMatch(byBasename, selected)).toBe(byBasename[1]); |
| 154 | expect(isSameRepository(byBasename[1]!, selected)).toBe(false); |
| 155 | |
| 156 | const byTitle = [ |
| 157 | makeProject("other", "server"), |
| 158 | makeProject("srv", "server", { title: "t3code" }), |
| 159 | ]; |
| 160 | expect(resolveEnvironmentProjectMatch(byTitle, selected)).toBe(byTitle[1]); |
| 161 | }); |
| 162 | |
| 163 | it("does not treat a known different repository as a basename or title match", () => { |
| 164 | const selected = makeProject("t3code", "mac", { |
| 165 | repositoryKey: "github.com/t3tools/t3code", |
| 166 | workspaceRoot: "/Users/me/t3code", |
| 167 | }); |
| 168 | const fork = makeProject("fork", "server", { |
| 169 | repositoryKey: "github.com/someone/t3code", |
| 170 | title: "t3code", |
| 171 | workspaceRoot: "/home/me/t3code", |
| 172 | }); |
| 173 | const unindexed = makeProject("unindexed", "server", { workspaceRoot: "/srv/t3code" }); |
| 174 | expect(resolveEnvironmentProjectMatch([fork, unindexed], selected)).toBe(unindexed); |
| 175 | expect(resolveEnvironmentProjectMatch([fork], selected)).toBe(fork); |
| 176 | expect(isSameRepository(fork, selected)).toBe(false); |
| 177 | }); |
| 178 | }); |
| 179 | |
| 180 | describe("resolveDraftProjectSelection", () => { |
| 181 | const projects = [makeProject("t3code"), makeProject("t3code-2")]; |
| 182 | const draft = { incomingShareId: undefined, pickerClosed: false }; |
| 183 | |
| 184 | it("opens the picker instead of auto-selecting the only project", () => { |
| 185 | expect(resolveDraftProjectSelection(null, projects, [makeScope(projects)], draft)).toEqual({ |
| 186 | kind: "pick", |
| 187 | }); |
| 188 | }); |
| 189 | |
| 190 | it("selects the only logical project for shared content", () => { |
| 191 | expect( |
| 192 | resolveDraftProjectSelection(null, projects, [makeScope(projects)], { |
| 193 | ...draft, |
| 194 | incomingShareId: "share", |
| 195 | }), |
| 196 | ).toEqual({ kind: "select", project: projects[0] }); |
| 197 | }); |
| 198 | |
| 199 | it("dismisses the draft when the first picker closes without a choice", () => { |
| 200 | expect( |
| 201 | resolveDraftProjectSelection(null, projects, [makeScope(projects)], { |
| 202 | ...draft, |
| 203 | pickerClosed: true, |
| 204 | }), |
| 205 | ).toEqual({ kind: "dismiss" }); |
| 206 | }); |
| 207 | |
| 208 | it("keeps the draft when the picker closes after a choice", () => { |
| 209 | expect( |
| 210 | resolveDraftProjectSelection("environment:t3code-2", projects, [makeScope(projects)], { |
| 211 | ...draft, |
| 212 | pickerClosed: true, |
| 213 | }), |
| 214 | ).toEqual({ kind: "preserve" }); |
| 215 | }); |
| 216 | |
| 217 | it("keeps a share draft asking for a project when its picker closes without a choice", () => { |
| 218 | const scopes = [makeScope([projects[0]!]), makeScope([projects[1]!])]; |
| 219 | expect( |
| 220 | resolveDraftProjectSelection(null, projects, scopes, { |
| 221 | incomingShareId: "share", |
| 222 | pickerClosed: true, |
| 223 | }), |
| 224 | ).toEqual({ kind: "pick" }); |
| 225 | }); |
| 226 | |
| 227 | it("starts a share draft in the last project when Use last project is on", () => { |
| 228 | const scopes = [makeScope([projects[0]!]), makeScope([projects[1]!])]; |
| 229 | const shareDraft = { |
| 230 | incomingShareId: "share", |
| 231 | pickerClosed: false, |
| 232 | useLastProject: undefined, |
| 233 | lastProject: { environmentId: "environment", projectId: "t3code-2" }, |
| 234 | }; |
| 235 | expect(resolveDraftProjectSelection(null, projects, scopes, shareDraft)).toEqual({ |
| 236 | kind: "select", |
| 237 | project: projects[1], |
| 238 | }); |
| 239 | expect( |
| 240 | resolveDraftProjectSelection(null, projects, scopes, { |
| 241 | ...shareDraft, |
| 242 | useLastProject: false, |
| 243 | }), |
| 244 | ).toEqual({ kind: "pick" }); |
| 245 | const archived = [projects[0]!, { ...projects[1]!, archivedAt: "2026-07-02T00:00:00.000Z" }]; |
| 246 | expect(resolveDraftProjectSelection(null, archived, scopes, shareDraft)).toEqual({ |
| 247 | kind: "pick", |
| 248 | }); |
| 249 | }); |
| 250 | }); |
| 251 | |
| 252 | describe("newTaskDestination", () => { |
| 253 | const picker = { draft: { requestId: "request" } }; |
| 254 | |
| 255 | function destination(options: { |
| 256 | readonly useLastProject: boolean | undefined; |
| 257 | readonly projects: ReadonlyArray<EnvironmentProject>; |
| 258 | readonly current?: EnvironmentProject; |
| 259 | readonly filtered?: EnvironmentProject; |
| 260 | readonly last?: { readonly environmentId: string; readonly projectId: string }; |
| 261 | }) { |
| 262 | return newTaskDestination({ |
| 263 | projects: options.projects, |
| 264 | requestId: "request", |
| 265 | useLastProject: options.useLastProject, |
| 266 | currentProject: options.current && { |
| 267 | environmentId: options.current.environmentId, |
| 268 | projectId: options.current.id, |
| 269 | }, |
| 270 | filteredProject: options.filtered && { |
| 271 | environmentId: options.filtered.environmentId, |
| 272 | projectId: options.filtered.id, |
| 273 | }, |
| 274 | lastProject: options.last, |
| 275 | }); |
| 276 | } |
| 277 | |
| 278 | it("starts in the current thread's project ahead of the filtered and remembered ones", () => { |
| 279 | const projects = [makeProject("remembered"), makeProject("current", "other-environment")]; |
| 280 | expect( |
| 281 | destination({ |
| 282 | useLastProject: true, |
| 283 | projects, |
| 284 | current: projects[1], |
| 285 | filtered: projects[0], |
| 286 | last: { environmentId: "environment", projectId: "remembered" }, |
| 287 | }), |
| 288 | ).toEqual({ |
| 289 | draft: { requestId: "request", environmentId: "other-environment", projectId: "current" }, |
| 290 | }); |
| 291 | }); |
| 292 | |
| 293 | it("starts in the thread list's filtered project ahead of the remembered one", () => { |
| 294 | const projects = [makeProject("bravo"), makeProject("alpha")]; |
| 295 | expect( |
| 296 | destination({ |
| 297 | useLastProject: true, |
| 298 | projects, |
| 299 | filtered: projects[1], |
| 300 | last: { environmentId: "environment", projectId: "bravo" }, |
| 301 | }), |
| 302 | ).toEqual({ |
| 303 | draft: { requestId: "request", environmentId: "environment", projectId: "alpha" }, |
| 304 | }); |
| 305 | }); |
| 306 | |
| 307 | it("opens the picker for an archived last project, but keeps an archived current one", () => { |
| 308 | const archived = { ...makeProject("archived"), archivedAt: "2026-01-01T00:00:00.000Z" }; |
| 309 | expect( |
| 310 | destination({ |
| 311 | useLastProject: true, |
| 312 | projects: [archived], |
| 313 | last: { environmentId: "environment", projectId: "archived" }, |
| 314 | }), |
| 315 | ).toEqual(picker); |
| 316 | expect(destination({ useLastProject: true, projects: [archived], current: archived })).toEqual({ |
| 317 | draft: { requestId: "request", environmentId: "environment", projectId: "archived" }, |
| 318 | }); |
| 319 | }); |
| 320 | |
| 321 | it("treats an unset preference as on", () => { |
| 322 | const projects = [makeProject("first"), makeProject("current")]; |
| 323 | expect(destination({ useLastProject: undefined, projects, current: projects[1] })).toEqual({ |
| 324 | draft: { requestId: "request", environmentId: "environment", projectId: "current" }, |
| 325 | }); |
| 326 | }); |
| 327 | |
| 328 | it("starts in the remembered project of its own environment when there is no current one", () => { |
| 329 | const projects = [makeProject("chosen"), makeProject("chosen", "other-environment")]; |
| 330 | expect( |
| 331 | destination({ |
| 332 | useLastProject: true, |
| 333 | projects, |
| 334 | last: { environmentId: "other-environment", projectId: "chosen" }, |
| 335 | }), |
| 336 | ).toEqual({ |
| 337 | draft: { requestId: "request", environmentId: "other-environment", projectId: "chosen" }, |
| 338 | }); |
| 339 | }); |
| 340 | |
| 341 | it("ignores a remembered project id from another environment", () => { |
| 342 | expect( |
| 343 | destination({ |
| 344 | useLastProject: true, |
| 345 | projects: [makeProject("chosen")], |
| 346 | last: { environmentId: "removed-environment", projectId: "chosen" }, |
| 347 | }), |
| 348 | ).toEqual(picker); |
| 349 | }); |
| 350 | |
| 351 | it("opens the picker rather than the first project when nothing matches", () => { |
| 352 | expect( |
| 353 | destination({ |
| 354 | useLastProject: true, |
| 355 | projects: [makeProject("first"), makeProject("second")], |
| 356 | current: makeProject("removed"), |
| 357 | }), |
| 358 | ).toEqual(picker); |
| 359 | }); |
| 360 | |
| 361 | it("always opens the picker when the preference is off", () => { |
| 362 | const only = makeProject("only"); |
| 363 | expect( |
| 364 | destination({ |
| 365 | useLastProject: false, |
| 366 | projects: [only], |
| 367 | current: only, |
| 368 | filtered: only, |
| 369 | last: { environmentId: "environment", projectId: "only" }, |
| 370 | }), |
| 371 | ).toEqual(picker); |
| 372 | }); |
| 373 | }); |
| 374 | |
| 375 | describe("resolveNewTaskScratchOption", () => { |
| 376 | const environmentId = EnvironmentId.make("environment"); |
| 377 | const otherId = EnvironmentId.make("other"); |
| 378 | const configs = new Map([[environmentId, { scratchWorkspaceRoot: "/scratch" }]]); |
| 379 | |
| 380 | it("offers the option from the cached config while the environment is still connecting", () => { |
| 381 | expect( |
| 382 | resolveNewTaskScratchOption({ |
| 383 | environments: [{ environmentId, connectionState: "connecting" }], |
| 384 | serverConfigs: configs, |
| 385 | selectedEnvironmentId: null, |
| 386 | }), |
| 387 | ).toEqual({ environmentId, ready: false }); |
| 388 | }); |
| 389 | |
| 390 | it("becomes ready without changing environment once connected", () => { |
| 391 | expect( |
| 392 | resolveNewTaskScratchOption({ |
| 393 | environments: [{ environmentId, connectionState: "connected" }], |
| 394 | serverConfigs: configs, |
| 395 | selectedEnvironmentId: null, |
| 396 | }), |
| 397 | ).toEqual({ environmentId, ready: true }); |
| 398 | }); |
| 399 | |
| 400 | it("hides the option when no environment reports a scratch root", () => { |
| 401 | expect( |
| 402 | resolveNewTaskScratchOption({ |
| 403 | environments: [{ environmentId, connectionState: "connected" }], |
| 404 | serverConfigs: new Map([[environmentId, {}]]), |
| 405 | selectedEnvironmentId: null, |
| 406 | }), |
| 407 | ).toBeNull(); |
| 408 | expect( |
| 409 | resolveNewTaskScratchOption({ |
| 410 | environments: [{ environmentId, connectionState: "connected" }], |
| 411 | serverConfigs: new Map(), |
| 412 | selectedEnvironmentId: null, |
| 413 | }), |
| 414 | ).toBeNull(); |
| 415 | }); |
| 416 | |
| 417 | it("prefers a connected environment over the selected one that is still reconnecting", () => { |
| 418 | expect( |
| 419 | resolveNewTaskScratchOption({ |
| 420 | environments: [ |
| 421 | { environmentId, connectionState: "reconnecting" }, |
| 422 | { environmentId: otherId, connectionState: "connected" }, |
| 423 | ], |
| 424 | serverConfigs: new Map([ |
| 425 | [environmentId, { scratchWorkspaceRoot: "/scratch" }], |
| 426 | [otherId, { scratchWorkspaceRoot: "/other-scratch" }], |
| 427 | ]), |
| 428 | selectedEnvironmentId: environmentId, |
| 429 | }), |
| 430 | ).toEqual({ environmentId: otherId, ready: true }); |
| 431 | }); |
| 432 | |
| 433 | it("keeps the selected environment when every offering environment is connected", () => { |
| 434 | expect( |
| 435 | resolveNewTaskScratchOption({ |
| 436 | environments: [ |
| 437 | { environmentId: otherId, connectionState: "connected" }, |
| 438 | { environmentId, connectionState: "connected" }, |
| 439 | ], |
| 440 | serverConfigs: new Map([ |
| 441 | [environmentId, { scratchWorkspaceRoot: "/scratch" }], |
| 442 | [otherId, { scratchWorkspaceRoot: "/other-scratch" }], |
| 443 | ]), |
| 444 | selectedEnvironmentId: environmentId, |
| 445 | }), |
| 446 | ).toEqual({ environmentId, ready: true }); |
| 447 | }); |
| 448 | }); |