Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Schema from "effect/Schema";
4import * as Effect from "effect/Effect";
5import * as Option from "effect/Option";
6
7import * as TokenStore from "../authorization/tokenStore.ts";
8import {
9 BearerConnectionCredential,
10 BearerConnectionProfile,
11 BearerConnectionRegistration,
12 RelayConnectionRegistration,
13 SshConnectionProfile,
14 SshConnectionRegistration,
15} from "../connection/catalog.ts";
16import {
17 BearerConnectionTarget,
18 ConnectionTransientError,
19 PrimaryConnectionTarget,
20 RelayConnectionTarget,
21 SshConnectionTarget,
22} from "../connection/model.ts";
23import {
24 GitHubRoutingPermissions,
25 makeGitHubRoutingPermissions,
26} from "../connection/githubRoutingPermissions.ts";
27import {
28 ConnectionCatalogDocument,
29 EMPTY_CONNECTION_CATALOG_DOCUMENT,
30 putRemoteDpopTokenInCatalog,
31 registerConnectionInCatalog,
32 removeConnectionFromCatalog,
33 setConnectionEnabledInCatalog,
34} from "./storageDocument.ts";
35
36const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCatalogDocument);
37
38const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
39const decodeCatalogDocument = Schema.decodeUnknownSync(ConnectionCatalogDocument);
40
41const RELAY_TARGET = new RelayConnectionTarget({
42 environmentId: ENVIRONMENT_ID,
43 label: "Remote",
44});
45const BEARER_TARGET = new BearerConnectionTarget({
46 environmentId: ENVIRONMENT_ID,
47 label: "Remote",
48 connectionId: "bearer-1",
49});
50const BEARER_PROFILE = new BearerConnectionProfile({
51 connectionId: BEARER_TARGET.connectionId,
52 environmentId: ENVIRONMENT_ID,
53 label: BEARER_TARGET.label,
54 httpBaseUrl: "https://remote.example.test",
55 wsBaseUrl: "wss://remote.example.test",
56});
57const BEARER_CREDENTIAL = new BearerConnectionCredential({
58 token: "bearer-token",
59});
60const REMOTE_TOKEN = new TokenStore.RemoteDpopAccessToken({
61 environmentId: ENVIRONMENT_ID,
62 label: "Remote",
63 endpoint: {
64 httpBaseUrl: "https://remote.example.test",
65 wsBaseUrl: "wss://remote.example.test",
66 providerKind: "cloudflare_tunnel",
67 },
68 accessToken: "dpop-token",
69 expiresAtEpochMs: 1_000_000,
70 dpopThumbprint: "thumbprint",
71});
72
73describe("ConnectionCatalogDocument", () => {
74 it.effect("persists explicit GitHub trust and forgets it when a connection is removed", () =>
75 Effect.gen(function* () {
76 let document = EMPTY_CONNECTION_CATALOG_DOCUMENT;
77 const entry = { target: BEARER_TARGET, profile: Option.some(BEARER_PROFILE), enabled: true };
78 const storage = {
79 read: Effect.sync(() => document.githubRoutingPermissions ?? []),
80 write: (githubRoutingPermissions: NonNullable<typeof document.githubRoutingPermissions>) =>
81 Effect.gen(function* () {
82 document = yield* decodeConnectionCatalogDocument({
83 ...document,
84 githubRoutingPermissions,
85 }).pipe(Effect.orDie);
86 }),
87 };
88 const permissions = yield* makeGitHubRoutingPermissions(storage);
89 expect(yield* permissions.get(entry)).toBe("off");
90 yield* permissions.set(entry, "read");
91 const restarted = yield* makeGitHubRoutingPermissions(storage);
92 expect(yield* restarted.get(entry)).toBe("read");
93
94 const changedEndpoint = {
95 ...entry,
96 profile: Option.some(
97 new BearerConnectionProfile({
98 ...BEARER_PROFILE,
99 httpBaseUrl: "https://different.example.test",
100 }),
101 ),
102 };
103 expect(yield* restarted.get(changedEndpoint)).toBe("off");
104 expect(
105 yield* restarted.get({
106 ...entry,
107 profile: Option.some(
108 new BearerConnectionProfile({
109 ...BEARER_PROFILE,
110 wsBaseUrl: "wss://different.example.test",
111 }),
112 ),
113 }),
114 ).toBe("off");
115 expect(
116 yield* restarted.get({ target: RELAY_TARGET, profile: Option.none(), enabled: true }),
117 ).toBe("off");
118 yield* restarted.set(entry, "read-write");
119 expect(yield* restarted.get(entry)).toBe("read-write");
120 yield* restarted.forget(ENVIRONMENT_ID);
121 expect(yield* restarted.get(entry)).toBe("off");
122 const afterRemoval = yield* makeGitHubRoutingPermissions(storage);
123 expect(yield* afterRemoval.get(entry)).toBe("off");
124
125 yield* permissions.set(entry, "read");
126 document = removeConnectionFromCatalog(document, BEARER_TARGET);
127 const afterCatalogRemoval = yield* makeGitHubRoutingPermissions(storage);
128 expect(yield* afterCatalogRemoval.get(entry)).toBe("off");
129 }),
130 );
131
132 it.effect("requires explicit trust for primary, relay, and SSH connections independently", () =>
133 Effect.gen(function* () {
134 const permissions = yield* makeGitHubRoutingPermissions({
135 read: Effect.succeed([]),
136 write: () => Effect.void,
137 });
138 const entries = [
139 {
140 target: new PrimaryConnectionTarget({
141 environmentId: ENVIRONMENT_ID,
142 label: "Local",
143 httpBaseUrl: "http://localhost:3000",
144 wsBaseUrl: "ws://localhost:3000",
145 }),
146 profile: Option.none(),
147 enabled: true,
148 },
149 { target: RELAY_TARGET, profile: Option.none(), enabled: true },
150 {
151 enabled: true,
152 target: new SshConnectionTarget({
153 environmentId: ENVIRONMENT_ID,
154 label: "SSH",
155 connectionId: "ssh-1",
156 }),
157 profile: Option.some(
158 new SshConnectionProfile({
159 environmentId: ENVIRONMENT_ID,
160 label: "SSH",
161 connectionId: "ssh-1",
162 target: { alias: "work", hostname: "work.example.test", username: "maria", port: 22 },
163 }),
164 ),
165 },
166 ];
167 for (const entry of entries) {
168 expect(yield* permissions.get(entry)).toBe("off");
169 yield* permissions.set(entry, "read");
170 expect(yield* permissions.get(entry)).toBe("read");
171 yield* permissions.set(entry, "off");
172 expect(yield* permissions.get(entry)).toBe("off");
173 }
174 }),
175 );
176
177 it.effect("does not enable GitHub routing when permission persistence fails", () =>
178 Effect.gen(function* () {
179 const entry = { target: BEARER_TARGET, profile: Option.some(BEARER_PROFILE), enabled: true };
180 expect(yield* (yield* GitHubRoutingPermissions).get(entry)).toBe("off");
181 const permissions = yield* makeGitHubRoutingPermissions({
182 read: Effect.succeed([]),
183 write: () =>
184 Effect.fail(
185 new ConnectionTransientError({
186 reason: "remote-unavailable",
187 detail: "storage unavailable",
188 }),
189 ),
190 });
191 yield* permissions.set(entry, "read-write").pipe(Effect.flip);
192 expect(yield* permissions.get(entry)).toBe("off");
193 }),
194 );
195
196 it.each([
197 { name: "legacy", accountId: undefined },
198 { name: "account-bound", accountId: "account-1" },
199 ])("round-trips a catalog containing a $name DPoP token", ({ accountId }) => {
200 const token = new TokenStore.RemoteDpopAccessToken({
201 ...REMOTE_TOKEN,
202 ...(accountId === undefined ? {} : { accountId }),
203 });
204 const document = {
205 ...EMPTY_CONNECTION_CATALOG_DOCUMENT,
206 targets: [RELAY_TARGET],
207 remoteDpopTokens: [token],
208 };
209 const schema = Schema.fromJsonString(ConnectionCatalogDocument);
210 const restored = Schema.decodeSync(schema)(Schema.encodeSync(schema)(document));
211
212 expect(restored).toEqual(document);
213 expect(restored.remoteDpopTokens[0]?.accountId).toBe(accountId);
214 });
215
216 it("registers a bearer connection as one catalog mutation", () => {
217 const document = registerConnectionInCatalog(
218 EMPTY_CONNECTION_CATALOG_DOCUMENT,
219 new BearerConnectionRegistration({
220 target: BEARER_TARGET,
221 profile: BEARER_PROFILE,
222 credential: BEARER_CREDENTIAL,
223 }),
224 );
225
226 expect(document.targets).toEqual([BEARER_TARGET]);
227 expect(document.profiles).toEqual([BEARER_PROFILE]);
228 expect(document.credentials).toEqual([
229 {
230 connectionId: BEARER_TARGET.connectionId,
231 credential: BEARER_CREDENTIAL,
232 },
233 ]);
234 });
235
236 it("replaces obsolete connection metadata without discarding a reusable DPoP token", () => {
237 const bearer = registerConnectionInCatalog(
238 {
239 ...EMPTY_CONNECTION_CATALOG_DOCUMENT,
240 remoteDpopTokens: [REMOTE_TOKEN],
241 },
242 new BearerConnectionRegistration({
243 target: BEARER_TARGET,
244 profile: BEARER_PROFILE,
245 credential: BEARER_CREDENTIAL,
246 }),
247 );
248 const relayTarget = new RelayConnectionTarget({
249 environmentId: ENVIRONMENT_ID,
250 label: "Remote",
251 });
252 const relay = registerConnectionInCatalog(
253 bearer,
254 new RelayConnectionRegistration({ target: relayTarget }),
255 );
256
257 expect(relay.targets).toEqual([relayTarget]);
258 expect(relay.profiles).toEqual([]);
259 expect(relay.credentials).toEqual([]);
260 expect(relay.remoteDpopTokens).toEqual([REMOTE_TOKEN]);
261 });
262
263 it("removes every catalog record owned by an explicit disconnect", () => {
264 const registered = registerConnectionInCatalog(
265 {
266 ...EMPTY_CONNECTION_CATALOG_DOCUMENT,
267 remoteDpopTokens: [REMOTE_TOKEN],
268 },
269 new BearerConnectionRegistration({
270 target: BEARER_TARGET,
271 profile: BEARER_PROFILE,
272 credential: BEARER_CREDENTIAL,
273 }),
274 );
275
276 expect(removeConnectionFromCatalog(registered, BEARER_TARGET)).toEqual(
277 EMPTY_CONNECTION_CATALOG_DOCUMENT,
278 );
279 });
280
281 it("stores a DPoP token for a registered relay environment", () => {
282 const registered = registerConnectionInCatalog(
283 EMPTY_CONNECTION_CATALOG_DOCUMENT,
284 new RelayConnectionRegistration({ target: RELAY_TARGET }),
285 );
286
287 expect(putRemoteDpopTokenInCatalog(registered, REMOTE_TOKEN)).toEqual({
288 ...registered,
289 remoteDpopTokens: [REMOTE_TOKEN],
290 });
291 });
292
293 it("ignores a late DPoP token after the environment is removed", () => {
294 const registered = registerConnectionInCatalog(
295 EMPTY_CONNECTION_CATALOG_DOCUMENT,
296 new RelayConnectionRegistration({ target: RELAY_TARGET }),
297 );
298 const removed = removeConnectionFromCatalog(registered, RELAY_TARGET);
299
300 expect(putRemoteDpopTokenInCatalog(removed, REMOTE_TOKEN)).toEqual(
301 EMPTY_CONNECTION_CATALOG_DOCUMENT,
302 );
303 });
304
305 it("removes a DPoP token stored before the environment is removed", () => {
306 const registered = registerConnectionInCatalog(
307 EMPTY_CONNECTION_CATALOG_DOCUMENT,
308 new RelayConnectionRegistration({ target: RELAY_TARGET }),
309 );
310 const refreshed = putRemoteDpopTokenInCatalog(registered, REMOTE_TOKEN);
311
312 expect(removeConnectionFromCatalog(refreshed, RELAY_TARGET)).toEqual(
313 EMPTY_CONNECTION_CATALOG_DOCUMENT,
314 );
315 });
316
317 it("does not store DPoP tokens for a different environment or connection kind", () => {
318 const bearer = registerConnectionInCatalog(
319 EMPTY_CONNECTION_CATALOG_DOCUMENT,
320 new BearerConnectionRegistration({
321 target: BEARER_TARGET,
322 profile: BEARER_PROFILE,
323 credential: BEARER_CREDENTIAL,
324 }),
325 );
326 const otherRelay = registerConnectionInCatalog(
327 EMPTY_CONNECTION_CATALOG_DOCUMENT,
328 new RelayConnectionRegistration({
329 target: new RelayConnectionTarget({
330 environmentId: EnvironmentId.make("environment-2"),
331 label: "Other relay",
332 }),
333 }),
334 );
335
336 expect(putRemoteDpopTokenInCatalog(bearer, REMOTE_TOKEN)).toBe(bearer);
337 expect(putRemoteDpopTokenInCatalog(otherRelay, REMOTE_TOKEN)).toBe(otherRelay);
338 });
339
340 it("decodes a document written before the disabled list existed", () => {
341 const decoded = decodeCatalogDocument({
342 schemaVersion: 1,
343 targets: [],
344 profiles: [],
345 credentials: [],
346 remoteDpopTokens: [],
347 });
348
349 expect(decoded.disabledEnvironmentIds).toEqual([]);
350 });
351
352 it("switches a saved environment off and back on without touching its records", () => {
353 const registered = registerConnectionInCatalog(
354 EMPTY_CONNECTION_CATALOG_DOCUMENT,
355 new BearerConnectionRegistration({
356 target: BEARER_TARGET,
357 profile: BEARER_PROFILE,
358 credential: BEARER_CREDENTIAL,
359 }),
360 );
361
362 const disabled = setConnectionEnabledInCatalog(registered, ENVIRONMENT_ID, false);
363 expect(disabled.disabledEnvironmentIds).toEqual([ENVIRONMENT_ID]);
364 expect(disabled.targets).toEqual(registered.targets);
365 expect(disabled.credentials).toEqual(registered.credentials);
366 // Idempotent: switching off twice stores the id once.
367 expect(
368 setConnectionEnabledInCatalog(disabled, ENVIRONMENT_ID, false).disabledEnvironmentIds,
369 ).toEqual([ENVIRONMENT_ID]);
370
371 expect(
372 setConnectionEnabledInCatalog(disabled, ENVIRONMENT_ID, true).disabledEnvironmentIds,
373 ).toEqual([]);
374 // Re-registering (editing label or URL) keeps the flag.
375 expect(
376 registerConnectionInCatalog(
377 disabled,
378 new BearerConnectionRegistration({
379 target: BEARER_TARGET,
380 profile: BEARER_PROFILE,
381 credential: BEARER_CREDENTIAL,
382 }),
383 ).disabledEnvironmentIds,
384 ).toEqual([ENVIRONMENT_ID]);
385 expect(removeConnectionFromCatalog(disabled, BEARER_TARGET).disabledEnvironmentIds).toEqual([]);
386 });
387
388 it("persists the normalized SSH profile beside its target", () => {
389 const target = new SshConnectionTarget({
390 environmentId: ENVIRONMENT_ID,
391 label: "SSH",
392 connectionId: "ssh-1",
393 });
394 const profile = new SshConnectionProfile({
395 connectionId: target.connectionId,
396 environmentId: target.environmentId,
397 label: target.label,
398 target: {
399 alias: "devbox",
400 hostname: "devbox.example.test",
401 username: "developer",
402 port: 22,
403 },
404 });
405 const document = registerConnectionInCatalog(
406 EMPTY_CONNECTION_CATALOG_DOCUMENT,
407 new SshConnectionRegistration({ target, profile }),
408 );
409
410 expect(document.targets).toEqual([target]);
411 expect(document.profiles).toEqual([profile]);
412 expect(document.credentials).toEqual([]);
413 });
414});