168 lines · 5.4 KB · TypeScript
RawDownload
import {  BearerConnectionTarget,  PrimaryConnectionTarget,} from "@t3tools/client-runtime/connection";import {  type DesktopEnvironmentBootstrap,  EnvironmentId,  PRIMARY_LOCAL_ENVIRONMENT_ID,} from "@t3tools/contracts";import { describe, expect, it } from "vite-plus/test"; import {  createDesktopSecondaryBootstrapsReader,  desktopLocalBackendId,  desktopLocalConnectionId,  isDesktopLocalConnectionTarget,} from "./desktopLocal"; describe("desktop local connection identity", () => {  it("preserves the desktop backend instance id", () => {    const target = new BearerConnectionTarget({      connectionId: desktopLocalConnectionId("wsl:Ubuntu"),      environmentId: EnvironmentId.make("environment-wsl"),      label: "WSL (Ubuntu)",    });     expect(isDesktopLocalConnectionTarget(target)).toBe(true);    expect(desktopLocalBackendId(target)).toBe("wsl:Ubuntu");  });   it("does not classify the primary environment as desktop-local", () => {    const target = new PrimaryConnectionTarget({      environmentId: EnvironmentId.make("environment-primary"),      httpBaseUrl: "http://127.0.0.1:3773",      label: "This device",      wsBaseUrl: "ws://127.0.0.1:3773",    });     expect(isDesktopLocalConnectionTarget(target)).toBe(false);    expect(desktopLocalBackendId(target)).toBeNull();  });}); describe("desktop local topology reads", () => {  it("reuses snapshots when polling returns fresh objects for the same topology", () => {    const secondary: DesktopEnvironmentBootstrap = {      id: "wsl:default",      label: "WSL",      runningDistro: "Ubuntu",      httpBaseUrl: "http://127.0.0.1:4000",      wsBaseUrl: "ws://127.0.0.1:4000",      bootstrapToken: "bootstrap-1",    };    let entries = [secondary];    const reader = createDesktopSecondaryBootstrapsReader(() => ({      getLocalEnvironmentBootstraps: () => entries.map((entry) => ({ ...entry })),    }));     const connected = reader.readSnapshot();    expect(reader.readSnapshot()).toBe(connected);    expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: connected });    expect(reader.readSnapshot()).toBe(connected);     entries = [];    const empty = reader.readSnapshot();    expect(empty).toEqual([]);    expect(reader.readSnapshot()).toBe(empty);  });   it.each<Partial<DesktopEnvironmentBootstrap>>([    { id: "wsl:Debian" },    { label: "Renamed backend" },    { runningDistro: "Debian" },    { httpBaseUrl: "http://127.0.0.1:4001" },    { wsBaseUrl: "ws://127.0.0.1:4001" },    { bootstrapToken: "bootstrap-2" },  ])("publishes bootstrap changes: %j", (change) => {    let secondary: DesktopEnvironmentBootstrap = {      id: "wsl:default",      label: "WSL",      runningDistro: "Ubuntu",      httpBaseUrl: "http://127.0.0.1:4000",      wsBaseUrl: "ws://127.0.0.1:4000",      bootstrapToken: "bootstrap-1",    };    const reader = createDesktopSecondaryBootstrapsReader(() => ({      getLocalEnvironmentBootstraps: () => [{ ...secondary }],    }));    const before = reader.readSnapshot();    secondary = { ...secondary, ...change };    const after = reader.readSnapshot();    expect(after).not.toBe(before);    expect(after).toEqual([secondary]);    expect(reader.readSnapshot()).toBe(after);  });   it("keeps the empty snapshot stable without a desktop bridge", () => {    const reader = createDesktopSecondaryBootstrapsReader(() => undefined);    expect(reader.readSnapshot()).toBe(reader.readSnapshot());  });   it("distinguishes a successful empty topology from a read failure", () => {    let readBootstraps = () => [];    const reader = createDesktopSecondaryBootstrapsReader(() => ({      getLocalEnvironmentBootstraps: () => readBootstraps(),    }));     expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [] });     const cause = new Error("IPC unavailable");    readBootstraps = () => {      throw cause;    };    expect(reader.readResult()).toEqual({ _tag: "Failure", cause });  });   it("filters the primary bootstrap from successful topology reads", () => {    const secondary = {      id: "wsl:Ubuntu",      label: "WSL: Ubuntu",      httpBaseUrl: "http://127.0.0.1:4000",      wsBaseUrl: "ws://127.0.0.1:4000",    };     const reader = createDesktopSecondaryBootstrapsReader(() => ({      getLocalEnvironmentBootstraps: () => [        {          ...secondary,          id: PRIMARY_LOCAL_ENVIRONMENT_ID,          label: "Windows",        },        secondary,      ],    }));     expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [secondary] });  });   it("retains the last successful snapshot only until another read succeeds", () => {    const secondary = {      id: "wsl:Ubuntu",      label: "WSL: Ubuntu",      httpBaseUrl: "http://127.0.0.1:4000",      wsBaseUrl: "ws://127.0.0.1:4000",    };    let readBootstraps = () => [secondary];    const reader = createDesktopSecondaryBootstrapsReader(() => ({      getLocalEnvironmentBootstraps: () => readBootstraps(),    }));     const connectedSnapshot = reader.readSnapshot();    expect(connectedSnapshot).toEqual([secondary]);     readBootstraps = () => {      throw new Error("IPC unavailable");    };    expect(reader.readSnapshot()).toBe(connectedSnapshot);     readBootstraps = () => [];    const removedSnapshot = reader.readSnapshot();    expect(removedSnapshot).toEqual([]);     readBootstraps = () => {      throw new Error("IPC unavailable again");    };    expect(reader.readSnapshot()).toBe(removedSnapshot);  });});