@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 134 lines 4.12 KB
Raw
1 import { expect } from "@alchemy.run/test";
2 import * as machines from "@distilled.cloud/fly-io/machines";
3 import * as Effect from "effect/Effect";
4 import * as Fiber from "effect/Fiber";
5 import * as HttpClient from "effect/http/HttpClient";
6 import * as Ref from "effect/Ref";
7 import * as Result from "effect/Result";
8 import * as Schedule from "effect/Schedule";
9 import * as Stream from "effect/Stream";
10
11 export const websiteServices = (path: string) => [
12 {
13 protocol: "tcp" as const,
14 internalPort: 3000,
15 autostop: "off" as const,
16 ports: [
17 { port: 80, handlers: ["http"], forceHttps: true },
18 { port: 443, handlers: ["tls", "http"] },
19 ],
20 checks: [
21 {
22 type: "http" as const,
23 port: 3000,
24 path,
25 interval: "5s",
26 timeout: "2s",
27 },
28 ],
29 },
30 ];
31
32 export const websiteChecks = (path: string) => ({
33 website: {
34 type: "http" as const,
35 port: 3000,
36 path,
37 interval: "5s",
38 timeout: "2s",
39 },
40 });
41
42 export const getText = (url: string) =>
43 HttpClient.get(url, {
44 headers: { connection: "close", "cache-control": "no-cache" },
45 }).pipe(
46 Effect.flatMap((response) =>
47 response.status === 200
48 ? response.text
49 : Effect.fail(new Error(`Website returned HTTP ${response.status}`)),
50 ),
51 Effect.timeout("10 seconds"),
52 );
53
54 export const initialText = (url: string) =>
55 getText(url).pipe(Effect.retry({ times: 8, schedule: Schedule.spaced("2 seconds") }));
56
57 export const startTraffic = (url: string) =>
58 Effect.gen(function* () {
59 const samples = yield* Ref.make<Array<{ body: string } | { failure: string }>>([]);
60 let active = true;
61 const fiber = yield* Stream.range(0, 1799, 1).pipe(
62 Stream.takeWhile(() => active),
63 Stream.runForEach(() =>
64 getText(url).pipe(
65 Effect.result,
66 Effect.flatMap((result) =>
67 Ref.update(samples, (values) => [
68 ...values,
69 Result.isSuccess(result)
70 ? { body: result.success }
71 : { failure: String(result.failure) },
72 ]),
73 ),
74 Effect.andThen(Effect.sleep("500 millis")),
75 ),
76 ),
77 Effect.timeout("15 minutes"),
78 Effect.forkScoped,
79 );
80 const finish = Effect.gen(function* () {
81 yield* Effect.sync(() => {
82 active = false;
83 });
84 yield* Fiber.join(fiber);
85 const values = yield* Ref.get(samples);
86 expect(values.filter((sample) => "failure" in sample)).toEqual([]);
87 return values.flatMap((sample) => ("body" in sample ? [sample.body] : []));
88 });
89 const waitFor = (predicate: (body: string) => boolean) =>
90 Ref.get(samples).pipe(
91 Effect.repeat({
92 until: (values) => values.some((sample) => "body" in sample && predicate(sample.body)),
93 times: 8,
94 schedule: Schedule.spaced("500 millis"),
95 }),
96 Effect.tap((values) =>
97 Effect.sync(() => {
98 expect(values.some((sample) => "body" in sample && predicate(sample.body))).toBe(true);
99 }),
100 ),
101 );
102 return { finish, waitFor };
103 });
104
105 export const assertOnlyMachine = (appName: string, machineId: string) =>
106 Effect.gen(function* () {
107 const live = (yield* machines.listMachines({ app_name: appName })).filter(
108 (machine) => machine.state !== "destroyed",
109 );
110 expect(live.map((machine) => machine.id)).toEqual([machineId]);
111 });
112
113 export const assertMachineGone = (appName: string, machineId: string) =>
114 Effect.gen(function* () {
115 const gone = yield* machines.getMachine({ app_name: appName, machine_id: machineId }).pipe(
116 Effect.map((machine) => machine.state === "destroyed"),
117 Effect.catchTag("NotFound", () => Effect.succeed(true)),
118 );
119 expect(gone).toBe(true);
120 });
121
122 export const assertAppGone = (appName: string) =>
123 Effect.gen(function* () {
124 const gone = yield* machines.getApp({ app_name: appName }).pipe(
125 Effect.as(false),
126 Effect.catchTag("NotFound", () => Effect.succeed(true)),
127 Effect.repeat({
128 until: (value) => value,
129 times: 8,
130 schedule: Schedule.spaced("1 second"),
131 }),
132 );
133 expect(gone).toBe(true);
134 });