Skip to content

Commit 8ff45db

Browse files
fix(mobile): replay terminal size after access changes
1 parent 540e083 commit 8ff45db

3 files changed

Lines changed: 204 additions & 17 deletions

File tree

‎apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import {
8484
} from "./terminalInput";
8585
import { createTerminalPasteSession } from "./terminalPaste";
8686
import { cacheTerminalGridSize, getCachedTerminalGridSize } from "./terminalUiState";
87+
import { useTerminalGridSync } from "./useTerminalGridSync";
8788

8889
const DEFAULT_TERMINAL_COLS = 80;
8990
const DEFAULT_TERMINAL_ROWS = 24;
@@ -359,6 +360,15 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
359360
const terminalKey = selectedThread
360361
? `${selectedThread.environmentId}:${selectedThread.id}:${terminalId}`
361362
: terminalId;
363+
useTerminalGridSync({
364+
environmentId: selectedThread?.environmentId ?? null,
365+
threadId: selectedThread?.id ?? null,
366+
terminalId,
367+
canOperate: canOperateTerminal,
368+
terminal,
369+
size: lastGridSize,
370+
resize: resizeTerminal,
371+
});
362372
const bufferReplayKey = useMemo(
363373
() => getTerminalBufferReplayKey({ terminalKey, fontSize }),
364374
[fontSize, terminalKey],
@@ -829,32 +839,15 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
829839
}
830840

