@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 83 lines 3.24 KB
Raw
1 import * as Railway from "@alchemy.run/railway";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import { Query } from "@distilled.cloud/core/query";
5 import { Railway as RailwaySdk } from "@distilled.cloud/railway";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8
9 const { test } = Test.make({ providers: Railway.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const verifyLoginSession = Query.fn((code: string) => RailwaySdk.loginSessionVerify({ code }));
14
15 const consumeLoginSession = Query.fn((code: string) => RailwaySdk.loginSessionConsume({ code }));
16
17 test.provider(
18 "create, verify, and cancel a login session",
19 (stack) =>
20 Effect.gen(function* () {
21 yield* stack.destroy();
22
23 const code = yield* Railway.provideAnonymousRailway(Railway.createLoginSession());
24 expect(code).toEqual(expect.any(String));
25 expect(code.length).toBeGreaterThan(0);
26
27 const url = Railway.loginSessionUrl(code, { hostname: "alchemy-test" });
28 expect(url.startsWith("https://railway.com/cli-login?d=")).toBe(true);
29 const encoded = url.slice("https://railway.com/cli-login?d=".length);
30 const payload = Buffer.from(encoded.replace(/-/g, "+").replace(/_/g, "/"), "base64").toString(
31 "utf8",
32 );
33 expect(payload).toContain(`wordCode=${code}`);
34 expect(payload).toContain("hostname=alchemy-test");
35
36 const verified = yield* Railway.provideAnonymousRailway(
37 verifyLoginSession(code).pipe(
38 Effect.catchTag("RailwayNotFound", () => Effect.succeed(false)),
39 ),
40 );
41 // Verify is a liveness check: true while the pairing session exists,
42 // even before the user authorizes in the browser.
43 expect(verified).toBe(true);
44
45 const beforeAuth = yield* Railway.provideAnonymousRailway(
46 consumeLoginSession(code).pipe(
47 Effect.catchTag("RailwayNotFound", () => Effect.succeed(null)),
48 ),
49 );
50 expect(beforeAuth).toBeNull();
51
52 const cancelled = yield* Railway.provideAnonymousRailway(Railway.cancelLoginSession(code));
53 expect(cancelled).toBe(true);
54
55 const cancelledAgain = yield* Railway.provideAnonymousRailway(
56 Railway.cancelLoginSession(code).pipe(
57 Effect.catchTag("RailwayNotFound", () => Effect.succeed(false)),
58 ),
59 );
60 expect(typeof cancelledAgain).toBe("boolean");
61
62 const verifiedAfter = yield* Railway.provideAnonymousRailway(
63 verifyLoginSession(code).pipe(
64 Effect.catchTag("RailwayNotFound", () => Effect.succeed(false)),
65 ),
66 );
67 expect(verifiedAfter).toBe(false);
68
69 // A cancelled session no longer exists: consume rejects it with the
70 // typed RailwayLoginSessionNotFound (BAD_USER_INPUT "does not exist").
71 const consumedAfter = yield* Railway.provideAnonymousRailway(
72 consumeLoginSession(code).pipe(
73 Effect.catchTag(["RailwayNotFound", "RailwayLoginSessionNotFound"], () =>
74 Effect.succeed(null),
75 ),
76 ),
77 );
78 expect(consumedAfter).toBeNull();
79
80 yield* stack.destroy();
81 }).pipe(logLevel),
82 { tags: ["provider:railway", "live"], timeout: 120_000 },
83 );