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

desktopWslState.test.ts

99 lines3.0 KB

1import type { DesktopWslState } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
4import { AtomRegistry } from "effect/unstable/reactivity";
5import { describe, expect, it, vi } from "vite-plus/test";
6​
7import { createDesktopWslStateAtom } from "./desktopWslState";
8​
9const wslState: DesktopWslState = {
10 available: true,
11 distro: null,
12 distros: [
13 {
14 isDefault: true,
15 name: "Ubuntu",
16 version: 2,
17 },
18 ],
19 enabled: true,
20 preflightError: null,
21 diagnostic: null,
22 wslOnly: false,
23};
24​
25describe("desktopWslState", () => {
26 it("retains the loaded snapshot when the settings screen remounts", async () => {
27 const getWslState = vi.fn(async () => wslState);
28 const atom = createDesktopWslStateAtom(() => ({ getWslState }));
29 const registry = AtomRegistry.make();
30​
31 const unmount = registry.mount(atom);
32 await vi.waitFor(() => {
33 expect(AsyncResult.value(registry.get(atom))).toEqual(
34 expect.objectContaining({ _tag: "Some", value: wslState }),
35 );
36 });
37 unmount();
38​
39 const remount = registry.mount(atom);
40 expect(AsyncResult.value(registry.get(atom))).toEqual(
41 expect.objectContaining({ _tag: "Some", value: wslState }),
42 );
43 expect(getWslState).toHaveBeenCalledTimes(1);
44​
45 remount();
46 registry.dispose();
47 });
48​
49 it("retains the desktop bridge failure as the load error cause", async () => {
50 const cause = new Error("wsl unavailable");
51 const atom = createDesktopWslStateAtom(() => ({
52 getWslState: async () => Promise.reject(cause),
53 }));
54 const registry = AtomRegistry.make();
55 registry.mount(atom);
56​
57 await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
58 const result = registry.get(atom);
59 if (!AsyncResult.isFailure(result)) throw new Error("Expected WSL state load to fail.");
60​
61 expect(Cause.squash(result.cause)).toEqual(
62 expect.objectContaining({
63 _tag: "DesktopWslStateLoadError",
64 cause,
65 }),
66 );
67 registry.dispose();
68 });
69​
70 it("replaces cached state with refreshed live desktop state", async () => {
71 const refreshedState: DesktopWslState = {
72 ...wslState,
73 preflightError: "WSL backend stopped unexpectedly.",
74 diagnostic: null,
75 };
76 let currentState = wslState;
77 const getWslState = vi.fn(async () => currentState);
78 const atom = createDesktopWslStateAtom(() => ({ getWslState }));
79 const registry = AtomRegistry.make();
80 registry.mount(atom);
81​
82 await vi.waitFor(() => {
83 expect(AsyncResult.value(registry.get(atom))).toEqual(
84 expect.objectContaining({ _tag: "Some", value: wslState }),
85 );
86 });
87​
88 currentState = refreshedState;
89 registry.refresh(atom);
90​
91 await vi.waitFor(() => {
92 expect(AsyncResult.value(registry.get(atom))).toEqual(
93 expect.objectContaining({ _tag: "Some", value: refreshedState }),
94 );
95 });
96 expect(getWslState).toHaveBeenCalledTimes(2);
97 registry.dispose();
98 });
99});