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

desktopLocal.test.ts

168 lines5.4 KB

1import {
2 BearerConnectionTarget,
3 PrimaryConnectionTarget,
4} from "@t3tools/client-runtime/connection";
5import {
6 type DesktopEnvironmentBootstrap,
7 EnvironmentId,
8 PRIMARY_LOCAL_ENVIRONMENT_ID,
9} from "@t3tools/contracts";
10import { describe, expect, it } from "vite-plus/test";
11​
12import {
13 createDesktopSecondaryBootstrapsReader,
14 desktopLocalBackendId,
15 desktopLocalConnectionId,
16 isDesktopLocalConnectionTarget,
17} from "./desktopLocal";
18​
19describe("desktop local connection identity", () => {
20 it("preserves the desktop backend instance id", () => {
21 const target = new BearerConnectionTarget({
22 connectionId: desktopLocalConnectionId("wsl:Ubuntu"),
23 environmentId: EnvironmentId.make("environment-wsl"),
24 label: "WSL (Ubuntu)",
25 });
26​
27 expect(isDesktopLocalConnectionTarget(target)).toBe(true);
28 expect(desktopLocalBackendId(target)).toBe("wsl:Ubuntu");
29 });
30​
31 it("does not classify the primary environment as desktop-local", () => {
32 const target = new PrimaryConnectionTarget({
33 environmentId: EnvironmentId.make("environment-primary"),
34 httpBaseUrl: "http://127.0.0.1:3773",
35 label: "This device",
36 wsBaseUrl: "ws://127.0.0.1:3773",
37 });
38​
39 expect(isDesktopLocalConnectionTarget(target)).toBe(false);
40 expect(desktopLocalBackendId(target)).toBeNull();
41 });
42});
43​
44describe("desktop local topology reads", () => {
45 it("reuses snapshots when polling returns fresh objects for the same topology", () => {
46 const secondary: DesktopEnvironmentBootstrap = {
47 id: "wsl:default",
48 label: "WSL",
49 runningDistro: "Ubuntu",
50 httpBaseUrl: "http://127.0.0.1:4000",
51 wsBaseUrl: "ws://127.0.0.1:4000",
52 bootstrapToken: "bootstrap-1",
53 };
54 let entries = [secondary];
55 const reader = createDesktopSecondaryBootstrapsReader(() => ({
56 getLocalEnvironmentBootstraps: () => entries.map((entry) => ({ ...entry })),
57 }));
58​
59 const connected = reader.readSnapshot();
60 expect(reader.readSnapshot()).toBe(connected);
61 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: connected });
62 expect(reader.readSnapshot()).toBe(connected);
63​
64 entries = [];
65 const empty = reader.readSnapshot();
66 expect(empty).toEqual([]);
67 expect(reader.readSnapshot()).toBe(empty);
68 });
69​
70 it.each<Partial<DesktopEnvironmentBootstrap>>([
71 { id: "wsl:Debian" },
72 { label: "Renamed backend" },
73 { runningDistro: "Debian" },
74 { httpBaseUrl: "http://127.0.0.1:4001" },
75 { wsBaseUrl: "ws://127.0.0.1:4001" },
76 { bootstrapToken: "bootstrap-2" },
77 ])("publishes bootstrap changes: %j", (change) => {
78 let secondary: DesktopEnvironmentBootstrap = {
79 id: "wsl:default",
80 label: "WSL",
81 runningDistro: "Ubuntu",
82 httpBaseUrl: "http://127.0.0.1:4000",
83 wsBaseUrl: "ws://127.0.0.1:4000",
84 bootstrapToken: "bootstrap-1",
85 };
86 const reader = createDesktopSecondaryBootstrapsReader(() => ({
87 getLocalEnvironmentBootstraps: () => [{ ...secondary }],
88 }));
89 const before = reader.readSnapshot();
90 secondary = { ...secondary, ...change };
91 const after = reader.readSnapshot();
92 expect(after).not.toBe(before);
93 expect(after).toEqual([secondary]);
94 expect(reader.readSnapshot()).toBe(after);
95 });
96​
97 it("keeps the empty snapshot stable without a desktop bridge", () => {
98 const reader = createDesktopSecondaryBootstrapsReader(() => undefined);
99 expect(reader.readSnapshot()).toBe(reader.readSnapshot());
100 });
101​
102 it("distinguishes a successful empty topology from a read failure", () => {
103 let readBootstraps = () => [];
104 const reader = createDesktopSecondaryBootstrapsReader(() => ({
105 getLocalEnvironmentBootstraps: () => readBootstraps(),
106 }));
107​
108 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [] });
109​
110 const cause = new Error("IPC unavailable");
111 readBootstraps = () => {
112 throw cause;
113 };
114 expect(reader.readResult()).toEqual({ _tag: "Failure", cause });
115 });
116​
117 it("filters the primary bootstrap from successful topology reads", () => {
118 const secondary = {
119 id: "wsl:Ubuntu",
120 label: "WSL: Ubuntu",
121 httpBaseUrl: "http://127.0.0.1:4000",
122 wsBaseUrl: "ws://127.0.0.1:4000",
123 };
124​
125 const reader = createDesktopSecondaryBootstrapsReader(() => ({
126 getLocalEnvironmentBootstraps: () => [
127 {
128 ...secondary,
129 id: PRIMARY_LOCAL_ENVIRONMENT_ID,
130 label: "Windows",
131 },
132 secondary,
133 ],
134 }));
135​
136 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [secondary] });
137 });
138​
139 it("retains the last successful snapshot only until another read succeeds", () => {
140 const secondary = {
141 id: "wsl:Ubuntu",
142 label: "WSL: Ubuntu",
143 httpBaseUrl: "http://127.0.0.1:4000",
144 wsBaseUrl: "ws://127.0.0.1:4000",
145 };
146 let readBootstraps = () => [secondary];
147 const reader = createDesktopSecondaryBootstrapsReader(() => ({
148 getLocalEnvironmentBootstraps: () => readBootstraps(),
149 }));
150​
151 const connectedSnapshot = reader.readSnapshot();
152 expect(connectedSnapshot).toEqual([secondary]);
153​
154 readBootstraps = () => {
155 throw new Error("IPC unavailable");
156 };
157 expect(reader.readSnapshot()).toBe(connectedSnapshot);
158​
159 readBootstraps = () => [];
160 const removedSnapshot = reader.readSnapshot();
161 expect(removedSnapshot).toEqual([]);
162​
163 readBootstraps = () => {
164 throw new Error("IPC unavailable again");
165 };
166 expect(reader.readSnapshot()).toBe(removedSnapshot);
167 });
168});