Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

RuntimeRequestService.test.ts

103 lines3.4 KB

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.each(["expired", "resolved"] as const)(
20 "responds only to resolved requests and preserves feedback (%s)",
21 (status) => {
22 const threadId = ThreadId.make("thread-runtime-request-expired");
23 const providerSessionId = ProviderSessionId.make("provider-session-runtime-request-expired");
24 const requestId = RuntimeRequestId.make("request-expired");
25 const respondToRuntimeRequest = vi.fn(
26 (_input: ProviderAdapter.ProviderAdapterV2RuntimeRequestResponseInput) => Effect.void,
27 );
28 const getSession = vi.fn(() =>
29 Effect.succeed(
30 Option.some({
31 respondToRuntimeRequest,
32 } as never),
33 ),
34 );
35 const projection = {
36 runtimeRequests: [
37 {
38 id: requestId,
39 nodeId: NodeId.make("node-runtime-request-expired"),
40 providerTurnId: null,
41 nativeRequestRef: null,
42 kind: "command",
43 status,
44 responseCapability: {
45 type: "live",
46 providerSessionId,
47 },
48 createdAt: DateTime.makeUnsafe("2026-07-29T00:00:00.000Z"),
49 resolvedAt: DateTime.makeUnsafe("2026-07-29T00:00:01.000Z"),
50 },
51 ],
52 } as unknown as OrchestrationV2ThreadProjection;
53 const layerTest = RuntimeRequestService.layer.pipe(
54 Layer.provide(
55 Layer.mergeAll(
56 Layer.mock(ProjectionStore.ProjectionStoreV2)({
57 getRuntimeRequest: (_threadId, requestId) =>
58 Effect.succeed(
59 projection.runtimeRequests.find((request) => request.id === requestId),
60 ),
61 }),
62 Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
63 get: getSession,
64 }),
65 ),
66 ),
67 );
68​
69 return Effect.gen(function* () {
70 const service = yield* RuntimeRequestService.RuntimeRequestServiceV2;
71 const response = service.respond({
72 threadId,
73 providerSessionId,
74 requestId,
75 decision: "decline",
76 feedback: "Use an explicit path.",
77 });
78 if (status === "resolved") {
79 yield* response;
80 assert.deepEqual(respondToRuntimeRequest.mock.calls, [
81 [
82 {
83 requestId,
84 decision: "decline",
85 feedback: "Use an explicit path.",
86 },
87 ],
88 ]);
89 return;
90 }
91 const error = yield* response.pipe(Effect.flip);
92​
93 assert.equal(error.reason, "request-not-ready");
94 assert.equal(
95 error.message,
96 `Runtime request ${requestId} on thread ${threadId} is not ready for response execution.`,
97 );
98 assert.isUndefined(error.cause);
99 assert.equal(getSession.mock.calls.length, 0);
100 assert.equal(respondToRuntimeRequest.mock.calls.length, 0);
101 }).pipe(Effect.provide(layerTest));
102 },
103);