The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/core/config.dart';
import 'package:harness/core/models.dart';
import 'package:harness/sharing/shared_harness_panel.dart';
import 'package:harness/shared/theme/app_theme.dart' as grid;
import 'package:harness/state/app_state.dart';
import 'package:harness/state/terminal_pane.dart';
import 'package:harness/terminal/terminal_session.dart';
import 'package:harness/terminal/terminal_binary.dart';
import 'support/real_fonts.dart';
void main() {
setUpAll(() async {
await loadRealFonts();
HttpOverrides.global = null;
});
const streamId = '00112233-4455-6677-8899-aabbccddeeff';
testWidgets(
'read-only terminal receives output, keeps its lease alive and never sends input or resize',
(tester) async {
final sent = <Map<String, dynamic>>[], binary = <TerminalBinaryFrame>[];
final session = TerminalSession(
machineId: 'shared',
agentId: 'agent',
agentName: 'Demo',
engineId: 'codex',
readOnly: true,
send: (type, payload) async {
sent.add({'type': type, ...payload});
return true;
},
sendBinary: (frame) async {
binary.add(frame);
return true;
},
);
await session.open();
await session.handleFrame('terminal_ready', {
'requestId': sent.first['requestId'],
'protocolVersion': 3,
'streamId': streamId,
'agentId': 'agent',
});
await session.handleBinary(
TerminalBinaryFrame(
kind: TerminalBinaryKind.keyframe,
streamId: streamId,
seq: 0,
bytes: Uint8List.fromList(utf8.encode('Live shared output')),
compressed: false,
cols: 110,
rows: 33,
),
);
expect(session.terminal.buffer.getText(), contains('Live shared output'));
expect(session.acceptsInput, isFalse);
session.terminal.textInput('forbidden');
session.resize(12, 12);
expect(await session.sendComposerText('forbidden'), isFalse);
expect(await session.pasteText('forbidden'), isFalse);
session.sendScrollCommand(true, 10);
await tester.pump(const Duration(seconds: 15));
expect(
sent.where((frame) => frame['type'] == 'terminal_alive'),
isNotEmpty,
);
expect(
sent.where((frame) => frame['type'] == 'terminal_resize'),
isEmpty,
);
expect(binary, isEmpty);
session.dispose();
},
);
for (final width in [1100.0, 620.0, 390.0]) {
testWidgets(
'shared pane live output, reconnect, revocation and renewal at width $width',
(tester) async {
tester.view.physicalSize = Size(width, 760);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final sockets = <WebSocket>[], requests = <Map<String, dynamic>>[];
final closingSockets = <WebSocket>{};
late HttpServer server;
await tester.runAsync(() async {
server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
server.listen((request) async {
final ws = await WebSocketTransformer.upgrade(request);
sockets.add(ws);
ws.listen((raw) {
if (closingSockets.contains(ws) ||
ws.readyState != WebSocket.open) {
return;
}
if (raw is! String) {
fail('An observer sent terminal bytes');
}
final frame = jsonDecode(raw) as Map<String, dynamic>;
requests.add(frame);
final p = frame['payload'] as Map;
if (frame['type'] == 'machine_select') {
ws.add(
jsonEncode({
'type': 'connected',
'payload': {'machineId': 'shared', 'readOnly': true},
}),
);
} else if (frame['type'] == 'terminal_open') {
ws.add(
jsonEncode({
'type': 'terminal_ready',
'payload': {
'requestId': p['requestId'],
'protocolVersion': 3,
'streamId': streamId,
'agentId': 'agent',
'readOnly': true,
},
}),
);
ws.add(
encodeTerminalLocal(
TerminalBinaryFrame(
kind: TerminalBinaryKind.keyframe,
streamId: streamId,
seq: 0,
bytes: Uint8List.fromList(
utf8.encode('LIVE_OBSERVER_OUTPUT'),
),
compressed: false,
cols: 110,
rows: 33,
),
)!,
);
} else if (frame['type'] == 'observer_comments') {
ws.add(
jsonEncode({
'type': 'observer_comments',
'payload': {
'requestId': p['requestId'],
'comments': [],
'canComment': true,
},
}),
);
} else if (frame['type'] == 'observer_viewer') {
ws.add(
jsonEncode({
'type': 'observer_viewer',
'payload': {
'state': 'waiting',
'message': 'The viewer will appear when this harness produces an output.',
},
}),
);
}
});
});
});
final app = AppNotifier(
config: AppConfig(
apiBaseUrl: 'http://127.0.0.1:9',
localCliBaseUrl: 'http://127.0.0.1:${server.port}',
),
authSession: AuthSession(),
configStore: null,
);
final grant = SharedHarness(
id: 'grant',
agentId: 'agent',
name: 'Climate dashboard',
engine: 'codex',
expiresAt: DateTime(2027),
);
final pane = TerminalPane(id: 1, machineId: 'shared', agentId: 'agent')
..sharedHarness = grant
..sharedOwnerName = 'D';
var hasAccess = true, closePressed = false;
Future<void> draw() => tester.pumpWidget(
MaterialApp(
theme: grid.buildAppTheme(brightness: Brightness.dark),
home: Scaffold(
body: SharedHarnessPanel(
notifier: app,
pane: pane,
grant: grant,
hasAccess: hasAccess,
visible: true,
onClose: () => closePressed = true,
// The loopback daemon fixture; Windows otherwise dials only its pipe.
allowLocalTcpFixtureForTest: true,
),
),
),
);
Future<void> settleNetwork(bool Function() ready) async {
for (var i = 0; i < 100; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)),
);
await tester.pump();
if (ready()) return;
}
fail('Observer UI did not reach expected state');
}
await draw();
await settleNetwork(() => find.text('Live').evaluate().isNotEmpty);
expect(requests.first['payload'], containsPair('shareId', 'grant'));
expect(find.text('View only'), findsOneWidget);
await tester.tap(find.widgetWithText(TextButton, 'Comments'));
await settleNetwork(
() => find.byKey(const Key('comment-input')).evaluate().isNotEmpty,
);
await tester.enterText(
find.byKey(const Key('comment-input')),
'Keep this draft',
);
await tester.tap(find.widgetWithText(TextButton, 'Watch'));
await tester.pump();
await tester.tap(find.widgetWithText(TextButton, 'Comments'));
await tester.pump();
expect(
tester
.widget<TextField>(find.byKey(const Key('comment-input')))
.controller!
.text,
'Keep this draft',
);
await tester.tap(find.widgetWithText(TextButton, 'Watch'));
await tester.pump();
// Receiving the request on the server does not mean its reply has
// reached the UI yet. Wait for the response we are about to inspect.
await settleNetwork(
() => requests.any((r) => r['type'] == 'observer_viewer'),
);
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 30)),
);
await tester.pump();
// A harness with no viewer yet gets the whole pane for its terminal:
// no empty viewer column, no Terminal/Viewer switch.
expect(
find.textContaining('The viewer will appear', skipOffstage: false),
findsNothing,
);
expect(find.text('Viewer'), findsNothing);
expect(
requests.map((r) => r['type']),
isNot(contains('terminal_resize')),
);
expect(tester.takeException(), isNull);
void viewer(Map<String, dynamic> payload) => sockets.last.add(
jsonEncode({'type': 'observer_viewer', 'payload': payload}),
);
viewer({'state': 'loading'});
await settleNetwork(
() => find
.text('Opening the viewer…', skipOffstage: false)
.evaluate()
.isNotEmpty,
);
viewer({'state': 'live', 'data': 'not-base64!'});
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 30)),
);
await tester.pump();
expect(tester.takeException(), isNull);
// The first frame that decodes brings the viewer to the front on a
// narrow pane: nobody had to find the "Viewer" tab.
viewer({'state': 'live', 'data': 'aW52YWxpZC1pbWFnZQ=='});
await settleNetwork(
() => find
.text('Waiting for the next viewer frame.')
.evaluate()
.isNotEmpty,
);
viewer({
'state': 'live',
'data': 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/lZkAAAAASUVORK5CYII=',
});
await settleNetwork(() => find.byType(Image).evaluate().isNotEmpty);
if (width < 880) {
// …and a person's own pick stands over any later frame.
await tester.tap(find.text('Terminal'));
await tester.pump();
viewer({
'state': 'live',
'data': 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/lZkAAAAASUVORK5CYII=',
});
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 30)),
);
await tester.pump();
expect(find.byType(Image), findsNothing);
}
await tester.runAsync(() async {
closingSockets.add(sockets.last);
await sockets.last.close(1001, 'Owner disconnected');
});
await settleNetwork(
() => find.text('Reconnecting').evaluate().isNotEmpty,
);
await tester.pump(const Duration(seconds: 1));
await settleNetwork(
() => sockets.length == 2 && find.text('Live').evaluate().isNotEmpty,
);
await tester.runAsync(() async {
closingSockets.add(sockets.last);
await sockets.last.close(4403, 'Access removed');
});
await settleNetwork(
() => find.text('Sharing ended').evaluate().isNotEmpty,
);
expect(find.text('Access removed'), findsOneWidget);
await tester.pump(const Duration(seconds: 30));
expect(sockets.length, 2);
hasAccess = false;
await draw();
hasAccess = true;
await draw();
await settleNetwork(
() => sockets.length == 3 && find.text('Live').evaluate().isNotEmpty,
);
hasAccess = false;
await draw();
expect(
find.text('Sharing ended'),
findsOneWidget,
);
await tester.tap(find.byTooltip('Close shared harness'));
expect(closePressed, isTrue);
await tester.pumpWidget(const SizedBox());
app.dispose();
await tester.runAsync(() async {
for (final ws in sockets) {
closingSockets.add(ws);
unawaited(ws.close());
}
await server.close(force: true);
});
await tester.pump(const Duration(seconds: 6));
},
);
}
testWidgets(
'a restored pane without an invitation opens in the ended state',
(tester) async {
final app = AppNotifier(
config: const AppConfig(
apiBaseUrl: 'http://127.0.0.1:9',
localCliBaseUrl: 'http://127.0.0.1:9',
),
authSession: AuthSession(),
configStore: null,
);
final grant = SharedHarness(
id: 'expired',
agentId: 'agent',
name: 'Demo',
expiresAt: DateTime(2000),
);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SharedHarnessPanel(
notifier: app,
pane: TerminalPane(id: 1, machineId: 'shared', agentId: 'agent'),
grant: grant,
hasAccess: false,
visible: true,
onClose: () {},
),
),
),
);
expect(find.text('Sharing ended'), findsOneWidget);
await tester.pumpWidget(const SizedBox());
app.dispose();
},
);
testWidgets(
'a harness the owner is not running says so instead of showing Live',
(tester) async {
tester.view.physicalSize = const Size(1100, 760);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final sockets = <WebSocket>[];
late HttpServer server;
await tester.runAsync(() async {
server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
server.listen((request) async {
final ws = await WebSocketTransformer.upgrade(request);
sockets.add(ws);
ws.listen((raw) {
if (raw is! String || ws.readyState != WebSocket.open) return;
final frame = jsonDecode(raw) as Map<String, dynamic>;
final p = frame['payload'] as Map;
if (frame['type'] == 'machine_select') {
ws.add(
jsonEncode({
'type': 'connected',
'payload': {'machineId': 'shared', 'readOnly': true},
}),
);
} else if (frame['type'] == 'terminal_open') {
ws.add(
jsonEncode({
'type': 'terminal_error',
'payload': {
'requestId': p['requestId'],
'protocolVersion': 3,
'code': 'TERMINAL_RUNTIME_UNAVAILABLE',
},
}),
);
}
});
});
});
final app = AppNotifier(
config: AppConfig(
apiBaseUrl: 'http://127.0.0.1:9',
localCliBaseUrl: 'http://127.0.0.1:${server.port}',
),
authSession: AuthSession(),
configStore: null,
);
final grant = SharedHarness(
id: 'grant',
agentId: 'agent',
name: 'Stopped harness',
engine: 'codex',
expiresAt: DateTime(2027),
);
await tester.pumpWidget(
MaterialApp(
theme: grid.buildAppTheme(brightness: Brightness.dark),
home: Scaffold(
body: SharedHarnessPanel(
notifier: app,
pane: TerminalPane(id: 1, machineId: 'shared', agentId: 'agent'),
grant: grant,
hasAccess: true,
visible: true,
onClose: () {},
allowLocalTcpFixtureForTest: true,
),
),
),
);
for (var i = 0; i < 100; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)),
);
await tester.pump();
if (find.text('Not running').evaluate().isNotEmpty) break;
}
expect(find.text('Not running'), findsOneWidget);
expect(find.text('Live'), findsNothing);
expect(find.textContaining('isn’t running right now'), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
app.dispose();
await tester.runAsync(() async {
for (final ws in sockets) {
unawaited(ws.close());
}
await server.close(force: true);
});
await tester.pump(const Duration(seconds: 6));
},
);
}