Open History to see the last commit
1import {
2 CommandId,
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 type OrchestrationReadModel,
7} from "@t3tools/contracts";
8import * as NodeServices from "@effect/platform-node/NodeServices";
9import { expect, it } from "@effect/vitest";
10import * as Effect from "effect/Effect";
11
12import { decideOrchestrationCommand } from "./decider.ts";
13
14const NOW = "2026-01-01T00:00:00.000Z";
15const DISABLED_AT = "2025-12-30T00:00:00.000Z";
16
17function makeReadModel(input: {
18 readonly autoSettleDisabledAt?: string | null;
19 readonly settledOverride?: "settled" | "active" | null;
20}): OrchestrationReadModel {
21 return {
22 snapshotSequence: 0,
23 projects: [],
24 threads: [
25 {
26 id: ThreadId.make("thread-1"),
27 projectId: ProjectId.make("project-1"),
28 title: "Thread",
29 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
30 runtimeMode: "full-access",
31 interactionMode: "default",
32 branch: null,
33 worktreePath: null,
34 pullRequests: [],
35 latestTurn: null,
36 createdAt: NOW,
37 updatedAt: NOW,
38 archivedAt: null,
39 settledOverride: input.settledOverride ?? null,
40 settledAt: input.settledOverride === "settled" ? NOW : null,
41 autoSettleDisabledAt: input.autoSettleDisabledAt ?? null,
42 deletedAt: null,
43 messages: [],
44 proposedPlans: [],
45 activities: [],
46 checkpoints: [],
47 session: null,
48 },
49 ],
50 updatedAt: NOW,
51 };
52}
53
54const events = (event: Effect.Success<ReturnType<typeof decideOrchestrationCommand>>) =>
55 Array.isArray(event) ? event : [event];
56
57it.layer(NodeServices.layer)("thread.auto-settle.set decider", (it) => {
58 it.effect("turning auto-settle off stamps autoSettleDisabledAt and updatedAt together", () =>
59 Effect.gen(function* () {
60 const [event] = events(
61 yield* decideOrchestrationCommand({
62 command: {
63 type: "thread.auto-settle.set",
64 commandId: CommandId.make("cmd-off"),
65 threadId: ThreadId.make("thread-1"),
66 enabled: false,
67 },
68 readModel: makeReadModel({}),
69 }),
70 );
71 expect(event?.type).toBe("thread.auto-settle-set");
72 if (event?.type === "thread.auto-settle-set") {
73 expect(event.payload.autoSettleDisabledAt).toBe(event.payload.updatedAt);
74 expect(event.payload.updatedAt).not.toBe(NOW);
75 }
76 }),
77 );
78
79 it.effect("turning it off again keeps the original stamp and updatedAt", () =>
80 Effect.gen(function* () {
81 const [event] = events(
82 yield* decideOrchestrationCommand({
83 command: {
84 type: "thread.auto-settle.set",
85 commandId: CommandId.make("cmd-off-again"),
86 threadId: ThreadId.make("thread-1"),
87 enabled: false,
88 },
89 readModel: makeReadModel({ autoSettleDisabledAt: DISABLED_AT }),
90 }),
91 );
92 expect(event?.type).toBe("thread.auto-settle-set");
93 if (event?.type === "thread.auto-settle-set") {
94 expect(event.payload.autoSettleDisabledAt).toBe(DISABLED_AT);
95 expect(event.payload.updatedAt).toBe(NOW);
96 }
97 }),
98 );
99
100 it.effect("turning auto-settle back on clears the stamp", () =>
101 Effect.gen(function* () {
102 const [event] = events(
103 yield* decideOrchestrationCommand({
104 command: {
105 type: "thread.auto-settle.set",
106 commandId: CommandId.make("cmd-on"),
107 threadId: ThreadId.make("thread-1"),
108 enabled: true,
109 },
110 readModel: makeReadModel({ autoSettleDisabledAt: DISABLED_AT }),
111 }),
112 );
113 expect(event?.type).toBe("thread.auto-settle-set");
114 if (event?.type === "thread.auto-settle-set") {
115 expect(event.payload.autoSettleDisabledAt).toBeNull();
116 expect(event.payload.updatedAt).not.toBe(NOW);
117 }
118 }),
119 );
120
121 it.effect("automatic settlement is rejected while auto-settle is off", () =>
122 Effect.gen(function* () {
123 const result = yield* Effect.exit(
124 decideOrchestrationCommand({
125 command: {
126 type: "thread.auto-settle",
127 commandId: CommandId.make("cmd-auto"),
128 threadId: ThreadId.make("thread-1"),
129 snapshotSequence: 0,
130 settledAt: NOW,
131 },
132 readModel: makeReadModel({ autoSettleDisabledAt: DISABLED_AT }),
133 }),
134 );
135 expect(result._tag).toBe("Failure");
136 }),
137 );
138
139 it.effect("a manual settle still works while auto-settle is off", () =>
140 Effect.gen(function* () {
141 const [event] = events(
142 yield* decideOrchestrationCommand({
143 command: {
144 type: "thread.settle",
145 commandId: CommandId.make("cmd-manual"),
146 threadId: ThreadId.make("thread-1"),
147 },
148 readModel: makeReadModel({ autoSettleDisabledAt: DISABLED_AT }),
149 }),
150 );
151 expect(event?.type).toBe("thread.settled");
152 }),
153 );
154});