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

desktopLocal.test.ts

120 lines3.8 KB

1import {
2 BearerConnectionTarget,
3 PrimaryConnectionTarget,
4} from "@t3tools/client-runtime/connection";
5import { EnvironmentId, PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";
6import { describe, expect, it } from "vite-plus/test";
7​
8import {
9 createDesktopSecondaryBootstrapsReader,
10 desktopLocalBackendId,
11 desktopLocalConnectionId,
12 isDesktopLocalConnectionTarget,
13 isWslConnectionTarget,
14} from "./desktopLocal";
15​
16describe("desktop local connection identity", () => {
17 it("preserves the desktop backend instance id", () => {
18 const target = new BearerConnectionTarget({
19 connectionId: desktopLocalConnectionId("wsl:Ubuntu"),
20 environmentId: EnvironmentId.make("environment-wsl"),
21 label: "WSL (Ubuntu)",
22 });
23​
24 expect(isDesktopLocalConnectionTarget(target)).toBe(true);
25 expect(desktopLocalBackendId(target)).toBe("wsl:Ubuntu");
26 expect(isWslConnectionTarget(target)).toBe(true);
27 });
28​
29 it("does not infer WSL for another desktop-local backend", () => {
30 const target = new BearerConnectionTarget({
31 connectionId: desktopLocalConnectionId("native-linux"),
32 environmentId: EnvironmentId.make("environment-native-linux"),
33 label: "Linux",
34 });
35​
36 expect(isDesktopLocalConnectionTarget(target)).toBe(true);
37 expect(isWslConnectionTarget(target)).toBe(false);
38 });
39​
40 it("does not classify the primary environment as desktop-local", () => {
41 const target = new PrimaryConnectionTarget({
42 environmentId: EnvironmentId.make("environment-primary"),
43 httpBaseUrl: "http://127.0.0.1:3773",
44 label: "This device",
45 wsBaseUrl: "ws://127.0.0.1:3773",
46 });
47​
48 expect(isDesktopLocalConnectionTarget(target)).toBe(false);
49 expect(desktopLocalBackendId(target)).toBeNull();
50 });
51});
52​
53describe("desktop local topology reads", () => {
54 it("distinguishes a successful empty topology from a read failure", () => {
55 let readBootstraps = () => [];
56 const reader = createDesktopSecondaryBootstrapsReader(() => ({
57 getLocalEnvironmentBootstraps: () => readBootstraps(),
58 }));
59​
60 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [] });
61​
62 const cause = new Error("IPC unavailable");
63 readBootstraps = () => {
64 throw cause;
65 };
66 expect(reader.readResult()).toEqual({ _tag: "Failure", cause });
67 });
68​
69 it("filters the primary bootstrap from successful topology reads", () => {
70 const secondary = {
71 id: "wsl:Ubuntu",
72 label: "WSL: Ubuntu",
73 httpBaseUrl: "http://127.0.0.1:4000",
74 wsBaseUrl: "ws://127.0.0.1:4000",
75 };
76​
77 const reader = createDesktopSecondaryBootstrapsReader(() => ({
78 getLocalEnvironmentBootstraps: () => [
79 {
80 ...secondary,
81 id: PRIMARY_LOCAL_ENVIRONMENT_ID,
82 label: "Windows",
83 },
84 secondary,
85 ],
86 }));
87​
88 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [secondary] });
89 });
90​
91 it("retains the last successful snapshot only until another read succeeds", () => {
92 const secondary = {
93 id: "wsl:Ubuntu",
94 label: "WSL: Ubuntu",
95 httpBaseUrl: "http://127.0.0.1:4000",
96 wsBaseUrl: "ws://127.0.0.1:4000",
97 };
98 let readBootstraps = () => [secondary];
99 const reader = createDesktopSecondaryBootstrapsReader(() => ({
100 getLocalEnvironmentBootstraps: () => readBootstraps(),
101 }));
102​
103 const connectedSnapshot = reader.readSnapshot();
104 expect(connectedSnapshot).toEqual([secondary]);
105​
106 readBootstraps = () => {
107 throw new Error("IPC unavailable");
108 };
109 expect(reader.readSnapshot()).toBe(connectedSnapshot);
110​
111 readBootstraps = () => [];
112 const removedSnapshot = reader.readSnapshot();
113 expect(removedSnapshot).toEqual([]);
114​
115 readBootstraps = () => {
116 throw new Error("IPC unavailable again");
117 };
118 expect(reader.readSnapshot()).toBe(removedSnapshot);
119 });
120});