@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 140 lines 4.73 KB
Raw
1 import { cacheUntilExpiry } from "@alchemy.run/cloudflare/Credentials";
2 import { describe, expect, it } from "@alchemy.run/test";
3 import {
4 apiTokenCredentials,
5 oauthCredentials,
6 type ResolvedCredentials,
7 } from "@distilled.cloud/cloudflare/Credentials";
8 import * as Clock from "effect/Clock";
9 import * as Duration from "effect/Duration";
10 import * as Effect from "effect/Effect";
11 import * as Redacted from "effect/Redacted";
12 import * as TestClock from "effect/testing/TestClock";
13
14 /**
15 * Regression spec for "alchemy dev breaks once the Cloudflare OAuth access
16 * token expires": `fromAuthProvider` used to memoize the first credential
17 * resolution forever (`Effect.cached`), so when the remote-bindings preview
18 * session needed refreshing hours into a dev session, the preview-session
19 * API was called with the long-dead access token and every remote binding
20 * failed until the process was restarted.
21 *
22 * `cacheUntilExpiry` is the replacement: cache while valid, re-resolve
23 * (which refreshes + persists the token) once the refresh window is
24 * reached, single-flight under concurrency.
25 */
26
27 const MINUTE_MS = 60_000;
28
29 const makeOAuthResolver = () => {
30 let resolutions = 0;
31 const resolve = Effect.gen(function* () {
32 resolutions++;
33 const now = yield* Clock.currentTimeMillis;
34 return oauthCredentials({
35 accessToken: Redacted.make(`token-${resolutions}`),
36 // each freshly resolved token is valid for 1 hour from "now"
37 expiresAt: now + 60 * MINUTE_MS,
38 }) as ResolvedCredentials;
39 });
40 return { resolve, count: () => resolutions };
41 };
42
43 describe(
44 "Cloudflare Credentials cacheUntilExpiry",
45 { tags: ["unit", "provider:cloudflare", "local"] },
46 () => {
47 it.effect("caches OAuth credentials while they are valid", () =>
48 Effect.gen(function* () {
49 const resolver = makeOAuthResolver();
50 const credentials = yield* cacheUntilExpiry(resolver.resolve);
51
52 const first = yield* credentials;
53 yield* TestClock.adjust(Duration.minutes(10));
54 const second = yield* credentials;
55
56 expect(resolver.count()).toBe(1);
57 expect(second).toBe(first);
58 }),
59 );
60
61 it.effect("re-resolves OAuth credentials once the refresh window is reached", () =>
62 Effect.gen(function* () {
63 const resolver = makeOAuthResolver();
64 const credentials = yield* cacheUntilExpiry(resolver.resolve);
65
66 const first = yield* credentials;
67 expect(first.type).toBe("oauth");
68
69 // 56 minutes in: inside the 5-minute refresh window of the 1h token.
70 yield* TestClock.adjust(Duration.minutes(56));
71 const second = yield* credentials;
72
73 expect(resolver.count()).toBe(2);
74 expect(second).not.toBe(first);
75
76 // The re-resolved token is cached again in turn.
77 yield* TestClock.adjust(Duration.minutes(10));
78 const third = yield* credentials;
79 expect(resolver.count()).toBe(2);
80 expect(third).toBe(second);
81 }),
82 );
83
84 it.effect("re-resolves OAuth credentials that are already fully expired", () =>
85 Effect.gen(function* () {
86 const resolver = makeOAuthResolver();
87 const credentials = yield* cacheUntilExpiry(resolver.resolve);
88
89 yield* credentials;
90 // The machine slept through the token's entire lifetime.
91 yield* TestClock.adjust(Duration.hours(6));
92 yield* credentials;
93
94 expect(resolver.count()).toBe(2);
95 }),
96 );
97
98 it.effect("caches non-expiring credentials (api tokens) forever", () =>
99 Effect.gen(function* () {
100 let resolutions = 0;
101 const resolve = Effect.sync(() => {
102 resolutions++;
103 return apiTokenCredentials({ apiToken: Redacted.make("static") }) as ResolvedCredentials;
104 });
105 const credentials = yield* cacheUntilExpiry(resolve);
106
107 yield* credentials;
108 yield* TestClock.adjust(Duration.days(365));
109 yield* credentials;
110
111 expect(resolutions).toBe(1);
112 }),
113 );
114
115 it.live("concurrent cold-cache resolutions are single-flight", () =>
116 Effect.gen(function* () {
117 let resolutions = 0;
118 const resolve = Effect.sleep("20 millis").pipe(
119 Effect.map(() => {
120 resolutions++;
121 return oauthCredentials({
122 accessToken: Redacted.make(`token-${resolutions}`),
123 expiresAt: Date.now() + 60 * MINUTE_MS,
124 }) as ResolvedCredentials;
125 }),
126 );
127 const credentials = yield* cacheUntilExpiry(resolve);
128
129 const results = yield* Effect.all([credentials, credentials, credentials, credentials], {
130 concurrency: "unbounded",
131 });
132
133 expect(resolutions).toBe(1);
134 for (const result of results) {
135 expect(result).toBe(results[0]);
136 }
137 }),
138 );
139 },
140 );