Runs list carries per-run token usage; app shows tokens beside size, Most tokens sort, Summary and stage tokens

This commit is contained in:
2026-11-03 22:04:55 +00:00
parent fa6c0864db
commit aa948bdfe4
19 changed files with 620 additions and 30 deletions
@@ -0,0 +1,142 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/domain/run_models.dart';
import 'package:pipelines_app/features/runs/domain/run_tokens.dart';
import 'package:pipelines_app/features/runs/ui/runs_filter_options.dart';
JsonMap _run(int id, {Map<String, dynamic>? usage, String? created}) => {
'id': id,
'created_at': created ?? '2026-10-2${id % 10}T12:00:00Z',
'usage': ?usage,
};
Map<String, dynamic> _usage(
int total, {
int stages = 1,
int? tokenStages,
bool split = false,
int input = 0,
int output = 0,
int costStages = 0,
double? cost,
}) => {
'stages': stages,
'tokens_total': total,
'tokens_known_stages': tokenStages ?? stages,
'split_known': split,
if (input > 0) 'tokens_in': input,
if (output > 0) 'tokens_out': output,
'cost_known_stages': costStages,
'cost_usd': ?cost,
};
void main() {
test('token counts are compact decimal multiples', () {
expect(formatTokenCount(0), '0');
expect(formatTokenCount(950), '950');
expect(formatTokenCount(1000), '1K');
expect(formatTokenCount(1234), '1.2K');
expect(formatTokenCount(340000), '340K');
expect(formatTokenCount(999600), '1M');
expect(formatTokenCount(22400015), '22.4M');
expect(formatTokenCount(1300000000), '1.3B');
});
test('a reported run total is labelled like the size', () {
final usage = runTokenUsage(_run(1, usage: _usage(22400015)))!;
expect(usage.partial, isFalse);
expect(tokenUsageLabel(usage), '22.4M tokens');
});
test('a partial total is a lower bound, never an estimate', () {
final usage = runTokenUsage(
_run(1, usage: _usage(9, stages: 2, tokenStages: 1)),
)!;
expect(usage.partial, isTrue);
expect(tokenUsageLabel(usage), '≥9 tokens');
expect(tokenUsageDetail(usage), '≥9 · 1 of 2 stages reported');
});
test('absent or unreported usage shows nothing', () {
expect(runTokenUsage(_run(1)), isNull);
expect(runTokenUsage({'id': 1, 'usage': 'bogus'}), isNull);
// Only a cost was reported: no token figure is invented.
expect(
runTokenUsage(_run(1, usage: _usage(0, tokenStages: 0, costStages: 1))),
isNull,
);
});
test('the detail adds the split and the reported cost', () {
final usage = runTokenUsage(
_run(
1,
usage: _usage(
22400015,
stages: 2,
split: true,
input: 20000010,
output: 2400005,
costStages: 2,
cost: 1.75,
),
),
)!;
expect(tokenUsageDetail(usage), '22.4M (20M in, 2.4M out)');
expect(tokenUsageCost(usage), '\$1.75');
final partialCost = runTokenUsage(
_run(1, usage: _usage(9, stages: 2, costStages: 1, cost: 0.1)),
)!;
expect(tokenUsageCost(partialCost), '≥\$0.10');
expect(tokenUsageCost(runTokenUsage(_run(1, usage: _usage(9)))!), isNull);
expect(
tokenUsageCost(
runTokenUsage(_run(1, usage: _usage(9, costStages: 1, cost: 0.0042)))!,
),
'\$0.0042',
);
});
test('a stage reports its own tokens only when it counted them', () {
final stage = stageTokenUsage({
'usage': {
'tokens_total': 1500,
'tokens_known': true,
'split_known': true,
'tokens_in': 1200,
'tokens_out': 300,
'cost_known': true,
'cost_usd': 0.02,
},
})!;
expect(tokenUsageDetail(stage), '1.5K (1.2K in, 300 out)');
expect(tokenUsageCost(stage), '\$0.02');
expect(
stageTokenUsage({
'usage': {'cost_known': true},
}),
isNull,
);
expect(stageTokenUsage({}), isNull);
});
test('Most tokens sorts reported totals first, largest first', () {
final runs = [
_run(1, usage: _usage(5)),
_run(2),
_run(3, usage: _usage(22400000)),
_run(4, usage: _usage(9, stages: 2, tokenStages: 1)),
_run(5),
];
final sorted = sortRuns(runs, sort: 'most_tokens');
expect(sorted.map((run) => run['id']), [3, 4, 1, 5, 2]);
});
test('Most tokens is offered beside Largest first and lists flat', () {
final keys = runSorts.map((option) => option.$1).toList();
expect(keys.indexOf('most_tokens'), keys.indexOf('largest_first') + 1);
expect(runSortLabel('most_tokens'), 'Most tokens');
expect(flatRunSorts, contains('most_tokens'));
});
}
@@ -24,6 +24,7 @@ JsonMap recordedRunFixture() => <String, dynamic>{
'retry_of': 12,
'diagnostic': 'Traceback: boom',
'completed_steps': 2,
'size_bytes': 5 * 1024 * 1024,
'input': <String, dynamic>{'prompt': 'ship it'},
'output': <String, dynamic>{'result': 'ok'},
'steps': <dynamic>[
@@ -64,6 +65,9 @@ const recordedLabels = [
'Started',
'Finished',
'Duration',
'Size on disk',
'Tokens',
'Cost',
'Retry of',
'Diagnostic',
];
@@ -91,6 +95,12 @@ void main() {
formatLocalTimestamp('2026-10-19T09:32:05Z'),
);
expect(rows.firstWhere((row) => row.label == 'Duration').value, '2m 5s');
expect(rows.firstWhere((row) => row.label == 'Size on disk').value, '5 MB');
expect(
rows.firstWhere((row) => row.label == 'Tokens').value,
'123 (100 in, 23 out)',
);
expect(rows.firstWhere((row) => row.label == 'Cost').value, '\$0.50');
expect(rows.firstWhere((row) => row.label == 'Retry of').value, 'run 12');
final diagnostic = rows.firstWhere((row) => row.label == 'Diagnostic');
expect(diagnostic.value, 'Traceback: boom');
@@ -86,6 +86,48 @@ void main() {
expect(find.text('Pipeline unavailable · 2m 5s · 3 steps'), findsOneWidget);
});
testWidgets('shows reported tokens beside the size, and nothing without', (
tester,
) async {
Future<void> pump(JsonMap run) => tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RunListRow(run: run, now: DateTime.utc(2026, 10, 22)),
),
),
);
final sized = {..._finished, 'size_bytes': 340 * 1024 * 1024};
await pump({
...sized,
'usage': {
'stages': 3,
'tokens_known_stages': 3,
'tokens_total': 22400015,
},
});
expect(
find.text('release · 2m 5s · 3 steps · 340 MB · 22.4M tokens'),
findsOneWidget,
);
await pump({
...sized,
'usage': {
'stages': 3,
'tokens_known_stages': 2,
'tokens_total': 22400015,
},
});
expect(
find.text('release · 2m 5s · 3 steps · 340 MB · ≥22.4M tokens'),
findsOneWidget,
);
// An older server's list carries no usage: the row is unchanged.
await pump(sized);
expect(find.text('release · 2m 5s · 3 steps · 340 MB'), findsOneWidget);
});
test('the run action sheet is titled with the run', () {
expect(runMoreMenuTitle(_finished), 'Ship the release notes');
expect(runMoreMenuTitle({'id': 5, 'name': 'nightly'}), 'nightly');
@@ -0,0 +1,46 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_content.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_visit_facts.dart';
final _reported = <String, dynamic>{
'stage': 'build',
'sequence': 1,
'status': 'completed',
'outcome': 'success',
'target': 'review',
'usage': {
'tokens_total': 1500000,
'tokens_known': true,
'split_known': true,
'tokens_in': 1200000,
'tokens_out': 300000,
'cost_known': true,
'cost_usd': 0.42,
},
};
final _unreported = <String, dynamic>{
'stage': 'gate',
'sequence': 2,
'status': 'completed',
};
void main() {
test('a visit lists its reported tokens and cost as facts', () {
final facts = detailVisitFacts(_reported);
expect(
facts,
contains((label: 'Tokens', value: '1.5M (1.2M in, 300K out)')),
);
expect(facts, contains((label: 'Cost', value: '\$0.42')));
final none = detailVisitFacts(_unreported);
expect(none.any((fact) => fact.label == 'Tokens'), isFalse);
expect(none.any((fact) => fact.label == 'Cost'), isFalse);
});
test('a stage row carries its compact token total', () {
final visits = detailTimeline([_reported, _unreported]);
expect(visits[0].tokens, '1.5M tokens');
expect(visits[1].tokens, isNull);
});
}