|
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | +import { |
| 3 | + CommandId, |
| 4 | + EnvironmentId, |
| 5 | + MessageId, |
| 6 | + ProviderInstanceId, |
| 7 | + ThreadId, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | + |
| 10 | +import { recoverPendingSendToComposer } from "./thread-outbox-recovery"; |
| 11 | +import type { QueuedThreadMessage } from "./thread-outbox-model"; |
| 12 | + |
| 13 | +function heldMessage(): QueuedThreadMessage { |
| 14 | + return { |
| 15 | + environmentId: EnvironmentId.make("environment-1"), |
| 16 | + threadId: ThreadId.make("thread-1"), |
| 17 | + messageId: MessageId.make("message-held"), |
| 18 | + commandId: CommandId.make("command-held"), |
| 19 | + text: "exact held text", |
| 20 | + attachments: [ |
| 21 | + { |
| 22 | + id: "held-image", |
| 23 | + previewUri: "file:///held.png", |
| 24 | + type: "image", |
| 25 | + name: "held.png", |
| 26 | + mimeType: "image/png", |
| 27 | + sizeBytes: 12, |
| 28 | + dataUrl: "data:image/png;base64,AQ==", |
| 29 | + }, |
| 30 | + ], |
| 31 | + modelSelection: { |
| 32 | + instanceId: ProviderInstanceId.make("codex_personal"), |
| 33 | + model: "gpt-5.4", |
| 34 | + options: [{ id: "reasoningEffort", value: "xhigh" }], |
| 35 | + }, |
| 36 | + runtimeMode: "approval-required", |
| 37 | + interactionMode: "plan", |
| 38 | + deliveryHold: { |
| 39 | + kind: "provider-binding-mismatch", |
| 40 | + reason: "Choose a destination.", |
| 41 | + }, |
| 42 | + createdAt: "2026-09-01T00:00:00.000Z", |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +describe("pending send composer recovery", () => { |
| 47 | + it("durably flushes the exact payload before CAS-removing the queue item", async () => { |
| 48 | + const message = heldMessage(); |
| 49 | + const order: string[] = []; |
| 50 | + const restore = vi.fn((draftKey: string, snapshot: unknown) => { |
| 51 | + order.push(`restore:${draftKey}`); |
| 52 | + expect(snapshot).toEqual({ |
| 53 | + text: message.text, |
| 54 | + attachments: message.attachments, |
| 55 | + modelSelection: message.modelSelection, |
| 56 | + runtimeMode: message.runtimeMode, |
| 57 | + interactionMode: message.interactionMode, |
| 58 | + }); |
| 59 | + }); |
| 60 | + const flushDraft = vi.fn(async () => { |
| 61 | + order.push("flush"); |
| 62 | + }); |
| 63 | + const removeIfCurrent = vi.fn(async (candidate: QueuedThreadMessage) => { |
| 64 | + order.push("remove"); |
| 65 | + expect(candidate).toBe(message); |
| 66 | + return true; |
| 67 | + }); |
| 68 | + |
| 69 | + await expect( |
| 70 | + recoverPendingSendToComposer( |
| 71 | + { message, draftKey: "environment-1:thread-1" }, |
| 72 | + { restore, flushDraft, removeIfCurrent }, |
| 73 | + ), |
| 74 | + ).resolves.toBe("removed"); |
| 75 | + expect(order).toEqual(["restore:environment-1:thread-1", "flush", "remove"]); |
| 76 | + }); |
| 77 | + |
| 78 | + it("keeps the queue item when the durable draft flush fails", async () => { |
| 79 | + const message = heldMessage(); |
| 80 | + const flushError = new Error("draft disk full"); |
| 81 | + const removeIfCurrent = vi.fn(async () => true); |
| 82 | + |
| 83 | + await expect( |
| 84 | + recoverPendingSendToComposer( |
| 85 | + { message, draftKey: "environment-1:thread-1" }, |
| 86 | + { |
| 87 | + restore: () => {}, |
| 88 | + flushDraft: async () => { |
| 89 | + throw flushError; |
| 90 | + }, |
| 91 | + removeIfCurrent, |
| 92 | + }, |
| 93 | + ), |
| 94 | + ).rejects.toBe(flushError); |
| 95 | + expect(removeIfCurrent).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("reports a concurrent queue CAS loss without deleting the newer item", async () => { |
| 99 | + const message = heldMessage(); |
| 100 | + const removeIfCurrent = vi.fn(async () => false); |
| 101 | + |
| 102 | + await expect( |
| 103 | + recoverPendingSendToComposer( |
| 104 | + { message, draftKey: "environment-1:thread-1" }, |
| 105 | + { restore: () => {}, flushDraft: async () => {}, removeIfCurrent }, |
| 106 | + ), |
| 107 | + ).resolves.toBe("queue-changed"); |
| 108 | + expect(removeIfCurrent).toHaveBeenCalledWith(message); |
| 109 | + }); |
| 110 | +}); |
0 commit comments