Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions apps/mobile/src/features/terminal/terminalLaunchContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EnvironmentId, ThreadId } from "@t3tools/contracts";

import {
resolveTerminalOpenLocation,
stageCommandTerminalLaunch,
stagePendingTerminalLaunch,
takePendingTerminalLaunch,
} from "./terminalLaunchContext";
Expand Down Expand Up @@ -55,6 +56,62 @@ describe("resolveTerminalOpenLocation", () => {
});
});

describe("command terminal launches", () => {
it("stages a command in the detail worktree with the project runtime environment", () => {
const target = {
environmentId: EnvironmentId.make("env-command"),
threadId: ThreadId.make("thread-command"),
terminalId: "term-follow-worktree",
};
const launch = stageCommandTerminalLaunch({
target,
workspaceRoot: "/repo/root",
threadShellWorktreePath: "/repo/worktrees/stale",
threadDetailWorktreePath: "/repo/worktrees/feature",
command: "tail -n +1 -F '/tmp/task.output'",
});

expect(launch).toEqual({
cwd: "/repo/worktrees/feature",
worktreePath: "/repo/worktrees/feature",
env: {
T3CODE_PROJECT_ROOT: "/repo/root",
T3CODE_WORKTREE_PATH: "/repo/worktrees/feature",
},
initialInput: " tail -n +1 -F '/tmp/task.output'\r",
});
expect(takePendingTerminalLaunch(target)).toEqual(launch);
expect(takePendingTerminalLaunch(target)).toBeNull();
});

it("falls back to the shell worktree and then the project checkout", () => {
for (const worktreePath of ["/repo/worktrees/feature", null]) {
const target = {
environmentId: EnvironmentId.make("env-command"),
threadId: ThreadId.make("thread-command"),
terminalId: worktreePath === null ? "term-project" : "term-shell-worktree",
};
stageCommandTerminalLaunch({
target,
workspaceRoot: "/repo/root",
threadShellWorktreePath: worktreePath,
threadDetailWorktreePath: null,
command: "printf 'hello'",
});

expect(takePendingTerminalLaunch(target)).toEqual({
cwd: worktreePath ?? "/repo/root",
worktreePath,
env: {
T3CODE_PROJECT_ROOT: "/repo/root",
...(worktreePath ? { T3CODE_WORKTREE_PATH: worktreePath } : {}),
},
initialInput: " printf 'hello'\r",
});
}
});
});

describe("pending terminal launches", () => {
it("stages and consumes launch details for a specific terminal target", () => {
const target = {
Expand Down
24 changes: 24 additions & 0 deletions apps/mobile/src/features/terminal/terminalLaunchContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
import {
projectScriptCwd,
projectScriptRuntimeEnv,
projectScriptTerminalInput,
} from "@t3tools/shared/projectScripts";
import { useState } from "react";

interface TerminalLocationLike {
Expand Down Expand Up @@ -37,6 +42,25 @@ export function stagePendingTerminalLaunch(input: {
});
}

export function stageCommandTerminalLaunch(input: {
readonly target: PendingTerminalLaunchTarget;
readonly workspaceRoot: string;
readonly threadShellWorktreePath: string | null;
readonly threadDetailWorktreePath: string | null;
readonly command: string;
}) {
const worktreePath = resolvePreferredThreadWorktreePath(input);
const project = { cwd: input.workspaceRoot };
const launch = {
cwd: projectScriptCwd({ project, worktreePath }),
worktreePath,
env: projectScriptRuntimeEnv({ project, worktreePath }),
initialInput: projectScriptTerminalInput(input.command),
};
stagePendingTerminalLaunch({ target: input.target, launch });
return launch;
}

export function takePendingTerminalLaunch(
target: PendingTerminalLaunchTarget,
): PendingTerminalLaunch | null {
Expand Down
Loading