|
1 | 1 | import { beforeEach, describe, expect, it } from "vitest"; |
2 | | -import { PendingCloudRecoveryState } from "./cloud-recovery-state.ts"; |
3 | | -import { readCloudSessionRecovery } from "./cloud-recovery.ts"; |
| 2 | +import { |
| 3 | + PendingCloudRecoveryState, |
| 4 | + resolveSubmissionOutcomeReason, |
| 5 | +} from "./cloud-recovery-state.ts"; |
| 6 | +import { readCloudSessionRecovery, writeCloudSessionRecovery } from "./cloud-recovery.ts"; |
4 | 7 |
|
5 | 8 | describe("pending cloud recovery state", () => { |
6 | 9 | beforeEach(() => sessionStorage.clear()); |
7 | 10 |
|
| 11 | + it.each([ |
| 12 | + { |
| 13 | + name: "a replacement Gateway", |
| 14 | + gatewayIdentityChanged: true, |
| 15 | + cloudDraftOwned: true, |
| 16 | + expected: "gateway-changed", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "a normal local submission", |
| 20 | + gatewayIdentityChanged: false, |
| 21 | + cloudDraftOwned: false, |
| 22 | + expected: "gateway-changed", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "an interrupted cloud draft", |
| 26 | + gatewayIdentityChanged: false, |
| 27 | + cloudDraftOwned: true, |
| 28 | + expected: "cloud-interrupted", |
| 29 | + }, |
| 30 | + ])("classifies $name accurately", ({ expected, gatewayIdentityChanged, cloudDraftOwned }) => { |
| 31 | + expect(resolveSubmissionOutcomeReason({ gatewayIdentityChanged, cloudDraftOwned })).toBe( |
| 32 | + expected, |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
8 | 36 | it("stages an idempotent create before the Gateway request", () => { |
9 | 37 | const pending = new PendingCloudRecoveryState(); |
10 | 38 | const createParams = pending.stageCreate({ |
@@ -130,4 +158,35 @@ describe("pending cloud recovery state", () => { |
130 | 158 | expect(captured?.attachments).not.toBe(pending.attachments); |
131 | 159 | expect(captured?.createParams).not.toBe(pending.createParams); |
132 | 160 | }); |
| 161 | + |
| 162 | + it("neutralizes a stale local owner without clearing newer durable recovery", () => { |
| 163 | + const pending = new PendingCloudRecoveryState(); |
| 164 | + expect( |
| 165 | + pending.stageCreate({ |
| 166 | + agentId: "cloud", |
| 167 | + profileId: "aws", |
| 168 | + message: "stale task", |
| 169 | + gatewayUrl: "ws://gateway.example", |
| 170 | + recoveryScope: "principal-a", |
| 171 | + createParams: { agentId: "cloud", message: "", worktree: true }, |
| 172 | + }), |
| 173 | + ).not.toBeNull(); |
| 174 | + const staleKey = pending.sessionKey; |
| 175 | + const newerRecovery = { |
| 176 | + sessionKey: "agent:cloud:newer", |
| 177 | + messageId: "message-newer", |
| 178 | + message: "newer task", |
| 179 | + profileId: "aws", |
| 180 | + agentId: "cloud", |
| 181 | + gatewayUrl: "ws://gateway.example", |
| 182 | + recoveryScope: "principal-a", |
| 183 | + phase: "dispatching" as const, |
| 184 | + }; |
| 185 | + expect(writeCloudSessionRecovery(newerRecovery)).toBe(true); |
| 186 | + |
| 187 | + pending.clearFor("ws://gateway.example", "principal-a", staleKey); |
| 188 | + |
| 189 | + expect(pending.sessionKey).toBe(""); |
| 190 | + expect(readCloudSessionRecovery("ws://gateway.example", "principal-a")).toEqual(newerRecovery); |
| 191 | + }); |
133 | 192 | }); |
0 commit comments