Skip to content

Commit 93b7ea6

Browse files
Julius Marmingeclaude
andcommitted
test: migrate three legacy manual Effect runners to @effect/vitest
The relay JWT test, the browser OTLP export test, and the managed relay credential-change test each used a manual runner (Effect.runPromise, ManagedRuntime.make, Effect.runForkWith) where it.effect with a forked child fiber or a scoped Layer.build does the same job, so their debt ceilings in the lint config can go. The OTLP tracer runs under TestClock.withLive because its export batch fiber is forked during layer build and ticks on a wall-clock interval. Also widen the override tuple type in vite.config.ts; the spread mapped entries inferred a plain array that tsgo rejected against OxlintOverride. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 192478e commit 93b7ea6

4 files changed

Lines changed: 79 additions & 86 deletions

File tree

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -358,58 +358,45 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
358358
).toEqual([activeThreadId]);
359359
});
360360

361-
it("signs the activity publish JWT and rejects tampering", async () => {
362-
const keyPair = NodeCrypto.generateKeyPairSync("ed25519", {
363-
privateKeyEncoding: { format: "pem", type: "pkcs8" },
364-
publicKeyEncoding: { format: "pem", type: "spki" },
365-
});
366-
const payload = {
367-
iss: "t3-env:env",
368-
aud: "https://relay.example.test",
369-
sub: "env",
370-
jti: "nonce-1",
371-
iat: 100,
372-
exp: 200,
373-
environmentId: state.environmentId,
374-
threadId: state.threadId,
375-
state,
376-
} satisfies RelayAgentActivityPublishProofPayload;
377-
const proof = await Effect.runPromise(
378-
AgentAwarenessRelay.signRelayAgentActivityPublishProof({
361+
it.effect("signs the activity publish JWT and rejects tampering", () =>
362+
Effect.gen(function* () {
363+
const keyPair = NodeCrypto.generateKeyPairSync("ed25519", {
364+
privateKeyEncoding: { format: "pem", type: "pkcs8" },
365+
publicKeyEncoding: { format: "pem", type: "spki" },
366+
});
367+
const payload = {
368+
iss: "t3-env:env",
369+
aud: "https://relay.example.test",
370+
sub: "env",
371+
jti: "nonce-1",
372+
iat: 100,
373+
exp: 200,
374+
environmentId: state.environmentId,
375+
threadId: state.threadId,
376+
state,
377+
} satisfies RelayAgentActivityPublishProofPayload;
378+
const proof = yield* AgentAwarenessRelay.signRelayAgentActivityPublishProof({
379379
privateKey: keyPair.privateKey,
380380
payload,
381-
}),
382-
);
383-
384-
await expect(
385-
Effect.runPromise(
386-
verifyRelayJwt({
387-
publicKey: keyPair.publicKey,
388-
token: proof,
389-
typ: RELAY_ACTIVITY_PUBLISH_TYP,
390-
issuer: "t3-env:env",
391-
audience: "https://relay.example.test",
392-
nowEpochSeconds: 150,
393-
}),
394-
),
395-
).resolves.toMatchObject({ jti: "nonce-1", state });
396-
await expect(
397-
Effect.runPromise(
381+
});
382+
const verify = (token: string) =>
398383
verifyRelayJwt({
399384
publicKey: keyPair.publicKey,
400-
token: (() => {
401-
const [header, body, signature = ""] = proof.split(".");
402-
const corruptedSignature = `${signature.startsWith("a") ? "b" : "a"}${signature.slice(1)}`;
403-
return `${header}.${body}.${corruptedSignature}`;
404-
})(),
385+
token,
405386
typ: RELAY_ACTIVITY_PUBLISH_TYP,
406387
issuer: "t3-env:env",
407388
audience: "https://relay.example.test",
408389
nowEpochSeconds: 150,
409-
}),
410-
),
411-
).rejects.toBeDefined();
412-
});
390+
});
391+
392+
expect(yield* verify(proof)).toMatchObject({ jti: "nonce-1", state });
393+
394+
const [header, body, signature = ""] = proof.split(".");
395+
const corruptedSignature = `${signature.startsWith("a") ? "b" : "a"}${signature.slice(1)}`;
396+
const rejection = yield* Effect.flip(verify(`${header}.${body}.${corruptedSignature}`));
397+
expect(rejection).toBeDefined();
398+
}),
399+
);
413400

