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

decider.workflowTaskStop.test.ts

121 lines3.8 KB

1import {
2 CommandId,
3 DEFAULT_PROVIDER_INTERACTION_MODE,
4 EventId,
5 ProjectId,
6 ThreadId,
7 ProviderInstanceId,
8} from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import * as NodeServices from "@effect/platform-node/NodeServices";
11import { expect, it } from "@effect/vitest";
12​
13import { decideOrchestrationCommand } from "./decider.ts";
14import { createEmptyReadModel, projectEvent } from "./projector.ts";
15​
16const asCommandId = (value: string): CommandId => CommandId.make(value);
17const asEventId = (value: string): EventId => EventId.make(value);
18const asProjectId = (value: string): ProjectId => ProjectId.make(value);
19const asThreadId = (value: string): ThreadId => ThreadId.make(value);
20​
21const now = "2026-01-01T00:00:00.000Z";
22​
23const seedReadModel = Effect.gen(function* () {
24 const initial = createEmptyReadModel(now);
25 const withProject = yield* projectEvent(initial, {
26 sequence: 1,
27 eventId: asEventId("evt-project-create"),
28 aggregateKind: "project",
29 aggregateId: asProjectId("project-workflow"),
30 type: "project.created",
31 occurredAt: now,
32 commandId: asCommandId("cmd-project-create"),
33 causationEventId: null,
34 correlationId: asCommandId("cmd-project-create"),
35 metadata: {},
36 payload: {
37 projectId: asProjectId("project-workflow"),
38 title: "Project Workflow",
39 workspaceRoot: "/tmp/project-workflow",
40 defaultModelSelection: null,
41 scripts: [],
42 createdAt: now,
43 updatedAt: now,
44 },
45 });
46​
47 return yield* projectEvent(withProject, {
48 sequence: 2,
49 eventId: asEventId("evt-thread-create"),
50 aggregateKind: "thread",
51 aggregateId: asThreadId("thread-workflow"),
52 type: "thread.created",
53 occurredAt: now,
54 commandId: asCommandId("cmd-thread-create"),
55 causationEventId: null,
56 correlationId: asCommandId("cmd-thread-create"),
57 metadata: {},
58 payload: {
59 threadId: asThreadId("thread-workflow"),
60 projectId: asProjectId("project-workflow"),
61 title: "Thread Workflow",
62 modelSelection: {
63 instanceId: ProviderInstanceId.make("codex"),
64 model: "gpt-5-codex",
65 },
66 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
67 runtimeMode: "approval-required",
68 branch: null,
69 worktreePath: null,
70 createdAt: now,
71 updatedAt: now,
72 },
73 });
74});
75​
76it.layer(NodeServices.layer)("decider thread.task.stop", (it) => {
77 it.effect("emits a thread.task-stop-requested event carrying the task id", () =>
78 Effect.gen(function* () {
79 const readModel = yield* seedReadModel;
80 const result = yield* decideOrchestrationCommand({
81 command: {
82 type: "thread.task.stop",
83 commandId: asCommandId("cmd-task-stop"),
84 threadId: asThreadId("thread-workflow"),
85 taskId: "task-9",
86 createdAt: now,
87 },
88 readModel,
89 });
90 const events = Array.isArray(result) ? result : [result];
91 expect(events).toHaveLength(1);
92 const event = events[0]!;
93 expect(event.type).toBe("thread.task-stop-requested");
94 expect(event.payload).toMatchObject({
95 threadId: asThreadId("thread-workflow"),
96 taskId: "task-9",
97 createdAt: now,
98 });
99 }),
100 );
101​
102 it.effect("rejects a task stop for a thread that does not exist", () =>
103 Effect.gen(function* () {
104 const readModel = yield* seedReadModel;
105 const error = yield* Effect.flip(
106 decideOrchestrationCommand({
107 command: {
108 type: "thread.task.stop",
109 commandId: asCommandId("cmd-task-stop-missing"),
110 threadId: asThreadId("thread-missing"),
111 taskId: "task-9",
112 createdAt: now,
113 },
114 readModel,
115 }),
116 );
117 expect(error.message).toContain("thread-missing");
118 expect(error.message).toContain("does not exist");
119 }),
120 );
121});