import { describe, expect, it } from "vitest"; import { DesktopReleases, newer } from "../src/desktop/updates.js"; const API = "https://git.example/api/v1/repos/o/r"; function fakeGitea(releases: unknown[], files: Record) { const calls: { url: string; auth: string | null }[] = []; const impl: typeof fetch = async (input, init) => { const url = String(input); calls.push({ url, auth: new Headers(init?.headers).get("authorization") }); if (url.startsWith(`${API}/releases`)) return Response.json(releases); const name = url.split("/").pop()!; if (name in files) return typeof files[name] === "string" ? new Response(files[name] as string) : Response.json(files[name]); return new Response("missing", { status: 404 }); }; return { impl, calls }; } const asset = (id: number, name: string) => ({ id, name, browser_download_url: `https://git.example/o/r/releases/download/x/${name}` }); describe("desktop updates", () => { it("compares versions", () => { expect(newer("0.3.0", "0.2.9")).toBe(true); expect(newer("0.10.0", "0.9.0")).toBe(true); expect(newer("0.2.0", "0.2.0")).toBe(false); expect(newer("0.2.0", "0.3.0")).toBe(false); expect(newer("1.0.0", "1.0.0-beta.2")).toBe(true); }); it("answers with the newest desktop release for the platform, through the API, and nothing when up to date", async () => { const releases = [ { id: 7, tag_name: "desktop-v0.3.0", draft: false, prerelease: false, body: "Catálogo local", published_at: "2026-10-13T09:00:00Z", assets: [asset(1, "update-darwin-universal.json"), asset(2, "PhotoReport_universal.app.tar.gz"), asset(4, "PhotoReport_0.3.0_universal.dmg")] }, { id: 9, tag_name: "desktop-v0.4.0", draft: true, prerelease: false, body: "", published_at: null, assets: [] }, { id: 5, tag_name: "v1.2.0", draft: false, prerelease: false, body: "", published_at: null, assets: [] }, { id: 3, tag_name: "desktop-v0.2.0", draft: false, prerelease: false, body: "", published_at: null, assets: [] }, ]; const files = { "update-darwin-universal.json": { version: "0.3.0", file: "PhotoReport_universal.app.tar.gz", signature: "dW50cnVzdGVkIGNvbW1lbnQ6IHNpZw==" }, "PhotoReport_universal.app.tar.gz": "installer bytes" }; const { impl, calls } = fakeGitea(releases, files); const r = new DesktopReleases({ api: API, token: "read-token-123" }, impl); const update = await r.updateFor("darwin", "aarch64", "0.2.0"); expect(update).toMatchObject({ version: "0.3.0", notes: "Catálogo local", url: "/v1/desktop/download/7/PhotoReport_universal.app.tar.gz", installer: "/v1/desktop/download/7/PhotoReport_0.3.0_universal.dmg" }); expect(await r.updateFor("darwin", "x86_64", "0.3.0")).toBeNull(); expect(await r.updateFor("windows", "x86_64", "0.2.0")).toBeNull(); expect(calls.every((c) => c.auth === "token read-token-123")).toBe(true); expect(calls.filter((c) => c.url.includes("/releases?")).length).toBe(1); // cached const file = await r.file(7, "PhotoReport_universal.app.tar.gz"); const chunks: Buffer[] = []; for await (const c of file!.body) chunks.push(Buffer.from(c)); expect(Buffer.concat(chunks).toString()).toBe("installer bytes"); // Only installers of the current release; never the manifests or other releases. expect(await r.file(3, "PhotoReport_universal.app.tar.gz")).toBeNull(); expect(await r.file(7, "update-darwin-universal.json")).toBeNull(); }); it("keeps offering the last release built for a platform while a newer one is still building", async () => { const releases = [ // 0.3.5: Linux done, the Mac still building (or switched off). { id: 12, tag_name: "desktop-v0.3.5", draft: false, prerelease: false, body: "", published_at: null, assets: [asset(20, "update-linux-x86_64.json"), asset(21, "PhotoReport_0.3.5_amd64.AppImage")] }, { id: 11, tag_name: "desktop-v0.3.4", draft: false, prerelease: false, body: "Mac", published_at: null, assets: [asset(10, "update-darwin-universal.json"), asset(11, "PhotoReport.app.tar.gz")] }, ]; const files = { "update-linux-x86_64.json": { version: "0.3.5", file: "PhotoReport_0.3.5_amd64.AppImage", signature: "dW50cnVzdGVkIGNvbW1lbnQ6IHNpZw==" }, "update-darwin-universal.json": { version: "0.3.4", file: "PhotoReport.app.tar.gz", signature: "dW50cnVzdGVkIGNvbW1lbnQ6IHNpZw==" }, "PhotoReport.app.tar.gz": "mac bytes", }; const r = new DesktopReleases({ api: API, token: "t" }, fakeGitea(releases, files).impl); expect(await r.updateFor("linux", "x86_64", "0.3.2")).toMatchObject({ version: "0.3.5", url: "/v1/desktop/download/12/PhotoReport_0.3.5_amd64.AppImage" }); expect(await r.updateFor("darwin", "aarch64", "0.3.2")).toMatchObject({ version: "0.3.4", url: "/v1/desktop/download/11/PhotoReport.app.tar.gz" }); expect(await r.file(11, "PhotoReport.app.tar.gz")).not.toBeNull(); }); });