| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 3 | |
| 4 | import { |
| 5 | resolvePreferredThreadWorktreePath, |
| 6 | resolveTerminalOpenLocation, |
| 7 | stagePendingTerminalLaunch, |
| 8 | takePendingTerminalLaunch, |
| 9 | } from "./terminalLaunchContext"; |
| 10 | |
| 11 | describe("resolvePreferredThreadWorktreePath", () => { |
| 12 | it("prefers thread detail worktree paths over thread shell paths", () => { |
| 13 | expect( |
| 14 | resolvePreferredThreadWorktreePath({ |
| 15 | threadShellWorktreePath: "/repo/root", |
| 16 | threadDetailWorktreePath: "/repo/worktrees/feature", |
| 17 | }), |
| 18 | ).toBe("/repo/worktrees/feature"); |
| 19 | }); |
| 20 | |
| 21 | it("falls back to the thread shell worktree path when detail is unavailable", () => { |
| 22 | expect( |
| 23 | resolvePreferredThreadWorktreePath({ |
| 24 | threadShellWorktreePath: "/repo/worktrees/feature", |
| 25 | threadDetailWorktreePath: null, |
| 26 | }), |
| 27 | ).toBe("/repo/worktrees/feature"); |
| 28 | }); |
| 29 | }); |
| 30 | |
| 31 | describe("resolveTerminalOpenLocation", () => { |
| 32 | it("uses the thread detail worktree path before the workspace root for a fresh mobile open", () => { |
| 33 | expect( |
| 34 | resolveTerminalOpenLocation({ |
| 35 | terminalLocation: null, |
| 36 | activeSessionLocation: null, |
| 37 | workspaceRoot: "/repo/root", |
| 38 | threadShellWorktreePath: null, |
| 39 | threadDetailWorktreePath: "/repo/worktrees/feature", |
| 40 | }), |
| 41 | ).toEqual({ |
| 42 | cwd: "/repo/worktrees/feature", |
| 43 | worktreePath: "/repo/worktrees/feature", |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | it("preserves the running terminal snapshot cwd when attaching to an existing session", () => { |
| 48 | expect( |
| 49 | resolveTerminalOpenLocation({ |
| 50 | terminalLocation: null, |
| 51 | activeSessionLocation: { |
| 52 | cwd: "/repo/worktrees/feature", |
| 53 | worktreePath: "/repo/worktrees/feature", |
| 54 | }, |
| 55 | workspaceRoot: "/repo/root", |
| 56 | threadShellWorktreePath: null, |
| 57 | threadDetailWorktreePath: "/repo/worktrees/other", |
| 58 | }), |
| 59 | ).toEqual({ |
| 60 | cwd: "/repo/worktrees/feature", |
| 61 | worktreePath: "/repo/worktrees/feature", |
| 62 | }); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe("pending terminal launches", () => { |
| 67 | it("stages and consumes launch details for a specific terminal target", () => { |
| 68 | const target = { |
| 69 | environmentId: EnvironmentId.make("env-1"), |
| 70 | threadId: ThreadId.make("thread-1"), |
| 71 | terminalId: "term-2", |
| 72 | }; |
| 73 | |
| 74 | stagePendingTerminalLaunch({ |
| 75 | target, |
| 76 | launch: { |
| 77 | cwd: "/repo/worktrees/feature", |
| 78 | worktreePath: "/repo/worktrees/feature", |
| 79 | env: { FOO: "bar" }, |
| 80 | initialInput: "pnpm dev\r", |
| 81 | }, |
| 82 | }); |
| 83 | |
| 84 | expect(takePendingTerminalLaunch(target)).toEqual({ |
| 85 | cwd: "/repo/worktrees/feature", |
| 86 | worktreePath: "/repo/worktrees/feature", |
| 87 | env: { FOO: "bar" }, |
| 88 | initialInput: "pnpm dev\r", |
| 89 | }); |
| 90 | expect(takePendingTerminalLaunch(target)).toBeNull(); |
| 91 | }); |
| 92 | |
| 93 | it("keeps pending launches isolated per terminal target", () => { |
| 94 | const primaryTarget = { |
| 95 | environmentId: EnvironmentId.make("env-1"), |
| 96 | threadId: ThreadId.make("thread-1"), |
| 97 | terminalId: "term-2", |
| 98 | }; |
| 99 | const otherTarget = { |
| 100 | environmentId: EnvironmentId.make("env-1"), |
| 101 | threadId: ThreadId.make("thread-1"), |
| 102 | terminalId: "term-3", |
| 103 | }; |
| 104 | |
| 105 | stagePendingTerminalLaunch({ |
| 106 | target: primaryTarget, |
| 107 | launch: { |
| 108 | cwd: "/repo/root", |
| 109 | worktreePath: null, |
| 110 | initialInput: "pnpm i\r", |
| 111 | }, |
| 112 | }); |
| 113 | |
| 114 | expect(takePendingTerminalLaunch(otherTarget)).toBeNull(); |
| 115 | expect(takePendingTerminalLaunch(primaryTarget)).toEqual({ |
| 116 | cwd: "/repo/root", |
| 117 | worktreePath: null, |
| 118 | env: undefined, |
| 119 | initialInput: "pnpm i\r", |
| 120 | }); |
| 121 | }); |
| 122 | }); |