// A check stage visit's recorded output: the per-command results the server // ran for a `check` stage, read without widget imports. // // Only an output map carrying a `checks` list is a check output; older // servers never send one, so every other step keeps its ordinary rendering. /// How one command ended. [unknown] keeps a status this client does not know /// (its raw word stays in [CheckCommandResult.statusWord]). enum CheckCommandStatus { passed, failed, timedOut, notRun, unknown } CheckCommandStatus checkCommandStatus(String word) => switch (word.trim().toLowerCase()) { 'passed' => CheckCommandStatus.passed, 'failed' => CheckCommandStatus.failed, 'timed_out' => CheckCommandStatus.timedOut, 'not_run' => CheckCommandStatus.notRun, _ => CheckCommandStatus.unknown, }; /// The status as the row reads it; an unknown status shows its raw word. String checkStatusLabel(CheckCommandResult check) => switch (check.status) { CheckCommandStatus.passed => 'Passed', CheckCommandStatus.failed => 'Failed', CheckCommandStatus.timedOut => 'Timed out', CheckCommandStatus.notRun => 'Not run', CheckCommandStatus.unknown => check.statusWord.isEmpty ? 'Unknown status' : check.statusWord, }; /// One command's recorded result. class CheckCommandResult { const CheckCommandResult({ required this.command, required this.statusWord, this.exitCode, this.durationMs, this.output = '', this.truncated = false, }); final String command; /// The server's status word verbatim. final String statusWord; /// Null when not recorded (a command that never ran has none). final int? exitCode; final int? durationMs; final String output; /// Whether the server cut [output] at its capture limit. final bool truncated; CheckCommandStatus get status => checkCommandStatus(statusWord); /// `exit 1 · 51s`, leaving out whichever fact was not recorded. String get meta => [ if (exitCode != null) 'exit $exitCode', if (durationMs != null) formatCheckDuration(durationMs!), ].join(' · '); } /// A check visit's whole recorded output. class CheckStepOutput { const CheckStepOutput({ required this.checks, this.summary, this.markdown, this.delivery = false, }); final List checks; /// The server's one-line summary (`1 of 2 checks failed: go test ./...`). final String? summary; /// The handoff document the next stage reads. final String? markdown; /// Whether this visit was the delivery gate. final bool delivery; } /// The check output recorded in [output], or null when it is not one. CheckStepOutput? checkStepOutput(dynamic output) { if (output is! Map) return null; final checks = output['checks']; if (checks is! List) return null; return CheckStepOutput( checks: [ for (final item in checks) if (item is Map) _check(item), ], summary: _text(output['summary']), markdown: _text(output['markdown']), delivery: output['delivery'] == true, ); } CheckCommandResult _check(Map item) => CheckCommandResult( command: '${item['command'] ?? ''}', statusWord: '${item['status'] ?? ''}'.trim(), exitCode: _int(item['exit_code']), durationMs: _int(item['duration_ms']), output: item['output'] is String ? item['output'] as String : '', truncated: item['truncated'] == true, ); /// A command duration: `850ms`, `2.1s`, `51s`, `3m 05s`. String formatCheckDuration(int ms) { if (ms < 1000) return '${ms}ms'; if (ms < 10000) { final tenths = (ms / 100).round(); return tenths % 10 == 0 ? '${tenths ~/ 10}s' : '${tenths / 10}s'; } final seconds = (ms / 1000).round(); if (seconds < 60) return '${seconds}s'; final minutes = seconds ~/ 60; final rest = (seconds % 60).toString().padLeft(2, '0'); if (minutes < 60) return '${minutes}m ${rest}s'; return '${minutes ~/ 60}h ${(minutes % 60).toString().padLeft(2, '0')}m'; } int? _int(dynamic value) { if (value is num) return value.toInt(); if (value is String) return int.tryParse(value.trim()); return null; } String? _text(dynamic value) { if (value is! String) return null; return value.trim().isEmpty ? null : value; }