mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-29 13:23:33 +01:00
Compare commits
2 Commits
c12faa3920
...
50f311b6b8
| Author | SHA1 | Date | |
|---|---|---|---|
| 50f311b6b8 | |||
| 2231850bfc |
@ -1,17 +1,75 @@
|
|||||||
# diet_guard_app
|
# diet_guard_app
|
||||||
|
|
||||||
A new Flutter project.
|
Companion app for `diet_guard`: log meals on the phone, and on the Linux
|
||||||
|
desktop, sharing one food log through GitHub-backed sync.
|
||||||
|
|
||||||
## Getting Started
|
## Two targets, and only two
|
||||||
|
|
||||||
This project is a starting point for a Flutter application.
|
| Target | What it is |
|
||||||
|
|---|---|
|
||||||
|
| Android | a normal Flutter app (`flutter build apk`) |
|
||||||
|
| Linux desktop | the Flutter **web** build, served by a local wrapper and shown in a Chrome `--app` window |
|
||||||
|
|
||||||
A few resources to get you started if this is your first Flutter project:
|
There is no `linux/` directory. Flutter's GTK embedder manages ~20fps at
|
||||||
|
3840x2160 -- measured on a stock `flutter create` counter app, so it is the
|
||||||
|
toolkit rather than this code -- where the same Dart rendered by Chrome
|
||||||
|
sustains ~144fps (`~/todo/docs/desktop-performance-findings.md`). Running
|
||||||
|
`flutter create --platforms linux` here would silently restore the slow path.
|
||||||
|
|
||||||
- [Learn Flutter](https://docs.flutter.dev/get-started/learn-flutter)
|
## Commands
|
||||||
- [Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
|
|
||||||
- [Flutter learning resources](https://docs.flutter.dev/reference/learning-resources)
|
|
||||||
|
|
||||||
For help getting started with Flutter development, view the
|
```bash
|
||||||
[online documentation](https://docs.flutter.dev/), which offers tutorials,
|
flutter test # the app's suite
|
||||||
samples, guidance on mobile development, and a full API reference.
|
flutter analyze
|
||||||
|
bash run.sh # build the web bundle and launch the desktop app
|
||||||
|
bash install_arch.sh # build + install the pacman package and .desktop entry
|
||||||
|
flutter build apk --release # the phone build
|
||||||
|
adb install -r build/app/outputs/flutter-apk/app-release.apk
|
||||||
|
```
|
||||||
|
|
||||||
|
Never `adb uninstall` or `pm clear` this package: both wipe the on-device log.
|
||||||
|
|
||||||
|
## The desktop wrapper
|
||||||
|
|
||||||
|
`bin/diet_guard_desktop.dart` is the other half of the desktop app, because a
|
||||||
|
browser cannot touch the filesystem. It:
|
||||||
|
|
||||||
|
- serves the built web assets from `build/web` (or `/opt/diet-guard-app/web`
|
||||||
|
once packaged);
|
||||||
|
- keeps an on-disk copy of every document and photo under
|
||||||
|
`~/.local/share/diet-guard-desktop/`, which is what makes a wiped Chrome
|
||||||
|
profile recoverable -- for photos it is the *only* copy, since they never
|
||||||
|
sync;
|
||||||
|
- holds the GitHub token (`~/.config/diet_guard_app/sync_token`, falling back
|
||||||
|
to the PC gate's `~/.config/diet_guard/sync_token`) and proxies
|
||||||
|
`api.github.com` plus the device flow, whose endpoints send no CORS headers
|
||||||
|
and so cannot be called from a page at all.
|
||||||
|
|
||||||
|
**The port (8732) and the Chrome `--user-data-dir` must stay fixed.**
|
||||||
|
IndexedDB is keyed by origin and lives inside that profile, so changing either
|
||||||
|
looks to the app like a device with no history.
|
||||||
|
|
||||||
|
## Platform seams
|
||||||
|
|
||||||
|
`dart:io` does not fail a web *compile*; it becomes a stub that throws at
|
||||||
|
runtime, so the symptom of a missed seam is a blank white window rather than a
|
||||||
|
build error. Every platform edge is a conditional export -- `document_store*`,
|
||||||
|
`blob_store*`, `background_tasks*`, `notification_backend*`, `token_vault*`,
|
||||||
|
`github_client_factory*`, `attached_image*`. Branch on `kIsWeb` *before* any
|
||||||
|
`Platform.is…`, which itself throws on web.
|
||||||
|
|
||||||
|
What the desktop genuinely loses, by design rather than omission:
|
||||||
|
|
||||||
|
- **Background reminders.** A browser runs nothing once its window is closed,
|
||||||
|
so the due-slot check runs in-page every 5 minutes while the window is open.
|
||||||
|
On the PC the real backstop is `diet-guard-gate.timer`, which locks the
|
||||||
|
screen.
|
||||||
|
- **Camera capture.** The browser picker is a file input.
|
||||||
|
- **Photo sharing between devices** -- unchanged: `imagePath` was always
|
||||||
|
stripped before push.
|
||||||
|
|
||||||
|
## Sync identity
|
||||||
|
|
||||||
|
Three devices push under `diet-guard-sync/devices/`: `pc` (the Python gate),
|
||||||
|
`phone`, and `desktop`. Each must keep its own id -- two devices sharing one
|
||||||
|
would overwrite each other's pushed log on every tick.
|
||||||
|
|||||||
84
app/lib/services/blob_store_indexeddb.dart
Normal file
84
app/lib/services/blob_store_indexeddb.dart
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/// IndexedDB-backed [BlobStore], independent of how the database was opened.
|
||||||
|
///
|
||||||
|
/// Split from `blob_store_web.dart` so a plain VM test can drive it against
|
||||||
|
/// `idb_shim`'s in-memory backend: the browser factory
|
||||||
|
/// (`idb_browser.dart`) cannot be imported outside a web compile, and photos
|
||||||
|
/// have no other copy anywhere -- they never sync.
|
||||||
|
library;
|
||||||
|
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:diet_guard_app/services/blob_store.dart';
|
||||||
|
import 'package:diet_guard_app/services/desktop_wrapper.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:idb_shim/idb_shim.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
/// [BlobStore] keeping photo bytes in IndexedDB, mirrored to the desktop
|
||||||
|
/// wrapper's on-disk copy.
|
||||||
|
///
|
||||||
|
/// Photos never sync (`imagePath` is stripped before push and ignored on
|
||||||
|
/// pull), so the wrapper mirror is the *only* thing standing between a wiped
|
||||||
|
/// Chrome profile and permanently losing them -- unlike the food log, which
|
||||||
|
/// GitHub would eventually restore.
|
||||||
|
class IndexedDbBlobStore implements BlobStore {
|
||||||
|
/// Creates a store over an already-open [database], mirroring to [baseUrl].
|
||||||
|
IndexedDbBlobStore(
|
||||||
|
this._database, {
|
||||||
|
this.baseUrl = desktopWrapperOrigin,
|
||||||
|
http.Client? httpClient,
|
||||||
|
}) : _client = httpClient ?? http.Client();
|
||||||
|
|
||||||
|
final Database _database;
|
||||||
|
final http.Client _client;
|
||||||
|
|
||||||
|
/// Origin of the desktop wrapper.
|
||||||
|
final String baseUrl;
|
||||||
|
|
||||||
|
/// Object store holding one entry per photo.
|
||||||
|
static const storeName = 'blobs';
|
||||||
|
|
||||||
|
/// IndexedDB database name.
|
||||||
|
static const databaseName = 'diet_guard_blobs';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String> put(String extension, Uint8List bytes) async {
|
||||||
|
final key = '${const Uuid().v4()}$extension';
|
||||||
|
final txn = _database.transaction(storeName, idbModeReadWrite);
|
||||||
|
await txn.objectStore(storeName).put(bytes, key);
|
||||||
|
await txn.completed;
|
||||||
|
await _writeMirror(key, bytes);
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Uint8List?> get(String key) async {
|
||||||
|
final txn = _database.transaction(storeName, idbModeReadOnly);
|
||||||
|
final value = await txn.objectStore(storeName).getObject(key);
|
||||||
|
await txn.completed;
|
||||||
|
if (value is Uint8List) return value;
|
||||||
|
if (value is List<int>) return Uint8List.fromList(value);
|
||||||
|
// Missing locally: recover from the wrapper's disk copy (cleared profile).
|
||||||
|
return _readMirror(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Uint8List?> _readMirror(String key) async {
|
||||||
|
try {
|
||||||
|
final response = await _client.get(_uriFor(key));
|
||||||
|
if (response.statusCode != 200 || response.bodyBytes.isEmpty) return null;
|
||||||
|
return response.bodyBytes;
|
||||||
|
} on Exception {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _writeMirror(String key, Uint8List bytes) async {
|
||||||
|
try {
|
||||||
|
await _client.post(_uriFor(key), body: bytes);
|
||||||
|
} on Exception {
|
||||||
|
// Best-effort by design, like the document mirror.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Uri _uriFor(String key) => Uri.parse('$baseUrl${WrapperPaths.blobs}$key');
|
||||||
|
}
|
||||||
@ -1,84 +1,12 @@
|
|||||||
/// IndexedDB-backed [BlobStore] for the browser-hosted desktop app.
|
/// Opens the IndexedDB-backed [BlobStore] in the browser.
|
||||||
library;
|
library;
|
||||||
|
|
||||||
import 'dart:typed_data';
|
|
||||||
|
|
||||||
import 'package:diet_guard_app/services/blob_store.dart';
|
import 'package:diet_guard_app/services/blob_store.dart';
|
||||||
import 'package:diet_guard_app/services/desktop_wrapper.dart';
|
import 'package:diet_guard_app/services/blob_store_indexeddb.dart';
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
import 'package:idb_shim/idb_browser.dart';
|
import 'package:idb_shim/idb_browser.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
|
||||||
|
|
||||||
/// [BlobStore] keeping photo bytes in IndexedDB, mirrored to the desktop
|
|
||||||
/// wrapper's on-disk copy.
|
|
||||||
///
|
|
||||||
/// Photos never sync (`imagePath` is stripped before push and ignored on
|
|
||||||
/// pull), so the wrapper mirror is the *only* thing standing between a wiped
|
|
||||||
/// Chrome profile and permanently losing them -- unlike the food log, which
|
|
||||||
/// GitHub would eventually restore.
|
|
||||||
class IndexedDbBlobStore implements BlobStore {
|
|
||||||
/// Creates a store over an already-open [database], mirroring to [baseUrl].
|
|
||||||
IndexedDbBlobStore(
|
|
||||||
this._database, {
|
|
||||||
this.baseUrl = desktopWrapperOrigin,
|
|
||||||
http.Client? httpClient,
|
|
||||||
}) : _client = httpClient ?? http.Client();
|
|
||||||
|
|
||||||
final Database _database;
|
|
||||||
final http.Client _client;
|
|
||||||
|
|
||||||
/// Origin of the desktop wrapper.
|
|
||||||
final String baseUrl;
|
|
||||||
|
|
||||||
/// Object store holding one entry per photo.
|
|
||||||
static const storeName = 'blobs';
|
|
||||||
|
|
||||||
/// IndexedDB database name.
|
|
||||||
static const databaseName = 'diet_guard_blobs';
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<String> put(String extension, Uint8List bytes) async {
|
|
||||||
final key = '${const Uuid().v4()}$extension';
|
|
||||||
final txn = _database.transaction(storeName, idbModeReadWrite);
|
|
||||||
await txn.objectStore(storeName).put(bytes, key);
|
|
||||||
await txn.completed;
|
|
||||||
await _writeMirror(key, bytes);
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Uint8List?> get(String key) async {
|
|
||||||
final txn = _database.transaction(storeName, idbModeReadOnly);
|
|
||||||
final value = await txn.objectStore(storeName).getObject(key);
|
|
||||||
await txn.completed;
|
|
||||||
if (value is Uint8List) return value;
|
|
||||||
if (value is List<int>) return Uint8List.fromList(value);
|
|
||||||
// Missing locally: recover from the wrapper's disk copy (cleared profile).
|
|
||||||
return _readMirror(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<Uint8List?> _readMirror(String key) async {
|
|
||||||
try {
|
|
||||||
final response = await _client.get(_uriFor(key));
|
|
||||||
if (response.statusCode != 200 || response.bodyBytes.isEmpty) return null;
|
|
||||||
return response.bodyBytes;
|
|
||||||
} on Exception {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _writeMirror(String key, Uint8List bytes) async {
|
|
||||||
try {
|
|
||||||
await _client.post(_uriFor(key), body: bytes);
|
|
||||||
} on Exception {
|
|
||||||
// Best-effort by design, like the document mirror.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Uri _uriFor(String key) => Uri.parse('$baseUrl${WrapperPaths.blobs}$key');
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Opens (creating if needed) the IndexedDB-backed blob store.
|
/// Opens (creating if needed) the IndexedDB-backed blob store.
|
||||||
|
// coverage:ignore-start
|
||||||
Future<BlobStore> openBlobStore() async {
|
Future<BlobStore> openBlobStore() async {
|
||||||
final database = await idbFactoryBrowser.open(
|
final database = await idbFactoryBrowser.open(
|
||||||
IndexedDbBlobStore.databaseName,
|
IndexedDbBlobStore.databaseName,
|
||||||
@ -92,3 +20,5 @@ Future<BlobStore> openBlobStore() async {
|
|||||||
);
|
);
|
||||||
return IndexedDbBlobStore(database);
|
return IndexedDbBlobStore(database);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// coverage:ignore-end
|
||||||
|
|||||||
87
app/lib/services/document_store_indexeddb.dart
Normal file
87
app/lib/services/document_store_indexeddb.dart
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
/// IndexedDB-backed [DocumentStore], independent of how the database was
|
||||||
|
/// opened.
|
||||||
|
///
|
||||||
|
/// Split from `document_store_web.dart` so a plain VM test can drive it
|
||||||
|
/// against `idb_shim`'s in-memory backend: the browser factory
|
||||||
|
/// (`idb_browser.dart`) cannot be imported outside a web compile, and this is
|
||||||
|
/// the code a wiped Chrome profile depends on for recovery.
|
||||||
|
library;
|
||||||
|
|
||||||
|
import 'package:diet_guard_app/services/desktop_wrapper.dart';
|
||||||
|
import 'package:diet_guard_app/services/document_store.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:idb_shim/idb_shim.dart';
|
||||||
|
|
||||||
|
/// [DocumentStore] backed by IndexedDB, mirrored to the desktop wrapper's
|
||||||
|
/// on-disk copy.
|
||||||
|
///
|
||||||
|
/// IndexedDB rather than `localStorage`: the latter caps at roughly 5-10MB per
|
||||||
|
/// origin and is evicted more eagerly, and these documents are the desktop
|
||||||
|
/// device's primary copy of the food log.
|
||||||
|
///
|
||||||
|
/// The wrapper mirror is the recovery path for a wiped Chrome profile. GitHub
|
||||||
|
/// sync would eventually restore the log too, but only what other devices have
|
||||||
|
/// already seen -- a meal logged on this desktop and never pushed exists
|
||||||
|
/// nowhere else. Reads fall back to the mirror when IndexedDB is empty; writes
|
||||||
|
/// to it are best-effort so the app still works opened in a plain browser tab
|
||||||
|
/// with no wrapper running.
|
||||||
|
class IndexedDbDocumentStore implements DocumentStore {
|
||||||
|
/// Creates a store over an already-open [database], mirroring to [baseUrl].
|
||||||
|
IndexedDbDocumentStore(
|
||||||
|
this._database, {
|
||||||
|
this.baseUrl = desktopWrapperOrigin,
|
||||||
|
http.Client? httpClient,
|
||||||
|
}) : _client = httpClient ?? http.Client();
|
||||||
|
|
||||||
|
final Database _database;
|
||||||
|
final http.Client _client;
|
||||||
|
|
||||||
|
/// Origin of the desktop wrapper.
|
||||||
|
final String baseUrl;
|
||||||
|
|
||||||
|
/// Object store holding one entry per document name.
|
||||||
|
static const storeName = 'documents';
|
||||||
|
|
||||||
|
/// IndexedDB database name.
|
||||||
|
static const databaseName = 'diet_guard';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String?> read(String name) async {
|
||||||
|
final txn = _database.transaction(storeName, idbModeReadOnly);
|
||||||
|
final value = await txn.objectStore(storeName).getObject(name);
|
||||||
|
await txn.completed;
|
||||||
|
if (value is String) return value;
|
||||||
|
// Empty IndexedDB: either a first run or a cleared profile. Recover from
|
||||||
|
// the wrapper's disk copy rather than silently starting from nothing.
|
||||||
|
return _readMirror(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> write(String name, String contents) async {
|
||||||
|
final txn = _database.transaction(storeName, idbModeReadWrite);
|
||||||
|
await txn.objectStore(storeName).put(contents, name);
|
||||||
|
await txn.completed;
|
||||||
|
await _writeMirror(name, contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String?> _readMirror(String name) async {
|
||||||
|
try {
|
||||||
|
final response = await _client.get(_uriFor(name));
|
||||||
|
if (response.statusCode != 200 || response.body.isEmpty) return null;
|
||||||
|
return response.body;
|
||||||
|
} on Exception {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _writeMirror(String name, String contents) async {
|
||||||
|
try {
|
||||||
|
await _client.post(_uriFor(name), body: contents);
|
||||||
|
} on Exception {
|
||||||
|
// Best-effort by design; see the class docs.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Uri _uriFor(String name) =>
|
||||||
|
Uri.parse('$baseUrl${WrapperPaths.documents}$name');
|
||||||
|
}
|
||||||
@ -1,86 +1,12 @@
|
|||||||
/// IndexedDB-backed [DocumentStore] used by the browser-hosted desktop app.
|
/// Opens the IndexedDB-backed [DocumentStore] in the browser.
|
||||||
library;
|
library;
|
||||||
|
|
||||||
import 'package:diet_guard_app/services/desktop_wrapper.dart';
|
|
||||||
import 'package:diet_guard_app/services/document_store.dart';
|
import 'package:diet_guard_app/services/document_store.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:diet_guard_app/services/document_store_indexeddb.dart';
|
||||||
import 'package:idb_shim/idb_browser.dart';
|
import 'package:idb_shim/idb_browser.dart';
|
||||||
|
|
||||||
/// [DocumentStore] backed by IndexedDB, mirrored to the desktop wrapper's
|
|
||||||
/// on-disk copy.
|
|
||||||
///
|
|
||||||
/// IndexedDB rather than `localStorage`: the latter caps at roughly 5-10MB per
|
|
||||||
/// origin and is evicted more eagerly, and these documents are the desktop
|
|
||||||
/// device's primary copy of the food log.
|
|
||||||
///
|
|
||||||
/// The wrapper mirror is the recovery path for a wiped Chrome profile. GitHub
|
|
||||||
/// sync would eventually restore the log too, but only what other devices have
|
|
||||||
/// already seen -- a meal logged on this desktop and never pushed exists
|
|
||||||
/// nowhere else. Reads fall back to the mirror when IndexedDB is empty; writes
|
|
||||||
/// to it are best-effort so the app still works opened in a plain browser tab
|
|
||||||
/// with no wrapper running.
|
|
||||||
class IndexedDbDocumentStore implements DocumentStore {
|
|
||||||
/// Creates a store over an already-open [database], mirroring to [baseUrl].
|
|
||||||
IndexedDbDocumentStore(
|
|
||||||
this._database, {
|
|
||||||
this.baseUrl = desktopWrapperOrigin,
|
|
||||||
http.Client? httpClient,
|
|
||||||
}) : _client = httpClient ?? http.Client();
|
|
||||||
|
|
||||||
final Database _database;
|
|
||||||
final http.Client _client;
|
|
||||||
|
|
||||||
/// Origin of the desktop wrapper.
|
|
||||||
final String baseUrl;
|
|
||||||
|
|
||||||
/// Object store holding one entry per document name.
|
|
||||||
static const storeName = 'documents';
|
|
||||||
|
|
||||||
/// IndexedDB database name.
|
|
||||||
static const databaseName = 'diet_guard';
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<String?> read(String name) async {
|
|
||||||
final txn = _database.transaction(storeName, idbModeReadOnly);
|
|
||||||
final value = await txn.objectStore(storeName).getObject(name);
|
|
||||||
await txn.completed;
|
|
||||||
if (value is String) return value;
|
|
||||||
// Empty IndexedDB: either a first run or a cleared profile. Recover from
|
|
||||||
// the wrapper's disk copy rather than silently starting from nothing.
|
|
||||||
return _readMirror(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> write(String name, String contents) async {
|
|
||||||
final txn = _database.transaction(storeName, idbModeReadWrite);
|
|
||||||
await txn.objectStore(storeName).put(contents, name);
|
|
||||||
await txn.completed;
|
|
||||||
await _writeMirror(name, contents);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<String?> _readMirror(String name) async {
|
|
||||||
try {
|
|
||||||
final response = await _client.get(_uriFor(name));
|
|
||||||
if (response.statusCode != 200 || response.body.isEmpty) return null;
|
|
||||||
return response.body;
|
|
||||||
} on Exception {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _writeMirror(String name, String contents) async {
|
|
||||||
try {
|
|
||||||
await _client.post(_uriFor(name), body: contents);
|
|
||||||
} on Exception {
|
|
||||||
// Best-effort by design; see the class docs.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Uri _uriFor(String name) =>
|
|
||||||
Uri.parse('$baseUrl${WrapperPaths.documents}$name');
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Opens (creating if needed) the IndexedDB-backed document store.
|
/// Opens (creating if needed) the IndexedDB-backed document store.
|
||||||
|
// coverage:ignore-start
|
||||||
Future<DocumentStore> openDocumentStore() async {
|
Future<DocumentStore> openDocumentStore() async {
|
||||||
final database = await idbFactoryBrowser.open(
|
final database = await idbFactoryBrowser.open(
|
||||||
IndexedDbDocumentStore.databaseName,
|
IndexedDbDocumentStore.databaseName,
|
||||||
@ -94,3 +20,5 @@ Future<DocumentStore> openDocumentStore() async {
|
|||||||
);
|
);
|
||||||
return IndexedDbDocumentStore(database);
|
return IndexedDbDocumentStore(database);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// coverage:ignore-end
|
||||||
|
|||||||
147
app/test/services/indexeddb_stores_test.dart
Normal file
147
app/test/services/indexeddb_stores_test.dart
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
// The IndexedDB stores are the desktop device's primary copy of the food log
|
||||||
|
// and the *only* copy of its photos, and their wrapper-mirror fallback is the
|
||||||
|
// recovery path for a wiped Chrome profile. `idb_shim`'s in-memory backend
|
||||||
|
// lets all of that run on the plain VM, so none of it has to ship untested
|
||||||
|
// just because it only executes in a browser.
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:diet_guard_app/services/blob_store_indexeddb.dart';
|
||||||
|
import 'package:diet_guard_app/services/desktop_wrapper.dart';
|
||||||
|
import 'package:diet_guard_app/services/document_store_indexeddb.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:http/testing.dart';
|
||||||
|
import 'package:idb_shim/idb_shim.dart';
|
||||||
|
|
||||||
|
/// Stands in for the desktop wrapper: records what was mirrored to it and
|
||||||
|
/// serves back whatever it has been seeded with.
|
||||||
|
class _FakeWrapper {
|
||||||
|
final Map<String, List<int>> stored = {};
|
||||||
|
var offline = false;
|
||||||
|
|
||||||
|
http.Client get client => MockClient((request) async {
|
||||||
|
if (offline) throw http.ClientException('no wrapper running');
|
||||||
|
final name = request.url.pathSegments.last;
|
||||||
|
if (request.method == 'POST') {
|
||||||
|
stored[name] = request.bodyBytes;
|
||||||
|
return http.Response('', 204);
|
||||||
|
}
|
||||||
|
final body = stored[name];
|
||||||
|
if (body == null) return http.Response('', 404);
|
||||||
|
return http.Response.bytes(body, 200);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Database> _openMemoryDb(String store) => newIdbFactoryMemory().open(
|
||||||
|
'test',
|
||||||
|
version: 1,
|
||||||
|
onUpgradeNeeded: (event) => event.database.createObjectStore(store),
|
||||||
|
);
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late _FakeWrapper wrapper;
|
||||||
|
|
||||||
|
setUp(() => wrapper = _FakeWrapper());
|
||||||
|
|
||||||
|
group('IndexedDbDocumentStore', () {
|
||||||
|
late IndexedDbDocumentStore store;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
store = IndexedDbDocumentStore(
|
||||||
|
await _openMemoryDb(IndexedDbDocumentStore.storeName),
|
||||||
|
httpClient: wrapper.client,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('round-trips a document', () async {
|
||||||
|
await store.write('food_log.json', '{"2026-07-20":[]}');
|
||||||
|
|
||||||
|
expect(await store.read('food_log.json'), '{"2026-07-20":[]}');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('mirrors every write to the wrapper', () async {
|
||||||
|
// The mirror is what makes a wiped Chrome profile recoverable, so a
|
||||||
|
// write that only reaches IndexedDB is a silent data-loss risk.
|
||||||
|
await store.write('food_log.json', '{"a":1}');
|
||||||
|
|
||||||
|
expect(utf8.decode(wrapper.stored['food_log.json']!), '{"a":1}');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('recovers from the wrapper when IndexedDB is empty', () async {
|
||||||
|
// Exactly the cleared-profile case: nothing local, everything on disk.
|
||||||
|
wrapper.stored['food_log.json'] = utf8.encode('{"recovered":true}');
|
||||||
|
|
||||||
|
expect(await store.read('food_log.json'), '{"recovered":true}');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns null when neither side has the document', () async {
|
||||||
|
expect(await store.read('food_bank.json'), null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns null when the wrapper is not running', () async {
|
||||||
|
// Opened as a plain browser tab: no wrapper, no mirror, still usable.
|
||||||
|
wrapper.offline = true;
|
||||||
|
|
||||||
|
expect(await store.read('food_log.json'), null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a write still succeeds with no wrapper running', () async {
|
||||||
|
wrapper.offline = true;
|
||||||
|
|
||||||
|
await store.write('food_log.json', '{"a":1}');
|
||||||
|
|
||||||
|
expect(await store.read('food_log.json'), '{"a":1}');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('IndexedDbBlobStore', () {
|
||||||
|
late IndexedDbBlobStore store;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
store = IndexedDbBlobStore(
|
||||||
|
await _openMemoryDb(IndexedDbBlobStore.storeName),
|
||||||
|
httpClient: wrapper.client,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('put returns a key ending in the extension and stores the bytes', () async {
|
||||||
|
final key = await store.put('.jpg', Uint8List.fromList([1, 2, 3]));
|
||||||
|
|
||||||
|
expect(key, endsWith('.jpg'));
|
||||||
|
expect(await store.get(key), [1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('put gives each photo its own key', () async {
|
||||||
|
final first = await store.put('.jpg', Uint8List.fromList([1]));
|
||||||
|
final second = await store.put('.jpg', Uint8List.fromList([1]));
|
||||||
|
|
||||||
|
expect(first, isNot(second));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('mirrors photo bytes to the wrapper', () async {
|
||||||
|
// Photos never sync -- `imagePath` is stripped before push -- so this
|
||||||
|
// mirror is the only thing standing between a cleared profile and
|
||||||
|
// losing them permanently.
|
||||||
|
final key = await store.put('.jpg', Uint8List.fromList([7, 7]));
|
||||||
|
|
||||||
|
expect(wrapper.stored[key], [7, 7]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('recovers a photo from the wrapper when IndexedDB is empty', () async {
|
||||||
|
wrapper.stored['photo.jpg'] = [4, 2];
|
||||||
|
|
||||||
|
expect(await store.get('photo.jpg'), [4, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns null for a photo neither side has', () async {
|
||||||
|
expect(await store.get('gone.jpg'), null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns null when the wrapper is not running', () async {
|
||||||
|
wrapper.offline = true;
|
||||||
|
|
||||||
|
expect(await store.get('gone.jpg'), null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -13,7 +13,12 @@
|
|||||||
# `pip install -r requirements.txt`), rebuilt only when requirements.txt
|
# `pip install -r requirements.txt`), rebuilt only when requirements.txt
|
||||||
# changes (hash-gated so day-to-day pushes stay fast);
|
# changes (hash-gated so day-to-day pushes stay fast);
|
||||||
# 2. `pre-commit run --all-files` (mirrors the pre-commit workflow);
|
# 2. `pre-commit run --all-files` (mirrors the pre-commit workflow);
|
||||||
# 3. `python -m pytest` inside that clean venv (mirrors Tests).
|
# 3. `python -m pytest` inside that clean venv (mirrors Tests);
|
||||||
|
# 4. `flutter analyze` + `flutter test` for the companion app in app/.
|
||||||
|
#
|
||||||
|
# The app is gated here rather than only by pre-commit because pre-commit has
|
||||||
|
# no Dart hooks at all: without this, the app's tests were never run by
|
||||||
|
# anything automatic, and a red app/ could be pushed with a green gate.
|
||||||
#
|
#
|
||||||
# Wired as the pre-push hook, so a red result blocks the push before CI ever
|
# Wired as the pre-push hook, so a red result blocks the push before CI ever
|
||||||
# sees it. Escape hatch for genuine emergencies: `git push --no-verify`.
|
# sees it. Escape hatch for genuine emergencies: `git push --no-verify`.
|
||||||
@ -29,6 +34,7 @@ ROOT="$(git rev-parse --show-toplevel)"
|
|||||||
readonly ROOT
|
readonly ROOT
|
||||||
cd "$ROOT"
|
cd "$ROOT"
|
||||||
|
|
||||||
|
readonly APP_DIR="$ROOT/app"
|
||||||
readonly VENV_DIR="$ROOT/.ci-mirror-venv"
|
readonly VENV_DIR="$ROOT/.ci-mirror-venv"
|
||||||
readonly HASH_FILE="$VENV_DIR/.requirements.sha256"
|
readonly HASH_FILE="$VENV_DIR/.requirements.sha256"
|
||||||
readonly REQ_PATH="$ROOT/$REQUIREMENTS_FILE"
|
readonly REQ_PATH="$ROOT/$REQUIREMENTS_FILE"
|
||||||
@ -82,11 +88,30 @@ run_pytest_clean_venv() {
|
|||||||
"$VENV_DIR/bin/python" -m pytest "$@" || fail "pytest (clean requirements.txt venv)"
|
"$VENV_DIR/bin/python" -m pytest "$@" || fail "pytest (clean requirements.txt venv)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Gate the Flutter companion app the same way: analyze (pre-commit has no Dart
|
||||||
|
# hooks) then the full test suite. Deliberately fails rather than skips when
|
||||||
|
# flutter is missing -- a gate that quietly passes on a machine without the
|
||||||
|
# toolchain is the same as no gate.
|
||||||
|
run_flutter_gates() {
|
||||||
|
if [[ ! -f "$APP_DIR/pubspec.yaml" ]]; then
|
||||||
|
log "no app/pubspec.yaml — skipping the Flutter gates"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
if ! command -v flutter >/dev/null 2>&1; then
|
||||||
|
fail "flutter is not on PATH but app/ exists (cannot verify the app)"
|
||||||
|
fi
|
||||||
|
log "flutter analyze (app/)"
|
||||||
|
(cd "$APP_DIR" && flutter analyze) || fail "flutter analyze"
|
||||||
|
log "flutter test (app/)"
|
||||||
|
(cd "$APP_DIR" && flutter test) || fail "flutter test"
|
||||||
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
require_file
|
require_file
|
||||||
ensure_venv
|
ensure_venv
|
||||||
run_precommit_all_files
|
run_precommit_all_files
|
||||||
run_pytest_clean_venv "$@"
|
run_pytest_clean_venv "$@"
|
||||||
|
run_flutter_gates
|
||||||
log "all CI gates passed locally — safe to push"
|
log "all CI gates passed locally — safe to push"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user