Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

updateState.test.ts

142 lines3.5 KB

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