Open History to see the last commit
1import { assert, it, vi } from "@effect/vitest";
2import {
3 NodeId,
4 ProviderSessionId,
5 RuntimeRequestId,
6 ThreadId,
7 type OrchestrationV2ThreadProjection,
8} from "@t3tools/contracts";
9import * as DateTime from "effect/DateTime";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Option from "effect/Option";
13
14import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
15import * as ProjectionStore from "./ProjectionStore.ts";
16import * as ProviderSessionManager from "./ProviderSessionManager.ts";
17import * as RuntimeRequestService from "./RuntimeRequestService.ts";
18
19it.effect("rejects expired runtime requests before invoking the live adapter", () => {
20 const threadId = ThreadId.make("thread-runtime-request-expired");
21 const providerSessionId = ProviderSessionId.make("provider-session-runtime-request-expired");
22 const requestId = RuntimeRequestId.make("request-expired");
23 const respondToRuntimeRequest = vi.fn(
24 (_input: ProviderAdapter.ProviderAdapterV2RuntimeRequestResponseInput) => Effect.void,
25 );
26 const getSession = vi.fn(() =>
27 Effect.succeed(
28 Option.some({
29 respondToRuntimeRequest,
30 } as never),
31 ),
32 );
33 const projection = {
34 runtimeRequests: [
35 {
36 id: requestId,
37 nodeId: NodeId.make("node-runtime-request-expired"),
38 providerTurnId: null,
39 nativeRequestRef: null,
40 kind: "command",
41 status: "expired",
42 responseCapability: {
43 type: "live",
44 providerSessionId,
45 },
46 createdAt: DateTime.makeUnsafe("2026-07-29T00:00:00.000Z"),
47 resolvedAt: DateTime.makeUnsafe("2026-07-29T00:00:01.000Z"),
48 },
49 ],
50 } as unknown as OrchestrationV2ThreadProjection;
51 const layerTest = RuntimeRequestService.layer.pipe(
52 Layer.provide(
53 Layer.mergeAll(
54 Layer.mock(ProjectionStore.ProjectionStoreV2)({
55 getRuntimeRequest: (_threadId, requestId) =>
56 Effect.succeed(projection.runtimeRequests.find((request) => request.id === requestId)),
57 }),
58 Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
59 get: getSession,
60 }),
61 ),
62 ),
63 );
64
65 return Effect.gen(function* () {
66 const service = yield* RuntimeRequestService.RuntimeRequestServiceV2;
67 const error = yield* service
68 .respond({
69 threadId,
70 providerSessionId,
71 requestId,
72 decision: "accept",
73 })
74 .pipe(Effect.flip);
75
76 assert.equal(error.reason, "request-not-ready");
77 assert.equal(
78 error.message,
79 `Runtime request ${requestId} on thread ${threadId} is not ready for response execution.`,
80 );
81 assert.isUndefined(error.cause);
82 assert.equal(getSession.mock.calls.length, 0);
83 assert.equal(respondToRuntimeRequest.mock.calls.length, 0);
84 }).pipe(Effect.provide(layerTest));
85});