Skip to content
Closed
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
93 changes: 93 additions & 0 deletions apps/server/src/cli/consoleStdin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";

const io = vi.hoisted(() => ({
platform: vi.fn(() => "win32"),
stat: vi.fn(() => ({ isCharacterDevice: (): boolean => true })),
open: vi.fn(() => 42),
close: vi.fn(),
isatty: vi.fn((fd: number) => fd !== 0),
}));

vi.mock("node:fs", () => ({ fstatSync: io.stat, openSync: io.open, closeSync: io.close }));
vi.mock("node:tty", () => ({
isatty: io.isatty,
ReadStream: class {
readonly isTTY = true;
},
}));
vi.mock("@t3tools/shared/HostProcess", () => ({ Platform: { defaultValue: io.platform } }));

const originalStdin = Object.getOwnPropertyDescriptor(process, "stdin")!;

beforeEach(() => {
vi.resetModules();
vi.resetAllMocks();
vi.stubEnv("T3CODE_DESKTOP_CLI_LAUNCHER", "1");
});

afterEach(() => {
Object.defineProperty(process, "stdin", originalStdin);
vi.unstubAllEnvs();
});

describe("attachConsoleStdin", () => {
it("reopens the console when Node replaces missing Windows stdin with NUL", async () => {
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");

expect(attachConsoleStdin()).toBe(42);
expect(process.stdin.isTTY).toBe(true);
expect(interactiveStdin()).toBe(42);
expect(process.env.T3CODE_DESKTOP_CLI_LAUNCHER).toBeUndefined();
});

it("still reopens a closed stdin", async () => {
io.stat.mockImplementation(() => {
throw new Error("EBADF");
});
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
expect(attachConsoleStdin()).toBe(42);
expect(interactiveStdin()).toBe(42);
});

it.each(["pipe", "file"])("preserves redirected %s input", async () => {
io.stat.mockReturnValue({ isCharacterDevice: () => false });
const stdin = process.stdin;
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
expect(attachConsoleStdin()).toBeUndefined();
expect(process.stdin).toBe(stdin);
expect(interactiveStdin()).toBe("inherit");
expect(io.open).not.toHaveBeenCalled();
});

it.each(["console input", "redirected output", "unmarked process", "non-Windows"])(
"leaves %s alone",
async (scenario) => {
if (scenario === "console input") io.isatty.mockReturnValue(true);
if (scenario === "redirected output") io.isatty.mockReturnValue(false);
if (scenario === "unmarked process") delete process.env.T3CODE_DESKTOP_CLI_LAUNCHER;
if (scenario === "non-Windows") io.platform.mockReturnValue("darwin");
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
expect(attachConsoleStdin()).toBeUndefined();
expect(interactiveStdin()).toBe("inherit");
expect(io.open).not.toHaveBeenCalled();
expect(process.env.T3CODE_DESKTOP_CLI_LAUNCHER).toBeUndefined();
},
);

it("keeps inherited input when CONIN$ cannot be opened", async () => {
io.open.mockImplementation(() => {
throw new Error("ENOENT");
});
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
expect(attachConsoleStdin()).toBeUndefined();
expect(interactiveStdin()).toBe("inherit");
});

it("closes a reopened descriptor that is not a console", async () => {
io.isatty.mockImplementation((fd) => fd === 1);
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
expect(attachConsoleStdin()).toBeUndefined();
expect(interactiveStdin()).toBe("inherit");
expect(io.close).toHaveBeenCalledWith(42);
});
});
21 changes: 11 additions & 10 deletions apps/server/src/cli/consoleStdin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
* `ELECTRON_RUN_AS_NODE`, and Electron's Windows executable is a GUI program:
* on start it points stdout and stderr at the parent's console but leaves
* stdin unset, so prompts see no terminal and children such as the agent
* `t3 triage` starts inherit nothing to read from. Input the user piped or
* redirected is a valid descriptor and is left alone, as is any run whose
* output is not a console (the desktop backend, scripts).
* `t3 triage` starts inherit nothing to read from. Node replaces missing
* descriptors with NUL, an open non-console character device. Piped input
* and files are left alone, as is any run whose output is not a console.
* Explicit `< NUL` is indistinguishable from missing launcher stdin and
* also reattaches when output is a console.
*/
function needsConsoleStdin(input: {
readonly platform: NodeJS.Platform;
readonly env: NodeJS.ProcessEnv;
readonly stdinOpen: boolean;
readonly stdinMissing: boolean;
readonly stdoutIsConsole: boolean;
}): boolean {
return (
input.platform === "win32" &&
// Set only by the launcher (DesktopCliShim), never for the desktop backend.
input.env.T3CODE_DESKTOP_CLI_LAUNCHER === "1" &&
!input.stdinOpen &&
input.stdinMissing &&
input.stdoutIsConsole
);
}

const isOpen = (fd: number) => {
const isMissingInput = (fd: number) => {
try {
NodeFS.fstatSync(fd);
return true;
return NodeFS.fstatSync(fd).isCharacterDevice() && !NodeTty.isatty(fd);
} catch {
return false;
return true;
}
};

Expand All @@ -48,7 +49,7 @@ export function attachConsoleStdin(): number | undefined {
const needed = needsConsoleStdin({
platform: HostProcess.Platform.defaultValue(),
env: process.env,
stdinOpen: isOpen(0),
stdinMissing: isMissingInput(0),
stdoutIsConsole: NodeTty.isatty(1),
});
// The marker describes this process's own start; a server started here
Expand Down
Loading