diet-guard/app/test/services/sync_settings_test.dart
Krzysztof kuhy Rudnicki a82047502f Add Flutter half of cross-device sync (Milestone 3)
Ports github_client.dart and sync_settings.dart from ~/todo (PAT-paste
instead of OAuth device flow), and writes a new (non-CRDT) sync_merge.dart
and sync_service.dart matching diet_guard's Python _sync_merge.py/_sync.py
algorithm exactly. Adds a settings screen for the PAT plus manual "Sync
now", and wires lifecycle-triggered auto-sync (launch + resumed/paused)
into the main logging screen, silent on failure per plan decision 4.

Also adds Linux desktop platform scaffolding so this and future UI changes
can be visually verified without a connected phone.

Verified end-to-end against the real kuhyx/diet-guard-sync GitHub API on a
Linux desktop build: Test connection and Sync now both round-trip to GitHub
and surface real auth errors correctly via SnackBar.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RH2BHCKbDTiYJUMG3rb9nq
2026-06-22 22:42:27 +02:00

122 lines
4.2 KiB
Dart

import 'package:diet_guard_app/services/sync_settings.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../fake_secure_storage.dart';
void main() {
// installFakeSecureStorage touches the test binary messenger, which needs
// the binding up first (widget tests get this for free via testWidgets).
TestWidgetsFlutterBinding.ensureInitialized();
test(
'load returns the kuhyx/diet-guard-sync defaults on a fresh install',
() async {
SharedPreferences.setMockInitialValues({});
installFakeSecureStorage();
final s = await SyncSettings.load();
expect(s.owner, 'kuhyx');
expect(s.repo, 'diet-guard-sync');
expect(s.token, '');
},
);
test('save stores the token in the keystore, not in prefs', () async {
SharedPreferences.setMockInitialValues({});
installFakeSecureStorage();
await const SyncSettings(owner: 'me', repo: 'notes', token: 'tok').save();
// Token must not linger in plaintext prefs once secured.
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString('sync.token'), isNull);
final s = await SyncSettings.load();
expect(s.owner, 'me');
expect(s.repo, 'notes');
expect(s.token, 'tok');
});
test('load reads the token straight from the keystore', () async {
SharedPreferences.setMockInitialValues({});
installFakeSecureStorage(initial: {'sync.token': 'fromKeystore'});
final s = await SyncSettings.load();
expect(s.token, 'fromKeystore');
});
test('load migrates a legacy plaintext token into the keystore', () async {
SharedPreferences.setMockInitialValues({'sync.token': 'legacy'});
installFakeSecureStorage();
final s = await SyncSettings.load();
expect(s.token, 'legacy');
// The plaintext copy is dropped once the secure write succeeds, and the
// value now resolves from the keystore on the next load.
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString('sync.token'), isNull);
final again = await SyncSettings.load();
expect(again.token, 'legacy');
});
test(
'load keeps the plaintext token when no secret service is available',
() async {
SharedPreferences.setMockInitialValues({'sync.token': 'plain'});
installFakeSecureStorage(throwing: true);
final s = await SyncSettings.load();
expect(s.token, 'plain');
// Never drop the only copy when the keystore write can't be confirmed.
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString('sync.token'), 'plain');
},
);
test('save falls back to plaintext prefs when the keystore fails', () async {
SharedPreferences.setMockInitialValues({});
installFakeSecureStorage(throwing: true);
await const SyncSettings(owner: 'o', repo: 'r', token: 'tok').save();
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString('sync.token'), 'tok');
});
test('save with an empty token clears the keystore entry', () async {
// Seed a keystore token, then save an empty token: it must be deleted
// and no plaintext copy written.
SharedPreferences.setMockInitialValues({});
installFakeSecureStorage(initial: {'sync.token': 'old'});
await const SyncSettings(owner: 'o', repo: 'r', token: '').save();
final prefs = await SharedPreferences.getInstance();
expect(prefs.getString('sync.token'), isNull);
final s = await SyncSettings.load();
expect(s.token, '');
});
test('isConfigured requires owner, repo and token', () {
expect(
const SyncSettings(owner: 'o', repo: 'r', token: 't').isConfigured,
isTrue,
);
expect(
const SyncSettings(owner: 'o', repo: 'r', token: '').isConfigured,
isFalse,
);
});
test('copyWith overrides only the given fields', () {
const base = SyncSettings(owner: 'o', repo: 'r', token: 't');
final next = base.copyWith(token: 'new');
expect(next.owner, 'o');
expect(next.repo, 'r');
expect(next.token, 'new');
// No-arg copy exercises the `?? this.x` fallback on every field.
final clone = base.copyWith();
expect(clone.owner, 'o');
expect(clone.repo, 'r');
expect(clone.token, 't');
});
}