t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/Cloudflare/Workers/DurableObjectCallbackContext.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

72 lines
2.4 KiB
TypeScript

import { fromDurableObjectState } from "@/Cloudflare/Workers/DurableObjectState.ts";
import { fromDurableObjectStorage } from "@/Cloudflare/Workers/DurableObjectStorage.ts";
import { RuntimeContext } from "@/RuntimeContext.ts";
import { describe, expect, it } from "alchemy-test";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
/**
* `waitUntil` captures the caller's context and provides it to the Effect it
* runs. `blockConcurrencyWhile` and `storage.transaction` run their closures
* through a bare `Effect.runPromise` on the raw platform promise, so a service
* provided to the calling fiber (a request-scoped deadline, a tracer, an alarm
* floor) must reach those closures the same way.
*/
class Marker extends Context.Service<Marker, { readonly value: string }>()(
"test/Marker",
) {}
const readMarker = Effect.map(Marker, (marker) => marker.value);
describe(
"Durable Object callbacks run with the caller's context",
{
tags: [
"unit",
"provider:cloudflare",
"provider:cloudflare:worker",
"local",
],
},
() => {
it.effect(
"blockConcurrencyWhile provides the calling fiber's services",
() =>
Effect.gen(function* () {
const state = fromDurableObjectState({
id: { toString: () => "id" },
storage: { sql: {} },
blockConcurrencyWhile: <T>(callback: () => Promise<T>) =>
callback(),
} as never);
const value = yield* state.blockConcurrencyWhile(() => readMarker);
expect(value).toBe("inside the gate");
}).pipe(
Effect.provideService(Marker, { value: "inside the gate" }),
Effect.provide(RuntimeContext.phantom),
),
);
it.effect("storage.transaction provides the calling fiber's services", () =>
Effect.gen(function* () {
const txn = { getAlarm: () => Promise.resolve(42) };
const storage = fromDurableObjectStorage({
sql: {},
transaction: <T>(closure: (txn: unknown) => Promise<T>) =>
closure(txn),
} as never);
const value = yield* storage.transaction((transaction) =>
Effect.all([readMarker, transaction.getAlarm()]),
);
expect(value).toEqual(["inside the transaction", 42]);
}).pipe(
Effect.provideService(Marker, { value: "inside the transaction" }),
Effect.provide(RuntimeContext.phantom),
),
);
},
);