mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-28 22:39:04 +01:00
app: test the IndexedDB stores instead of shipping them unverified
The IndexedDB stores are the desktop device's primary copy of the food log and the *only* copy of its photos -- photos never sync -- and their wrapper-mirror fallback is what makes a wiped Chrome profile recoverable. All of that was invisible to the suite: the files import `idb_browser.dart`, which cannot be imported outside a web compile, so they were simply absent from the VM test run rather than covered. Splitting the store classes from the browser-factory `open…()` functions lets them run against `idb_shim`'s in-memory backend on the plain VM, which is what the flutter-desktop-to-chrome skill recommends the shim for. Twelve tests now pin the round-trip, the mirror write, the recover-from-mirror path, and the no-wrapper-running case (a plain browser tab, where the app must still work). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
c12faa3920
commit
2231850bfc
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user