import "dart:io"; import "package:flutter_test/flutter_test.dart"; import "package:garage_updates/core.dart"; import "support/fake_server.dart"; void main() { late FakeServer server; late UpdateClient client; setUp(() async { server = await FakeServer.start(const []); client = UpdateClient(project: "proj_1", apiBase: "${server.base}/", httpClient: HttpClient()); }); tearDown(() async { client.close(); await server.close(); }); Future check() => client.check(platform: "windows", arch: "x64", build: 12); Map latest({Object? build = 13}) => { "label": "v2610-1", "build": build, "notes": "Fixed the thing", "published_at": "2026-10-01T10:00:00Z", "asset": { "url": "https://cdn.example.com/app.zip", "filename": "app.zip", "format": "zip", "size": 1234, "sha256": "ABCDEF", }, }; test("asks the right url", () async { server.updateBody = {"status": "current", "minimum_build": null, "latest": null}; await check(); expect(server.log.single, "GET /v1/updates/proj_1"); final uri = client.checkUri(platform: "macos", arch: "arm64", build: 7); expect(uri.queryParameters, {"platform": "macos", "arch": "arm64", "build": "7"}); // the trailing slash on apiBase didnt double up expect(uri.path, "/v1/updates/proj_1"); }); test("current", () async { server.updateBody = {"status": "current", "minimum_build": 4, "latest": null}; final info = await check(); expect(info.status, UpdateStatus.current); expect(info.minimumBuild, 4); expect(info.latest, isNull); expect(info.hasUpdate, isFalse); }); test("available", () async { server.updateBody = {"status": "available", "minimum_build": null, "latest": latest()}; final info = await check(); expect(info.status, UpdateStatus.available); expect(info.hasUpdate, isTrue); expect(info.isRequired, isFalse); final l = info.latest!; expect(l.label, "v2610-1"); expect(l.build, 13); expect(l.notes, "Fixed the thing"); expect(l.publishedAt, DateTime.utc(2026, 10, 1, 10)); expect(l.asset.url, "https://cdn.example.com/app.zip"); expect(l.asset.filename, "app.zip"); expect(l.asset.format, "zip"); expect(l.asset.size, 1234); // lower cased, its compared against our own hex expect(l.asset.sha256, "abcdef"); }); test("required", () async { server.updateBody = {"status": "required", "minimum_build": 13, "latest": latest()}; final info = await check(); expect(info.status, UpdateStatus.required); expect(info.isRequired, isTrue); expect(info.minimumBuild, 13); }); test("unknown", () async { server.updateBody = {"status": "unknown", "minimum_build": null, "latest": null}; expect((await check()).status, UpdateStatus.unknown); }); test("a status we've never heard of is unknown", () async { server.updateBody = {"status": "sideways", "latest": latest()}; expect((await check()).status, UpdateStatus.unknown); }); test("numbers as strings, missing keys and the wrong types are put up with", () async { final l = latest(build: "13"); (l["asset"] as Map)["size"] = "1234"; (l["asset"] as Map).remove("format"); l["notes"] = 42; l.remove("published_at"); server.updateBody = {"status": "available", "minimum_build": "9", "latest": l}; final info = await check(); expect(info.minimumBuild, 9); expect(info.latest!.build, 13); expect(info.latest!.notes, ""); expect(info.latest!.publishedAt, isNull); expect(info.latest!.asset.size, 1234); // worked out from the filename expect(info.latest!.asset.format, "zip"); }); test("available with nothing usable to download is unknown", () async { final l = latest(); l.remove("asset"); server.updateBody = {"status": "available", "latest": l}; final info = await check(); expect(info.status, UpdateStatus.unknown); expect(info.hasUpdate, isFalse); }); test("a body that isnt json, or isnt an object, is unknown", () async { server.updateBody = "bad gateway"; expect((await check()).status, UpdateStatus.unknown); server.updateBody = [1, 2, 3]; expect((await check()).status, UpdateStatus.unknown); }); test("a relative asset url is resolved against the api", () async { final l = latest(); (l["asset"] as Map)["url"] = "/v1/updates/proj_1/download/a1"; server.updateBody = {"status": "available", "latest": l}; final info = await check(); expect(info.latest!.asset.url, "${server.base}/v1/updates/proj_1/download/a1"); }); test("an error status throws the store api's code", () async { server.updateStatus = 404; server.updateBody = {"error": "not_found", "message": "No such project"}; await expectLater( check(), throwsA(isA() .having((e) => e.code, "code", "not_found") .having((e) => e.message, "message", "No such project")), ); }); test("an error status with no json body still throws", () async { server.updateStatus = 502; server.updateBody = "bad gateway"; await expectLater(check(), throwsA(isA().having((e) => e.code, "code", "http_502"))); }); test("a server that isnt there throws network", () async { await server.close(); await expectLater(check(), throwsA(isA().having((e) => e.code, "code", "network"))); }); }