Open History to see the last commit
1import {
2 AuthStandardClientScopes,
3 EnvironmentId,
4 PRIMARY_LOCAL_ENVIRONMENT_ID,
5 type DesktopBridge,
6 type DesktopSshEnvironmentTarget,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10
11import {
12 canRetainCachedPlatformRegistrationAfterRefreshFailure,
13 canReuseCachedPlatformRegistration,
14 primaryRegistrationToRetainAfterTopologyRead,
15 provisionDesktopSshEnvironment,
16 readPrimaryEnvironmentTargetResult,
17 secondaryRegistrationsToRetainAfterTopologyRead,
18 secondaryBearerExpiresAtEpochMs,
19 secondaryBearerRefreshAtEpochMs,
20} from "./platform.ts";
21
22const TARGET: DesktopSshEnvironmentTarget = {
23 alias: "devbox",
24 hostname: "devbox.example.test",
25 username: "developer",
26 port: 22,
27};
28
29function makeBridge(
30 calls: string[],
31 options?: { readonly failDescriptor?: boolean },
32): DesktopBridge {
33 return {
34 ensureSshEnvironment: async (target: DesktopSshEnvironmentTarget) => {
35 calls.push("ensure");
36 return {
37 target,
38 httpBaseUrl: "http://127.0.0.1:3201/",
39 wsBaseUrl: "ws://127.0.0.1:3201/",
40 pairingToken: "pairing-token",
41 };
42 },
43 fetchSshEnvironmentDescriptor: async () => {
44 calls.push("descriptor");
45 if (options?.failDescriptor === true) {
46 throw new Error("descriptor unavailable");
47 }
48 return {
49 environmentId: EnvironmentId.make("environment-ssh"),
50 label: "SSH environment",
51 platform: {
52 os: "linux",
53 arch: "x64",
54 },
55 serverVersion: "0.0.0-test",
56 capabilities: {
57 repositoryIdentity: true,
58 },
59 };
60 },
61 bootstrapSshBearerSession: async () => {
62 calls.push("token");
63 return {
64 access_token: "bearer-token",
65 issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
66 token_type: "Bearer",
67 expires_in: 3_600,
68 scope: AuthStandardClientScopes.join(" "),
69 };
70 },
71 } as unknown as DesktopBridge;
72}
73
74describe("desktop SSH pairing", () => {
75 it.effect("fetches the descriptor before consuming the one-time credential", () =>
76 Effect.gen(function* () {
77 const calls: string[] = [];
78
79 const provisioned = yield* provisionDesktopSshEnvironment(makeBridge(calls), TARGET);
80
81 expect(provisioned.environmentId).toBe(EnvironmentId.make("environment-ssh"));
82 expect(calls).toEqual(["ensure", "descriptor", "token"]);
83 }),
84 );
85
86 it.effect("does not consume the credential when adding a route to a different machine", () =>
87 Effect.gen(function* () {
88 const calls: string[] = [];
89
90 const error = yield* provisionDesktopSshEnvironment(
91 makeBridge(calls),
92 TARGET,
93 EnvironmentId.make("some-other-machine"),
94 ).pipe(Effect.flip);
95
96 expect(error).toMatchObject({ reason: "configuration" });
97 expect(calls).toEqual(["ensure", "descriptor"]);
98 }),
99 );
100
101 it.effect("does not consume the credential when descriptor discovery fails", () =>
102 Effect.gen(function* () {
103 const calls: string[] = [];
104
105 yield* provisionDesktopSshEnvironment(
106 makeBridge(calls, { failDescriptor: true }),
107 TARGET,
108 ).pipe(Effect.flip);
109
110 expect(calls).toEqual(["ensure", "descriptor"]);
111 }),
112 );
113});
114
115describe("desktop-local bearer cache", () => {
116 const registration = {} as never;
117
118 it("refreshes a secondary bearer before it expires", () => {
119 const issuedAtEpochMs = 10_000;
120 const refreshAtEpochMs = secondaryBearerRefreshAtEpochMs(issuedAtEpochMs, 60);
121 const expiresAtEpochMs = secondaryBearerExpiresAtEpochMs(issuedAtEpochMs, 60);
122 const cached = {
123 expiresAtEpochMs,
124 signature: "secondary-signature",
125 registration,
126 refreshAtEpochMs,
127 };
128
129 expect(refreshAtEpochMs).toBe(65_000);
130 expect(canReuseCachedPlatformRegistration(cached, cached.signature, 64_999)).toBe(true);
131 expect(canReuseCachedPlatformRegistration(cached, cached.signature, 65_000)).toBe(false);
132 expect(
133 canRetainCachedPlatformRegistrationAfterRefreshFailure(cached, cached.signature, 69_999),
134 ).toBe(true);
135 expect(
136 canRetainCachedPlatformRegistrationAfterRefreshFailure(cached, cached.signature, 70_000),
137 ).toBe(false);
138 });
139
140 it("does not cache credentials whose lifetime is shorter than the refresh skew", () => {
141 const refreshAtEpochMs = secondaryBearerRefreshAtEpochMs(10_000, 3);
142 const cached = {
143 expiresAtEpochMs: secondaryBearerExpiresAtEpochMs(10_000, 3),
144 signature: "secondary-signature",
145 registration,
146 refreshAtEpochMs,
147 };
148
149 expect(refreshAtEpochMs).toBe(10_000);
150 expect(canReuseCachedPlatformRegistration(cached, cached.signature, 10_000)).toBe(false);
151 });
152
153 it("retains only unexpired secondaries after a topology read failure", () => {
154 const valid = {
155 expiresAtEpochMs: 20_000,
156 signature: "valid-secondary",
157 registration,
158 refreshAtEpochMs: 15_000,
159 };
160 const previous = new Map([
161 ["valid-secondary", valid],
162 [
163 "expired-secondary",
164 {
165 expiresAtEpochMs: 10_000,
166 signature: "expired-secondary",
167 registration,
168 refreshAtEpochMs: 5_000,
169 },
170 ],
171 ]);
172
173 expect(
174 secondaryRegistrationsToRetainAfterTopologyRead(
175 previous,
176 { _tag: "Failure", cause: new Error("IPC unavailable") },
177 10_000,
178 ),
179 ).toEqual(new Map([["valid-secondary", valid]]));
180 });
181
182 it("treats a successful empty topology as authoritative removal", () => {
183 const previous = new Map([
184 [
185 "secondary",
186 {
187 expiresAtEpochMs: 20_000,
188 signature: "secondary",
189 registration,
190 refreshAtEpochMs: 15_000,
191 },
192 ],
193 ]);
194
195 expect(
196 secondaryRegistrationsToRetainAfterTopologyRead(
197 previous,
198 { _tag: "Success", bootstraps: [] },
199 10_000,
200 ),
201 ).toEqual(new Map());
202 });
203});
204
205describe("primary topology cache", () => {
206 const registration = {} as never;
207 const cached = {
208 signature: "primary|http://127.0.0.1:3773/|ws://127.0.0.1:3773/",
209 registration,
210 };
211 const previous = new Map([[PRIMARY_LOCAL_ENVIRONMENT_ID, cached]]);
212
213 it("captures synchronous primary target read failures", () => {
214 const cause = new Error("invalid primary target");
215
216 expect(
217 readPrimaryEnvironmentTargetResult(() => {
218 throw cause;
219 }),
220 ).toEqual({ _tag: "Failure", cause });
221 });
222
223 it("retains the cached primary after a transient topology read failure", () => {
224 expect(
225 primaryRegistrationToRetainAfterTopologyRead(previous, {
226 _tag: "Failure",
227 cause: new Error("IPC unavailable"),
228 }),
229 ).toBe(cached);
230 });
231
232 it("treats a successful primary absence as authoritative removal", () => {
233 expect(
234 primaryRegistrationToRetainAfterTopologyRead(previous, {
235 _tag: "Success",
236 target: null,
237 }),
238 ).toBeUndefined();
239 });
240});