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
53 changes: 50 additions & 3 deletions apps/server/src/terminalManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ describe("TerminalManager", () => {

function makeManager(
historyLineLimit = 5,
options: { shellResolver?: () => string } = {},
options: {
shellResolver?: () => string;
subprocessChecker?: (terminalPid: number) => Promise<boolean>;
subprocessPollIntervalMs?: number;
} = {},
) {
const logsDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3code-terminal-"));
tempDirs.push(logsDir);
Expand All @@ -147,6 +151,10 @@ describe("TerminalManager", () => {
ptyAdapter,
historyLineLimit,
shellResolver: options.shellResolver ?? (() => "/bin/bash"),
...(options.subprocessChecker ? { subprocessChecker: options.subprocessChecker } : {}),
...(options.subprocessPollIntervalMs
? { subprocessPollIntervalMs: options.subprocessPollIntervalMs }
: {}),
});
return { logsDir, ptyAdapter, manager };
}
Expand Down Expand Up @@ -273,6 +281,42 @@ describe("TerminalManager", () => {
manager.dispose();
});

it("emits subprocess activity events when child-process state changes", async () => {
let hasRunningSubprocess = false;
const { manager } = makeManager(5, {
subprocessChecker: async () => hasRunningSubprocess,
subprocessPollIntervalMs: 20,
});
const events: TerminalEvent[] = [];
manager.on("event", (event) => {
events.push(event);
});

await manager.open(openInput());
await waitFor(() => events.some((event) => event.type === "started"));
expect(events.some((event) => event.type === "activity")).toBe(false);

hasRunningSubprocess = true;
await waitFor(
() =>
events.some(
(event) => event.type === "activity" && event.hasRunningSubprocess === true,
),
1_200,
);

hasRunningSubprocess = false;
await waitFor(
() =>
events.some(
(event) => event.type === "activity" && event.hasRunningSubprocess === false,
),
1_200,
);

manager.dispose();
});

it("caps persisted history to configured line limit", async () => {
const { manager, ptyAdapter } = makeManager(3);
await manager.open(openInput());
Expand Down Expand Up @@ -334,15 +378,18 @@ describe("TerminalManager", () => {
manager.dispose();
});

it("loads existing legacy transcript filenames and keeps new naming for writes", async () => {
it("migrates legacy transcript filenames to terminal-scoped history path on open", async () => {
const { manager, logsDir } = makeManager();
const legacyPath = path.join(logsDir, "thread-1.log");
const nextPath = historyLogPath(logsDir);
fs.writeFileSync(legacyPath, "legacy-line\n", "utf8");

const snapshot = await manager.open(openInput());

expect(snapshot.history).toBe("legacy-line\n");
expect(fs.existsSync(legacyPath)).toBe(true);
expect(fs.existsSync(nextPath)).toBe(true);
expect(fs.readFileSync(nextPath, "utf8")).toBe("legacy-line\n");
expect(fs.existsSync(legacyPath)).toBe(false);

manager.dispose();
});
Expand Down
Loading