@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 106 lines 4.13 KB
Raw
1 import { fromDurableObjectState } from "@alchemy.run/cloudflare/Workers/DurableObjectState";
2 import { fromDurableObjectStorage } from "@alchemy.run/cloudflare/Workers/DurableObjectStorage";
3 import { describe, expect, it } from "@alchemy.run/test";
4 import { RuntimeContext } from "alchemy/RuntimeContext";
5 import * as Cause from "effect/Cause";
6 import * as Context from "effect/Context";
7 import * as Deferred from "effect/Deferred";
8 import * as Effect from "effect/Effect";
9 import * as Exit from "effect/Exit";
10 import * as Fiber from "effect/Fiber";
11
12 /**
13 * `waitUntil` captures the caller's context and provides it to the Effect it
14 * runs. `blockConcurrencyWhile` and `storage.transaction` run their closures
15 * through a bare `Effect.runPromise` on the raw platform promise, so a service
16 * provided to the calling fiber (a request-scoped deadline, a tracer, an alarm
17 * floor) must reach those closures the same way.
18 */
19 class Marker extends Context.Service<Marker, { readonly value: string }>()("test/Marker") {}
20
21 const readMarker = Effect.map(Marker, (marker) => marker.value);
22
23 describe(
24 "Durable Object callbacks run with the caller's context",
25 {
26 tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"],
27 },
28 () => {
29 it.effect("blockConcurrencyWhile provides the calling fiber's services", () =>
30 Effect.gen(function* () {
31 const state = fromDurableObjectState({
32 id: { toString: () => "id" },
33 storage: { sql: {} },
34 blockConcurrencyWhile: <T>(callback: () => Promise<T>) => callback(),
35 } as never);
36
37 const value = yield* state.blockConcurrencyWhile(() => readMarker);
38
39 expect(value).toBe("inside the gate");
40 }).pipe(
41 Effect.provideService(Marker, { value: "inside the gate" }),
42 Effect.provide(RuntimeContext.phantom),
43 ),
44 );
45
46 it.effect("storage.transaction provides the calling fiber's services", () =>
47 Effect.gen(function* () {
48 const txn = { getAlarm: () => Promise.resolve(42) };
49 const storage = fromDurableObjectStorage({
50 sql: {},
51 transaction: <T>(closure: (txn: unknown) => Promise<T>) => closure(txn),
52 } as never);
53
54 const value = yield* storage.transaction((transaction) =>
55 Effect.all([readMarker, transaction.getAlarm()]),
56 );
57
58 expect(value).toEqual(["inside the transaction", 42]);
59 }).pipe(
60 Effect.provideService(Marker, { value: "inside the transaction" }),
61 Effect.provide(RuntimeContext.phantom),
62 ),
63 );
64
65 it.live("storage.transaction interrupts a callback that synchronously cancels its caller", () =>
66 Effect.gen(function* () {
67 const entered = yield* Deferred.make<Fiber.Fiber<unknown, unknown>>();
68 let cleaned = false;
69 const storage = fromDurableObjectStorage({
70 sql: {},
71 // Native storage starts the callback after its caller has suspended.
72 transaction: <T>(closure: (txn: unknown) => Promise<T>) =>
73 Promise.resolve().then(() => closure({})),
74 } as never);
75
76 const caller = yield* Effect.withFiber((parent) =>
77 storage.transaction(() =>
78 Effect.withFiber((callback) =>
79 Effect.gen(function* () {
80 yield* Deferred.succeed(entered, callback);
81 yield* Effect.sync(() => parent.interruptUnsafe());
82 yield* Effect.never;
83 }),
84 ).pipe(
85 Effect.ensuring(
86 Effect.sync(() => {
87 cleaned = true;
88 }),
89 ),
90 ),
91 ),
92 ).pipe(Effect.forkScoped);
93 const callback = yield* Deferred.await(entered);
94
95 yield* Effect.gen(function* () {
96 const exit = yield* Fiber.await(caller).pipe(Effect.timeout("1 second"));
97 expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
98 expect(cleaned).toBe(true);
99 }).pipe(
100 // A regression must fail without leaving the callback stuck.
101 Effect.ensuring(Fiber.interrupt(callback)),
102 );
103 }).pipe(Effect.provide(RuntimeContext.phantom)),
104 );
105 },
106 );