wake-alarm/phone_app/test/sick_day_interop_writer_test.dart
Krzysztof kuhy Rudnicki 597901dc08 phone: sick-day interop both ways with keep; docs
check.sh now runs Dart->Python (keep, unsorted/duplicate input, a cleared
keep day, a keep-only edit that must not re-stamp state) and
Python->Dart (0.2.0 keep sets plus a 0.1.0 record with no keep, a stale
keep and a malformed one), Python 0.2.0 as the oracle with explicit
paths and HOME pointed at a scratch dir. DOCS-sick-days.md and the
SickDays.kt contract describe the alarm-only native cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PAhbNszs5JKdtnD4uBi3xp
2026-10-16 15:31:11 +02:00

59 lines
2.1 KiB
Dart

import 'dart:io';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:wake_alarm_sync/services/sick_day_keep.dart';
import 'package:wake_alarm_sync/services/sick_day_pool.dart';
/// Half of the cross-language check (`scripts/sick_days_interop/check.sh`).
///
/// Writes a pool through the Dart rules to `SICK_DAYS_INTEROP_OUT`; the
/// script reads it back with `sickdays` (Python) and replays the same edits
/// there. The other direction is `sick_day_interop_reader_test.dart`.
/// Under a plain `flutter test` the variable is unset and this does nothing.
class _FilePersistence implements LogPersistence {
_FilePersistence(this.file);
final File file;
@override
Future<String?> read() async =>
file.existsSync() ? file.readAsStringSync() : null;
@override
Future<void> write(String text) async => file.writeAsStringSync(text);
}
void main() {
test('writes the interop fixture when asked', () async {
final out = Platform.environment['SICK_DAYS_INTEROP_OUT'];
if (out == null) return;
final pool = await SickDayPool.open(
persistence: _FilePersistence(File(out)),
deviceId: 'dart-interop',
);
final now = DateTime(2026, 10, 16);
bool never(DateTime _) => false;
// Today sick keeping nothing; 2026-10-20 sick keeping workout + alarm
// (given unsorted, with a duplicate); 2026-10-21 marked keeping diet,
// then cleared; 2026-10-22 marked, then a keep-only edit to leetcode;
// 2026-10-23 untouched. `verify.py dart` replays exactly this.
await pool.add(now, isFree: never, now: now);
await pool.add(
DateTime(2026, 10, 20),
isFree: never,
now: now,
keep: [SickLocker.workout, SickLocker.alarm, SickLocker.workout],
);
await pool.add(
DateTime(2026, 10, 21),
isFree: never,
now: now,
keep: [SickLocker.diet],
);
await pool.remove(DateTime(2026, 10, 21), now: now);
await pool.add(DateTime(2026, 10, 22), isFree: never, now: now);
await pool.setKept(DateTime(2026, 10, 22), [SickLocker.leetcode], now: now);
});
}