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
test(mobile): the day log files, the burst filter, HTTP lines, the launch timeline
dee57a380a50587
diff --git a/mobile/test/analytics/analytics_payload_test.dart b/mobile/test/analytics/analytics_payload_test.dartindex 46d503131..199d57a84 100644--- a/mobile/test/analytics/analytics_payload_test.dart+++ b/mobile/test/analytics/analytics_payload_test.dart@@ -246,6 +246,37 @@ void main() {});});+ group('the recorder', () {+ test('a release build records nothing, and hands out no ids', () {+ const log = NoopAnalyticsLog();+ expect(log.queued('app_opened', const {}, DateTime(2026)), 0);+ log.attempted(0, const {});+ log.settled(0, AnalyticsEventStatus.sent);+ });++ test(+ 'the ring keeps the newest rows; a row pushed out settles nothing',+ () {+ final log = AnalyticsLogStream(maxEntries: 2);+ final first = log.queued('first_event', const {}, DateTime(2026));+ log.queued('second_event', const {}, DateTime(2026));+ log.queued('third_event', const {}, DateTime(2026));++ expect(log.entries.map((e) => e.name), ['third_event', 'second_event']);+ log.attempted(first, const {});+ log.settled(first, AnalyticsEventStatus.sent);+ expect(log.entries.every((e) => e.attempts == 0), isTrue);+ },+ );++ test('a body that will not re-indent is shown as it is', () {+ final log = AnalyticsLogStream();+ final id = log.queued('app_opened', const {}, DateTime(2026));+ log.attempted(id, {'value': _Unencodable()});+ expect(log.entries.single.payload, '{value: unencodable}');+ });+ });+group('the sink', () {tearDown(() {setAnalyticsForTest(null);diff --git a/mobile/test/logging/file_logs_test.dart b/mobile/test/logging/file_logs_test.dartnew file mode 100644index 000000000..d500adcaa--- /dev/null+++ b/mobile/test/logging/file_logs_test.dart@@ -0,0 +1,194 @@+import 'dart:io';++import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/logging/app_log.dart';+import 'package:harness_mobile/logging/error_burst_filter.dart';+import 'package:harness_mobile/logging/log_file.dart';++/// The day files a phone writes under `~/.harness/logs` — here in a temporary+/// directory, never a real Harness home.+void main() {+ late Directory dir;+ late DateTime now;++ setUp(() {+ dir = Directory.systemTemp.createTempSync('file_logs_test');+ now = DateTime(2026, 9, 27, 9, 5, 7);+ });++ tearDown(() => dir.deleteSync(recursive: true));++ DailyLogFile daily({int retentionDays = 14}) =>+ DailyLogFile(dir, 'app', retentionDays: retentionDays, clock: () => now);++ List<String> names() =>+ [for (final e in dir.listSync()) e.uri.pathSegments.last]..sort();++ group('stamps', () {+ test('a dated file name, a wall clock, and a short duration', () {+ expect(dailyLogName('app', DateTime(2026, 1, 2)), 'app-20260102.log');+ expect(logStamp(DateTime(2026, 1, 2, 3, 4, 5)), '2026-01-02 03:04:05');+ expect(logClock(DateTime(2026, 1, 2, 13, 4, 5)), '13:04:05');+ expect(logDuration(const Duration(seconds: 59)), '59s');+ expect(logDuration(const Duration(seconds: 61)), '1m1s');+ });+ });++ group('the day file', () {+ test('appends a line to today\'s file, creating the folder', () {+ final file = DailyLogFile(+ Directory('${dir.path}/logs'),+ 'app',+ clock: () => now,+ );+ file.append('one');+ file.append('two');++ expect(file.currentFile.path, endsWith('/logs/app-20260927.log'));+ expect(file.currentFile.readAsStringSync(), 'one\ntwo\n');+ });++ test('a new day writes a new file and prunes past the retention', () {+ for (final day in ['20260901', '20260912', '20260913', '20260926']) {+ File('${dir.path}/app-$day.log').writeAsStringSync('old\n');+ }+ // Not this base's, or not a dated name: never touched.+ File('${dir.path}/cli-20260101.log').writeAsStringSync('cli\n');+ File('${dir.path}/app-notes.log').writeAsStringSync('mine\n');+ File('${dir.path}/app-20260101.txt').writeAsStringSync('mine\n');++ daily().append('today');++ expect(names(), [+ 'app-20260101.txt',+ 'app-20260913.log',+ 'app-20260926.log',+ 'app-20260927.log',+ 'app-notes.log',+ 'cli-20260101.log',+ ]);+ });++ test('pruning runs once per day, not on every line', () {+ final file = daily();+ file.append('first');+ File('${dir.path}/app-20200101.log').writeAsStringSync('old\n');+ file.append('second');+ expect(File('${dir.path}/app-20200101.log').existsSync(), isTrue);++ now = now.add(const Duration(days: 1));+ file.append('tomorrow');+ expect(File('${dir.path}/app-20200101.log').existsSync(), isFalse);+ expect(File('${dir.path}/app-20260928.log').existsSync(), isTrue);+ });++ test('a retention of zero keeps everything', () {+ File('${dir.path}/app-20200101.log').writeAsStringSync('old\n');+ daily(retentionDays: 0).append('today');+ expect(File('${dir.path}/app-20200101.log').existsSync(), isTrue);+ });++ test('a folder that cannot be made costs the line, never the caller', () {+ final blocked = File('${dir.path}/blocked')..writeAsStringSync('file');+ final file = DailyLogFile(+ Directory('${blocked.path}/logs'),+ 'app',+ clock: () => now,+ );+ file.append('lost');+ expect(file.currentFile.existsSync(), isFalse);+ });+ });++ group('the app log', () {+ late DailyLogFile file;+ late ErrorBurstFilter burst;+ late FileAppLog log;++ setUp(() {+ file = daily();+ burst = ErrorBurstFilter(clock: () => now);+ log = FileAppLog(file, burst: burst, clock: () => now);+ });++ List<String> lines() => file.currentFile.readAsLinesSync();++ test('one stamped line per event, level and category in columns', () {+ log.debug('ws', 'frame');+ log.info('startup', 'firstFrame @12ms');+ log.warn('api', 'GET /api/machines → 503', error: 'unavailable');++ expect(lines(), [+ '[2026-09-27 09:05:07] DEBUG ws frame',+ '[2026-09-27 09:05:07] INFO startup firstFrame @12ms',+ '[2026-09-27 09:05:07] WARN api GET /api/machines → 503 '+ 'err=unavailable',+ ]);+ });++ test('a failure carries its stack, indented under it', () {+ log.failure(+ 'flutter',+ 'build threw',+ error: StateError('bad'),+ stackTrace: StackTrace.fromString('#0 main\n#1 run\n'),+ );++ expect(lines(), [+ '[2026-09-27 09:05:07] ERROR flutter build threw err=Bad state: bad',+ ' #0 main',+ ' #1 run',+ ]);+ });++ test('an error repeating every frame is written once, then counted', () {+ for (var i = 0; i < 5; i++) {+ log.failure('flutter', 'paint threw');+ }+ expect(lines(), hasLength(1));++ now = now.add(const Duration(seconds: 31));+ log.failure('flutter', 'paint threw');+ expect(+ lines().last,+ '[2026-09-27 09:05:38] ERROR flutter paint threw '+ '[+4 identical since the last copy]',+ );+ });++ test('lower levels are said once by choice and never filtered', () {+ log.warn('ws', 'redialling');+ log.warn('ws', 'redialling');+ expect(lines(), hasLength(2));+ });++ test('the default sink is silent', () {+ const NoopAppLog().record(AppLogLevel.error, 'app', 'nothing');+ });+ });++ group('the burst filter', () {+ test('matches on the first line, so a changing tail is one burst', () {+ final filter = ErrorBurstFilter(clock: () => now);+ expect(filter.admit('boom\nat frame 1'), 'boom\nat frame 1');+ expect(filter.admit('boom\nat frame 2'), isNull);+ expect(filter.admit('other'), 'other');+ expect(filter.trackedCount, 2);+ });++ test('a very long first line is signed by its first 200 characters', () {+ final filter = ErrorBurstFilter(clock: () => now);+ final head = 'x' * 200;+ expect(filter.admit('${head}a'), isNotNull);+ expect(filter.admit('${head}b'), isNull);+ });++ test('never tracks more than its bound', () {+ final filter = ErrorBurstFilter(clock: () => now);+ for (var i = 0; i < 300; i++) {+ filter.admit('error $i');+ }+ expect(filter.trackedCount, lessThanOrEqualTo(256));+ });+ });+}diff --git a/mobile/test/logging/log_sinks_test.dart b/mobile/test/logging/log_sinks_test.dartnew file mode 100644index 000000000..a9c8af769--- /dev/null+++ b/mobile/test/logging/log_sinks_test.dart@@ -0,0 +1,195 @@+import 'package:dio/dio.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:harness_mobile/logging/app_log.dart';+import 'package:harness_mobile/logging/http_log.dart';+import 'package:harness_mobile/logging/log_stream.dart';+import 'package:harness_mobile/logging/log_stream_sinks.dart';+import 'package:harness_mobile/logging/startup_trace.dart';++import '../viewer/fake_http.dart';++/// Every line the app log was handed, as the file would read it.+class _Recording implements AppLog {+ final lines = <({AppLogLevel level, String category, String message})>[];+ final errors = <Object?>[];++ @override+ void record(+ AppLogLevel level,+ String category,+ String message, {+ Object? error,+ StackTrace? stackTrace,+ }) {+ lines.add((level: level, category: category, message: message));+ errors.add(error);+ }+}++void main() {+ late _Recording recording;++ setUp(() {+ recording = _Recording();+ appLog = recording;+ });++ tearDown(() => appLog = const NoopAppLog());++ group('HTTP calls in the timeline', () {+ Dio dio(Map<String, FakeReply> replies) =>+ attachHttpLog(FakeHttp(replies).dio());++ test(+ 'a call that worked is one debug line: verb, url, status, time',+ () async {+ await dio({+ '/api/machines': (status: 200, body: {'success': true}),+ }).get<Object?>('/api/machines');++ final line = recording.lines.single;+ expect(line.level, AppLogLevel.debug);+ expect(line.category, 'api');+ expect(+ line.message,+ matches(+ RegExp(r'^GET https://fake\.invalid/api/machines → 200 \(\d+ms\)$'),+ ),+ );+ },+ );++ test('an answer that is not a success is a warning', () async {+ await dio({+ '/api/desk': (status: 503, body: {'success': false}),+ }).get<Object?>('/api/desk');++ expect(recording.lines.single.level, AppLogLevel.warn);+ expect(recording.lines.single.message, contains('→ 503'));+ });++ test(+ 'no answer at all is a warning with the reason, never a body',+ () async {+ await expectLater(+ dio(const {}).post<Object?>(+ '/api/auth/exchange',+ data: {'code': 'one-time', 'state': 's', 'tx': 't'},+ ),+ throwsA(isA<DioException>()),+ );++ final line = recording.lines.single;+ expect(line.level, AppLogLevel.warn);+ expect(+ line.message,+ startsWith('POST https://fake.invalid/api/auth/exchange → failed'),+ );+ expect(recording.errors.single, isNotNull);+ expect('${recording.lines}', isNot(contains('one-time')));+ },+ );++ test('a credential in the query never reaches the line', () async {+ await dio({'/api/voice/stt': (status: 200, body: const {})}).get<Object?>(+ '/api/voice/stt',+ queryParameters: {'lang': 'vi', 'token': 'live-secret'},+ );++ expect(recording.lines.single.message, contains('token=<redacted>'));+ expect(recording.lines.single.message, contains('lang=vi'));+ expect(recording.lines.single.message, isNot(contains('live-secret')));+ });+ });++ group('the launch timeline', () {+ test('a mark is a moment, stamped with its offset', () {+ StartupTrace.mark('firstFrame');+ expect(recording.lines.single.category, 'startup');+ expect(+ recording.lines.single.message,+ matches(RegExp(r'^firstFrame @\d+ms$')),+ );+ });++ test('a span says what it cost and where it fell', () async {+ expect(await StartupTrace.time('prefs.load', () async => 7), 7);+ expect(StartupTrace.timeSync('parse', () => 'ok'), 'ok');++ expect(recording.lines.map((l) => l.message), [+ matches(RegExp(r'^prefs\.load took \d+ms \(at \d+ms\)$')),+ matches(RegExp(r'^parse took \d+ms \(at \d+ms\)$')),+ ]);+ });++ test('a step that throws is still timed, then rethrown', () async {+ await expectLater(+ StartupTrace.time<void>(+ 'boot.configLoad',+ () async => throw StateError('x'),+ ),+ throwsStateError,+ );+ expect(+ () => StartupTrace.timeSync<void>('sync', () => throw StateError('y')),+ throwsStateError,+ );+ expect(recording.lines, hasLength(2));+ expect(recording.lines.first.message, startsWith('boot.configLoad took'));+ });+ });++ group('the debug build\'s mirror', () {+ test('a line goes to every sink, the file first', () {+ final stream = LogStream();+ FanoutAppLog([recording, StreamAppLog(stream)]).record(+ AppLogLevel.error,+ 'ws',+ 'dial failed',+ error: StateError('refused'),+ stackTrace: StackTrace.fromString('#0 dial'),+ );++ expect(recording.lines.single.message, 'dial failed');+ final entry = stream.entries.single;+ expect(entry.message, 'dial failed');+ expect(entry.category, 'ws');+ expect(entry.error, 'Bad state: refused');+ expect(entry.stackTrace, '#0 dial');+ expect(entry.status, LogStatus.failed);+ expect(entry.hasDetail, isTrue);+ });++ test('levels read as the list draws them', () {+ final stream = LogStream();+ final sink = StreamAppLog(stream);+ sink.warn('ws', 'redialling');+ sink.info('app', 'launched');++ expect(stream.entries.map((e) => e.status), [+ LogStatus.event,+ LogStatus.warned,+ ]);+ expect(stream.entries.first.hasDetail, isFalse);+ });++ test(+ 'the ring keeps the newest lines, and notifies once per burst',+ () async {+ final stream = LogStream(maxEntries: 3);+ var notified = 0;+ stream.addListener(() => notified++);+ for (var i = 0; i < 5; i++) {+ stream.add(AppLogLevel.info, 'app', 'line $i');+ }+ expect(stream.entries.map((e) => e.message), [+ 'line 4',+ 'line 3',+ 'line 2',+ ]);+ await Future<void>.delayed(Duration.zero);+ expect(notified, 1);+ },+ );+ });+}