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