import { EnvironmentId } from "@t3tools/contracts"; import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay"; import { describe, expect, it } from "vite-plus/test"; import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-types"; import { splitEnvironmentSections } from "./environmentSections"; function connectedEnvironment( input: Omit, "environmentId"> & { readonly environmentId: string; readonly isRelayManaged: boolean; }, ): ConnectedEnvironmentSummary { return { environmentId: EnvironmentId.make(input.environmentId), environmentLabel: input.environmentLabel ?? input.environmentId, displayUrl: input.displayUrl ?? `https://${input.environmentId}.example.test/`, isRelayManaged: input.isRelayManaged, connectionState: input.connectionState ?? "connected", connectionError: input.connectionError ?? null, connectionErrorTraceId: input.connectionErrorTraceId ?? null, }; } function cloudEnvironment(environmentId: string): RelayClientEnvironmentRecord { return { environmentId: EnvironmentId.make(environmentId), label: environmentId, endpoint: { httpBaseUrl: `https://${environmentId}.cloud.example.test/`, wsBaseUrl: `wss://${environmentId}.cloud.example.test/ws`, providerKind: "cloudflare_tunnel", }, linkedAt: "2026-01-01T00:00:00.000Z", }; } describe("mobile environment settings sections", () => { it("keeps saved relay-managed connections under T3 Cloud", () => { const local = connectedEnvironment({ environmentId: "environment-local", isRelayManaged: false, }); const cloud = connectedEnvironment({ environmentId: "environment-cloud", isRelayManaged: true, }); const sections = splitEnvironmentSections({ connectedEnvironments: [cloud, local], cloudEnvironments: [ cloudEnvironment("environment-cloud"), cloudEnvironment("environment-new"), ], }); expect(sections.localEnvironments).toEqual([local]); expect(sections.connectedCloudEnvironments).toEqual([cloud]); expect( sections.availableCloudEnvironments.map((environment) => environment.environmentId), ).toEqual([EnvironmentId.make("environment-new")]); }); it("keeps saved relay-managed connections visible when cloud listing is unavailable", () => { const cloud = connectedEnvironment({ environmentId: "environment-cloud", isRelayManaged: true, connectionState: "reconnecting", connectionError: "Environment did not respond before the connection timeout.", }); const sections = splitEnvironmentSections({ connectedEnvironments: [cloud], cloudEnvironments: null, }); expect(sections.localEnvironments).toEqual([]); expect(sections.connectedCloudEnvironments).toEqual([cloud]); expect(sections.availableCloudEnvironments).toEqual([]); }); it("keeps an available saved relay environment as a fallback when listing is unavailable", () => { const cloud = connectedEnvironment({ environmentId: "environment-cloud", isRelayManaged: true, connectionState: "available", }); const sections = splitEnvironmentSections({ connectedEnvironments: [cloud], cloudEnvironments: null, }); expect(sections.connectedCloudEnvironments).toEqual([cloud]); expect(sections.availableCloudEnvironments).toEqual([]); }); it("does not duplicate a saved relay environment in the available cloud listing", () => { const cloud = connectedEnvironment({ environmentId: "environment-cloud", isRelayManaged: true, connectionState: "available", }); const listedCloud = cloudEnvironment("environment-cloud"); const sections = splitEnvironmentSections({ connectedEnvironments: [cloud], cloudEnvironments: [listedCloud], }); expect(sections.connectedCloudEnvironments).toEqual([cloud]); expect(sections.availableCloudEnvironments).toEqual([]); }); it("keeps failed relay environments in the local connection row", () => { const cloud = connectedEnvironment({ environmentId: "environment-cloud", isRelayManaged: true, connectionState: "error", connectionError: "Connection failed.", }); const sections = splitEnvironmentSections({ connectedEnvironments: [cloud], cloudEnvironments: [cloudEnvironment("environment-cloud")], }); expect(sections.connectedCloudEnvironments).toEqual([cloud]); expect(sections.availableCloudEnvironments).toEqual([]); }); });