Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

2.5 KB
Raw Blame
import { describe, expect, it, vi } from "vitest";
import {
	runUnblockedDownloadAndInstall,
	shouldQuitAndInstallAfterRestartPrompt,
	shouldStartBackgroundUpdateTimer,
} from "./background-update";

describe("background update policy", () => {
	it("starts no timer on a Store / non-owning channel", () => {
		expect(shouldStartBackgroundUpdateTimer({ isPackaged: true, ownsItsUpdates: false })).toBe(
			false,
		);
		expect(shouldStartBackgroundUpdateTimer({ isPackaged: false, ownsItsUpdates: true })).toBe(
			false,
		);
		expect(shouldStartBackgroundUpdateTimer({ isPackaged: true, ownsItsUpdates: true })).toBe(true);
	});

	it("does not call quitAndInstall until Restart Now returns 0", async () => {
		const install = vi.fn();
		const cancelled = await runUnblockedDownloadAndInstall({
			download: async () => ({ kind: "downloaded", version: "1.10.0" }),
			blocked: () => null,
			confirmRestart: async () => 1,
			install,
		});
		expect(cancelled).toEqual({ status: "cancelled" });
		expect(install).not.toHaveBeenCalled();
		expect(shouldQuitAndInstallAfterRestartPrompt(1)).toBe(false);

		const installed = await runUnblockedDownloadAndInstall({
			download: async () => ({ kind: "downloaded", version: "1.10.0" }),
			blocked: () => null,
			confirmRestart: async () => 0,
			install,
		});
		expect(installed).toEqual({ status: "installed" });
		expect(install).toHaveBeenCalledTimes(1);
		expect(shouldQuitAndInstallAfterRestartPrompt(0)).toBe(true);
	});

	it("hands the download error back so the dialog can show its message", async () => {
		const error = new Error("ECONNRESET mid-download");
		const failed = await runUnblockedDownloadAndInstall({
			download: async () => ({ kind: "failed", error }),
			blocked: () => null,
			confirmRestart: async () => 0,
			install: vi.fn(),
		});
		expect(failed).toEqual({ status: "failed", error });
	});

	it("never reaches the restart prompt without a downloaded update", async () => {
		// downloadSelfUpdate cannot return these today, but the type admits
		// them; a current/unsupported outcome must not prompt or install.
		for (const kind of ["current", "unsupported"] as const) {
			const confirmRestart = vi.fn(async () => 0);
			const install = vi.fn();
			const result = await runUnblockedDownloadAndInstall({
				download: async () => ({ kind }),
				blocked: () => null,
				confirmRestart,
				install,
			});
			expect(result).toEqual({ status: "unavailable" });
			expect(confirmRestart).not.toHaveBeenCalled();
			expect(install).not.toHaveBeenCalled();
		}
	});
});