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