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

main
ts 45 lines 1.97 KB
Raw
1 import { OAUTH_CLIENT_ID, exchange, usesCurrentClient } from "@alchemy.run/planetscale/OAuthClient";
2 import { describe, expect, it } from "@alchemy.run/test";
3 import * as Effect from "effect/Effect";
4 import * as FetchHttpClient from "effect/http/FetchHttpClient";
5 import * as Redacted from "effect/Redacted";
6
7 describe(
8 "PlanetScale OAuth client credentials",
9 { tags: ["unit", "provider:planetscale", "local"] },
10 () => {
11 it("maps the token response onto stored credentials", async () => {
12 // SAFETY: the test double implements the callable fetch contract; Bun's
13 // nonstandard static `preconnect` member is not used by FetchHttpClient.
14 const fetch = (async (_input, _init) =>
15 new Response(
16 JSON.stringify({
17 access_token: "access",
18 refresh_token: "refresh",
19 expires_in: 3600,
20 scope: "read write",
21 }),
22 { status: 200, headers: { "Content-Type": "application/json" } },
23 )) as typeof globalThis.fetch;
24
25 const before = Date.now();
26 const credentials = await Effect.runPromise(
27 exchange("code").pipe(Effect.provideService(FetchHttpClient.Fetch, fetch)),
28 );
29 expect(credentials.type).toBe("oauth");
30 expect(credentials.clientId).toBe(OAUTH_CLIENT_ID);
31 expect(Redacted.value(credentials.access)).toBe("access");
32 expect(Redacted.value(credentials.refresh)).toBe("refresh");
33 expect(credentials.scopes).toEqual(["read", "write"]);
34 expect(credentials.expires).toBeGreaterThanOrEqual(before + 3_600_000 - 1_000);
35 });
36
37 it("flags credentials issued to a rotated client id as stale", () => {
38 expect(usesCurrentClient({ clientId: OAUTH_CLIENT_ID })).toBe(true);
39 expect(usesCurrentClient({ clientId: "pscale_app_old" })).toBe(false);
40 // Credentials persisted before clientId was stored must read as stale so
41 // login falls back to a fresh browser authorization.
42 expect(usesCurrentClient({})).toBe(false);
43 });
44 },
45 );