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
160 lines
5.1 KiB
Dart
160 lines
5.1 KiB
Dart
import "dart:convert";
|
|
import "dart:io";
|
|
|
|
import "package:flutter_test/flutter_test.dart";
|
|
import "package:garage_updates/core.dart";
|
|
|
|
void main() {
|
|
late Directory dir;
|
|
late StateFile state;
|
|
|
|
setUp(() {
|
|
dir = Directory.systemTemp.createTempSync("gu_state_");
|
|
state = StateFile(File("${dir.path}/garage_updates.json"));
|
|
});
|
|
|
|
tearDown(() => dir.deleteSync(recursive: true));
|
|
|
|
test("a missing file reads as null", () {
|
|
expect(state.read(), isNull);
|
|
});
|
|
|
|
test("reads what the stub writes", () {
|
|
state.file.writeAsStringSync(
|
|
'{ "exe": "garage_stash.exe", "current": 12, "previous": 9, "failed_starts": 2, "blocked": [11] }',
|
|
);
|
|
final s = state.read()!;
|
|
|
|
expect(s.exe, "garage_stash.exe");
|
|
expect(s.current, 12);
|
|
expect(s.previous, 9);
|
|
expect(s.failedStarts, 2);
|
|
expect(s.blocked, [11]);
|
|
});
|
|
|
|
test("a half written file reads as null rather than throwing", () {
|
|
state.file.writeAsStringSync('{ "exe": "garage_stash.exe", "curr');
|
|
expect(state.read(), isNull);
|
|
|
|
state.file.writeAsStringSync("");
|
|
expect(state.read(), isNull);
|
|
|
|
state.file.writeAsStringSync("[1, 2]");
|
|
expect(state.read(), isNull);
|
|
});
|
|
|
|
test("missing and mistyped fields get sane defaults", () {
|
|
state.file.writeAsStringSync('{ "current": "12", "blocked": [11, "x", 11, 10.0], "failed_starts": null }');
|
|
final s = state.read()!;
|
|
|
|
expect(s.exe, isNull);
|
|
expect(s.current, 12);
|
|
expect(s.previous, isNull);
|
|
expect(s.failedStarts, 0);
|
|
expect(s.blocked, [11, 10]);
|
|
});
|
|
|
|
test("writing keeps the keys we dont know about", () {
|
|
state.file.writeAsStringSync('{ "exe": "a.exe", "current": 12, "app_id": "net.imbenji.stash", "later": {"x": 1} }');
|
|
|
|
expect(state.update((s) => s.failedStarts = 0), isTrue);
|
|
|
|
final j = jsonDecode(state.file.readAsStringSync()) as Map;
|
|
expect(j["app_id"], "net.imbenji.stash");
|
|
expect(j["later"], {"x": 1});
|
|
expect(j["exe"], "a.exe");
|
|
expect(j["current"], 12);
|
|
expect(j["previous"], isNull);
|
|
expect(j["failed_starts"], 0);
|
|
expect(j["blocked"], isEmpty);
|
|
});
|
|
|
|
test("the write is a replace, no temp file is left", () {
|
|
state.write(UpdateState(exe: "a.exe", current: 1));
|
|
state.write(UpdateState(exe: "a.exe", current: 2, previous: 1, blocked: [3]));
|
|
|
|
expect(dir.listSync().map((e) => e.uri.pathSegments.last), ["garage_updates.json"]);
|
|
final s = state.read()!;
|
|
expect(s.current, 2);
|
|
expect(s.previous, 1);
|
|
expect(s.blocked, [3]);
|
|
});
|
|
|
|
test("update wont write over a file it couldnt read", () {
|
|
state.file.writeAsStringSync('{ "exe": "garage_stash.exe", "curr');
|
|
expect(state.update((s) => s.failedStarts = 0), isFalse);
|
|
expect(state.file.readAsStringSync(), '{ "exe": "garage_stash.exe", "curr');
|
|
});
|
|
|
|
test("update can start a file that isnt there", () {
|
|
state = StateFile(File("${dir.path}/deeper/still/state.json"));
|
|
expect(state.update((s) => s.current = 5, createIfMissing: true), isTrue);
|
|
expect(state.read()!.current, 5);
|
|
});
|
|
|
|
group("the users file", () {
|
|
late UserStateFile mine;
|
|
|
|
setUp(() => mine = UserStateFile(File("${dir.path}/local/garage_updates.json")));
|
|
|
|
test("reads what the stub writes", () {
|
|
mine.file.parent.createSync();
|
|
mine.file.writeAsStringSync('{\n "failed_starts": 2,\n "blocked": [13, 13],\n "for_current": 14,\n "run_instead": null\n}\n');
|
|
final u = mine.read()!;
|
|
|
|
expect(u.failedStarts, 2);
|
|
expect(u.blocked, [13]);
|
|
expect(u.forCurrent, 14);
|
|
expect(u.runInstead, isNull);
|
|
});
|
|
|
|
test("missing, empty and half written all read as null", () {
|
|
expect(mine.read(), isNull);
|
|
|
|
mine.file.parent.createSync();
|
|
mine.file.writeAsStringSync("");
|
|
expect(mine.read(), isNull);
|
|
|
|
mine.file.writeAsStringSync('{ "failed_starts": 2, "bl');
|
|
expect(mine.read(), isNull);
|
|
// and isnt written over
|
|
expect(mine.update((u) => u.failedStarts = 0), isFalse);
|
|
expect(mine.file.readAsStringSync(), '{ "failed_starts": 2, "bl');
|
|
});
|
|
|
|
test("update doesnt start a file unless its told to", () {
|
|
expect(mine.update((u) => u.failedStarts = 0), isFalse);
|
|
expect(mine.file.existsSync(), isFalse);
|
|
|
|
expect(mine.update((u) => u.failedStarts = 1, createIfMissing: true), isTrue);
|
|
expect(mine.file.parent.listSync().map((e) => e.uri.pathSegments.last), ["garage_updates.json"]);
|
|
expect(jsonDecode(mine.file.readAsStringSync()), {
|
|
"failed_starts": 1,
|
|
"blocked": [],
|
|
"for_current": null,
|
|
"run_instead": null,
|
|
});
|
|
});
|
|
|
|
test("the count is only good for the current it was made against", () {
|
|
final u = UserState(failedStarts: 2, blocked: [13], forCurrent: 13, runInstead: 12);
|
|
|
|
u.settleFor(13);
|
|
expect(u.failedStarts, 2);
|
|
expect(u.runInstead, 12);
|
|
|
|
u.settleFor(14);
|
|
expect(u.failedStarts, 0);
|
|
expect(u.runInstead, isNull);
|
|
expect(u.forCurrent, 14);
|
|
expect(u.blocked, [13]);
|
|
});
|
|
});
|
|
|
|
test("a write that cant happen says false", () {
|
|
// a folder where the file should be
|
|
Directory(state.file.path).createSync();
|
|
expect(state.write(UpdateState(current: 1)), isFalse);
|
|
});
|
|
}
|