414401
it.effect("keeps the orchestration listener armed until relay config is installed", () =>
415402
Effect.scoped(
@@ -557,10 +544,11 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
557544
Effect.scoped(
558545
Effect.gen(function* () {
559546
const originalFetch = globalThis.fetch;
560-
const context = yield* Effect.context<never>();
561-
const runFork = Effect.runForkWith(context);
562547
const events = yield* Queue.unbounded<OrchestrationEvent>();
563-
const fetchSeen = yield* Deferred.make<URL>();
548+
let resolveFetchSeen: (url: URL) => void = () => {};
549+
const fetchSeen = new Promise<URL>((resolve) => {
550+
resolveFetchSeen = resolve;
551+
});
564552
const userSpans: Array<string> = [];
565553
const productSpans: Array<string> = [];
566554
const collectingTracer = (spans: Array<string>) =>
@@ -648,7 +636,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
648636
? input
649637
: (input as unknown as { readonly url: string }).url,
650638
);
651-
runFork(Deferred.succeed(fetchSeen, url));
639+
resolveFetchSeen(url);
652640
return Promise.resolve(Response.json({ ok: true, deliveries: [] }));
653641
}) as unknown as typeof fetch;
654642
yield* Effect.addFinalizer(() =>
@@ -707,7 +695,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
707695
occurredAt: now,
708696
} as unknown as OrchestrationEvent);
709697

710-
const url = yield* Deferred.await(fetchSeen).pipe(Effect.timeout("2 seconds"));
698+
const url = yield* Effect.promise(() => fetchSeen).pipe(Effect.timeout("2 seconds"));
711699
expect(url.origin).toBe("https://transport.example.test");
712700
expect(productSpans).toContain("makePublishProof");
713701
expect(userSpans).not.toContain("makePublishProof");

‎apps/server/src/server.test.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import * as Effect from "effect/Effect";
5656
import * as FileSystem from "effect/FileSystem";
5757
import * as Fiber from "effect/Fiber";
5858
import * as Layer from "effect/Layer";
59-
import * as ManagedRuntime from "effect/ManagedRuntime";
6059
import * as Option from "effect/Option";
6160
import * as Path from "effect/Path";
6261
import * as PubSub from "effect/PubSub";
@@ -398,7 +397,9 @@ const makeBrowserOtlpPayload = (spanName: string) =>
398397
({ close }) => Effect.promise(close),
399398
);
400399

401-
const runtime = ManagedRuntime.make(
400+
// The exporter's batch fiber is forked while the layer builds and ticks on
401+
// a wall-clock interval, so the whole tracer runs on the live clock.
402+
yield* Layer.build(
402403
OtlpTracer.layer({
403404
url: collector.url,
404405
exportInterval: "10 millis",
@@ -411,14 +412,13 @@ const makeBrowserOtlpPayload = (spanName: string) =>
411412
},
412413
},
413414
}).pipe(Layer.provide(browserOtlpTracingLayer)),
415+
).pipe(
416+
Effect.flatMap((tracing) =>
417+
Effect.void.pipe(Effect.withSpan(spanName), Effect.provideContext(tracing)),
418+
),
419+
TestClock.withLive,
414420
);
415421

