Files
openchamber-android-native/test/usage_stats_test.dart
T
blumia b65498d73e feat: stats page, and the client becomes Chamber Go
- 全部功能 gains 统计 (Stats): ranges Today/7/30/90 days/all time, a
  project scope (reusing the shared project sheet, plus "All projects"),
  session/prompt/token/cost/active-days/streak tiles, a daily activity
  chart (weekly past 92 days), token composition, efficiency tiles, model
  ranking by tokens/cost/steps and on-request tool-call stats; data comes
  from /api/experimental/session/stats and the day buckets use the device's
  IANA zone (a small platform channel; desktop falls back to UTC)
- the model bars draw a full-width track with a left-aligned fill, so a
  small token share reads as a sliver instead of an empty bar
- AI token strings read 词元 (not 令牌) in Chinese
- the client's display name is Chamber Go (launcher, iOS, Linux window,
  About), package unchanged; pairing/login labels and the user-agent
  follow, so a newly paired device shows "Chamber Go" on the web client's
  "Connect to this server" list
- bump to 0.5.6
2026-11-26 17:18:09 +08:00

115 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');
});
}