mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-22 13:22:18 +01:00
The Dart half of the credential sync: a `KuchniaCredentialService` that owns the value locally, a `sync_merge_kuchnia.dart` adapter mirroring the Python one, and a `_syncKuchniaCredential` tick alongside the banks and budget. The phone can now receive the panel password, which is the prerequisite for it fetching anything itself. One source of truth, per the answered question: the value lives in the synced document store and nowhere else. Deliberately *not* mirrored into `flutter_secure_storage` -- two homes for one value is how a stale password survives a change made on the other device, and the credential has to be readable synchronously at merge time anyway. Gated by a second shared fixture, `tests/fixtures/kuchnia_credential.json`, asserted by both suites. Two cases carry the weight: - an **unparsable** edit time must fall back to the same clock on both sides (Python catches `ValueError` from `fromisoformat`, Dart's `tryParse` returns null). Verified they agree on `wall_time_ms == 0`; mutating that expectation fails both suites. - a **blank half** contributes nothing, so a half-filled settings form cannot push an empty-string password that wins LWW against a real one. `KuchniaCredentialService.init()` is called in the WorkManager isolate as well as `main()`. A fresh isolate has its own static state and `_syncKuchniaCredential` no-ops when the singleton is missing, so without it a background tick would neither publish this device's credential nor receive a peer's -- and on the phone the background tick is most of the syncing there is. Same class of trap as the `initSyncDeviceId()` one already documented there. `.toDouble()` came off the bank record: the fields are already `double`, so it was a no-op the analyzer flags. The field *types* are what keep the encoding byte-identical to Python's, and `kuchnia_parity_test.dart` asserts that. 654 Dart tests, 1040 Python tests, 100% Python branch coverage. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QQpCiPaSbyRpwsLkH3dgsX
136 lines
4.9 KiB
Dart
136 lines
4.9 KiB
Dart
/// The catering credential's local storage.
|
|
///
|
|
/// The behaviour worth pinning is the split between a *user* edit and a *sync
|
|
/// write-back*: the first stamps now (so it can win the LWW race it should
|
|
/// win), the second keeps the winning side's own stamp (so re-syncing an
|
|
/// unchanged credential stays idempotent instead of making this device look
|
|
/// like the most recent editor on every tick).
|
|
library;
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:diet_guard_app/services/document_store.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_credential_service.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
/// In-memory store, so these tests need no plugin channel.
|
|
class _MemoryStore implements DocumentStore {
|
|
final Map<String, String> documents = {};
|
|
int writes = 0;
|
|
|
|
@override
|
|
Future<String?> read(String name) async => documents[name];
|
|
|
|
@override
|
|
Future<void> write(String name, String contents) async {
|
|
documents[name] = contents;
|
|
writes++;
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
late _MemoryStore store;
|
|
|
|
setUp(() {
|
|
store = _MemoryStore();
|
|
KuchniaCredentialService.resetForTesting();
|
|
});
|
|
|
|
tearDown(KuchniaCredentialService.resetForTesting);
|
|
|
|
test('reads back nothing before anything is stored', () async {
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
expect(KuchniaCredentialService.username, isEmpty);
|
|
expect(KuchniaCredentialService.password, isEmpty);
|
|
expect(KuchniaCredentialService.isConfigured, isFalse);
|
|
});
|
|
|
|
test('a user edit round-trips and stamps a time', () async {
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
await KuchniaCredentialService.instance.save('me@example.com', 'hunter2');
|
|
|
|
expect(KuchniaCredentialService.username, 'me@example.com');
|
|
expect(KuchniaCredentialService.password, 'hunter2');
|
|
expect(KuchniaCredentialService.editedAt, isNotEmpty);
|
|
expect(KuchniaCredentialService.isConfigured, isTrue);
|
|
});
|
|
|
|
test('a stored credential survives a reload', () async {
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
await KuchniaCredentialService.instance.save('me@example.com', 'hunter2');
|
|
|
|
KuchniaCredentialService.resetForTesting();
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
expect(KuchniaCredentialService.username, 'me@example.com');
|
|
expect(KuchniaCredentialService.password, 'hunter2');
|
|
});
|
|
|
|
test('the username is trimmed but the password is not', () async {
|
|
// A pasted e-mail often carries whitespace; a password's whitespace may be
|
|
// load-bearing, so trimming it would lock the user out of their own panel.
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
await KuchniaCredentialService.instance.save(' me@example.com ', ' pw ');
|
|
|
|
expect(KuchniaCredentialService.username, 'me@example.com');
|
|
expect(KuchniaCredentialService.password, ' pw ');
|
|
});
|
|
|
|
test('a sync write-back keeps the winning edit time', () async {
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
await KuchniaCredentialService.instance.applySynced(
|
|
'peer@example.com',
|
|
'peer-pass',
|
|
'2026-08-20T09:00:00.000',
|
|
);
|
|
expect(KuchniaCredentialService.editedAt, '2026-08-20T09:00:00.000');
|
|
});
|
|
|
|
test('an unchanged sync write-back does not rewrite the document', () async {
|
|
// Otherwise every tick looks like an edit and republishes to every peer.
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
await KuchniaCredentialService.instance.applySynced(
|
|
'peer@example.com',
|
|
'peer-pass',
|
|
'2026-08-20T09:00:00.000',
|
|
);
|
|
final writesAfterFirst = store.writes;
|
|
|
|
await KuchniaCredentialService.instance.applySynced(
|
|
'peer@example.com',
|
|
'peer-pass',
|
|
'2026-08-20T09:00:00.000',
|
|
);
|
|
expect(store.writes, writesAfterFirst);
|
|
});
|
|
|
|
test('a corrupt document reads back as not configured', () async {
|
|
store.documents[KuchniaCredentialService.documentName] = '{not json';
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
expect(KuchniaCredentialService.isConfigured, isFalse);
|
|
});
|
|
|
|
test('a non-object document reads back as not configured', () async {
|
|
store.documents[KuchniaCredentialService.documentName] = '[1, 2]';
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
expect(KuchniaCredentialService.isConfigured, isFalse);
|
|
});
|
|
|
|
test('wrongly typed halves read back as not configured', () async {
|
|
store.documents[KuchniaCredentialService.documentName] = jsonEncode({
|
|
'username': 'me@example.com',
|
|
'password': 42,
|
|
});
|
|
await KuchniaCredentialService.initForTesting(store);
|
|
expect(KuchniaCredentialService.password, isEmpty);
|
|
expect(KuchniaCredentialService.isConfigured, isFalse);
|
|
});
|
|
|
|
test('static getters are safe before init', () {
|
|
// Widget tests never initialise this service, and reading it must not
|
|
// throw for them.
|
|
expect(KuchniaCredentialService.username, isEmpty);
|
|
expect(KuchniaCredentialService.isConfigured, isFalse);
|
|
expect(KuchniaCredentialService.isInitialized, isFalse);
|
|
});
|
|
}
|