Open History to see the last commit
1import { describe, expect, it } from "vitest";
2import type { DesktopUpdateState } from "@t3tools/contracts";
3
4import {
5 getCanRetryAfterDownloadFailure,
6 getAutoUpdateDisabledReason,
7 nextStatusAfterDownloadFailure,
8 shouldBroadcastDownloadProgress,
9} from "./updateState";
10
11const baseState: DesktopUpdateState = {
12 enabled: true,
13 status: "idle",
14 currentVersion: "1.0.0",
15 hostArch: "x64",
16 appArch: "x64",
17 runningUnderArm64Translation: false,
18 availableVersion: null,
19 downloadedVersion: null,
20 downloadPercent: null,
21 checkedAt: null,
22 message: null,
23 errorContext: null,
24 canRetry: false,
25};
26
27describe("shouldBroadcastDownloadProgress", () => {
28 it("broadcasts the first downloading progress update", () => {
29 expect(
30 shouldBroadcastDownloadProgress(
31 { ...baseState, status: "downloading", downloadPercent: null },
32 1,
33 ),
34 ).toBe(true);
35 });
36
37 it("skips progress updates within the same 10% bucket", () => {
38 expect(
39 shouldBroadcastDownloadProgress(
40 { ...baseState, status: "downloading", downloadPercent: 11.2 },
41 18.7,
42 ),
43 ).toBe(false);
44 });
45
46 it("broadcasts progress updates when a new 10% bucket is reached", () => {
47 expect(
48 shouldBroadcastDownloadProgress(
49 { ...baseState, status: "downloading", downloadPercent: 19.9 },
50 20.1,
51 ),
52 ).toBe(true);
53 });
54
55 it("broadcasts progress updates when a retry resets the download percentage", () => {
56 expect(
57 shouldBroadcastDownloadProgress(
58 { ...baseState, status: "downloading", downloadPercent: 50.4 },
59 0.2,
60 ),
61 ).toBe(true);
62 });
63});
64
65describe("getAutoUpdateDisabledReason", () => {
66 it("reports development builds as disabled", () => {
67 expect(
68 getAutoUpdateDisabledReason({
69 isDevelopment: true,
70 isPackaged: false,
71 platform: "darwin",
72 appImage: undefined,
73 disabledByEnv: false,
74 }),
75 ).toContain("packaged production builds");
76 });
77
78 it("reports env-disabled auto updates", () => {
79 expect(
80 getAutoUpdateDisabledReason({
81 isDevelopment: false,
82 isPackaged: true,
83 platform: "darwin",
84 appImage: undefined,
85 disabledByEnv: true,
86 }),
87 ).toContain("T3CODE_DISABLE_AUTO_UPDATE");
88 });
89
90 it("reports linux non-AppImage builds as disabled", () => {
91 expect(
92 getAutoUpdateDisabledReason({
93 isDevelopment: false,
94 isPackaged: true,
95 platform: "linux",
96 appImage: undefined,
97 disabledByEnv: false,
98 }),
99 ).toContain("AppImage");
100 });
101});
102
103describe("nextStatusAfterDownloadFailure", () => {
104 it("returns available when an update version is still known", () => {
105 expect(
106 nextStatusAfterDownloadFailure({
107 ...baseState,
108 status: "downloading",
109 availableVersion: "1.1.0",
110 }),
111 ).toBe("available");
112 });
113
114 it("returns error when no update version can be retried", () => {
115 expect(
116 nextStatusAfterDownloadFailure({
117 ...baseState,
118 status: "downloading",
119 availableVersion: null,
120 }),
121 ).toBe("error");
122 });
123});
124
125describe("getCanRetryAfterDownloadFailure", () => {
126 it("returns true when an available version is still present", () => {
127 expect(
128 getCanRetryAfterDownloadFailure({
129 ...baseState,
130 status: "downloading",
131 availableVersion: "1.1.0",
132 }),
133 ).toBe(true);
134 });
135
136 it("returns false when no version is available to retry", () => {
137 expect(
138 getCanRetryAfterDownloadFailure({
139 ...baseState,
140 status: "downloading",
141 availableVersion: null,
142 }),
143 ).toBe(false);
144 });
145});