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