Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2import { CommandId, EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
3import type { QueuedThreadMessage } from "./thread-outbox-model";
4
5const state = vi.hoisted(() => ({
6 dispatching: {} as Record<string, true>,
7 held: {} as Record<string, true>,
8 draft: {
9 text: "Existing draft",
10 attachments: [] as { id: string }[],
11 freshContext: undefined as boolean | undefined,
12 },
13 confirm: vi.fn(async () => true),
14 remove: vi.fn(async () => true),
15 flush: vi.fn(async () => {}),
16}));
17vi.mock("./atom-registry", () => ({
18 appAtomRegistry: {
19 get: (atom: string) => (atom === "dispatching" ? state.dispatching : state.held),
20 },
21}));
22vi.mock("./use-thread-outbox", () => ({
23 dispatchingQueuedMessageIdsAtom: "dispatching",
24 editingQueuedMessageIdsAtom: "editing",
25 holdEditingQueuedMessage: (id: string) => {
26 state.held[id] = true;
27 },
28 releaseEditingQueuedMessage: (id: string) => {
29 delete state.held[id];
30 },
31}));
32vi.mock("./thread-outbox", () => ({
33 confirmThreadOutboxMessageQueued: state.confirm,
34 threadOutboxRevision: () => 1,
35}));
36vi.mock("./thread-outbox-removal", () => ({ removeThreadOutboxMessage: state.remove }));
37vi.mock("./use-composer-drafts", () => ({
38 waitForComposerDraftsLoaded: async () => {},
39 getComposerDraftSnapshot: () => state.draft,
40 mergeComposerDraftContent: async (_key: string, message: QueuedThreadMessage) => {
41 state.draft = {
42 ...state.draft,
43 text: `${state.draft.text}\n\n${message.text}`,
44 attachments: [...state.draft.attachments, ...message.attachments],
45 };
46 },
47 updateComposerDraftSettings: (_key: string, patch: { readonly freshContext?: boolean }) => {
48 state.draft = { ...state.draft, ...patch };
49 },
50 flushComposerDrafts: state.flush,
51 undoComposerDraftMerge: async (_key: string, snapshot: typeof state.draft) => {
52 state.draft = snapshot;
53 },
54}));
55import { editPendingThreadMessage } from "./edit-pending-thread-message";
56
57const message: QueuedThreadMessage = {
58 environmentId: EnvironmentId.make("env"),
59 threadId: ThreadId.make("thread"),
60 messageId: MessageId.make("message"),
61 commandId: CommandId.make("command"),
62 text: "Queued task",
63 freshContext: true,
64 createdAt: "2026-09-06T10:00:00.000Z",
65 attachments: [
66 {
67 id: "file",
68 type: "file",
69 name: "notes.txt",
70 mimeType: "text/plain",
71 sizeBytes: 10,
72 fileUri: "file:///notes.txt",
73 },
74 ],
75};
76beforeEach(() => {
77 vi.clearAllMocks();
78 state.dispatching = {};
79 state.held = {};
80 state.draft = { text: "Existing draft", attachments: [], freshContext: undefined };
81 state.confirm.mockResolvedValue(true);
82 state.remove.mockResolvedValue(true);
83 state.flush.mockResolvedValue(undefined);
84});
85describe("editing a pending message", () => {
86 it("locks delivery and persists text, attachments, and freshContext before removing the queued copy", async () => {
87 state.confirm.mockImplementationOnce(async () => {
88 expect(state.held[message.messageId]).toBe(true);
89 return true;
90 });
91 state.remove.mockImplementationOnce(async () => {
92 expect(state.flush).toHaveBeenCalled();
93 expect(state.draft.text).toBe("Existing draft\n\nQueued task");
94 expect(state.draft.attachments).toEqual(message.attachments);
95 expect(state.draft.freshContext).toBe(true);
96 return true;
97 });
98 expect(await editPendingThreadMessage(message)).toBe(true);
99 expect(state.held).toEqual({});
100 });
101 it("does not reclaim a message already being dispatched", async () => {
102 state.dispatching = { [message.messageId]: true };
103 expect(await editPendingThreadMessage(message)).toBe(false);
104 expect(state.confirm).not.toHaveBeenCalled();
105 expect(state.draft.text).toBe("Existing draft");
106 });
107 it("rolls back the draft if removing the queued message fails", async () => {
108 state.remove.mockRejectedValueOnce(new Error("disk error"));
109 await expect(editPendingThreadMessage(message)).rejects.toThrow("disk error");
110 expect(state.draft).toEqual({
111 text: "Existing draft",
112 attachments: [],
113 freshContext: undefined,
114 });
115 expect(state.held).toEqual({});
116 });
117 it("rolls back when a newer queue revision wins", async () => {
118 state.remove.mockResolvedValueOnce(false);
119 expect(await editPendingThreadMessage(message)).toBe(false);
120 expect(state.draft.text).toBe("Existing draft");
121 });
122});