Files
pipelines/app/test/features/runs/domain/run_tokens_test.dart
T

192 lines
5.8 KiB
Dart

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)))!),
'Not reported',
);
expect(
tokenUsageCost(
runTokenUsage(_run(1, usage: _usage(9, costStages: 1, cost: 0.0042)))!,
),
'\$0.0042',
);
});
test('the detail splits fresh from cached when every stage reported it', () {
final usage = runTokenUsage(
_run(
1,
usage: {
..._usage(22400000, split: true, input: 900000, output: 150000),
'cache_known': true,
'cache_read': 21300000,
'cache_write': 50000,
},
),
)!;
expect(tokenUsageLabel(usage), '22.4M tokens');
expect(usage.fresh, 1100000);
expect(
tokenUsageDetail(usage),
'22.4M · 1.1M fresh, 21.3M cached · 900K in, 150K out',
);
// An older row (or a partial cache split) has no cache figure: just the
// total and what else it reported.
final old = runTokenUsage(
_run(
1,
usage: _usage(22400000, split: true, input: 900000, output: 150000),
),
)!;
expect(old.fresh, isNull);
expect(tokenUsageDetail(old), '22.4M · 900K in, 150K out');
});
test('a zero or missing "known" cost is not reported, never \$0', () {
// Subscription models report cost_known with no positive cost_usd.
for (final usage in [
_usage(9, costStages: 1),
_usage(9, costStages: 1, cost: 0),
]) {
final tokens = runTokenUsage(_run(1, usage: usage))!;
expect(tokens.cost, isNull);
expect(tokenUsageCost(tokens), 'Not reported');
}
final stage = stageTokenUsage({
'usage': {'tokens_total': 9, 'tokens_known': true, 'cost_known': true},
})!;
expect(tokenUsageCost(stage), 'Not reported');
});
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'));
});
}