Open History to see the last commit
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} from "./desktopLocal";
14
15describe("desktop local connection identity", () => {
16 it("preserves the desktop backend instance id", () => {
17 const target = new BearerConnectionTarget({
18 connectionId: desktopLocalConnectionId("wsl:Ubuntu"),
19 environmentId: EnvironmentId.make("environment-wsl"),
20 label: "WSL (Ubuntu)",
21 });
22
23 expect(isDesktopLocalConnectionTarget(target)).toBe(true);
24 expect(desktopLocalBackendId(target)).toBe("wsl:Ubuntu");
25 });
26
27 it("does not classify the primary environment as desktop-local", () => {
28 const target = new PrimaryConnectionTarget({
29 environmentId: EnvironmentId.make("environment-primary"),
30 httpBaseUrl: "http://127.0.0.1:3773",
31 label: "This device",
32 wsBaseUrl: "ws://127.0.0.1:3773",
33 });
34
35 expect(isDesktopLocalConnectionTarget(target)).toBe(false);
36 expect(desktopLocalBackendId(target)).toBeNull();
37 });
38});
39
40describe("desktop local topology reads", () => {
41 it("distinguishes a successful empty topology from a read failure", () => {
42 let readBootstraps = () => [];
43 const reader = createDesktopSecondaryBootstrapsReader(() => ({
44 getLocalEnvironmentBootstraps: () => readBootstraps(),
45 }));
46
47 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [] });
48
49 const cause = new Error("IPC unavailable");
50 readBootstraps = () => {
51 throw cause;
52 };
53 expect(reader.readResult()).toEqual({ _tag: "Failure", cause });
54 });
55
56 it("filters the primary bootstrap from successful topology reads", () => {
57 const secondary = {
58 id: "wsl:Ubuntu",
59 label: "WSL: Ubuntu",
60 httpBaseUrl: "http://127.0.0.1:4000",
61 wsBaseUrl: "ws://127.0.0.1:4000",
62 };
63
64 const reader = createDesktopSecondaryBootstrapsReader(() => ({
65 getLocalEnvironmentBootstraps: () => [
66 {
67 ...secondary,
68 id: PRIMARY_LOCAL_ENVIRONMENT_ID,
69 label: "Windows",
70 },
71 secondary,
72 ],
73 }));
74
75 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [secondary] });
76 });
77
78 it("retains the last successful snapshot only until another read succeeds", () => {
79 const secondary = {
80 id: "wsl:Ubuntu",
81 label: "WSL: Ubuntu",
82 httpBaseUrl: "http://127.0.0.1:4000",
83 wsBaseUrl: "ws://127.0.0.1:4000",
84 };
85 let readBootstraps = () => [secondary];
86 const reader = createDesktopSecondaryBootstrapsReader(() => ({
87 getLocalEnvironmentBootstraps: () => readBootstraps(),
88 }));
89
90 const connectedSnapshot = reader.readSnapshot();
91 expect(connectedSnapshot).toEqual([secondary]);
92
93 readBootstraps = () => {
94 throw new Error("IPC unavailable");
95 };
96 expect(reader.readSnapshot()).toBe(connectedSnapshot);
97
98 readBootstraps = () => [];
99 const removedSnapshot = reader.readSnapshot();
100 expect(removedSnapshot).toEqual([]);
101
102 readBootstraps = () => {
103 throw new Error("IPC unavailable again");
104 };
105 expect(reader.readSnapshot()).toBe(removedSnapshot);
106 });
107});