@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, 04:45 UTC

main
ts 144 lines 5.14 KB
Raw
1 import { expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import * as Effect from "effect/Effect";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import * as Layer from "effect/Layer";
6 import * as Ref from "effect/Ref";
7 import { isResolved } from "@/Diff.ts";
8 import * as Plan from "@/Plan";
9 import { Platform, type Main, type PlatformProps } from "@/Platform.ts";
10 import * as Provider from "@/Provider.ts";
11 import { Resource } from "@/Resource";
12 import {
13 createHostRuntimeContext,
14 type HostRuntimeContext,
15 ServerHost,
16 } from "@/Runtime/Process.ts";
17 import * as Stack from "@/Stack";
18 import { Stage } from "@/Stage";
19 import { inMemoryState, State } from "@/State";
20
21 // A minimal hosted Platform (like AWS.ECS.Task / AWS.EC2.Instance) whose
22 // runtime context is built by the shared `createHostRuntimeContext`. Its
23 // provider is a no-op so the plan never touches the cloud.
24 interface HostProps extends PlatformProps {
25 main?: string;
26 }
27
28 interface Host extends Resource<"Test.Host", HostProps, { ok: boolean }> {}
29
30 type HostServices = ServerHost;
31 type HostShape = Main<HostServices>;
32
33 const Host: Platform<Host, HostServices, HostShape, HostRuntimeContext> = Platform("Test.Host", {
34 createRuntimeContext: createHostRuntimeContext("Test.Host"),
35 });
36
37 const hostProvider = () =>
38 Provider.succeed(Host, {
39 list: () => Effect.succeed([]),
40 diff: Effect.fn(function* ({ news }) {
41 if (!isResolved(news)) return undefined;
42 }),
43 reconcile: Effect.fn(function* ({ output }) {
44 return output ?? { ok: true };
45 }),
46 delete: Effect.fn(function* () {}),
47 });
48
49 const { test } = Test.make({
50 providers: hostProvider(),
51 state: inMemoryState(),
52 });
53
54 const makePlan = <A, Err, Req>(
55 effect: Effect.Effect<A, Err, Req>,
56 ): Effect.Effect<Plan.Plan<A>, Err, State> =>
57 effect.pipe(
58 // @ts-expect-error - Stack.make's typing erases R unsoundly here
59 Stack.make({
60 name: "test",
61 providers: Layer.empty,
62 state: inMemoryState(),
63 }),
64 Effect.provideService(Stage, "test"),
65 Effect.flatMap((stackSpec: any) => Plan.make(stackSpec)),
66 Effect.provide(hostProvider()),
67 );
68
69 // Unit-level: the shared host context exposes run / serve / exports and folds
70 // both run-registered loops and serve-registered handlers into a single
71 // `exports.program` that the generated container/instance entry runs.
72 test(
73 "createHostRuntimeContext collects run + serve into exports.program",
74 Effect.gen(function* () {
75 const ctx = createHostRuntimeContext("Test.Host")("my-host");
76
77 expect(ctx.Type).toBe("Test.Host");
78 expect(typeof ctx.run).toBe("function");
79 expect(typeof ctx.serve).toBe("function");
80 expect(typeof ctx.get).toBe("function");
81 expect(typeof ctx.set).toBe("function");
82
83 const ran = yield* Ref.make<string[]>([]);
84 yield* ctx.run(Ref.update(ran, (xs) => [...xs, "loop"]));
85 // serve registers an HTTP handler runner; with no HttpServer bound it is a
86 // harmless no-op, so the program completes without crashing.
87 yield* ctx.serve(Effect.succeed(HttpServerResponse.text("ok")));
88
89 const { program } = yield* ctx.exports;
90 yield* program;
91
92 expect(yield* Ref.get(ran)).toEqual(["loop"]);
93 }),
94 { tags: ["unit", "local"] },
95 );
96
97 // Regression for #706: the generated container/instance entrypoint resolves the
98 // long-running program via `RuntimeContext.exports` (an Effect) and then runs
99 // `.program`. `exports` is an Effect, so the entry MUST flat-map it before
100 // touching `.program` — reaching for `exports.program` directly yields
101 // `undefined`. This asserts both the (wrong) direct access and the (correct)
102 // resolved access, mirroring the entry's `flatMap(exports) → flatMap(.program)`.
103 test(
104 "exports must be resolved before reading .program (generated-entry contract)",
105 Effect.gen(function* () {
106 const ran = yield* Ref.make(false);
107 const ctx = createHostRuntimeContext("Test.Host")("entry");
108 yield* ctx.run(Ref.set(ran, true));
109
110 // Direct access — what the broken entry did — is undefined.
111 expect((ctx.exports as { program?: unknown }).program).toBeUndefined();
112
113 // Correct access: resolve the Effect, then run the program.
114 const { program } = yield* ctx.exports;
115 yield* program;
116 expect(yield* Ref.get(ran)).toBe(true);
117 }),
118 { tags: ["unit", "local"] },
119 );
120
121 // Regression for #706: a hosted Platform program can `yield* ServerHost` and
122 // call `host.run(...)` during plan. Before the fix this died with
123 // "Service not found: Alchemy::ServerHost". Returning `{ fetch }` additionally
124 // exercises the host `serve` path (previously "No serve handler").
125 test(
126 "Platform provides ServerHost to a hosted program during plan",
127 Effect.gen(function* () {
128 const plan = yield* Host(
129 "MyHost",
130 { main: "index.ts" },
131 Effect.gen(function* () {
132 const host = yield* ServerHost;
133 // The long-running loop — only registered during plan, never run.
134 yield* host.run(Effect.void);
135 return {
136 fetch: Effect.succeed(HttpServerResponse.text("ok")),
137 };
138 }),
139 ).pipe(makePlan);
140
141 expect(plan.resources["MyHost"]?.action).toBe("create");
142 }),
143 { tags: ["unit", "local"] },
144 );