Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay";
3import { describe, expect, it } from "vite-plus/test";
4import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-types";
5import { relayManagedEnvironmentIds, splitEnvironmentSections } from "./environmentSections";
6
7function connectedEnvironment(
8 input: Omit<Partial<ConnectedEnvironmentSummary>, "environmentId"> & {
9 readonly environmentId: string;
10 readonly isRelayManaged: boolean;
11 },
12): ConnectedEnvironmentSummary {
13 return {
14 environmentId: EnvironmentId.make(input.environmentId),
15 environmentLabel: input.environmentLabel ?? input.environmentId,
16 displayUrl: input.displayUrl ?? `https://${input.environmentId}.example.test/`,
17 isRelayManaged: input.isRelayManaged,
18 connectionState: input.connectionState ?? "connected",
19 connectionError: input.connectionError ?? null,
20 connectionErrorTraceId: input.connectionErrorTraceId ?? null,
21 };
22}
23
24function cloudEnvironment(environmentId: string): RelayClientEnvironmentRecord {
25 return {
26 environmentId: EnvironmentId.make(environmentId),
27 label: environmentId,
28 endpoint: {
29 httpBaseUrl: `https://${environmentId}.cloud.example.test/`,
30 wsBaseUrl: `wss://${environmentId}.cloud.example.test/ws`,
31 providerKind: "cloudflare_tunnel",
32 },
33 linkedAt: "2026-01-01T00:00:00.000Z",
34 };
35}
36
37describe("relayManagedEnvironmentIds", () => {
38 it("leaves out a backend that was saved directly", () => {
39 const ids = relayManagedEnvironmentIds([
40 connectedEnvironment({ environmentId: "environment-local", isRelayManaged: false }),
41 connectedEnvironment({ environmentId: "environment-cloud", isRelayManaged: true }),
42 ]);
43
44 expect([...ids]).toEqual([EnvironmentId.make("environment-cloud")]);
45 });
46});
47
48describe("mobile environment settings sections", () => {
49 it("keeps saved relay-managed connections under T3 Connect", () => {
50 const local = connectedEnvironment({
51 environmentId: "environment-local",
52 isRelayManaged: false,
53 });
54 const cloud = connectedEnvironment({
55 environmentId: "environment-cloud",
56 isRelayManaged: true,
57 });
58
59 const sections = splitEnvironmentSections({
60 connectedEnvironments: [cloud, local],
61 cloudEnvironments: [
62 cloudEnvironment("environment-cloud"),
63 cloudEnvironment("environment-new"),
64 ],
65 });
66
67 expect(sections.localEnvironments).toEqual([local]);
68 expect(sections.connectedCloudEnvironments).toEqual([cloud]);
69 expect(
70 sections.availableCloudEnvironments.map((environment) => environment.environmentId),
71 ).toEqual([EnvironmentId.make("environment-new")]);
72 });
73
74 it("keeps saved relay-managed connections visible when cloud listing is unavailable", () => {
75 const cloud = connectedEnvironment({
76 environmentId: "environment-cloud",
77 isRelayManaged: true,
78 connectionState: "reconnecting",
79 connectionError: "Environment did not respond before the connection timeout.",
80 });
81
82 const sections = splitEnvironmentSections({
83 connectedEnvironments: [cloud],
84 cloudEnvironments: null,
85 });
86
87 expect(sections.localEnvironments).toEqual([]);
88 expect(sections.connectedCloudEnvironments).toEqual([cloud]);
89 expect(sections.availableCloudEnvironments).toEqual([]);
90 });
91
92 it("keeps an available saved relay environment as a fallback when listing is unavailable", () => {
93 const cloud = connectedEnvironment({
94 environmentId: "environment-cloud",
95 isRelayManaged: true,
96 connectionState: "available",
97 });
98
99 const sections = splitEnvironmentSections({
100 connectedEnvironments: [cloud],
101 cloudEnvironments: null,
102 });
103
104 expect(sections.connectedCloudEnvironments).toEqual([cloud]);
105 expect(sections.availableCloudEnvironments).toEqual([]);
106 });
107
108 it("does not duplicate a saved relay environment in the available cloud listing", () => {
109 const cloud = connectedEnvironment({
110 environmentId: "environment-cloud",
111 isRelayManaged: true,
112 connectionState: "available",
113 });
114 const listedCloud = cloudEnvironment("environment-cloud");
115
116 const sections = splitEnvironmentSections({
117 connectedEnvironments: [cloud],
118 cloudEnvironments: [listedCloud],
119 });
120
121 expect(sections.connectedCloudEnvironments).toEqual([cloud]);
122 expect(sections.availableCloudEnvironments).toEqual([]);
123 });
124
125 it("still offers a cloud environment saved directly as a local backend", () => {
126 const local = connectedEnvironment({
127 environmentId: "environment-cloud",
128 isRelayManaged: false,
129 });
130
131 const sections = splitEnvironmentSections({
132 connectedEnvironments: [local],
133 cloudEnvironments: [cloudEnvironment("environment-cloud")],
134 });
135
136 expect(sections.localEnvironments).toEqual([local]);
137 expect(sections.connectedCloudEnvironments).toEqual([]);
138 expect(
139 sections.availableCloudEnvironments.map((environment) => environment.environmentId),
140 ).toEqual([EnvironmentId.make("environment-cloud")]);
141 });
142
143 it("keeps failed relay environments in the local connection row", () => {
144 const cloud = connectedEnvironment({
145 environmentId: "environment-cloud",
146 isRelayManaged: true,
147 connectionState: "error",
148 connectionError: "Connection failed.",
149 });
150
151 const sections = splitEnvironmentSections({
152 connectedEnvironments: [cloud],
153 cloudEnvironments: [cloudEnvironment("environment-cloud")],
154 });
155
156 expect(sections.connectedCloudEnvironments).toEqual([cloud]);
157 expect(sections.availableCloudEnvironments).toEqual([]);
158 });
159});