todo-app/bin/todo_desktop.dart
Krzysztof kuhy Rudnicki c37a9ef118 Add the desktop wrapper, and stop the test suite eating BACKLOG.md
The desktop app is a browser now, so it cannot write files. This adds the other
half: a loopback HTTP server that serves the web build and owns the two files
the browser cannot touch -- `~/todo/BACKLOG.md`, which the user's tooling and
the todo MCP server read, and a disk copy of the note log so a wiped Chrome
profile is not a total-loss event for unsynced notes.

Notable details:

- Binds to 127.0.0.1 only. The endpoints overwrite files in the user's home
  with no authentication, so a routable bind would let anything on the network
  rewrite the backlog.
- Serves explicit content types. CanvasKit refuses a `.wasm` labelled anything
  else and the app then renders nothing at all.
- Mirroring to disk is debounced. The store persists on every keystroke, so
  mirroring eagerly meant an HTTP request plus a whole-log write per character;
  IndexedDB still gets every keystroke, only the durability copy waits.

Separately, this fixes a pre-existing destructive bug found while testing:
`settings_screen_test` did real file I/O and overwrote the user's canonical
`~/todo/BACKLOG.md` with a single fake note on *every* `flutter test` run. It
went unnoticed because the app rewrites the file whenever it launches. The
export destination is now injectable and the test redirects it to a temp dir;
verified that a full suite run leaves the real file byte-identical.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-20 13:33:25 +02:00

77 lines
2.6 KiB
Dart

// coverage:ignore-file
// Entry point for the desktop wrapper: resolves real paths, starts the server,
// and launches the browser. The serving logic it delegates to is covered by
// test/wrapper_server_test.dart.
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:todo/desktop/wrapper_server.dart';
/// Port must stay fixed: the browser keys `localStorage` (the GitHub token) and
/// IndexedDB (the notes) by origin, so a different port looks like a different
/// app with no token and no notes. Kept in step with lib/sync/desktop_wrapper.
const _port = 8730;
Future<void> main(List<String> args) async {
final home = Platform.environment['HOME'];
if (home == null) {
stderr.writeln('HOME is not set; cannot resolve the backlog path.');
exit(1);
}
// Assets sit next to this executable when installed; --web-root overrides for
// development runs straight out of the repo.
final webRoot = _argValue(args, '--web-root') ??
p.join(p.dirname(Platform.resolvedExecutable), 'web');
if (!Directory(webRoot).existsSync()) {
stderr.writeln('web assets not found at $webRoot');
exit(1);
}
final server = WrapperServer(
webRoot: webRoot,
backlogPath: p.join(home, 'todo', 'BACKLOG.md'),
logPath: p.join(home, '.local', 'share', 'todo-desktop', 'todo_notes.json'),
);
await server.start(_port);
stdout.writeln('todo desktop serving on http://localhost:$_port');
if (_argValue(args, '--no-browser') == null) {
await _launchBrowser(home);
// The browser owns the session: when its window closes, so does the app.
await server.stop();
}
}
/// Launches the app in a Chrome-family browser with a **stable** profile
/// directory, since the token and notes live in that profile.
Future<void> _launchBrowser(String home) async {
const candidates = [
'/opt/google/chrome/chrome',
'/usr/bin/chromium',
'/usr/bin/google-chrome-stable',
];
final browser = candidates.firstWhere(
(path) => File(path).existsSync(),
orElse: () => '',
);
if (browser.isEmpty) {
stderr.writeln('No Chrome-family browser found; open '
'http://localhost:$_port manually.');
return;
}
final profile = p.join(home, '.local', 'share', 'todo-desktop', 'profile');
final process = await Process.start(browser, [
'--app=http://localhost:$_port',
'--user-data-dir=$profile',
'--no-first-run',
]);
await process.exitCode;
}
String? _argValue(List<String> args, String name) {
final index = args.indexOf(name);
if (index == -1 || index + 1 >= args.length) return null;
return args[index + 1];
}