diet-guard/app/test/services/body_test_support.dart
Krzysztof kuhy Rudnicki e761926df4
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
Body tab: weight/body-fat logs, BMI, ideal weight, goal-based kcal target, workouts + Health Connect steps, week planner
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-28 09:03:03 +02:00

75 lines
1.8 KiB
Dart

// Shared fakes for the Body feature's tests: an in-memory DocumentStore and
// an in-memory RemoteStore (path -> text), enough for `syncLog` and the
// wake-alarm ingestion, which only list directories and read/write files.
import 'package:crdt_sync/crdt_sync.dart';
import 'package:diet_guard_app/services/document_store.dart';
/// Documents kept in a map.
class MemoryDocStore implements DocumentStore {
/// Stored documents by name.
final Map<String, String> documents = {};
@override
Future<String?> read(String name) async => documents[name];
@override
Future<void> write(String name, String contents) async {
documents[name] = contents;
}
}
/// A RemoteStore over a flat `path -> text` map.
class MemoryRemote implements RemoteStore {
/// Creates the store, optionally pre-seeded with [files].
MemoryRemote([Map<String, String>? files]) : files = {...?files};
/// Every file, by full path.
final Map<String, String> files;
/// Set to make every call throw, to test failure handling.
Object? failWith;
void _maybeFail() {
final error = failWith;
if (error != null) throw error;
}
@override
Future<List<String>> listDirectory(String path) async {
_maybeFail();
final prefix = '$path/';
return {
for (final key in files.keys)
if (key.startsWith(prefix)) key.substring(prefix.length).split('/')[0],
}.toList();
}
@override
Future<String?> getFileText(String path) async {
_maybeFail();
return files[path];
}
@override
Future<void> putFileText(
String path,
String text, {
required String message,
}) async {
_maybeFail();
files[path] = text;
}
@override
Future<void> deleteFile(String path, {String message = ''}) async {
files.remove(path);
}
@override
Future<bool> canAccessRemote() async => true;
@override
void close() {}
}