mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 19:33:55 +01:00
Four asks, plus the fixes two review passes turned up. Removals (all unused): - "Repeat last meal" button, and the reward/temptation-bundling prompt it was the sole trigger for. Replaced with a post-log progress card showing kcal against budget, macros, and the adherence streak. - The multi-item meal builder, on both the gate and the phone. The stored `components` field stays: nothing writes it now, but historical composite entries and the food bank still read it and it is part of the wire format. - Meal photos entirely: 13 files, `imagePath`, the photo filter, the wrapper's /blobs/ route, image_picker, and the CAMERA permission. Reminder false alarms had three real causes, all fixed: - Logging never reconciled notifications, so a reminder sat on screen up to 15 minutes after the meal was logged -- on the phone itself. - The background check never pulled, so a PC-logged meal nagged forever until the app was opened. Now mirrors _cli_gate._should_lock: cheap local check, network only when a slot looks due, then re-check. - Meals before 08:00 / after 22:00 were attributed to no slot at all. New slot_for_log/slotForLog clamps them to the first/last slot, byte-identical across Python and Dart. Deliberately separate from elapsed_slots, which would otherwise make every slot fall due at 23:00. Budget history, so lowering the budget stops rewriting the past: - Forward-only (effective_from, kcal) entries; each day is judged against the budget in force then. Verified live: setting 2000 left a 36-day adherence streak and the whole calendar pixel-identical. - Rides as hist:<date> fields on the existing budget record. crdt_sync merges per-field over the union of field names and both devices push the merged log, so a device that predates the feature relays them untouched -- no coordinated rollout, no second document. - The seed guard tests emptiness, not file presence: _sync can write an empty history after merging with a pre-feature peer, and a presence check would then never seed again, silently reclassifying every past day. One shared source of truth for every piece of data: - Body weight now travels as its own `weight` CRDT field. Inside `value` it was collateral damage of whole-map LWW -- any phone budget edit deleted it, and the protein target with it. - Both halves of the food bank sync. The curated half (foods added without eating them) was the last device-local state. The derived half merges with count as the clock, so LWW means max-count-wins; the log stays authoritative for membership, because a CRDT union never shrinks and a peer's stale copy would otherwise resurrect a deleted food permanently. - Bank adapters honour tombstones, and Dart parses bank records defensively: a cast failure there is an Error, not an Exception, so one malformed remote record would have broken phone sync permanently. _test_guard.py blocks any pathlib write to real user state while pytest is in the process, whatever conftest is loaded. A throwaway test run outside the package conftest destroyed a real GitHub token during this work; this makes that class of accident impossible rather than merely discouraged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
289 lines
9.7 KiB
Dart
289 lines
9.7 KiB
Dart
// The desktop wrapper is the other half of the web-build desktop app: it
|
|
// serves the assets, owns the on-disk mirror of the app's storage, and fronts
|
|
// GitHub. These tests drive it over a real loopback socket, since its
|
|
// contract is HTTP.
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/desktop/github_proxy.dart';
|
|
import 'package:diet_guard_app/desktop/wrapper_server.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:path/path.dart' as p;
|
|
|
|
void main() {
|
|
late Directory tempDir;
|
|
late String webRoot;
|
|
late String dataDir;
|
|
late WrapperServer server;
|
|
late List<http.Request> outbound;
|
|
late Map<String, http.Response> canned;
|
|
|
|
Future<void> startServer() async {
|
|
outbound = [];
|
|
canned = {};
|
|
final proxy = GitHubProxy(
|
|
tokenPath: p.join(tempDir.path, 'config', 'sync_token'),
|
|
fallbackTokenPath: p.join(tempDir.path, 'config', 'fallback_token'),
|
|
httpClient: MockClient((request) async {
|
|
outbound.add(request);
|
|
return canned[request.url.toString()] ??
|
|
http.Response('{"ok":true}', 200);
|
|
}),
|
|
);
|
|
server = WrapperServer(
|
|
webRoot: webRoot,
|
|
dataDir: dataDir,
|
|
gitHubProxy: proxy,
|
|
);
|
|
await server.start(0);
|
|
}
|
|
|
|
Uri url(String path) => Uri.parse('http://localhost:${server.port}$path');
|
|
|
|
setUp(() async {
|
|
tempDir = await Directory.systemTemp.createTemp('diet_guard_wrapper_');
|
|
webRoot = p.join(tempDir.path, 'web');
|
|
dataDir = p.join(tempDir.path, 'data');
|
|
await Directory(webRoot).create(recursive: true);
|
|
File(p.join(webRoot, 'index.html')).writeAsStringSync('<html>app</html>');
|
|
File(p.join(webRoot, 'main.wasm')).writeAsBytesSync([0, 97, 115, 109]);
|
|
await startServer();
|
|
});
|
|
|
|
tearDown(() async {
|
|
await server.stop();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
group('static assets', () {
|
|
test('serves index.html at the root', () async {
|
|
final response = await http.get(url('/'));
|
|
|
|
expect(response.statusCode, 200);
|
|
expect(response.body, '<html>app</html>');
|
|
expect(response.headers['content-type'], contains('text/html'));
|
|
});
|
|
|
|
test('serves .wasm as application/wasm', () async {
|
|
// CanvasKit refuses any other content type and the app then renders
|
|
// nothing at all -- the failure looks like a blank window, not an error.
|
|
final response = await http.get(url('/main.wasm'));
|
|
|
|
expect(response.statusCode, 200);
|
|
expect(response.headers['content-type'], 'application/wasm');
|
|
});
|
|
|
|
test('404s a missing asset', () async {
|
|
expect((await http.get(url('/nope.js'))).statusCode, 404);
|
|
});
|
|
});
|
|
|
|
group('document mirror', () {
|
|
test('round-trips a document', () async {
|
|
final posted = await http.post(
|
|
url('/documents/food_log.json'),
|
|
body: '{"2026-07-20":[]}',
|
|
);
|
|
final fetched = await http.get(url('/documents/food_log.json'));
|
|
|
|
expect(posted.statusCode, 204);
|
|
expect(fetched.body, '{"2026-07-20":[]}');
|
|
expect(
|
|
File(p.join(dataDir, 'documents', 'food_log.json')).readAsStringSync(),
|
|
'{"2026-07-20":[]}',
|
|
);
|
|
});
|
|
|
|
test('404s an unmirrored document', () async {
|
|
expect(
|
|
(await http.get(url('/documents/food_bank.json'))).statusCode,
|
|
404,
|
|
);
|
|
});
|
|
|
|
test('rejects a traversing name instead of writing outside dataDir', () {
|
|
// The mirror writes into the user's home directory, so a `..` segment
|
|
// would turn a document write into an arbitrary-file overwrite.
|
|
expect(
|
|
http
|
|
.post(url('/documents/..%2F..%2Fevil'), body: 'x')
|
|
.then((r) => r.statusCode),
|
|
completion(400),
|
|
);
|
|
});
|
|
|
|
test('rejects a method other than GET/POST', () async {
|
|
expect((await http.delete(url('/documents/x.json'))).statusCode, 405);
|
|
});
|
|
});
|
|
|
|
group('github proxy', () {
|
|
test('reports no token before one is stored', () async {
|
|
final response = await http.get(url('/github/auth/status'));
|
|
|
|
expect(jsonDecode(response.body), {'configured': false});
|
|
});
|
|
|
|
test('stores a pasted token and then reports configured', () async {
|
|
await http.post(url('/github/auth/token'), body: 'ghp_pasted');
|
|
|
|
final response = await http.get(url('/github/auth/status'));
|
|
|
|
expect(jsonDecode(response.body), {'configured': true});
|
|
expect(
|
|
File(p.join(tempDir.path, 'config', 'sync_token')).readAsStringSync(),
|
|
'ghp_pasted',
|
|
);
|
|
});
|
|
|
|
test(
|
|
'falls back to the PC gate token when it has none of its own',
|
|
() async {
|
|
// The Python side already keeps a token for diet-guard-sync.timer;
|
|
// reusing it is what makes the desktop app need no second setup.
|
|
final fallback = File(p.join(tempDir.path, 'config', 'fallback_token'))
|
|
..createSync(recursive: true)
|
|
..writeAsStringSync('ghp_from_gate\n');
|
|
addTearDown(fallback.deleteSync);
|
|
|
|
final response = await http.get(url('/github/auth/status'));
|
|
|
|
expect(jsonDecode(response.body), {'configured': true});
|
|
},
|
|
);
|
|
|
|
test('attaches the token to a proxied API call', () async {
|
|
await http.post(url('/github/auth/token'), body: 'ghp_stored');
|
|
canned['https://api.github.com/repos/kuhyx/syncs'] = http.Response(
|
|
'{"name":"syncs"}',
|
|
200,
|
|
);
|
|
|
|
final response = await http.get(url('/github/api/repos/kuhyx/syncs'));
|
|
|
|
expect(response.body, '{"name":"syncs"}');
|
|
expect(outbound.single.headers['Authorization'], 'Bearer ghp_stored');
|
|
});
|
|
|
|
test('keeps a device-flow token server-side', () async {
|
|
canned['https://github.com/login/oauth/access_token'] = http.Response(
|
|
'{"access_token":"ghp_device"}',
|
|
200,
|
|
);
|
|
|
|
final response = await http.post(
|
|
url('/github/auth/device/poll'),
|
|
body: jsonEncode({'client_id': 'cid', 'device_code': 'dc'}),
|
|
);
|
|
|
|
// The point of the proxy: the browser is told "ok", never the token.
|
|
expect(jsonDecode(response.body), {'status': 'ok'});
|
|
expect(response.body, isNot(contains('ghp_device')));
|
|
expect(
|
|
File(p.join(tempDir.path, 'config', 'sync_token')).readAsStringSync(),
|
|
'ghp_device',
|
|
);
|
|
});
|
|
|
|
test('passes a still-pending device-flow poll straight back', () async {
|
|
canned['https://github.com/login/oauth/access_token'] = http.Response(
|
|
'{"error":"authorization_pending"}',
|
|
200,
|
|
);
|
|
|
|
final response = await http.post(
|
|
url('/github/auth/device/poll'),
|
|
body: jsonEncode({'client_id': 'cid', 'device_code': 'dc'}),
|
|
);
|
|
|
|
expect(jsonDecode(response.body), {'error': 'authorization_pending'});
|
|
});
|
|
|
|
test('forwards the device-code request with the client id', () async {
|
|
canned['https://github.com/login/device/code'] = http.Response(
|
|
'{"device_code":"dc","user_code":"ABCD-1234"}',
|
|
200,
|
|
);
|
|
|
|
final response = await http.post(
|
|
url('/github/auth/device/start'),
|
|
body: jsonEncode({'client_id': 'cid'}),
|
|
);
|
|
|
|
expect(jsonDecode(response.body), containsPair('user_code', 'ABCD-1234'));
|
|
expect(outbound.single.bodyFields['client_id'], 'cid');
|
|
});
|
|
|
|
test('404s an unknown /github path', () async {
|
|
expect((await http.get(url('/github/nope'))).statusCode, 404);
|
|
});
|
|
|
|
test('clears the stored token when handed an empty one', () async {
|
|
await http.post(url('/github/auth/token'), body: 'ghp_stored');
|
|
|
|
await http.post(url('/github/auth/token'), body: '');
|
|
|
|
expect(jsonDecode((await http.get(url('/github/auth/status'))).body), {
|
|
'configured': false,
|
|
});
|
|
});
|
|
|
|
test('ignores an unreadable token file rather than failing', () async {
|
|
// A directory where the token file should be: readToken must fall
|
|
// through to "no token" instead of throwing out of every request.
|
|
Directory(
|
|
p.join(tempDir.path, 'config', 'sync_token'),
|
|
).createSync(recursive: true);
|
|
|
|
expect(jsonDecode((await http.get(url('/github/auth/status'))).body), {
|
|
'configured': false,
|
|
});
|
|
});
|
|
|
|
test('passes a non-JSON device-flow response straight back', () async {
|
|
canned['https://github.com/login/oauth/access_token'] = http.Response(
|
|
'<html>rate limited</html>',
|
|
200,
|
|
);
|
|
|
|
final response = await http.post(
|
|
url('/github/auth/device/poll'),
|
|
body: jsonEncode({'client_id': 'cid', 'device_code': 'dc'}),
|
|
);
|
|
|
|
expect(response.body, contains('rate limited'));
|
|
});
|
|
|
|
test('treats a malformed request body as empty fields', () async {
|
|
final response = await http.post(
|
|
url('/github/auth/device/start'),
|
|
body: 'not json',
|
|
);
|
|
|
|
expect(response.statusCode, 200);
|
|
expect(outbound.single.bodyFields['client_id'], 'null');
|
|
});
|
|
});
|
|
|
|
group('content types', () {
|
|
test('labels each asset kind the browser is strict about', () {
|
|
ContentType typeOf(String name) =>
|
|
WrapperServer.contentTypeFor('/web/$name');
|
|
|
|
expect(typeOf('main.dart.js').toString(), contains('text/javascript'));
|
|
expect(typeOf('m.mjs').toString(), contains('text/javascript'));
|
|
expect(typeOf('manifest.json').mimeType, 'application/json');
|
|
expect(typeOf('style.css').toString(), contains('text/css'));
|
|
expect(typeOf('icon.png').mimeType, 'image/png');
|
|
expect(typeOf('photo.jpg').mimeType, 'image/jpeg');
|
|
expect(typeOf('logo.svg').mimeType, 'image/svg+xml');
|
|
expect(typeOf('f.ttf').mimeType, 'font/ttf');
|
|
expect(typeOf('f.otf').mimeType, 'font/otf');
|
|
expect(typeOf('f.woff2').mimeType, 'font/woff2');
|
|
expect(typeOf('data.bin').mimeType, 'application/octet-stream');
|
|
});
|
|
});
|
|
}
|