Files
ImBenjiandClaude Opus 5.5 d3b232f0ee feat(garage_updates): an auto updater for garage apps
Asks Garage for the newest release of a project, downloads it with resume,
checks the sha256, installs it beside the old one and restarts into it.
Splash on garage_ui, a native stub and a first time installer for windows,
bundle swap on mac, the system installer on android. No-op on web.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KHotisA4hV2xmuZsHTksFX
2026-10-12 06:37:02 +01:00

181 lines
5.9 KiB
Dart

import "dart:convert";
import "dart:io";
import "package:flutter_test/flutter_test.dart";
import "package:garage_updates/core.dart";
import "support/fake_install.dart";
import "support/fake_server.dart";
// the engine end to end against the pretend windows install: a real check, a
// real download off the fake server, the fake stub doing the installing
void main() {
late FakeServer server;
late FakeWindowsInstall fake;
late UpdateEngine engine;
final exits = <int>[];
Map<String, dynamic> answer(String status, {int build = 13, int? minimum}) => {
"status": status,
"minimum_build": minimum,
"latest": server.release(build: build).toJson(),
};
setUp(() async {
exits.clear();
server = await FakeServer.start(noise(64 * 1024));
fake = FakeWindowsInstall.create();
final found = WindowsInstaller.locate(fake.exe.path, environment: const {});
engine = UpdateEngine(
project: "proj_1",
build: 12,
installer: found.installer,
client: UpdateClient(project: "proj_1", apiBase: server.base, httpClient: HttpClient()),
downloader: Downloader(httpClient: HttpClient(), retries: 0),
arch: "x64",
exitProcess: exits.add,
);
});
tearDown(() async {
engine.close();
await server.close();
fake.dispose();
});
test("check, download, install, restart", () async {
server.updateBody = answer("available");
final order = <String>[];
engine.onBeforeRestart = () async {
// the stub has allready been run by the time the app is asked to tidy up
order.add("before restart, stub runs so far: ${fake.runs.length}");
};
final info = await engine.check();
expect(info.status, UpdateStatus.available);
expect(server.log.single, "GET /v1/updates/proj_1");
final latest = info.latest!;
expect(await engine.isDownloaded(latest), isFalse);
await engine.download(latest);
expect(await engine.isDownloaded(latest), isTrue);
final zip = "${fake.root.path}/staging/update-13.zip";
expect(File(zip).existsSync(), isTrue);
await engine.install(latest);
expect(exits, isEmpty, reason: "installing must not restart by itself");
await engine.restart(args: ["--route=/x"]);
expect(exits, [0]);
expect(order, ["before restart, stub runs so far: 1"]);
for (var i = 0; i < 100 && fake.runs.length < 2; i++) {
await Future<void>.delayed(const Duration(milliseconds: 50));
}
expect(fake.runs, [
["--garage-apply", zip, "13"],
["--route=/x"],
]);
// the stub was handed the bytes the server sent
expect(File("${fake.root.path}/app-13/unpacked-from.zip").readAsBytesSync(), server.data);
});
test("an onBeforeRestart that throws doesnt stop the restart", () async {
engine.onBeforeRestart = () => throw StateError("the agent wouldnt stop");
await engine.restart();
expect(exits, [0]);
});
test("a build that failed to start here is never offered again", () async {
// 11 is in the fake installs blocked list
server.updateBody = answer("available", build: 11);
engine = UpdateEngine(
project: "proj_1",
build: 9,
installer: engine.installer,
client: engine.client,
downloader: engine.downloader,
arch: "x64",
exitProcess: exits.add,
);
final lines = await printsOf(() async {
final first = await engine.check();
expect(first.status, UpdateStatus.current);
expect(first.latest, isNull);
expect(first.hasUpdate, isFalse);
await engine.check();
});
// said once, not every six hours for ever
expect(lines.where((l) => l.contains("failed to start on this machine")).length, 1);
});
test("a build that failed for this user on a read only install isnt offered to them again", () async {
// the stub went back to 12 for them. root still says 13 and blocks nothing
Directory("${fake.root.path}/app-13").createSync();
fake.stateFile.writeAsStringSync('{ "exe": "garage_stash.exe", "current": 13, "previous": 12, "failed_starts": 0, "blocked": [] }');
for (var i = 0; i < 4; i++) {
fake.plainLaunch(readOnly: true);
}
server.updateBody = answer("available", build: 13);
engine = UpdateEngine(
project: "proj_1",
build: 12,
installer: fake.installer(readOnly: true),
client: engine.client,
downloader: engine.downloader,
arch: "x64",
exitProcess: exits.add,
);
final info = await engine.check();
expect(info.status, UpdateStatus.current);
expect(info.hasUpdate, isFalse);
// the one after it is
server.updateBody = answer("available", build: 14);
expect((await engine.check()).latest!.build, 14);
});
test("a blocked build that is also required stays required, with nothing to install", () async {
server.updateBody = answer("required", build: 11, minimum: 11);
engine = UpdateEngine(
project: "proj_1",
build: 9,
installer: engine.installer,
client: engine.client,
downloader: engine.downloader,
arch: "x64",
);
final info = await engine.check();
expect(info.status, UpdateStatus.required);
expect(info.latest, isNull);
});
test("an answer offering our own build, or an older one, is current", () async {
server.updateBody = answer("available", build: 12);
expect((await engine.check()).status, UpdateStatus.current);
server.updateBody = answer("available", build: 5);
expect((await engine.check()).hasUpdate, isFalse);
});
test("install before download says so", () async {
await expectLater(
engine.install(server.release()),
throwsA(isA<UpdateError>().having((e) => e.code, "code", "not_downloaded")),
);
expect(fake.runs, isEmpty);
});
test("markHealthy reaches the state file", () async {
await engine.markHealthy();
expect((jsonDecode(fake.stateFile.readAsStringSync()) as Map)["failed_starts"], 0);
});
}