mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-22 06:02:18 +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
181 lines
5.2 KiB
Dart
181 lines
5.2 KiB
Dart
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
|
|
import 'package:wake_alarm_sync/services/free_day_pool.dart';
|
|
import 'package:wake_alarm_sync/services/free_day_sync.dart';
|
|
|
|
class _MemPersistence implements LogPersistence {
|
|
String? text;
|
|
|
|
@override
|
|
Future<String?> read() async => text;
|
|
|
|
@override
|
|
Future<void> write(String value) async => text = value;
|
|
}
|
|
|
|
/// An in-memory `RemoteStore`: a flat path -> text map.
|
|
class _MemRemote implements RemoteStore {
|
|
final Map<String, String> files = {};
|
|
|
|
@override
|
|
Future<List<String>> listDirectory(String path) async {
|
|
final prefix = '$path/';
|
|
return files.keys
|
|
.where((k) => k.startsWith(prefix))
|
|
.map((k) => k.substring(prefix.length).split('/').first)
|
|
.toSet()
|
|
.toList();
|
|
}
|
|
|
|
@override
|
|
Future<String?> getFileText(String path) async => files[path];
|
|
|
|
@override
|
|
Future<void> putFileText(
|
|
String path,
|
|
String text, {
|
|
required String message,
|
|
}) async => files[path] = text;
|
|
|
|
@override
|
|
Future<void> deleteFile(String path, {String? message}) async =>
|
|
files.remove(path);
|
|
|
|
@override
|
|
Future<bool> canAccessRemote() async => true;
|
|
|
|
@override
|
|
void close() {}
|
|
}
|
|
|
|
class _Broken implements RemoteStore {
|
|
@override
|
|
Future<List<String>> listDirectory(String path) async =>
|
|
throw RemoteSyncError('offline');
|
|
|
|
@override
|
|
Future<String?> getFileText(String path) async =>
|
|
throw RemoteSyncError('offline');
|
|
|
|
@override
|
|
Future<void> putFileText(
|
|
String path,
|
|
String text, {
|
|
required String message,
|
|
}) async => throw RemoteSyncError('offline');
|
|
|
|
@override
|
|
Future<void> deleteFile(String path, {String? message}) async {}
|
|
|
|
@override
|
|
Future<bool> canAccessRemote() async => false;
|
|
|
|
@override
|
|
void close() {}
|
|
}
|
|
|
|
final _today = DateTime(2026, 9, 11);
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
late List<MethodCall> calls;
|
|
late AlarmScheduler scheduler;
|
|
var channelThrows = false;
|
|
|
|
setUp(() {
|
|
calls = <MethodCall>[];
|
|
channelThrows = false;
|
|
const channel = MethodChannel(AlarmScheduler.channelName);
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (call) async {
|
|
calls.add(call);
|
|
if (channelThrows) throw PlatformException(code: 'no_platform');
|
|
return null;
|
|
});
|
|
scheduler = AlarmScheduler(channel: channel);
|
|
});
|
|
|
|
Future<FreeDayPool> openPool() =>
|
|
FreeDayPool.open(persistence: _MemPersistence(), deviceId: 'phone');
|
|
|
|
List<String> lastCache() =>
|
|
((calls.last.arguments as Map)['dates'] as List).cast<String>();
|
|
|
|
test("pushes the local pool and pulls a peer's day into it", () async {
|
|
final remote = _MemRemote();
|
|
// The PC marked a day, under its own device directory.
|
|
final pcHlc = Hlc.newTick('pc');
|
|
remote.files['${FreeDayPool.pathPrefix}/pc/${FreeDayPool.filename}'] =
|
|
logToJson({
|
|
'2026-09-20': Record(
|
|
id: '2026-09-20',
|
|
fields: {'state': ('free', pcHlc), 'consumed': (false, pcHlc)},
|
|
),
|
|
});
|
|
final pool = await openPool();
|
|
await pool.mark(DateTime(2026, 9, 12), now: _today);
|
|
|
|
final sync = FreeDaySync(
|
|
openClient: () async => remote,
|
|
scheduler: scheduler,
|
|
);
|
|
expect(await sync.sync(pool), 'Synced ✓');
|
|
|
|
expect(pool.isFree(DateTime(2026, 9, 20)), isTrue, reason: 'pulled');
|
|
final pushed =
|
|
remote.files['${FreeDayPool.pathPrefix}/phone/${FreeDayPool.filename}'];
|
|
expect(pushed, contains('2026-09-12'), reason: 'pushed');
|
|
expect(calls.last.method, 'setFreeDays');
|
|
expect(lastCache(), ['2026-09-12', '2026-09-20']);
|
|
});
|
|
|
|
test(
|
|
'a device not connected to Firebase still refreshes the cache',
|
|
() async {
|
|
final pool = await openPool();
|
|
await pool.mark(DateTime(2026, 9, 12), now: _today);
|
|
final sync = FreeDaySync(
|
|
openClient: () async => null,
|
|
scheduler: scheduler,
|
|
);
|
|
expect(await sync.sync(pool), contains('connect Firebase'));
|
|
expect(lastCache(), ['2026-09-12']);
|
|
},
|
|
);
|
|
|
|
test('an unreachable remote reports an error and keeps the cache', () async {
|
|
final pool = await openPool();
|
|
await pool.mark(DateTime(2026, 9, 12), now: _today);
|
|
final sync = FreeDaySync(
|
|
openClient: () async => _Broken(),
|
|
scheduler: scheduler,
|
|
);
|
|
expect(await sync.sync(pool), startsWith('Error:'));
|
|
expect(pool.isFree(DateTime(2026, 9, 12)), isTrue);
|
|
expect(lastCache(), ['2026-09-12']);
|
|
});
|
|
|
|
test('a client factory that throws is reported, not propagated', () async {
|
|
final pool = await openPool();
|
|
final sync = FreeDaySync(
|
|
openClient: () async => throw StateError('keystore locked'),
|
|
scheduler: scheduler,
|
|
);
|
|
expect(await sync.sync(pool), contains('keystore locked'));
|
|
expect(calls.last.method, 'setFreeDays');
|
|
});
|
|
|
|
test('a missing platform channel does not fail the sync', () async {
|
|
channelThrows = true;
|
|
final pool = await openPool();
|
|
final sync = FreeDaySync(
|
|
openClient: () async => _MemRemote(),
|
|
scheduler: scheduler,
|
|
);
|
|
expect(await sync.sync(pool), 'Synced ✓');
|
|
});
|
|
}
|