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

desktopUpdate.test.ts

134 lines4.7 KB

1import type { DesktopUpdateState } from "@v12/contracts";
2import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
3import { AtomRegistry } from "effect/unstable/reactivity";
4import { afterEach, describe, expect, it, vi } from "vite-plus/test";
5​
6import { createDesktopUpdateStateAtom, DesktopUpdateStateReadError } from "./desktopUpdate";
7​
8const baseState: DesktopUpdateState = {
9 enabled: true,
10 status: "idle",
11 channel: "latest",
12 currentVersion: "1.0.0",
13 hostArch: "x64",
14 appArch: "x64",
15 runningUnderArm64Translation: false,
16 availableVersion: null,
17 downloadedVersion: null,
18 downloadPercent: null,
19 checkedAt: null,
20 message: null,
21 errorContext: null,
22 canRetry: false,
23};
24​
25afterEach(() => {
26 vi.restoreAllMocks();
27});
28​
29describe("desktopUpdateStateAtom", () => {
30 it("loads once, retains state, and follows desktop update events", async () => {
31 let listener: ((state: DesktopUpdateState) => void) | undefined;
32 const unsubscribe = vi.fn();
33 const getUpdateState = vi.fn(async () => baseState);
34 const onUpdateState = vi.fn((nextListener: (state: DesktopUpdateState) => void) => {
35 listener = nextListener;
36 return unsubscribe;
37 });
38 const atom = createDesktopUpdateStateAtom(() => ({ getUpdateState, onUpdateState }));
39 const registry = AtomRegistry.make();
40​
41 const unmount = registry.mount(atom);
42 await vi.waitFor(() => {
43 expect(AsyncResult.getOrElse(registry.get(atom), () => null)).toEqual(baseState);
44 });
45​
46 const downloadedState: DesktopUpdateState = {
47 ...baseState,
48 status: "downloaded",
49 availableVersion: "1.1.0",
50 downloadedVersion: "1.1.0",
51 };
52 listener?.(downloadedState);
53​
54 await vi.waitFor(() => {
55 expect(AsyncResult.getOrElse(registry.get(atom), () => null)).toEqual(downloadedState);
56 });
57 unmount();
58​
59 const remount = registry.mount(atom);
60 expect(AsyncResult.getOrElse(registry.get(atom), () => null)).toEqual(downloadedState);
61 expect(getUpdateState).toHaveBeenCalledTimes(1);
62 expect(onUpdateState).toHaveBeenCalledTimes(1);
63​
64 remount();
65 registry.dispose();
66 expect(unsubscribe).toHaveBeenCalledTimes(1);
67 });
68​
69 it("does not let a slower initial read overwrite a newer update event", async () => {
70 let resolveInitial: ((state: DesktopUpdateState) => void) | undefined;
71 let listener: ((state: DesktopUpdateState) => void) | undefined;
72 const atom = createDesktopUpdateStateAtom(() => ({
73 getUpdateState: () =>
74 new Promise<DesktopUpdateState>((resolve) => {
75 resolveInitial = resolve;
76 }),
77 onUpdateState: (nextListener) => {
78 listener = nextListener;
79 return () => undefined;
80 },
81 }));
82 const registry = AtomRegistry.make();
83 registry.mount(atom);
84​
85 await vi.waitFor(() => expect(listener).toBeDefined());
86 const newerState: DesktopUpdateState = { ...baseState, status: "checking" };
87 listener?.(newerState);
88 resolveInitial?.(baseState);
89​
90 await vi.waitFor(() => {
91 expect(AsyncResult.getOrElse(registry.get(atom), () => null)).toEqual(newerState);
92 });
93 registry.dispose();
94 });
95​
96 it("keeps listening when the initial desktop state read fails", async () => {
97 let listener: ((state: DesktopUpdateState) => void) | undefined;
98 const cause = new Error("IPC unavailable");
99 const reportError = vi.spyOn(console, "log").mockImplementation(() => undefined);
100 const getUpdateState = vi.fn(async () => Promise.reject(cause));
101 const atom = createDesktopUpdateStateAtom(() => ({
102 getUpdateState,
103 onUpdateState: (nextListener) => {
104 listener = nextListener;
105 return () => undefined;
106 },
107 }));
108 const registry = AtomRegistry.make();
109 registry.mount(atom);
110​
111 await vi.waitFor(() => expect(listener).toBeDefined());
112 await vi.waitFor(() => expect(reportError).toHaveBeenCalledOnce());
113 expect(getUpdateState).toHaveBeenCalledTimes(3);
114 const [, errorMessage, errorContext] = reportError.mock.calls[0] ?? [];
115 expect(errorMessage).toBe("Failed to read the initial desktop update state after 3 attempts.");
116 expect(errorContext).toMatchObject({
117 errorTag: "DesktopUpdateStateReadError",
118 attemptCount: 3,
119 });
120 const loggedError = (errorContext as { readonly error: unknown }).error;
121 expect(loggedError).toBeInstanceOf(DesktopUpdateStateReadError);
122 expect(loggedError).toMatchObject({
123 _tag: "DesktopUpdateStateReadError",
124 attemptCount: 3,
125 });
126 expect((loggedError as DesktopUpdateStateReadError).cause).toBe(cause);
127​
128 listener?.(baseState);
129 await vi.waitFor(() => {
130 expect(AsyncResult.getOrElse(registry.get(atom), () => null)).toEqual(baseState);
131 });
132 registry.dispose();
133 });
134});