416-
try {
417-
yield* Effect.promise(() => runtime.runPromise(Effect.void.pipe(Effect.withSpan(spanName))));
418-
} finally {
419-
yield* Effect.promise(() => runtime.dispose());
420-
}
421-
422422
const request = yield* Effect.raceFirst(
423423
Effect.promise(() => collector.firstRequest).pipe(Effect.orDie),
424424
Effect.sleep(Duration.seconds(1)).pipe(

‎packages/client-runtime/src/relay/managedRelayState.test.ts‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -243,30 +243,38 @@ describe("createManagedRelayQueryManager", () => {
243243
}),
244244
);
245245

246-
it("emits credential changes only when the managed relay account changes", async () => {
247-
setManagedRelaySession(registry, {
248-
accountId: "account-1",
249-
readClerkToken: () => Promise.resolve("first-token"),
250-
});
251-
const changes = Effect.runPromise(
252-
managedRelayAccountChanges(registry).pipe(Stream.take(2), Stream.runCollect),
253-
);
254-
await vi.waitFor(() => {
255-
expect(registry.getNodes().get(managedRelaySessionAtom)?.listeners.size).toBeGreaterThan(0);
256-
});
246+
it.effect("emits credential changes only when the managed relay account changes", () =>
247+
Effect.gen(function* () {
248+
setManagedRelaySession(registry, {
249+
accountId: "account-1",
250+
readClerkToken: () => Promise.resolve("first-token"),
251+
});
252+
const changes = yield* managedRelayAccountChanges(registry).pipe(
253+
Stream.take(2),
254+
Stream.runCollect,
255+
Effect.forkChild,
256+
);
257+
yield* Effect.promise(() =>
258+
vi.waitFor(() => {
259+
expect(registry.getNodes().get(managedRelaySessionAtom)?.listeners.size).toBeGreaterThan(
260+
0,
261+
);
262+
}),
263+
);
257264

258-
setManagedRelaySession(registry, {
259-
accountId: "account-1",
260-
readClerkToken: () => Promise.resolve("refreshed-token"),
261-
});
262-
setManagedRelaySession(registry, {
263-
accountId: "account-2",
264-
readClerkToken: () => Promise.resolve("second-token"),
265-
});
266-
setManagedRelaySession(registry, null);
265+
setManagedRelaySession(registry, {
266+
accountId: "account-1",
267+
readClerkToken: () => Promise.resolve("refreshed-token"),
268+
});
269+
setManagedRelaySession(registry, {
270+
accountId: "account-2",
271+
readClerkToken: () => Promise.resolve("second-token"),
272+
});
273+
setManagedRelaySession(registry, null);
267274

268-
expect(Array.from(await changes)).toEqual(["account-2", null]);
269-
});
275+
expect(Array.from(yield* Fiber.join(changes))).toEqual(["account-2", null]);
276+
}),
277+
);
270278

271279
it("shares one Clerk token read across concurrent relay list and status queries", async () => {
272280
const secondEnvironment = {

‎vite.config.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,10 @@ export default defineConfig({
180180
"apps/server/src/provider/Layers/ProviderService.test.ts": 2,
181181
"apps/server/src/provider/Layers/ProviderSessionReaper.test.ts": 12,
182182
"apps/server/src/provider/acp/CursorAcpSupport.test.ts": 1,
183-
"apps/server/src/relay/AgentAwarenessRelay.test.ts": 4,
184-
"apps/server/src/server.test.ts": 1,
185-
"packages/client-runtime/src/relay/managedRelayState.test.ts": 1,
186-
}).map(([file, maxOccurrences]) => ({
187-
files: [file],
188-
rules: { "t3code/no-manual-effect-runtime-in-tests": ["error", { maxOccurrences }] },
189-
})),
183+
}).map(([file, maxOccurrences]) => {
184+
const rule: ["error", { maxOccurrences: number }] = ["error", { maxOccurrences }];
185+
return { files: [file], rules: { "t3code/no-manual-effect-runtime-in-tests": rule } };
186+
}),
190187
],
191188
options: {
192189
reportUnusedDisableDirectives: "error",

0 commit comments

Comments
 (0)