@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 155 lines 5.36 KB
Raw
1 import * as Railway from "@alchemy.run/railway";
2 import { RPC_PATH_PREFIX, RPC_TOKEN_HEADER } from "@alchemy.run/railway/rpc-token";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11 import { Api, ApiLive } from "./fixtures/rpc-api.ts";
12 import Caller from "./fixtures/rpc-caller.ts";
13 import Greeter from "./fixtures/rpc-greeter.ts";
14 import Query from "./fixtures/rpc-query.ts";
15
16 const { test } = Test.make({ providers: Railway.providers() });
17
18 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
19
20 class NotReady extends Data.TaggedError("NotReady")<{
21 status: number;
22 body?: string;
23 }> {
24 override get message() {
25 return this.body === undefined
26 ? `status ${this.status}`
27 : `status ${this.status}: ${this.body}`;
28 }
29 }
30
31 const getText = (url: string) =>
32 Effect.gen(function* () {
33 const client = yield* HttpClient.HttpClient;
34 return yield* client.get(url).pipe(
35 Effect.timeout("15 seconds"),
36 Effect.flatMap((res) =>
37 res.status === 200
38 ? res.text
39 : res.text.pipe(
40 Effect.flatMap((body) => Effect.fail(new NotReady({ status: res.status, body }))),
41 ),
42 ),
43 Effect.retry({
44 schedule: Schedule.spaced("4 seconds"),
45 times: 10,
46 }),
47 );
48 });
49
50 test.provider.skip(
51 "Function-to-Function schemaless RPC stays on the private mesh (canvas start command max 96KB)",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 const greeter = yield* Greeter;
59 const caller = yield* Caller;
60 return { greeter, caller };
61 }),
62 );
63
64 expect(created.greeter.dnsName).toEqual(`${created.greeter.name}.railway.internal`);
65 expect(created.greeter.dnsName.endsWith(".railway.internal")).toEqual(true);
66 expect(created.greeter.rpcToken.length).toBeGreaterThanOrEqual(32);
67 expect(created.greeter.url).toEqual(expect.any(String));
68 expect(created.caller.url).toEqual(expect.any(String));
69
70 const greeterPublic = yield* getText(created.greeter.url!);
71 expect(greeterPublic).toEqual("greeter");
72
73 const client = yield* HttpClient.HttpClient;
74 const publicRpc = yield* client.execute(
75 HttpClientRequest.post(`${created.greeter.url}${RPC_PATH_PREFIX}greet`).pipe(
76 HttpClientRequest.bodyText(JSON.stringify(["sam"]), "application/json"),
77 HttpClientRequest.setHeader(RPC_TOKEN_HEADER, created.greeter.rpcToken),
78 ),
79 );
80 expect(publicRpc.status).toEqual(401);
81
82 const greeting = yield* getText(created.caller.url!);
83 expect(greeting).toEqual("hello sam");
84
85 yield* stack.destroy();
86 }).pipe(logLevel),
87 {
88 tags: ["provider:railway", "provider:railway:bind", "provider:railway:function", "live"],
89 timeout: 120_000,
90 },
91 );
92
93 test.provider.skip(
94 "Service and Function bind each other in tagged form over the private mesh (canvas start command max 96KB)",
95 (stack) =>
96 Effect.gen(function* () {
97 yield* stack.destroy();
98
99 const created = yield* stack.deploy(
100 Effect.gen(function* () {
101 const query = yield* Query;
102 const api = yield* Api;
103 return { query, api };
104 }).pipe(Effect.provide(ApiLive)),
105 );
106
107 expect(created.query.url).toEqual(expect.any(String));
108 expect(created.api.url).toEqual(expect.any(String));
109
110 yield* Effect.log(`tagged rpc urls query=${created.query.url} api=${created.api.url}`);
111
112 // Probe the Service origin first (Docker). The Function canvas
113 // used to hang GET `/` while this side was already serving.
114 const fromService = yield* getText(created.api.url!);
115 expect(fromService).toEqual("api");
116
117 const fromFunction = yield* getText(created.query.url!);
118 expect(fromFunction).toEqual("query");
119
120 const viaFunction = yield* getText(`${created.query.url}/pong`);
121 expect(viaFunction).toEqual("pong");
122
123 const viaService = yield* getText(`${created.api.url}/hello`);
124 expect(viaService).toEqual("hello sam");
125
126 const client = yield* HttpClient.HttpClient;
127 const functionRpc = yield* client.execute(
128 HttpClientRequest.post(`${created.query.url}${RPC_PATH_PREFIX}greet`).pipe(
129 HttpClientRequest.bodyText(JSON.stringify(["sam"]), "application/json"),
130 HttpClientRequest.setHeader(RPC_TOKEN_HEADER, created.query.rpcToken),
131 ),
132 );
133 expect(functionRpc.status).toEqual(401);
134
135 const serviceRpc = yield* client.execute(
136 HttpClientRequest.post(`${created.api.url}${RPC_PATH_PREFIX}ping`).pipe(
137 HttpClientRequest.bodyText("[]", "application/json"),
138 HttpClientRequest.setHeader(RPC_TOKEN_HEADER, created.api.rpcToken),
139 ),
140 );
141 expect(serviceRpc.status).toEqual(401);
142
143 yield* stack.destroy();
144 }).pipe(logLevel),
145 {
146 tags: [
147 "provider:railway",
148 "provider:railway:bind",
149 "provider:railway:function",
150 "provider:railway:service",
151 "live",
152 ],
153 timeout: 120_000,
154 },
155 );