| ... | ... | @@ -24,6 +24,11 @@ function makeReadModel( |
| 24 | 24 | session: OrchestrationSession | null = null, |
| 25 | 25 | activities: OrchestrationThread["activities"] = [], |
| 26 | 26 | messages: OrchestrationThread["messages"] = [], |
| 27 | lifecycle: { |
| 28 | readonly pinnedAt?: string | null; |
| 29 | readonly snoozedUntil?: string | null; |
| 30 | readonly snoozedAt?: string | null; |
| 31 | } = {}, |
| 27 | 32 | ): OrchestrationReadModel { |
| 28 | 33 | return { |
| 29 | 34 | snapshotSequence: 0, |
| ... | ... | @@ -44,6 +49,9 @@ function makeReadModel( |
| 44 | 49 | archivedAt, |
| 45 | 50 | settledOverride, |
| 46 | 51 | settledAt: settledOverride === "settled" ? SETTLED_AT : null, |
| 52 | snoozedUntil: lifecycle.snoozedUntil ?? null, |
| 53 | snoozedAt: lifecycle.snoozedAt ?? (lifecycle.snoozedUntil != null ? SETTLED_AT : null), |
| 54 | pinnedAt: lifecycle.pinnedAt ?? null, |
| 47 | 55 | deletedAt: null, |
| 48 | 56 | messages, |
| 49 | 57 | proposedPlans: [], |
| ... | ... | @@ -69,7 +77,7 @@ function makeSession(status: OrchestrationSession["status"]): OrchestrationSessi |
| 69 | 77 | } |
| 70 | 78 | |
| 71 | 79 | it.layer(NodeServices.layer)("settled thread decider", (it) => { |
| 72 | | it.effect("settles active threads and re-emits idempotently for settled ones", () => |
| 80 | it.effect("settles awake threads without a redundant wake and re-emits idempotently", () => |
| 73 | 81 | Effect.gen(function* () { |
| 74 | 82 | const event = yield* decideOrchestrationCommand({ |
| 75 | 83 | command: { |
| ... | ... | @@ -108,6 +116,75 @@ it.layer(NodeServices.layer)("settled thread decider", (it) => { |
| 108 | 116 | }), |
| 109 | 117 | ); |
| 110 | 118 | |
| 119 | it.effect("settling a snoozed thread also wakes it", () => |
| 120 | Effect.gen(function* () { |
| 121 | const result = yield* decideOrchestrationCommand({ |
| 122 | command: { |
| 123 | type: "thread.settle", |
| 124 | commandId: CommandId.make("cmd-settle-snoozed"), |
| 125 | threadId: ThreadId.make("thread-1"), |
| 126 | }, |
| 127 | readModel: makeReadModel(null, null, null, [], [], { |
| 128 | snoozedUntil: "1970-01-02T09:00:00.000Z", |
| 129 | }), |
| 130 | }); |
| 131 | const events = Array.isArray(result) ? result : [result]; |
| 132 | expect(events.map((entry) => entry.type)).toEqual(["thread.settled", "thread.unsnoozed"]); |
| 133 | const settled = events.find((entry) => entry.type === "thread.settled"); |
| 134 | const unsnoozed = events.find((entry) => entry.type === "thread.unsnoozed"); |
| 135 | if (settled?.type === "thread.settled" && unsnoozed?.type === "thread.unsnoozed") { |
| 136 | expect(unsnoozed.payload.reason).toBe("user"); |
| 137 | expect(unsnoozed.payload.updatedAt).toBe(settled.payload.updatedAt); |
| 138 | } |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | it.effect("repeated settle repairs legacy settled and snoozed state", () => |
| 143 | Effect.gen(function* () { |
| 144 | const result = yield* decideOrchestrationCommand({ |
| 145 | command: { |
| 146 | type: "thread.settle", |
| 147 | commandId: CommandId.make("cmd-settle-snoozed-again"), |
| 148 | threadId: ThreadId.make("thread-1"), |
| 149 | }, |
| 150 | readModel: makeReadModel("settled", null, null, [], [], { |
| 151 | snoozedUntil: "1970-01-02T09:00:00.000Z", |
| 152 | }), |
| 153 | }); |
| 154 | const events = Array.isArray(result) ? result : [result]; |
| 155 | expect(events.map((entry) => entry.type)).toEqual(["thread.settled", "thread.unsnoozed"]); |
| 156 | const settled = events.find((entry) => entry.type === "thread.settled"); |
| 157 | const unsnoozed = events.find((entry) => entry.type === "thread.unsnoozed"); |
| 158 | if (settled?.type === "thread.settled" && unsnoozed?.type === "thread.unsnoozed") { |
| 159 | expect(settled.payload.settledAt).toBe(SETTLED_AT); |
| 160 | expect(settled.payload.updatedAt).toBe(NOW); |
| 161 | expect(unsnoozed.payload.updatedAt).not.toBe(NOW); |
| 162 | } |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | it.effect("settling a pinned and snoozed thread clears the pin and snooze", () => |
| 167 | Effect.gen(function* () { |
| 168 | const result = yield* decideOrchestrationCommand({ |
| 169 | command: { |
| 170 | type: "thread.settle", |
| 171 | commandId: CommandId.make("cmd-settle-pinned-snoozed"), |
| 172 | threadId: ThreadId.make("thread-1"), |
| 173 | }, |
| 174 | readModel: makeReadModel(null, null, null, [], [], { |
| 175 | pinnedAt: SETTLED_AT, |
| 176 | snoozedUntil: "1970-01-02T09:00:00.000Z", |
| 177 | }), |
| 178 | }); |
| 179 | const events = Array.isArray(result) ? result : [result]; |
| 180 | expect(events.map((entry) => entry.type)).toEqual([ |
| 181 | "thread.settled", |
| 182 | "thread.unpinned", |
| 183 | "thread.unsnoozed", |
| 184 | ]); |
| 185 | }), |
| 186 | ); |
| 187 | |
| 111 | 188 | it.effect("rejects settling a thread with a live session", () => |
| 112 | 189 | Effect.gen(function* () { |
| 113 | 190 | for (const status of ["starting", "running"] as const) { |