Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import {
4 createInitialDesktopUpdateState,
5 reduceDesktopUpdateStateOnCheckFailure,
6 reduceDesktopUpdateStateOnCheckStart,
7 reduceDesktopUpdateStateOnDownloadComplete,
8 reduceDesktopUpdateStateOnDownloadFailure,
9 reduceDesktopUpdateStateOnDownloadProgress,
10 reduceDesktopUpdateStateOnDownloadStart,
11 reduceDesktopUpdateStateOnInstallFailure,
12 reduceDesktopUpdateStateOnNoUpdate,
13 reduceDesktopUpdateStateOnUpdateAvailable,
14} from "./updateMachine";
15
16const runtimeInfo = {
17 hostArch: "x64",
18 appArch: "x64",
19 runningUnderArm64Translation: false,
20} as const;
21
22describe("updateMachine", () => {
23 it("clears transient errors when a check starts", () => {
24 const state = reduceDesktopUpdateStateOnCheckStart(
25 {
26 ...createInitialDesktopUpdateState("1.0.0", runtimeInfo),
27 enabled: true,
28 status: "error",
29 message: "network",
30 errorContext: "check",
31 canRetry: true,
32 },
33 "2026-03-04T00:00:00.000Z",
34 );
35
36 expect(state.status).toBe("checking");
37 expect(state.message).toBeNull();
38 expect(state.errorContext).toBeNull();
39 expect(state.canRetry).toBe(false);
40 });
41
42 it("records a check failure without exposing an action", () => {
43 const state = reduceDesktopUpdateStateOnCheckFailure(
44 {
45 ...createInitialDesktopUpdateState("1.0.0", runtimeInfo),
46 enabled: true,
47 status: "checking",
48 },
49 "network unavailable",
50 "2026-03-04T00:00:00.000Z",
51 );
52
53 expect(state.status).toBe("error");
54 expect(state.errorContext).toBe("check");
55 expect(state.canRetry).toBe(true);
56 });
57
58 it("preserves available version on download failure for retry", () => {
59 const state = reduceDesktopUpdateStateOnDownloadFailure(
60 {
61 ...createInitialDesktopUpdateState("1.0.0", runtimeInfo),
62 enabled: true,
63 status: "downloading",
64 availableVersion: "1.1.0",
65 downloadPercent: 43,
66 },
67 "checksum mismatch",
68 );
69
70 expect(state.status).toBe("available");
71 expect(state.availableVersion).toBe("1.1.0");
72 expect(state.errorContext).toBe("download");
73 expect(state.canRetry).toBe(true);
74 });
75
76 it("transitions to downloaded and then preserves install retry state", () => {
77 const downloaded = reduceDesktopUpdateStateOnDownloadComplete(
78 {
79 ...createInitialDesktopUpdateState("1.0.0", runtimeInfo),
80 enabled: true,
81 status: "downloading",
82 availableVersion: "1.1.0",
83 },
84 "1.1.0",
85 );
86 const failedInstall = reduceDesktopUpdateStateOnInstallFailure(downloaded, "backend shutdown timed out");
87
88 expect(downloaded.status).toBe("downloaded");
89 expect(downloaded.downloadedVersion).toBe("1.1.0");
90 expect(failedInstall.status).toBe("downloaded");
91 expect(failedInstall.errorContext).toBe("install");
92 expect(failedInstall.canRetry).toBe(true);
93 });
94
95 it("clears stale download state when no update is available", () => {
96 const state = reduceDesktopUpdateStateOnNoUpdate(
97 {
98 ...createInitialDesktopUpdateState("1.0.0", runtimeInfo),
99 enabled: true,
100 status: "error",
101 availableVersion: "1.1.0",
102 downloadedVersion: "1.1.0",
103 message: "old failure",
104 errorContext: "download",
105 canRetry: true,
106 },
107 "2026-03-04T00:00:00.000Z",
108 );
109
110 expect(state.status).toBe("up-to-date");
111 expect(state.availableVersion).toBeNull();
112 expect(state.downloadedVersion).toBeNull();
113 expect(state.message).toBeNull();
114 expect(state.errorContext).toBeNull();
115 });
116
117 it("tracks available, download start, and progress cleanly", () => {
118 const available = reduceDesktopUpdateStateOnUpdateAvailable(
119 {
120 ...createInitialDesktopUpdateState("1.0.0", runtimeInfo),
121 enabled: true,
122 status: "checking",
123 },
124 "1.1.0",
125 "2026-03-04T00:00:00.000Z",
126 );
127 const downloading = reduceDesktopUpdateStateOnDownloadStart(available);
128 const progress = reduceDesktopUpdateStateOnDownloadProgress(downloading, 55.5);
129
130 expect(available.status).toBe("available");
131 expect(downloading.status).toBe("downloading");
132 expect(downloading.downloadPercent).toBe(0);
133 expect(progress.downloadPercent).toBe(55.5);
134 expect(progress.errorContext).toBeNull();
135 });
136});