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

storageDocument.test.ts

146 lines4.3 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3​
4import * as TokenStore from "../authorization/tokenStore.ts";
5import {
6 BearerConnectionCredential,
7 BearerConnectionProfile,
8 BearerConnectionRegistration,
9 RelayConnectionRegistration,
10 SshConnectionProfile,
11 SshConnectionRegistration,
12} from "../connection/catalog.ts";
13import {
14 BearerConnectionTarget,
15 RelayConnectionTarget,
16 SshConnectionTarget,
17} from "../connection/model.ts";
18import {
19 EMPTY_CONNECTION_CATALOG_DOCUMENT,
20 registerConnectionInCatalog,
21 removeConnectionFromCatalog,
22} from "./storageDocument.ts";
23​
24const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
25​
26const BEARER_TARGET = new BearerConnectionTarget({
27 environmentId: ENVIRONMENT_ID,
28 label: "Remote",
29 connectionId: "bearer-1",
30});
31const BEARER_PROFILE = new BearerConnectionProfile({
32 connectionId: BEARER_TARGET.connectionId,
33 environmentId: ENVIRONMENT_ID,
34 label: BEARER_TARGET.label,
35 httpBaseUrl: "https://remote.example.test",
36 wsBaseUrl: "wss://remote.example.test",
37});
38const BEARER_CREDENTIAL = new BearerConnectionCredential({
39 token: "bearer-token",
40});
41const REMOTE_TOKEN = new TokenStore.RemoteDpopAccessToken({
42 environmentId: ENVIRONMENT_ID,
43 label: "Remote",
44 endpoint: {
45 httpBaseUrl: "https://remote.example.test",
46 wsBaseUrl: "wss://remote.example.test",
47 providerKind: "cloudflare_tunnel",
48 },
49 accessToken: "dpop-token",
50 expiresAtEpochMs: 1_000_000,
51 dpopThumbprint: "thumbprint",
52});
53​
54describe("ConnectionCatalogDocument", () => {
55 it("registers a bearer connection as one catalog mutation", () => {
56 const document = registerConnectionInCatalog(
57 EMPTY_CONNECTION_CATALOG_DOCUMENT,
58 new BearerConnectionRegistration({
59 target: BEARER_TARGET,
60 profile: BEARER_PROFILE,
61 credential: BEARER_CREDENTIAL,
62 }),
63 );
64​
65 expect(document.targets).toEqual([BEARER_TARGET]);
66 expect(document.profiles).toEqual([BEARER_PROFILE]);
67 expect(document.credentials).toEqual([
68 {
69 connectionId: BEARER_TARGET.connectionId,
70 credential: BEARER_CREDENTIAL,
71 },
72 ]);
73 });
74​
75 it("replaces obsolete connection metadata without discarding a reusable DPoP token", () => {
76 const bearer = registerConnectionInCatalog(
77 {
78 ...EMPTY_CONNECTION_CATALOG_DOCUMENT,
79 remoteDpopTokens: [REMOTE_TOKEN],
80 },
81 new BearerConnectionRegistration({
82 target: BEARER_TARGET,
83 profile: BEARER_PROFILE,
84 credential: BEARER_CREDENTIAL,
85 }),
86 );
87 const relayTarget = new RelayConnectionTarget({
88 environmentId: ENVIRONMENT_ID,
89 label: "Remote",
90 });
91 const relay = registerConnectionInCatalog(
92 bearer,
93 new RelayConnectionRegistration({ target: relayTarget }),
94 );
95​
96 expect(relay.targets).toEqual([relayTarget]);
97 expect(relay.profiles).toEqual([]);
98 expect(relay.credentials).toEqual([]);
99 expect(relay.remoteDpopTokens).toEqual([REMOTE_TOKEN]);
100 });
101​
102 it("removes every catalog record owned by an explicit disconnect", () => {
103 const registered = registerConnectionInCatalog(
104 {
105 ...EMPTY_CONNECTION_CATALOG_DOCUMENT,
106 remoteDpopTokens: [REMOTE_TOKEN],
107 },
108 new BearerConnectionRegistration({
109 target: BEARER_TARGET,
110 profile: BEARER_PROFILE,
111 credential: BEARER_CREDENTIAL,
112 }),
113 );
114​
115 expect(removeConnectionFromCatalog(registered, BEARER_TARGET)).toEqual(
116 EMPTY_CONNECTION_CATALOG_DOCUMENT,
117 );
118 });
119​
120 it("persists the normalized SSH profile beside its target", () => {
121 const target = new SshConnectionTarget({
122 environmentId: ENVIRONMENT_ID,
123 label: "SSH",
124 connectionId: "ssh-1",
125 });
126 const profile = new SshConnectionProfile({
127 connectionId: target.connectionId,
128 environmentId: target.environmentId,
129 label: target.label,
130 target: {
131 alias: "devbox",
132 hostname: "devbox.example.test",
133 username: "developer",
134 port: 22,
135 },
136 });
137 const document = registerConnectionInCatalog(
138 EMPTY_CONNECTION_CATALOG_DOCUMENT,
139 new SshConnectionRegistration({ target, profile }),
140 );
141​
142 expect(document.targets).toEqual([target]);
143 expect(document.profiles).toEqual([profile]);
144 expect(document.credentials).toEqual([]);
145 });
146});