Merge branch 'worktree-agent-afe96053a37e02d6f'

This commit is contained in:
2026-11-26 03:27:28 +00:00
23 changed files with 1198 additions and 110 deletions
@@ -55,6 +55,117 @@ void main() {
},
);
test('output after a quiet spell is shown at once; streaming output at '
'most every show gap', () async {
var shown = 0;
final writes = <String>[];
final feed = ChatShellTerminalFeed(
(text, _) => writes.add(text),
show: () => shown++,
budget: Duration.zero,
slice: 10,
showGap: const Duration(milliseconds: 300),
deferShows: true,
);
feed.add('abc', 3);
// The first output, written whole at once: shown at once.
expect(writes, ['abc']);
expect(shown, 1);
feed.add('x' * 35, 38);
await turn();
await turn();
// Behind, within the gap: not shown again.
expect(writes.length, 4);
expect(shown, 1);
await turn();
await turn();
// Caught up within the gap: shown when the gap has passed.
expect(writes.skip(1).join(), 'x' * 35);
expect(feed.idle, isTrue);
expect(shown, 1);
await Future<void>.delayed(const Duration(milliseconds: 400));
expect(shown, 2);
// After a quiet spell: at once again.
await Future<void>.delayed(const Duration(milliseconds: 400));
feed.add('q', 39);
expect(shown, 3);
// An action waiting behind output sees it shown first.
feed.add('y' * 25, 64);
var shownForAction = -1;
feed.then(() => shownForAction = shown);
for (var i = 0; i < 5; i++) {
await turn();
}
expect(shownForAction, 4);
expect(shown, 4);
feed.dispose();
});
test('without deferred shows (natively) written output is shown at once; '
'only a burst still behind waits for the gap', () async {
var shown = 0;
final feed = ChatShellTerminalFeed(
(_, _) {},
show: () => shown++,
budget: Duration.zero,
slice: 10,
showGap: const Duration(hours: 1),
deferShows: false,
);
feed.add('abc', 3);
feed.add('def', 6);
expect(shown, 2);
feed.add('x' * 35, 41);
await turn();
await turn();
expect(shown, 2);
for (var i = 0; i < 3; i++) {
await turn();
}
expect(feed.idle, isTrue);
expect(shown, 3);
});
test('every turn shows its state with no show gap', () async {
var shown = 0;
final feed = ChatShellTerminalFeed(
(_, _) {},
show: () => shown++,
budget: Duration.zero,
slice: 10,
showGap: Duration.zero,
);
feed.add('x' * 30, 30);
for (var i = 0; i < 5; i++) {
await turn();
}
expect(feed.idle, isTrue);
expect(shown, 3);
});
test('slices follow the measured speed within the slice', () async {
final sizes = <int>[];
final feed = ChatShellTerminalFeed(
(text, _) {
sizes.add(text.length);
// About 1 ms per 1000 units.
final until = Stopwatch()..start();
while (until.elapsedMicroseconds < text.length) {}
},
budget: const Duration(milliseconds: 6),
slice: 16 * 1024,
);
feed.add('y' * 100000, 100000);
while (!feed.idle) {
await turn();
}
expect(sizes.reduce((a, b) => a + b), 100000);
expect(sizes.every((s) => s <= 16 * 1024), isTrue);
// After the first slice measured the speed, no write takes much more
// than a turn's budget (6 ms, about 6000 units here).
expect(sizes.skip(1).every((s) => s <= 8000), isTrue, reason: '$sizes');
});
test('a surrogate pair is never split between two writes', () {
final writes = <String>[];
final feed = ChatShellTerminalFeed((text, _) => writes.add(text), slice: 3);
@@ -0,0 +1,226 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
/// Every cell of every line, the cursor and the wrap flags: what two
/// terminals must agree on to have drawn the same output.
List<Object> state(Terminal terminal) {
final buffer = terminal.buffer;
final cells = <Object>[buffer.height, buffer.cursorX, buffer.cursorY];
final cell = CellData.empty();
for (var y = 0; y < buffer.height; y++) {
final line = buffer.lines[y];
cells.add(line.isWrapped);
for (var x = 0; x < line.length; x++) {
line.getCellData(x, cell);
cells.addAll([
cell.foreground,
cell.background,
cell.flags,
cell.content,
]);
}
}
return cells;
}
// Sized before the scrollback shrinks: the default 24 rows must fit first.
Terminal make({int width = 10, int height = 4, int maxLines = 50}) =>
Terminal(maxLines: 100)
..resize(width, height)
..maxLines = maxLines;
void main() {
group('printable ASCII runs', () {
// [setup] goes through the parser in both terminals; [text] is written
// as one run in the first and one character at a time through
// Buffer.writeChar (the path every character took before) in the other.
void same(String setup, String text, {int width = 10}) {
final fast = make(width: width)..write(setup);
final slow = make(width: width)..write(setup);
fast.write(text);
for (final unit in text.codeUnits) {
slow.buffer.writeChar(unit);
}
expect(state(fast), state(slow), reason: '$setup | $text');
}
test('wrap across rows and into the scrollback', () {
same('', 'abcdefghij' * 7 + 'xyz');
same('abc', 'defghijklmnopqrstuvwxyz0123456789');
same('\x1b[3;1H', 'a' * 95);
});
test('a full row waits at the last column, as one character does', () {
same('', 'abcdefghij');
same('abcdefghi', 'j');
same('abcdefghij', 'k');
});
test('without autowrap', () {
same('\x1b[?7l', 'abcdefghijklmnop');
same('\x1b[?7lab', 'cdefghijklmnop');
});
test('in the cursor style (colours, bold, links)', () {
same('\x1b[1;31;44m', 'styled text that wraps');
same('\x1b]8;;https://example.com\x1b\\', 'linked text');
});
test('over a wide character', () {
// Writing over the second column of a wide character blanks its
// first; over the first column, as before.
same('中文\r\x1b[C', 'ab');
same('中文\r', 'abc');
});
test('in the DEC line drawing set', () {
same('\x1b(0', 'lqqk xx mqqj');
});
test('in insert mode the run shifts nothing (as one character)', () {
same('hello\r\x1b[4h', 'ab');
});
});
test('a run and the characters around it parse as before', () {
final terminal = make(width: 20);
terminal.write('ab\x1b[31mcd\x1b[0méf\u{1F600}g\r\nnext');
final buffer = terminal.buffer;
expect(buffer.lines[0].getText(), 'abcdéf\u{1F600}g');
expect(buffer.lines[0].getForeground(2), CellColor.named | 1);
expect(buffer.lines[0].getForeground(4), 0);
expect(buffer.lines[1].getText().trimRight(), 'next');
});
test('a sequence split between writes resumes (rollback)', () {
final terminal = make(width: 20);
terminal.write('ab\x1b[3');
terminal.write('1mcd\x1b');
terminal.write('[0me');
final line = terminal.buffer.lines[0];
expect(line.getText().trimRight(), 'abcde');
expect(line.getForeground(1), 0);
expect(line.getForeground(2), CellColor.named | 1);
expect(line.getForeground(4), 0);
});
test('a surrogate pair is one character; split halves are not', () {
final terminal = make(width: 20);
terminal.write('a\u{1F600}b');
final line = terminal.buffer.lines[0];
expect(line.getCodePoint(1), 0x1F600);
expect(line.getWidth(1), 2);
expect(line.getCodePoint(3), 'b'.codeUnitAt(0));
expect(terminal.buffer.cursorX, 4);
});
test('REP repeats the last character of a run', () {
final terminal = make(width: 20);
terminal.write('xyz\x1b[3b');
expect(terminal.buffer.lines[0].getText().trimRight(), 'xyzzzz');
});
group('lines leaving the scrollback are reused', () {
test('as empty lines: output keeps its text, wraps and widths', () {
final terminal = make(width: 10, height: 3, maxLines: 6);
final reference = make(width: 10, height: 3, maxLines: 1000);
final output = [
for (var i = 0; i < 40; i++)
i.isEven
? '\x1b[4${i % 8}mline $i that wraps\x1b[0m\r\n'
: '中$i\r\n',
].join();
terminal.write(output);
reference.write(output);
final buffer = terminal.buffer;
expect(buffer.height, 6);
final tail = reference.buffer.height - 6;
for (var y = 0; y < 6; y++) {
final line = buffer.lines[y];
final expected = reference.buffer.lines[tail + y];
expect(line.getText(), expected.getText(), reason: 'row $y');
expect(line.isWrapped, expected.isWrapped, reason: 'row $y');
expect(line.getTrimmedLength(), expected.getTrimmedLength());
for (var x = 0; x < 10; x++) {
expect(line.getBackground(x), expected.getBackground(x));
expect(line.getWidth(x), expected.getWidth(x));
}
}
});
test('a reused line has a new version, so its picture is redrawn', () {
final terminal = make(width: 10, height: 2, maxLines: 2);
terminal.write('a\r\nb');
final top = terminal.buffer.lines[0];
final version = top.version;
terminal.write('\r\nc');
expect(identical(terminal.buffer.lines[1], top), isTrue);
expect(top.version, isNot(version));
expect(top.getText().trimRight(), 'c');
});
test('a line with an anchor (a selection end) leaves detached', () {
final terminal = make(width: 10, height: 2, maxLines: 2);
terminal.write('a\r\nb');
final top = terminal.buffer.lines[0];
final anchor = terminal.buffer.createAnchor(0, 0);
terminal.write('\r\nc');
expect(anchor.attached, isFalse);
expect(top.attached, isFalse);
expect(top.getText().trimRight(), 'a');
expect(terminal.buffer.lines[1].getText().trimRight(), 'c');
});
test('a top scroll region feeding a full scrollback wraps around', () {
// Line feeds at the bottom of a region that starts at the top insert
// into the scrollback rather than push; well past its capacity.
String screen(Terminal terminal) => [
for (var y = 0; y < 4; y++)
terminal.buffer.lines[terminal.buffer.scrollBack + y]
.getText()
.trimRight(),
].join('|');
final small = make(width: 10, height: 4, maxLines: 8);
final large = make(width: 10, height: 4, maxLines: 1000);
for (final terminal in [small, large]) {
terminal.write('\x1b[1;3r\x1b[4;1Hstatus\x1b[1;1H');
for (var i = 0; i < 50; i++) {
terminal.write('\x1b[3;1Hrow $i\n');
}
}
expect(small.buffer.height, 8);
expect(screen(small), screen(large));
for (var y = 0; y < 8; y++) {
expect(small.buffer.lines[y].index, y);
}
});
test('in a scroll region on the alternate screen', () {
final terminal = make(width: 10, height: 4);
terminal.write('\x1b[?1049h\x1b[2;3r\x1b[1;1Htop\x1b[4;1Hbottom');
terminal.write('\x1b[2;1H\x1b[41mone\r\n\x1b[0mtwo\r\nthree\r\nfour');
final buffer = terminal.buffer;
String row(int y) => buffer.lines[y].getText().trimRight();
expect([row(0), row(1), row(2), row(3)], [
'top',
'three',
'four',
'bottom',
]);
// The reused lines carry nothing of what scrolled out.
expect(buffer.lines[2].getBackground(0), 0);
expect(buffer.lines[2].getBackground(9), 0);
});
});
test('a write without notify leaves notifying to the host', () {
final terminal = make();
var heard = 0;
terminal.addListener(() => heard++);
terminal.write('quiet', notify: false);
expect(heard, 0);
terminal.write('loud');
expect(heard, 1);
expect(terminal.buffer.lines[0].getText().trimRight(), 'quietloud');
});
}