mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 12:47:39 +01:00
Eleventh batch of the 250-line sweep (over-cap count 12 -> 5). Every test file in the repo, Python and Dart, is now under cap; the five left are all in app/lib/. wrapper_server_test.dart 349 -> 145 (+ proxy, account siblings) app_settings_service_test.dart 321 -> 162 (+ sync sibling) average_service_test.dart 299 -> 158 (+ periods sibling) settings_screen_test.dart 274 -> 189 (+ battery sibling) day_status_service_test.dart 270 -> 161 (+ streaks sibling) edit_entry_screen_test.dart 269 -> 159 (+ foodbank sibling) day_status_calendar_test.dart 256 -> 237 (+ _test_support sibling) day_status_calendar_test is a single group whose tests close over group-local state, so it could not be split by block. Its ~25 lines of file-level fixtures moved to `day_status_calendar_test_support.dart` instead, which required the same publicisation as the sync support file (`_theme` -> `appTheme`, `_wrap` -> `wrapInApp`, `_colorOfDay` -> `colorOfDay`) with doc comments. Block counts were compared against HEAD per family rather than trusting the suite total, since the duplication bug in the previous batch passed green at an inflated count. All seven families match exactly (29/26/34/11/34/8/14). Verified: flutter test 555 (unchanged), flutter analyze clean, pre-commit clean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
122 lines
4.0 KiB
Dart
122 lines
4.0 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:io';
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
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({
|
|
bool serveSyncAccount = false,
|
|
String? syncConfigDir,
|
|
}) 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,
|
|
serveSyncAccount: serveSyncAccount,
|
|
syncConfigDir: syncConfigDir,
|
|
);
|
|
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('sync-account provisioning', () {
|
|
/// Writes [files] into a temp config dir and restarts with the route on.
|
|
Future<String> enableWith(Map<String, String> files) async {
|
|
final configDir = Directory(p.join(tempDir.path, 'crdt-sync'))
|
|
..createSync(recursive: true);
|
|
files.forEach((name, contents) {
|
|
File(p.join(configDir.path, name)).writeAsStringSync(contents);
|
|
});
|
|
await server.stop();
|
|
await startServer(serveSyncAccount: true, syncConfigDir: configDir.path);
|
|
return 'http://localhost:${server.port}';
|
|
}
|
|
|
|
test('is 404 when not enabled', () async {
|
|
// The default, and the whole security posture: a credential route must
|
|
// not be reachable just because the wrapper is running.
|
|
final response = await http.get(url(kSyncAccountPath));
|
|
|
|
expect(response.statusCode, HttpStatus.notFound);
|
|
});
|
|
|
|
test('serves the account when enabled', () async {
|
|
final origin = await enableWith({
|
|
'firebase.json': '{"email":"a@b.c"}',
|
|
'password': 'pw\n',
|
|
});
|
|
|
|
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
|
|
final account = FirebaseAccount.tryParse(response.body);
|
|
|
|
expect(response.statusCode, HttpStatus.ok);
|
|
expect(account?.email, 'a@b.c');
|
|
expect(account?.password, 'pw');
|
|
});
|
|
|
|
test('is 404 when the config files are absent', () async {
|
|
final origin = await enableWith({});
|
|
|
|
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
|
|
|
|
expect(response.statusCode, HttpStatus.notFound);
|
|
});
|
|
|
|
test('is 404 when firebase.json has no usable email', () async {
|
|
final origin = await enableWith({
|
|
'firebase.json': '{"apiKey":"x"}',
|
|
'password': 'pw',
|
|
});
|
|
|
|
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
|
|
|
|
expect(response.statusCode, HttpStatus.notFound);
|
|
});
|
|
});
|
|
}
|