Open History to see the last commit
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 resolveOnboardingSetup,
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("does not substitute a remote machine when the local primary is offline", () => {
118 const offlinePrimary = { ...primaryEnvironment, connection: { phase: "disconnected" } };
119
120 expect(
121 resolveOnboardingTargetEnvironment({
122 mode: "local",
123 environments: [offlinePrimary, olderRelay],
124 primaryEnvironment: offlinePrimary,
125 pairedEnvironmentId: null,
126 }),
127 ).toBeNull();
128 });
129
130 it("uses the newest connected remote when no exact machine was selected", () => {
131 expect(
132 resolveOnboardingTargetEnvironment({
133 mode: "connect",
134 environments: [primaryEnvironment, olderRelay, newerRelay],
135 primaryEnvironment,
136 pairedEnvironmentId: null,
137 }),
138 ).toBe(newerRelay);
139 });
140
141 it("ignores direct, SSH, and desktop-managed connections in Connect mode", () => {
142 expect(
143 resolveOnboardingTargetEnvironment({
144 mode: "connect",
145 environments: [
146 primaryEnvironment,
147 olderRelay,
148 pairedRemote,
149 sshEnvironment,
150 desktopLocalEnvironment,
151 ],
152 primaryEnvironment,
153 pairedEnvironmentId: null,
154 }),
155 ).toBe(olderRelay);
156 });
157
158 it("uses the primary computer when no relay connection exists", () => {
159 expect(
160 resolveOnboardingTargetEnvironment({
161 mode: "connect",
162 environments: [primaryEnvironment, pairedRemote, sshEnvironment, desktopLocalEnvironment],
163 primaryEnvironment,
164 pairedEnvironmentId: null,
165 }),
166 ).toBe(primaryEnvironment);
167 });
168});
169
170describe("resolveOnboardingSetup", () => {
171 const computer = (id: string, phase: string) => ({
172 environmentId: EnvironmentId.make(id),
173 connection: { phase },
174 });
175 const local = computer("local", "connected");
176
177 it("skips selected computers that will not connect on their own", () => {
178 const environments = [
179 local,
180 computer("switched-off", "available"),
181 computer("offline", "offline"),
182 computer("failing", "reconnecting"),
183 computer("old-client", "unsupported"),
184 ];
185 expect(
186 resolveOnboardingSetup(
187 environments,
188 new Set(environments.map((environment) => environment.environmentId)),
189 ),
190 ).toEqual({
191 ready: true,
192 environmentIds: [local.environmentId],
193 skippedIds: environments.slice(1).map((environment) => environment.environmentId),
194 });
195 });
196
197 it("waits for a first connection attempt to settle", () => {
198 const connecting = computer("new", "connecting");
199 expect(
200 resolveOnboardingSetup(
201 [local, connecting],
202 new Set([local.environmentId, connecting.environmentId]),
203 ).ready,
204 ).toBe(false);
205 });
206
207 // regression: #16887 onboarding Continue locked up on computers that won't connect
208 it("is not ready without a connected selection", () => {
209 expect(resolveOnboardingSetup([local], new Set()).ready).toBe(false);
210 expect(
211 resolveOnboardingSetup([computer("off", "available")], new Set([EnvironmentId.make("off")]))
212 .ready,
213 ).toBe(false);
214 });
215});