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

RuntimePolicy.test.ts

126 lines4.1 KB

1import { assert, it } from "@effect/vitest";
2import {
3 type ModelSelection,
4 type OrchestrationV2AppThread,
5 ProjectId,
6 ProviderInstanceId,
7 type RuntimeMode,
8 type ServerProvider,
9 ThreadId,
10} from "@t3tools/contracts";
11import * as DateTime from "effect/DateTime";
12import * as Effect from "effect/Effect";
13import * as Layer from "effect/Layer";
14import * as Stream from "effect/Stream";
15​
16import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
17import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
18import * as ProjectStore from "./ProjectStore.ts";
19import * as RuntimePolicy from "./RuntimePolicy.ts";
20​
21const projectId = ProjectId.make("project:runtime-policy");
22const providerInstanceId = ProviderInstanceId.make("codex");
23const modelSelection = {
24 instanceId: providerInstanceId,
25 model: "gpt-5.5",
26} satisfies ModelSelection;
27​
28function makeThread(input: {
29 readonly now: DateTime.Utc;
30 readonly worktreePath: string | null;
31 readonly runtimeMode?: RuntimeMode;
32}): OrchestrationV2AppThread {
33 const threadId = ThreadId.make("thread:runtime-policy");
34 return {
35 createdBy: "user",
36 creationSource: "web",
37 id: threadId,
38 projectId,
39 title: "Runtime policy",
40 providerInstanceId,
41 modelSelection,
42 runtimeMode: input.runtimeMode ?? "full-access",
43 interactionMode: "default",
44 branch: null,
45 worktreePath: input.worktreePath,
46 activeProviderThreadId: null,
47 lineage: {
48 parentThreadId: null,
49 relationshipToParent: null,
50 rootThreadId: threadId,
51 },
52 forkedFrom: null,
53 createdAt: input.now,
54 updatedAt: input.now,
55 archivedAt: null,
56 settledOverride: null,
57 settledAt: null,
58 lastVisitedAt: null,
59 deletedAt: null,
60 };
62​
63const grokInstanceId = ProviderInstanceId.make("grok");
64const supportedRuntimeModesByInstance = new Map<ProviderInstanceId, ReadonlyArray<RuntimeMode>>([
65 [grokInstanceId, ["approval-required", "auto", "full-access"]],
66]);
67const providerInstanceFor = (instanceId: ProviderInstanceId) =>
68 ({
69 snapshot: {
70 getSnapshot: Effect.succeed({
71 supportedRuntimeModes: supportedRuntimeModesByInstance.get(instanceId),
72 } as ServerProvider),
73 },
74 }) as ProviderInstance;
75​
76const layerTest = RuntimePolicy.layerFromProjectStore.pipe(
77 Layer.provide(
78 Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, {
79 getInstance: (instanceId) => Effect.succeed(providerInstanceFor(instanceId)),
80 listInstances: Effect.succeed([]),
81 listUnavailable: Effect.succeed([]),
82 streamChanges: Stream.empty,
83 subscribeChanges: Effect.never,
84 }),
85 ),
86 Layer.provide(
87 Layer.mock(ProjectStore.ProjectStoreV2)({
88 get: () =>
89 Effect.succeedSome({
90 projectId,
91 title: "Project",
92 workspaceRoot: "/project-root",
93 defaultModelSelection: null,
94 defaultThreadEnvMode: null,
95 autoPull: false,
96 faviconPath: null,
97 projectIcon: null,
98 scripts: [],
99 createdAt: "2026-06-21T00:00:00.000Z",
100 updatedAt: "2026-06-21T00:00:00.000Z",
101 deletedAt: null,
102 }),
103 }),
104 ),
105);
106​
107it.layer(layerTest)("RuntimePolicyV2", (it) => {
108 it.effect("runs a mode the provider does not offer in Supervised", () =>
109 Effect.gen(function* () {
110 const policy = yield* RuntimePolicy.RuntimePolicyV2;
111 const now = yield* DateTime.now;
112 const modeFor = (instanceId: ProviderInstanceId, runtimeMode: RuntimeMode) =>
113 policy
114 .resolve({
115 thread: makeThread({ now, worktreePath: null, runtimeMode }),
116 modelSelection: { instanceId, model: "test-model" },
117 })
118 .pipe(Effect.map((resolved) => resolved.runtimeMode));
119​
120 assert.equal(yield* modeFor(grokInstanceId, "auto-accept-edits"), "approval-required");
121 assert.equal(yield* modeFor(grokInstanceId, "auto"), "auto");
122 assert.equal(yield* modeFor(grokInstanceId, "full-access"), "full-access");
123 assert.equal(yield* modeFor(providerInstanceId, "auto-accept-edits"), "auto-accept-edits");
124 }),
125 );
126});