import { describe, expect, it } from "vitest"; import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts"; import { getArm64IntelBuildWarningDescription, getDesktopUpdateActionError, getDesktopUpdateButtonTooltip, isDesktopUpdateButtonDisabled, resolveDesktopUpdateButtonAction, shouldHighlightDesktopUpdateError, shouldShowArm64IntelBuildWarning, shouldShowDesktopUpdateButton, shouldToastDesktopUpdateActionResult, } from "./desktopUpdate.logic"; const baseState: DesktopUpdateState = { enabled: true, status: "idle", currentVersion: "1.0.0", hostArch: "x64", appArch: "x64", runningUnderArm64Translation: false, availableVersion: null, downloadedVersion: null, downloadPercent: null, checkedAt: null, message: null, errorContext: null, canRetry: false, }; describe("desktop update button state", () => { it("shows a download action when an update is available", () => { const state: DesktopUpdateState = { ...baseState, status: "available", availableVersion: "1.1.0" }; expect(shouldShowDesktopUpdateButton(state)).toBe(true); expect(resolveDesktopUpdateButtonAction(state)).toBe("download"); }); it("keeps retry action available after a download error", () => { const state: DesktopUpdateState = { ...baseState, status: "error", availableVersion: "1.1.0", message: "network timeout", errorContext: "download", canRetry: true, }; expect(shouldShowDesktopUpdateButton(state)).toBe(true); expect(resolveDesktopUpdateButtonAction(state)).toBe("download"); expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to retry"); }); it("keeps install action available after an install error", () => { const state: DesktopUpdateState = { ...baseState, status: "error", downloadedVersion: "1.1.0", availableVersion: "1.1.0", message: "shutdown timeout", errorContext: "install", canRetry: true, }; expect(shouldShowDesktopUpdateButton(state)).toBe(true); expect(resolveDesktopUpdateButtonAction(state)).toBe("install"); expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to retry"); }); it("hides the button for non-actionable check errors", () => { const state: DesktopUpdateState = { ...baseState, status: "error", message: "network unavailable", errorContext: "check", canRetry: true, }; expect(shouldShowDesktopUpdateButton(state)).toBe(false); expect(resolveDesktopUpdateButtonAction(state)).toBe("none"); }); it("disables the button while downloading", () => { const state: DesktopUpdateState = { ...baseState, status: "downloading", availableVersion: "1.1.0", downloadPercent: 42.5, }; expect(shouldShowDesktopUpdateButton(state)).toBe(true); expect(isDesktopUpdateButtonDisabled(state)).toBe(true); expect(getDesktopUpdateButtonTooltip(state)).toContain("42%"); }); }); describe("getDesktopUpdateActionError", () => { it("returns user-visible message for accepted failed attempts", () => { const result: DesktopUpdateActionResult = { accepted: true, completed: false, state: { ...baseState, status: "available", availableVersion: "1.1.0", message: "checksum mismatch", errorContext: "download", canRetry: true, }, }; expect(getDesktopUpdateActionError(result)).toBe("checksum mismatch"); }); it("ignores messages for non-accepted attempts", () => { const result: DesktopUpdateActionResult = { accepted: false, completed: false, state: { ...baseState, status: "error", message: "background failure", errorContext: "check", canRetry: false, }, }; expect(getDesktopUpdateActionError(result)).toBeNull(); }); it("ignores messages for successful attempts", () => { const result: DesktopUpdateActionResult = { accepted: true, completed: true, state: { ...baseState, status: "downloaded", downloadedVersion: "1.1.0", availableVersion: "1.1.0", message: null, errorContext: null, canRetry: true, }, }; expect(getDesktopUpdateActionError(result)).toBeNull(); }); }); describe("desktop update UI helpers", () => { it("toasts only for accepted incomplete actions", () => { expect( shouldToastDesktopUpdateActionResult({ accepted: true, completed: false, state: baseState, }), ).toBe(true); expect( shouldToastDesktopUpdateActionResult({ accepted: true, completed: true, state: baseState, }), ).toBe(false); }); it("highlights only actionable updater errors", () => { expect( shouldHighlightDesktopUpdateError({ ...baseState, status: "error", errorContext: "download", canRetry: true, }), ).toBe(true); expect( shouldHighlightDesktopUpdateError({ ...baseState, status: "error", errorContext: "check", canRetry: true, }), ).toBe(false); }); it("shows an Apple Silicon warning for Intel builds under Rosetta", () => { const state: DesktopUpdateState = { ...baseState, hostArch: "arm64", appArch: "x64", runningUnderArm64Translation: true, }; expect(shouldShowArm64IntelBuildWarning(state)).toBe(true); expect(getArm64IntelBuildWarningDescription(state)).toContain("Apple Silicon"); expect(getArm64IntelBuildWarningDescription(state)).toContain("Intel build"); }); it("changes the warning copy when a native build update is ready to download", () => { const state: DesktopUpdateState = { ...baseState, hostArch: "arm64", appArch: "x64", runningUnderArm64Translation: true, status: "available", availableVersion: "1.1.0", }; expect(getArm64IntelBuildWarningDescription(state)).toContain("Download the available update"); }); });