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

platform.test.ts

225 lines6.4 KB

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;
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 descriptor discovery fails", () =>
87 Effect.gen(function* () {
88 const calls: string[] = [];
89​
90 yield* provisionDesktopSshEnvironment(
91 makeBridge(calls, { failDescriptor: true }),
92 TARGET,
93 ).pipe(Effect.flip);
94​
95 expect(calls).toEqual(["ensure", "descriptor"]);
96 }),
97 );
98});
99​
100describe("desktop-local bearer cache", () => {
101 const registration = {} as never;
102​
103 it("refreshes a secondary bearer before it expires", () => {
104 const issuedAtEpochMs = 10_000;
105 const refreshAtEpochMs = secondaryBearerRefreshAtEpochMs(issuedAtEpochMs, 60);
106 const expiresAtEpochMs = secondaryBearerExpiresAtEpochMs(issuedAtEpochMs, 60);
107 const cached = {
108 expiresAtEpochMs,
109 signature: "secondary-signature",
110 registration,
111 refreshAtEpochMs,
112 };
113​
114 expect(refreshAtEpochMs).toBe(65_000);
115 expect(canReuseCachedPlatformRegistration(cached, cached.signature, 64_999)).toBe(true);
116 expect(canReuseCachedPlatformRegistration(cached, cached.signature, 65_000)).toBe(false);
117 expect(
118 canRetainCachedPlatformRegistrationAfterRefreshFailure(cached, cached.signature, 69_999),
119 ).toBe(true);
120 expect(
121 canRetainCachedPlatformRegistrationAfterRefreshFailure(cached, cached.signature, 70_000),
122 ).toBe(false);
123 });
124​
125 it("does not cache credentials whose lifetime is shorter than the refresh skew", () => {
126 const refreshAtEpochMs = secondaryBearerRefreshAtEpochMs(10_000, 3);
127 const cached = {
128 expiresAtEpochMs: secondaryBearerExpiresAtEpochMs(10_000, 3),
129 signature: "secondary-signature",
130 registration,
131 refreshAtEpochMs,
132 };
133​
134 expect(refreshAtEpochMs).toBe(10_000);
135 expect(canReuseCachedPlatformRegistration(cached, cached.signature, 10_000)).toBe(false);
136 });
137​
138 it("retains only unexpired secondaries after a topology read failure", () => {
139 const valid = {
140 expiresAtEpochMs: 20_000,
141 signature: "valid-secondary",
142 registration,
143 refreshAtEpochMs: 15_000,
144 };
145 const previous = new Map([
146 ["valid-secondary", valid],
147 [
148 "expired-secondary",
149 {
150 expiresAtEpochMs: 10_000,
151 signature: "expired-secondary",
152 registration,
153 refreshAtEpochMs: 5_000,
154 },
155 ],
156 ]);
157​
158 expect(
159 secondaryRegistrationsToRetainAfterTopologyRead(
160 previous,
161 { _tag: "Failure", cause: new Error("IPC unavailable") },
162 10_000,
163 ),
164 ).toEqual(new Map([["valid-secondary", valid]]));
165 });
166​
167 it("treats a successful empty topology as authoritative removal", () => {
168 const previous = new Map([
169 [
170 "secondary",
171 {
172 expiresAtEpochMs: 20_000,
173 signature: "secondary",
174 registration,
175 refreshAtEpochMs: 15_000,
176 },
177 ],
178 ]);
179​
180 expect(
181 secondaryRegistrationsToRetainAfterTopologyRead(
182 previous,
183 { _tag: "Success", bootstraps: [] },
184 10_000,
185 ),
186 ).toEqual(new Map());
187 });
188});
189​
190describe("primary topology cache", () => {
191 const registration = {} as never;
192 const cached = {
193 signature: "primary|http://127.0.0.1:3773/|ws://127.0.0.1:3773/",
194 registration,
195 };
196 const previous = new Map([[PRIMARY_LOCAL_ENVIRONMENT_ID, cached]]);
197​
198 it("captures synchronous primary target read failures", () => {
199 const cause = new Error("invalid primary target");
200​
201 expect(
202 readPrimaryEnvironmentTargetResult(() => {
203 throw cause;
204 }),
205 ).toEqual({ _tag: "Failure", cause });
206 });
207​
208 it("retains the cached primary after a transient topology read failure", () => {
209 expect(
210 primaryRegistrationToRetainAfterTopologyRead(previous, {
211 _tag: "Failure",
212 cause: new Error("IPC unavailable"),
213 }),
214 ).toBe(cached);
215 });
216​
217 it("treats a successful primary absence as authoritative removal", () => {
218 expect(
219 primaryRegistrationToRetainAfterTopologyRead(previous, {
220 _tag: "Success",
221 target: null,
222 }),
223 ).toBeUndefined();
224 });
225});