mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-23 16:06:31 +01:00
The shared pool (~/utils/freedays) was CLI-only: the gates on the PC stood down, but the phone kept ringing at 08:00 and there was nowhere to mark a day from bed. This is the one picker for every app. FreeDayPool is a Dart port of the Python rules with the identical record shape (id = ISO date; state/consumed/actor/reason on one tick; un-mark is a state flip, never a tombstone), verified by a real cross-language round trip: a log written here reads back through freedays.is_free_day and remaining() with the same verdicts and count, and the PC's live pool reads here as the same 27 of 35 that `freedays status` prints. The alarm fires from Kotlin with no engine running, so the pool is mirrored into a native SharedPreferences set after every mark, release and sync (channel method setFreeDays), and AlarmReceiver consults it before starting AlarmService. Fail closed: a missing or corrupt cache rings. All three branches exercised on the API 36 AVD by broadcasting the receiver. Firebase only for this namespace -- no GitHub mirror; the alarm record keeps its own because un-migrated installs depend on it. Known gap, documented rather than papered over: a day marked on the PC reaches the phone at the next app launch or picker open, not overnight. The ring path takes no background dependency by design, so mark on the phone, or open the app once after marking on the PC. Verified on the phone (release build, 23181JEGR08034): picker shows the budget, marks 2026-09-30 with a reason, pushes it to freedays-sync/devices/<uuid>, releases it with a full refund back to 35. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019pBgaXSJKyY3DarYpDatDF
222 lines
7.2 KiB
Dart
222 lines
7.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:wake_alarm_sync/services/free_day_pool.dart';
|
|
|
|
class _MemPersistence implements LogPersistence {
|
|
String? text;
|
|
|
|
@override
|
|
Future<String?> read() async => text;
|
|
|
|
@override
|
|
Future<void> write(String value) async => text = value;
|
|
}
|
|
|
|
final _today = DateTime(2026, 9, 11);
|
|
final _tomorrow = DateTime(2026, 9, 12);
|
|
final _yesterday = DateTime(2026, 9, 10);
|
|
|
|
Future<FreeDayPool> _open([_MemPersistence? persistence]) => FreeDayPool.open(
|
|
persistence: persistence ?? _MemPersistence(),
|
|
deviceId: 'phone-test',
|
|
);
|
|
|
|
void main() {
|
|
group('dates', () {
|
|
test('toIso pads and parseIso round-trips', () {
|
|
expect(FreeDayPool.toIso(DateTime(2026, 1, 5)), '2026-01-05');
|
|
expect(FreeDayPool.parseIso('2026-01-05'), DateTime(2026, 1, 5));
|
|
});
|
|
|
|
test('parseIso rejects junk and impossible dates', () {
|
|
expect(FreeDayPool.parseIso('alarm'), isNull);
|
|
expect(FreeDayPool.parseIso('2026-02-30'), isNull);
|
|
expect(FreeDayPool.parseIso('2026-9-1'), isNull);
|
|
});
|
|
|
|
test('dateOnly drops the time of day and today() is local', () {
|
|
expect(
|
|
FreeDayPool.dateOnly(DateTime(2026, 9, 11, 23, 59)),
|
|
DateTime(2026, 9, 11),
|
|
);
|
|
expect(FreeDayPool.today().hour, 0);
|
|
});
|
|
});
|
|
|
|
group('mark', () {
|
|
test('a future day becomes free and is not consumed', () async {
|
|
final pool = await _open();
|
|
await pool.mark(_tomorrow, reason: 'travelling', now: _today);
|
|
expect(pool.isFree(_tomorrow), isTrue);
|
|
expect(pool.reasonFor(_tomorrow), 'travelling');
|
|
expect(pool.freeIsoDates(), ['2026-09-12']);
|
|
expect(pool.spent(2026), 1);
|
|
expect(pool.remaining(2026), FreeDayPool.annualBudget - 1);
|
|
});
|
|
|
|
test('today is allowed and immediately consumed', () async {
|
|
final pool = await _open();
|
|
await pool.mark(_today, now: _today);
|
|
expect(pool.isFree(_today), isTrue);
|
|
await pool.release(_today, now: _today);
|
|
expect(pool.isFree(_today), isFalse);
|
|
// Those hours were already unguarded: no refund.
|
|
expect(pool.spent(2026), 1);
|
|
});
|
|
|
|
test('the time of day on the picked date is ignored', () async {
|
|
final pool = await _open();
|
|
await pool.mark(DateTime(2026, 9, 12, 15, 30), now: _today);
|
|
expect(pool.isFree(_tomorrow), isTrue);
|
|
});
|
|
|
|
test('a past day is refused', () async {
|
|
final pool = await _open();
|
|
await expectLater(
|
|
pool.mark(_yesterday, now: _today),
|
|
throwsA(
|
|
isA<FreeDayPolicyError>().having(
|
|
(e) => e.toString(),
|
|
'message',
|
|
contains('already passed'),
|
|
),
|
|
),
|
|
);
|
|
expect(pool.freeDays(), isEmpty);
|
|
});
|
|
|
|
test('an already-free day is refused', () async {
|
|
final pool = await _open();
|
|
await pool.mark(_tomorrow, now: _today);
|
|
await expectLater(
|
|
pool.mark(_tomorrow, now: _today),
|
|
throwsA(isA<FreeDayPolicyError>()),
|
|
);
|
|
});
|
|
|
|
test('the budget is per calendar year and refuses the 36th', () async {
|
|
final pool = await _open();
|
|
for (var i = 0; i < FreeDayPool.annualBudget; i++) {
|
|
await pool.mark(DateTime(2026, 10, 1 + i), now: _today);
|
|
}
|
|
expect(pool.remaining(2026), 0);
|
|
await expectLater(
|
|
pool.mark(DateTime(2026, 12, 24), now: _today),
|
|
throwsA(isA<FreeDayPolicyError>()),
|
|
);
|
|
// Next year is a fresh pool.
|
|
await pool.mark(DateTime(2027, 1, 2), now: _today);
|
|
expect(pool.remaining(2027), FreeDayPool.annualBudget - 1);
|
|
});
|
|
});
|
|
|
|
group('release', () {
|
|
test('a future day is refunded', () async {
|
|
final pool = await _open();
|
|
await pool.mark(_tomorrow, now: _today);
|
|
await pool.release(_tomorrow, now: _today);
|
|
expect(pool.isFree(_tomorrow), isFalse);
|
|
expect(pool.spent(2026), 0);
|
|
// And can be marked again: a flip, not a tombstone.
|
|
await pool.mark(_tomorrow, now: _today);
|
|
expect(pool.isFree(_tomorrow), isTrue);
|
|
});
|
|
|
|
test('a day that is not free is refused', () async {
|
|
final pool = await _open();
|
|
await expectLater(
|
|
pool.release(_tomorrow, now: _today),
|
|
throwsA(isA<FreeDayPolicyError>()),
|
|
);
|
|
});
|
|
|
|
test('consumed is sticky across a release and re-mark', () async {
|
|
final pool = await _open();
|
|
await pool.mark(_tomorrow, now: _today);
|
|
// The day arrives, then is released after the fact.
|
|
await pool.release(_tomorrow, now: _tomorrow);
|
|
expect(pool.spent(2026), 1);
|
|
// Re-marking a later day does not resurrect the refund either.
|
|
await pool.mark(DateTime(2026, 9, 20), now: _tomorrow);
|
|
expect(pool.spent(2026), 2);
|
|
});
|
|
});
|
|
|
|
group('storage', () {
|
|
test("records use the Python package's exact shape", () async {
|
|
final persistence = _MemPersistence();
|
|
final pool = await _open(persistence);
|
|
await pool.mark(_tomorrow, reason: 'r', now: _today);
|
|
final json = jsonDecode(persistence.text!) as Map<String, dynamic>;
|
|
final record = json['2026-09-12'] as Map<String, dynamic>;
|
|
final fields = record['fields'] as Map<String, dynamic>;
|
|
expect(
|
|
fields.keys,
|
|
containsAll(['state', 'consumed', 'actor', 'reason']),
|
|
);
|
|
expect((fields['state'] as List).first, 'free');
|
|
expect((fields['consumed'] as List).first, false);
|
|
expect((fields['actor'] as List).first, 'phone-test');
|
|
expect(record['deleted'], false);
|
|
// Every field shares one clock tick.
|
|
final ticks = fields.values.map((v) => (v as List)[1]).toSet();
|
|
expect(ticks, hasLength(1));
|
|
});
|
|
|
|
test('a tombstoned or malformed record reads as not free', () async {
|
|
final persistence = _MemPersistence();
|
|
final hlc = Hlc.newTick('other').toStr();
|
|
persistence.text = jsonEncode({
|
|
'2026-09-12': {
|
|
'id': '2026-09-12',
|
|
'fields': {
|
|
'state': ['free', hlc],
|
|
},
|
|
'deleted': true,
|
|
'deleted_hlc': hlc,
|
|
},
|
|
'2026-09-13': {
|
|
'id': '2026-09-13',
|
|
'fields': {
|
|
'state': [7, hlc],
|
|
},
|
|
'deleted': false,
|
|
'deleted_hlc': null,
|
|
},
|
|
'not-a-date': {
|
|
'id': 'not-a-date',
|
|
'fields': {
|
|
'state': ['free', hlc],
|
|
},
|
|
'deleted': false,
|
|
'deleted_hlc': null,
|
|
},
|
|
});
|
|
final pool = await _open(persistence);
|
|
expect(pool.isFree(_tomorrow), isFalse);
|
|
expect(pool.isFree(DateTime(2026, 9, 13)), isFalse);
|
|
expect(pool.freeDays(), isEmpty);
|
|
expect(pool.spent(2026), 0);
|
|
expect(pool.reasonFor(_tomorrow), '');
|
|
});
|
|
|
|
test('remaining clamps at zero after a merge over budget', () async {
|
|
final pool = await _open();
|
|
final peer = <String, Record>{};
|
|
final hlc = Hlc.newTick('peer');
|
|
for (var i = 0; i < FreeDayPool.annualBudget + 3; i++) {
|
|
final id = FreeDayPool.toIso(DateTime(2026, 1, 1 + i));
|
|
peer[id] = Record(
|
|
id: id,
|
|
fields: {'state': ('free', hlc), 'consumed': (true, hlc)},
|
|
);
|
|
}
|
|
await pool.replaceAll(peer);
|
|
expect(pool.snapshot(), hasLength(FreeDayPool.annualBudget + 3));
|
|
expect(pool.remaining(2026), 0);
|
|
});
|
|
});
|
|
}
|