Skip to content
Source
Sign inSign up

desktopLocal.test.ts

110 lines3.6 KB

1import { type DesktopEnvironmentBootstrap, PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { createDesktopSecondaryBootstrapsReader } from "./desktopLocal";
5​
6describe("desktop local topology reads", () => {
7 it("reuses snapshots when polling returns fresh objects for the same topology", () => {
8 const secondary: DesktopEnvironmentBootstrap = {
9 id: "wsl:default",
10 label: "WSL",
11 runningDistro: "Ubuntu",
12 httpBaseUrl: "http://127.0.0.1:4000",
13 wsBaseUrl: "ws://127.0.0.1:4000",
14 bootstrapToken: "bootstrap-1",
15 };
16 let entries = [secondary];
17 const reader = createDesktopSecondaryBootstrapsReader(() => ({
18 getLocalEnvironmentBootstraps: () => entries.map((entry) => ({ ...entry })),
19 }));
20​
21 const connected = reader.readSnapshot();
22 expect(reader.readSnapshot()).toBe(connected);
23 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: connected });
24 expect(reader.readSnapshot()).toBe(connected);
25​
26 entries = [];
27 const empty = reader.readSnapshot();
28 expect(empty).toEqual([]);
29 expect(reader.readSnapshot()).toBe(empty);
30 });
31​
32 it.each<Partial<DesktopEnvironmentBootstrap>>([
33 { id: "wsl:Debian" },
34 { label: "Renamed backend" },
35 { runningDistro: "Debian" },
36 { httpBaseUrl: "http://127.0.0.1:4001" },
37 { wsBaseUrl: "ws://127.0.0.1:4001" },
38 { bootstrapToken: "bootstrap-2" },
39 ])("publishes bootstrap changes: %j", (change) => {
40 let secondary: DesktopEnvironmentBootstrap = {
41 id: "wsl:default",
42 label: "WSL",
43 runningDistro: "Ubuntu",
44 httpBaseUrl: "http://127.0.0.1:4000",
45 wsBaseUrl: "ws://127.0.0.1:4000",
46 bootstrapToken: "bootstrap-1",
47 };
48 const reader = createDesktopSecondaryBootstrapsReader(() => ({
49 getLocalEnvironmentBootstraps: () => [{ ...secondary }],
50 }));
51 const before = reader.readSnapshot();
52 secondary = { ...secondary, ...change };
53 const after = reader.readSnapshot();
54 expect(after).not.toBe(before);
55 expect(after).toEqual([secondary]);
56 expect(reader.readSnapshot()).toBe(after);
57 });
58​
59 it("filters the primary bootstrap from successful topology reads", () => {
60 const secondary = {
61 id: "wsl:Ubuntu",
62 label: "WSL: Ubuntu",
63 httpBaseUrl: "http://127.0.0.1:4000",
64 wsBaseUrl: "ws://127.0.0.1:4000",
65 };
66​
67 const reader = createDesktopSecondaryBootstrapsReader(() => ({
68 getLocalEnvironmentBootstraps: () => [
69 {
70 ...secondary,
71 id: PRIMARY_LOCAL_ENVIRONMENT_ID,
72 label: "Windows",
73 },
74 secondary,
75 ],
76 }));
77​
78 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [secondary] });
79 });
80​
81 it("retains the last successful snapshot only until another read succeeds", () => {
82 const secondary = {
83 id: "wsl:Ubuntu",
84 label: "WSL: Ubuntu",
85 httpBaseUrl: "http://127.0.0.1:4000",
86 wsBaseUrl: "ws://127.0.0.1:4000",
87 };
88 let readBootstraps = () => [secondary];
89 const reader = createDesktopSecondaryBootstrapsReader(() => ({
90 getLocalEnvironmentBootstraps: () => readBootstraps(),
91 }));
92​
93 const connectedSnapshot = reader.readSnapshot();
94 expect(connectedSnapshot).toEqual([secondary]);
95​
96 readBootstraps = () => {
97 throw new Error("IPC unavailable");
98 };
99 expect(reader.readSnapshot()).toBe(connectedSnapshot);
100​
101 readBootstraps = () => [];
102 const removedSnapshot = reader.readSnapshot();
103 expect(removedSnapshot).toEqual([]);
104​
105 readBootstraps = () => {
106 throw new Error("IPC unavailable again");
107 };
108 expect(reader.readSnapshot()).toBe(removedSnapshot);
109 });
110});