Skip to content

Commit 8b61761

Browse files
tarasclaude
andauthored
fix(repl-bench): feed a mounted wheel burst as one input chunk (#1532)
The PTY driver writes a burst's notches as one string, and Terminal.Scroll documents that the notches of a run arrive together. The mounted driver fed them one notch at a time, so an idle REPL drew the first notch alone and the rest as a second frame, and the pause after the burst returned on the first. `page.wheel` now takes a notch count and feeds the repeated sequence once. Refs #1530 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 546049f commit 8b61761

3 files changed

Lines changed: 82 additions & 9 deletions

File tree

‎examples/repl-bench/lib/driver-mounted.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ function* answer(page: ReplPage, command: Command, pendingInput: PendingInput):
120120
return null;
121121
}
122122
pendingInput.from ??= page.screen().index;
123-
for (let notch = 0; notch < Math.abs(command.delta); notch += 1) {
124-
yield* page.wheel(command.delta < 0 ? "up" : "down", command.at);
125-
}
123+
yield* page.wheel(command.delta < 0 ? "up" : "down", command.at, Math.abs(command.delta));
126124
return null;
127125
}
128126
case "pause": {
@@ -143,6 +141,12 @@ function* answer(page: ReplPage, command: Command, pendingInput: PendingInput):
143141
}
144142
}
145143

144+
/** Answer driver commands against a mounted page, in the order they are sent. */
145+
export function mountedAnswers(page: ReplPage): (command: Command) => Operation<unknown> {
146+
const pendingInput: PendingInput = { from: undefined };
147+
return (command) => answer(page, command, pendingInput);
148+
}
149+
146150
// The page is mounted in a root scope, which inherits nothing. An entry the
147151
// mounted REPL runs therefore never passes through the testing session of the
148152
// document that mounted it, and that session admits one execution.
@@ -168,14 +172,13 @@ export function* connectMounted(
168172
ready.reject(error instanceof Error ? error : new Error(String(error)));
169173
}
170174
});
171-
const page = yield* ready.operation;
172-
const pendingInput: PendingInput = { from: undefined };
175+
const answerOne = mountedAnswers(yield* ready.operation);
173176
// A command that fails is handed back as a result. Thrown inside the scope it
174177
// would fail the scope too, and one refused step would end the mounted REPL.
175178
return function* (command) {
176179
const result: Result<unknown> = yield* scope.run(function* () {
177180
try {
178-
return Ok(yield* answer(page, command, pendingInput));
181+
return Ok(yield* answerOne(command));
179182
} catch (error) {
180183
return Err(error instanceof Error ? error : new Error(String(error)));
181184
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { beforeEach, describe, it } from "@executablemd/test-support/bdd";
2+
import { journalBeforeEach } from "@executablemd/test-support/failure-journal";
3+
import { expect } from "@executablemd/test-support/expect";
4+
import type { Operation } from "effection";
5+
import { useReplPage } from "../../../packages/cli/tests/support/repl-page.ts";
6+
import type { ReplPage, ScreenReading } from "../../../packages/cli/tests/support/repl-page.ts";
7+
import { generateTypingHistory } from "../../../packages/cli/tests/support/typing-history.ts";
8+
import { mountedAnswers } from "../lib/driver-mounted.ts";
9+
import type { Command } from "../lib/protocol.ts";
10+
11+
const OWNER = "examples/repl-bench/tests/driver-mounted.test.ts";
12+
13+
const READING = { row: 10, column: 60 };
14+
15+
/** The payload line at the top of the entry reading, the leftmost pane that shows one. */
16+
function readingTop(reading: ScreenReading): number {
17+
for (const row of reading.rows) {
18+
const match = /TAIL-[A-Z]+-(\d{3})/.exec(row);
19+
if (match !== null) {
20+
return Number(match[1]);
21+
}
22+
}
23+
throw new Error(`no payload line in the reading: ${JSON.stringify(reading.rows)}`);
24+
}
25+
26+
interface Burst {
27+
readonly chunks: number;
28+
readonly writes: number;
29+
readonly moved: number;
30+
}
31+
32+
function* burst(
33+
page: ReplPage,
34+
answer: (command: Command) => Operation<unknown>,
35+
delta: number,
36+
): Operation<Burst> {
37+
const before = page.screen();
38+
const fed = page.repl.terminal.fed;
39+
yield* answer({ action: "scroll", delta, at: READING });
40+
yield* answer({ action: "pause", ticks: 1, tick: 16 });
41+
const after = page.screen();
42+
return {
43+
chunks: page.repl.terminal.fed - fed,
44+
writes: after.index - before.index,
45+
moved: readingTop(after) - readingTop(before),
46+
};
47+
}
48+
49+
describe("mounted driver", () => {
50+
beforeEach(journalBeforeEach(OWNER));
51+
52+
// A terminal delivers the notches of one burst together. Fed one notch at a
53+
// time, an idle REPL draws the first notch alone and the rest as a second
54+
// frame, and the pause after the burst returns on the first of them.
55+
it("feeds a wheel burst as one input chunk and draws it as one frame", function* () {
56+
const page = yield* useReplPage({ size: { columns: 200, rows: 52 } });
57+
yield* generateTypingHistory(page, 2, 32, true);
58+
const answer = mountedAnswers(page);
59+
yield* burst(page, answer, 1);
60+
61+
expect(yield* burst(page, answer, 3)).toEqual({ chunks: 1, writes: 1, moved: 9 });
62+
expect(yield* burst(page, answer, -3)).toEqual({ chunks: 1, writes: 1, moved: -9 });
63+
});
64+
});

‎packages/cli/tests/support/repl-page.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ export interface ReplPage {
159159
press(label: string, scope?: ScreenScope): Operation<void>;
160160
/** Send one named key. Waits for nothing. */
161161
key(name: PageKey): Operation<void>;
162-
wheel(direction: "up" | "down", at: ScreenCell): Operation<void>;
162+
/**
163+
* Send wheel notches at one cell as one input chunk, as a terminal delivers a
164+
* burst. Waits for nothing.
165+
*/
166+
wheel(direction: "up" | "down", at: ScreenCell, notches?: number): Operation<void>;
163167
/** Send text to whatever holds focus. Waits for nothing. */
164168
type(text: string): Operation<void>;
165169
/** Resize the terminal and wait for a reading the new size produced. */
@@ -430,8 +434,10 @@ export function* useReplPage(
430434
repl.terminal.feed(KEYS[name]);
431435
}
432436

433-
function* wheel(direction: "up" | "down", at: ScreenCell): Operation<void> {
434-
repl.terminal.feed(`\x1b[<${direction === "up" ? 64 : 65};${at.column + 1};${at.row + 1}M`);
437+
function* wheel(direction: "up" | "down", at: ScreenCell, notches = 1): Operation<void> {
438+
repl.terminal.feed(
439+
`\x1b[<${direction === "up" ? 64 : 65};${at.column + 1};${at.row + 1}M`.repeat(notches),
440+
);
435441
}
436442

437443
function* type(text: string): Operation<void> {

0 commit comments

Comments
 (0)