114 lines
3.5 KiB
Dart
114 lines
3.5 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|||
|
|
import 'package:openchamber/api/usage_stats.dart';
|
||
|
|
|
||
|
|
void main() {
|
||
|
|
test('a report reads tokens, tools and models', () {
|
||
|
|
final stats = UsageStats.fromJson({
|
||
|
|
'range': {'from': 1000, 'to': 2000},
|
||
|
|
'sessions': 3,
|
||
|
|
'subagents': 5,
|
||
|
|
'prompts': 12,
|
||
|
|
'steps': 240,
|
||
|
|
'tokens': {
|
||
|
|
'input': 100,
|
||
|
|
'output': 40,
|
||
|
|
'reasoning': 10,
|
||
|
|
'cache': {'read': 900, 'write': 50},
|
||
|
|
},
|
||
|
|
'cost': 0.25,
|
||
|
|
'activeDays': 2,
|
||
|
|
'streak': 1,
|
||
|
|
'activity': [
|
||
|
|
{'date': '2026-11-25', 'steps': 40},
|
||
|
|
],
|
||
|
|
'tools': {
|
||
|
|
'mode': 'detail',
|
||
|
|
'totals': {'calls': 9, 'succeeded': 8, 'failed': 1, 'unfinished': 0},
|
||
|
|
'usage': [
|
||
|
|
{
|
||
|
|
'name': 'read',
|
||
|
|
'calls': 4,
|
||
|
|
'succeeded': 4,
|
||
|
|
'failed': 0,
|
||
|
|
'unfinished': 0,
|
||
|
|
'durationP50': 25.5,
|
||
|
|
},
|
||
|
|
],
|
||
|
|
},
|
||
|
|
'models': [
|
||
|
|
{
|
||
|
|
'model': {'providerID': 'p', 'id': 'm', 'variant': 'high'},
|
||
|
|
'steps': 240,
|
||
|
|
'tokens': {
|
||
|
|
'input': 100,
|
||
|
|
'output': 40,
|
||
|
|
'reasoning': 10,
|
||
|
|
'cache': {'read': 900, 'write': 50},
|
||
|
|
},
|
||
|
|
'cost': 0.25,
|
||
|
|
},
|
||
|
|
],
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(stats.isEmpty, isFalse);
|
||
|
|
expect(stats.sessions, 3);
|
||
|
|
expect(stats.tokens.total, 1100);
|
||
|
|
expect(stats.tokens.produced, 50);
|
||
|
|
expect(stats.tools.hasTotals, isTrue);
|
||
|
|
expect(stats.tools.rows.single.durationP50, 25.5);
|
||
|
|
expect(stats.models.single.variant, 'high');
|
||
|
|
expect(stats.models.single.tokens.cacheRead, 900);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('an empty report says so', () {
|
||
|
|
expect(const UsageStats().isEmpty, isTrue);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('ranges start at local midnight', () {
|
||
|
|
final now = DateTime(2026, 11, 26, 15, 30);
|
||
|
|
expect(UsageRange.day.startFrom(now),
|
||
|
|
DateTime(2026, 11, 26).millisecondsSinceEpoch);
|
||
|
|
expect(UsageRange.week.startFrom(now),
|
||
|
|
DateTime(2026, 11, 20).millisecondsSinceEpoch);
|
||
|
|
expect(UsageRange.month.startFrom(now),
|
||
|
|
DateTime(2026, 10, 28).millisecondsSinceEpoch);
|
||
|
|
expect(UsageRange.all.startFrom(now), isNull);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('activity fills quiet days and groups long ranges into weeks', () {
|
||
|
|
final from = DateTime(2026, 11, 20).millisecondsSinceEpoch;
|
||
|
|
final to = DateTime(2026, 11, 26).millisecondsSinceEpoch;
|
||
|
|
final series = buildUsageActivity(UsageStats(
|
||
|
|
from: from,
|
||
|
|
to: to,
|
||
|
|
activity: const [
|
||
|
|
UsageActivityDay('2026-11-21', 4),
|
||
|
|
UsageActivityDay('2026-11-24', 6),
|
||
|
|
],
|
||
|
|
));
|
||
|
|
expect(series.weekly, isFalse);
|
||
|
|
expect(series.bars.length, 6);
|
||
|
|
expect(series.bars.map((bar) => bar.steps), [0, 4, 0, 0, 6, 0]);
|
||
|
|
expect(series.max, 6);
|
||
|
|
|
||
|
|
// More than 92 days become weekly bars.
|
||
|
|
final long = buildUsageActivity(UsageStats(
|
||
|
|
from: DateTime(2026, 1, 1).millisecondsSinceEpoch,
|
||
|
|
to: DateTime(2026, 6, 1).millisecondsSinceEpoch,
|
||
|
|
));
|
||
|
|
expect(long.weekly, isTrue);
|
||
|
|
expect(long.bars.length, lessThanOrEqualTo(92));
|
||
|
|
});
|
||
|
|
|
||
|
|
test('the number and duration helpers read like the web client', () {
|
||
|
|
expect(usageCompactNumber(999), '999');
|
||
|
|
expect(usageCompactNumber(1234), '1.2K');
|
||
|
|
expect(usageCompactNumber(1234567), '1.2M');
|
||
|
|
expect(usageCompactNumber(123.4), '123');
|
||
|
|
expect(usagePercent(0.1234), '12.3%');
|
||
|
|
expect(usageCost(1.5), r'$1.50');
|
||
|
|
expect(usageDuration(48), '48 ms');
|
||
|
|
expect(usageDuration(1500), '1.5 s');
|
||
|
|
expect(usageDuration(65000), '1 min 5 s');
|
||
|
|
});
|
||
|
|
}
|