831841
setLastGridSize(size);
832-
if (!canOperateTerminal || !selectedThread || !isRunning) {
833-
return;
834-
}
835-
836-
void resizeTerminal({
837-
environmentId: selectedThread.environmentId,
838-
input: {
839-
threadId: selectedThread.id,
840-
terminalId,
841-
cols: size.cols,
842-
rows: size.rows,
843-
},
844-
});
845842
},
846843
[
847-
isRunning,
848844
lastGridSize.cols,
849845
lastGridSize.rows,
850846
bufferReplayKey,
851847
readyBufferReplayKey,
852848
routeEnvironmentId,
853849
routeThreadId,
854-
resizeTerminal,
855-
canOperateTerminal,
856850
scheduleBufferReplayReady,
857-
selectedThread,
858851
terminalId,
859852
terminalKey,
860853
],
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import * as NodeModule from "node:module";
2+
import { EMPTY_TERMINAL_BUFFER_STATE } from "@t3tools/client-runtime/state/terminal";
3+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
4+
import { act, createElement, type ReactNode } from "react";
5+
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6+
7+
import { useTerminalGridSync } from "./useTerminalGridSync";
8+
9+
// Mobile already depends on ReactDOM but does not install its browser type declarations.
10+
const { createRoot } = NodeModule.createRequire(import.meta.url)("react-dom/client") as {
11+
createRoot(container: Element): {
12+
render(children: ReactNode): void;
13+
unmount(): void;
14+
};
15+
};
16+
17+
type Input = Parameters<typeof useTerminalGridSync>[0];
18+
let root: ReturnType<typeof createRoot>;
19+
const resize = vi.fn<Input["resize"]>();
20+
21+
function GridProbe(input: Input) {
22+
useTerminalGridSync(input);
23+
return null;
24+
}
25+
26+
function session({
27+
generation = 1,
28+
version = 1,
29+
status = "running",
30+
}: {
31+
readonly generation?: number;
32+
readonly version?: number;
33+
readonly status?: Input["terminal"]["status"];
34+
} = {}): Input["terminal"] {
35+
return { output: { ...EMPTY_TERMINAL_BUFFER_STATE.output, generation }, version, status };
36+
}
37+
38+
function input(): Input {
39+
return {
40+
environmentId: EnvironmentId.make("environment"),
41+
threadId: ThreadId.make("thread"),
42+
terminalId: "term-1",
43+
canOperate: true,
44+
terminal: session(),
45+
size: { cols: 80, rows: 24 },
46+
resize,
47+
};
48+
}
49+
50+
function render(value: Input) {
51+
return act(() => root.render(createElement(GridProbe, value)));
52+
}
53+
54+
beforeEach(() => {
55+
resize.mockClear();
56+
// The probe renders no DOM, but ReactDOM needs an event target to run real effects.
57+
const document = {
58+
nodeType: 9,
59+
addEventListener() {},
60+
removeEventListener() {},
61+
};
62+
const container = {
63+
nodeType: 1,
64+
tagName: "DIV",
65+
namespaceURI: "http://www.w3.org/1999/xhtml",
66+
ownerDocument: document,
67+
addEventListener() {},
68+
removeEventListener() {},
69+
};
70+
vi.stubGlobal("document", document);
71+
vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget });
72+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
73+
root = createRoot(container as unknown as HTMLElement);
74+
});
75+
76+
afterEach(async () => {
77+
await act(() => root.unmount());
78+
vi.unstubAllGlobals();
79+
});
80+
81+
it("replays an observer's measured grid once its writable attachment snapshot arrives", async () => {
82+
let current = { ...input(), canOperate: false };
83+
await render(current);
84+
current = { ...current, size: { cols: 120, rows: 40 } };
85+
await render(current);
86+
expect(resize).not.toHaveBeenCalled();
87+
88+
current = {
89+
...current,
90+
canOperate: true,
91+
terminal: session({ generation: 2, version: 0 }),
92+
};
93+
await render(current);
94+
expect(resize).not.toHaveBeenCalled();
95+
96+
current = { ...current, terminal: session({ generation: 2 }) };
97+
await render(current);
98+
expect(resize).toHaveBeenCalledExactlyOnceWith({
99+
environmentId: "environment",
100+
input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 40 },
101+
});
102+
103+
await render({ ...current, terminal: session({ generation: 2, version: 3 }) });
104+
await render({ ...current, size: { ...current.size } });
105+
expect(resize).toHaveBeenCalledOnce();
106+
107+
await render({ ...current, size: { cols: 120, rows: 41 } });
108+
expect(resize).toHaveBeenCalledTimes(2);
109+
expect(resize).toHaveBeenLastCalledWith({
110+
environmentId: "environment",
111+
input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 41 },
112+
});
113+
});
114+
115+
it("replays an unchanged grid after reconnecting with a new attachment generation", async () => {
116+
const current = { ...input(), size: { cols: 120, rows: 40 } };
117+
await render(current);
118+
expect(resize).toHaveBeenCalledOnce();
119+
120+
await render({ ...current, terminal: session({ generation: 2, version: 0 }) });
121+
expect(resize).toHaveBeenCalledOnce();
122+
123+
await render({ ...current, terminal: session({ generation: 2 }) });
124+
expect(resize).toHaveBeenCalledTimes(2);
125+
expect(resize).toHaveBeenLastCalledWith({
126+
environmentId: "environment",
127+
input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 40 },
128+
});
129+
130+
// React may observe the next snapshot without rendering the empty seed first.
131+
await render({ ...current, terminal: session({ generation: 3 }) });
132+
expect(resize).toHaveBeenCalledTimes(3);
133+
});
134+
135+
it.each([
136+
{ name: "permission is revoked", change: { canOperate: false } },
137+
{ name: "the attachment is pending", change: { terminal: session({ version: 0 }) } },
138+
{ name: "the process has exited", change: { terminal: session({ status: "exited" }) } },
139+
])("retains measurements without resizing while $name", async ({ change }) => {
140+
const current = input();
141+
await render(current);
142+
expect(resize).toHaveBeenCalledOnce();
143+
144+
await render({ ...current, ...change, size: { cols: 120, rows: 40 } });
145+
await render({ ...current, ...change, size: { cols: 120, rows: 41 } });
146+
expect(resize).toHaveBeenCalledOnce();
147+
148+
await render({ ...current, size: { cols: 120, rows: 41 } });
149+
expect(resize).toHaveBeenCalledTimes(2);
150+
expect(resize).toHaveBeenLastCalledWith({
151+
environmentId: "environment",
152+
input: { threadId: "thread", terminalId: "term-1", cols: 120, rows: 41 },
153+
});
154+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { TerminalSessionState } from "@t3tools/client-runtime/state/terminal";
2+
import type { EnvironmentId, TerminalResizeInput, ThreadId } from "@t3tools/contracts";
3+
import { useEffect } from "react";
4+
5+
import type { TerminalGridSize } from "./terminalUiState";
6+
7+
/** Replay the measured grid when a writable attachment becomes ready or reconnects. */
8+
export function useTerminalGridSync({
9+
environmentId,
10+
threadId,
11+
terminalId,
12+
canOperate,
13+
terminal,
14+
size,
15+
resize,
16+
}: {
17+
readonly environmentId: EnvironmentId | null;
18+
readonly threadId: ThreadId | null;
19+
readonly terminalId: string;
20+
readonly canOperate: boolean;
21+
readonly terminal: Pick<TerminalSessionState, "output" | "status" | "version">;
22+
readonly size: TerminalGridSize;
23+
readonly resize: (target: {
24+
readonly environmentId: EnvironmentId;
25+
readonly input: TerminalResizeInput;
26+
}) => void;
27+
}): void {
28+
const generation =
29+
canOperate && terminal.version > 0 && terminal.status === "running"
30+
? terminal.output.generation
31+
: null;
32+
33+
useEffect(() => {
34+
if (generation === null || environmentId === null || threadId === null) return;
35+
resize({
36+
environmentId,
37+
input: { threadId, terminalId, cols: size.cols, rows: size.rows },
38+
});
39+
}, [environmentId, generation, resize, size.cols, size.rows, terminalId, threadId]);
40+
}

0 commit comments

Comments
 (0)