Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

targetEnvironment.logic.test.ts

211 lines5.9 KB

1import {
2 BearerConnectionTarget,
3 PrimaryConnectionTarget,
4 RelayConnectionTarget,
5 SshConnectionTarget,
6} from "@t3tools/client-runtime/connection";
7import { EnvironmentId } from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9​
10import {
11 isOnboardingRelayEnvironment,
12 resolveOnboardingTargetEnvironment,
13} from "./targetEnvironment.logic";
14​
15const primaryEnvironment = {
16 environmentId: EnvironmentId.make("primary"),
17 connection: { phase: "connected" },
18 entry: {
19 target: new PrimaryConnectionTarget({
20 environmentId: EnvironmentId.make("primary"),
21 label: "This computer",
22 httpBaseUrl: "http://127.0.0.1:3773",
23 wsBaseUrl: "ws://127.0.0.1:3773",
24 }),
25 },
26 label: "This computer",
27} as const;
28​
29const olderRelay = {
30 environmentId: EnvironmentId.make("older-remote"),
31 connection: { phase: "connected" },
32 entry: {
33 target: new RelayConnectionTarget({
34 environmentId: EnvironmentId.make("older-remote"),
35 label: "Older computer",
36 }),
37 },
38 label: "Older computer",
39} as const;
40​
41const newerRelay = {
42 environmentId: EnvironmentId.make("newer-relay"),
43 connection: { phase: "connected" },
44 entry: {
45 target: new RelayConnectionTarget({
46 environmentId: EnvironmentId.make("newer-relay"),
47 label: "New computer",
48 }),
49 },
50 label: "New computer",
51} as const;
52​
53const pairedRemote = {
54 environmentId: EnvironmentId.make("paired-remote"),
55 connection: { phase: "connected" },
56 entry: {
57 target: new BearerConnectionTarget({
58 environmentId: EnvironmentId.make("paired-remote"),
59 label: "Direct computer",
60 connectionId: "paired-remote",
61 }),
62 },
63 label: "Direct computer",
64} as const;
65​
66const sshEnvironment = {
67 environmentId: EnvironmentId.make("ssh-remote"),
68 connection: { phase: "connected" },
69 entry: {
70 target: new SshConnectionTarget({
71 environmentId: EnvironmentId.make("ssh-remote"),
72 label: "SSH computer",
73 connectionId: "ssh-remote",
74 }),
75 },
76 label: "SSH computer",
77} as const;
78​
79const desktopLocalEnvironment = {
80 environmentId: EnvironmentId.make("desktop-local-wsl"),
81 connection: { phase: "connected" },
82 entry: {
83 target: new BearerConnectionTarget({
84 environmentId: EnvironmentId.make("desktop-local-wsl"),
85 label: "WSL",
86 connectionId: "local:wsl:Ubuntu",
87 }),
88 },
89 label: "WSL",
90} as const;
91​
92describe("resolveOnboardingTargetEnvironment", () => {
93 it("waits for the exact paired machine instead of using an older connected machine", () => {
94 const pendingPairedRemote = { ...pairedRemote, connection: { phase: "connecting" } };
95​
96 expect(
97 resolveOnboardingTargetEnvironment({
98 mode: "direct",
99 environments: [primaryEnvironment, olderRelay, pendingPairedRemote],
100 primaryEnvironment,
101 pairedEnvironmentId: pairedRemote.environmentId,
102 }),
103 ).toBeNull();
104 });
105​
106 it("uses the exact paired machine once it connects", () => {
107 expect(
108 resolveOnboardingTargetEnvironment({
109 mode: "direct",
110 environments: [primaryEnvironment, olderRelay, pairedRemote],
111 primaryEnvironment,
112 pairedEnvironmentId: pairedRemote.environmentId,
113 }),
114 ).toBe(pairedRemote);
115 });
116​
117 it("waits for a newly paired machine that has not appeared in the catalog", () => {
118 expect(
119 resolveOnboardingTargetEnvironment({
120 mode: "direct",
121 environments: [primaryEnvironment, olderRelay],
122 primaryEnvironment,
123 pairedEnvironmentId: pairedRemote.environmentId,
124 }),
125 ).toBeNull();
126 });
127​
128 it("uses the primary machine for local onboarding", () => {
129 expect(
130 resolveOnboardingTargetEnvironment({
131 mode: "local",
132 environments: [primaryEnvironment, olderRelay],
133 primaryEnvironment,
134 pairedEnvironmentId: null,
135 }),
136 ).toBe(primaryEnvironment);
137 });
138​
139 it("does not substitute a remote machine when the local primary is offline", () => {
140 const offlinePrimary = { ...primaryEnvironment, connection: { phase: "disconnected" } };
141​
142 expect(
143 resolveOnboardingTargetEnvironment({
144 mode: "local",
145 environments: [offlinePrimary, olderRelay],
146 primaryEnvironment: offlinePrimary,
147 pairedEnvironmentId: null,
148 }),
149 ).toBeNull();
150 });
151​
152 it("uses the newest connected remote when no exact machine was selected", () => {
153 expect(
154 resolveOnboardingTargetEnvironment({
155 mode: "connect",
156 environments: [primaryEnvironment, olderRelay, newerRelay],
157 primaryEnvironment,
158 pairedEnvironmentId: null,
159 }),
160 ).toBe(newerRelay);
161 });
162​
163 it("ignores direct, SSH, and desktop-managed connections in Connect mode", () => {
164 expect(
165 resolveOnboardingTargetEnvironment({
166 mode: "connect",
167 environments: [
168 primaryEnvironment,
169 olderRelay,
170 pairedRemote,
171 sshEnvironment,
172 desktopLocalEnvironment,
173 ],
174 primaryEnvironment,
175 pairedEnvironmentId: null,
176 }),
177 ).toBe(olderRelay);
178 });
179​
180 it("uses the primary computer when no relay connection exists", () => {
181 expect(
182 resolveOnboardingTargetEnvironment({
183 mode: "connect",
184 environments: [primaryEnvironment, pairedRemote, sshEnvironment, desktopLocalEnvironment],
185 primaryEnvironment,
186 pairedEnvironmentId: null,
187 }),
188 ).toBe(primaryEnvironment);
189 });
190​
191 it("falls back to the connected primary when no remote is available", () => {
192 expect(
193 resolveOnboardingTargetEnvironment({
194 mode: "connect",
195 environments: [primaryEnvironment],
196 primaryEnvironment,
197 pairedEnvironmentId: null,
198 }),
199 ).toBe(primaryEnvironment);
200 });
201});
202​
203describe("isOnboardingRelayEnvironment", () => {
204 it("includes only T3 Connect relay targets", () => {
205 expect(
206 [olderRelay, pairedRemote, sshEnvironment, desktopLocalEnvironment].filter(
207 isOnboardingRelayEnvironment,
208 ),
209 ).toEqual([olderRelay]);
210 });
211});