@@ -3,7 +3,7 @@ import { describe, expect, it, vi } from "vite-plus/test"; |
3 | 3 |
|
4 | 4 | import type { DraftComposerAttachment } from "../../lib/composerImages"; |
5 | 5 | import type { IncomingShareDraft } from "../sharing/incoming-share-model"; |
6 | | -import type { ComposerDraft } from "../../state/use-composer-drafts"; |
| 6 | +import { undoComposerDraftMergeState, type ComposerDraft } from "../../state/use-composer-drafts"; |
7 | 7 | import { |
8 | 8 | makeIncomingShareImports, |
9 | 9 | type ShareImportDeps, |
@@ -13,7 +13,34 @@ import { |
13 | 13 | } from "./incomingShareImport"; |
14 | 14 |
|
15 | 15 | const destination = { environmentId: "env-1", projectId: "project-1" }; |
16 | | -const edited: ComposerDraft = { text: "typed after the restore", attachments: [] }; |
| 16 | +const edited: ComposerDraft = { text: "typed after the undo", attachments: [] }; |
| 17 | + |
| 18 | +/** The draft store's own undo, applied to the test's draft map. */ |
| 19 | +function undoInto( |
| 20 | + drafts: Map<string, ComposerDraft>, |
| 21 | + draftKey: string, |
| 22 | + snapshot: ComposerDraft, |
| 23 | + merged: ComposerDraft, |
| 24 | +): void { |
| 25 | + const next = undoComposerDraftMergeState(Object.fromEntries(drafts), draftKey, snapshot, merged); |
| 26 | + drafts.clear(); |
| 27 | + for (const [key, draft] of Object.entries(next)) drafts.set(key, draft); |
| 28 | +} |
| 29 | + |
| 30 | +function mergeInto( |
| 31 | + drafts: Map<string, ComposerDraft>, |
| 32 | + draftKey: string, |
| 33 | + content: { |
| 34 | + readonly text: string; |
| 35 | + readonly attachments: ReadonlyArray<DraftComposerAttachment>; |
| 36 | + }, |
| 37 | +): void { |
| 38 | + const existing = drafts.get(draftKey) ?? { text: "", attachments: [] }; |
| 39 | + drafts.set(draftKey, { |
| 40 | + text: `${existing.text}\n${content.text}`, |
| 41 | + attachments: [...existing.attachments, ...content.attachments], |
| 42 | + }); |
| 43 | +} |
17 | 44 |
|
18 | 45 | function fileAttachment(id: string): DraftComposerAttachment { |
19 | 46 | return { |
@@ -66,17 +93,13 @@ function setup(overrides: Partial<ShareImportDeps> = {}) { |
66 | 93 | calls.push("consume"); |
67 | 94 | }), |
68 | 95 | readDraft: (draftKey) => drafts.get(draftKey) ?? { text: "", attachments: [] }, |
69 | | - restoreDraft: vi.fn(async (draftKey, snapshot) => { |
70 | | - calls.push("restore"); |
71 | | - drafts.set(draftKey, snapshot); |
| 96 | + undoMerge: vi.fn(async (draftKey, snapshot, merged) => { |
| 97 | + calls.push("undo"); |
| 98 | + undoInto(drafts, draftKey, snapshot, merged); |
72 | 99 | }), |
73 | 100 | mergeIntoDraft: vi.fn(async (draftKey, content) => { |
74 | 101 | calls.push("merge"); |
75 | | - const existing = drafts.get(draftKey) ?? { text: "", attachments: [] }; |
76 | | - drafts.set(draftKey, { |
77 | | - text: `${existing.text}\n${content.text}`, |
78 | | - attachments: [...existing.attachments, ...content.attachments], |
79 | | - }); |
| 102 | + mergeInto(drafts, draftKey, content); |
80 | 103 | return { skippedAttachmentCount: 0 }; |
81 | 104 | }), |
82 | 105 | selectAttachments: (incoming) => ({ |
@@ -170,7 +193,7 @@ describe("incoming share imports", () => { |
170 | 193 | await imports.cancel(target); |
171 | 194 |
|
172 | 195 | expect(watched.phase()).toBe("cancelled"); |
173 | | - expect(calls).toEqual(["reserve", "merge", "restore", "release"]); |
| 196 | + expect(calls).toEqual(["reserve", "merge", "undo", "release"]); |
174 | 197 | expect(drafts.get("draft-a")).toEqual({ text: "mine", attachments: [] }); |
175 | 198 | // Unconsumed: the inbox still owns the files. |
176 | 199 | expect(deps.releaseUnusedAttachments).not.toHaveBeenCalled(); |
@@ -200,7 +223,7 @@ describe("incoming share imports", () => { |
200 | 223 |
|
201 | 224 | await imports.cancel(target); |
202 | 225 | expect(watched.phase()).toBe("cancelled"); |
203 | | - expect(deps.restoreDraft).toHaveBeenCalledTimes(1); |
| 226 | + expect(deps.undoMerge).toHaveBeenCalledTimes(1); |
204 | 227 | expect(drafts.get("draft-a")).toEqual({ text: "mine", attachments: [] }); |
205 | 228 | expect(deps.leave).toHaveBeenCalledTimes(1); |
206 | 229 | }); |
@@ -227,23 +250,27 @@ describe("incoming share imports", () => { |
227 | 250 | watched.unmount(); |
228 | 251 | }); |
229 | 252 |
|
230 | | - it.each([ |
231 | | - ["edited", (drafts: Map<string, ComposerDraft>) => drafts.set("draft-a", edited)], |
232 | | - ["deleted", (drafts: Map<string, ComposerDraft>) => drafts.delete("draft-a")], |
233 | | - ] as const)( |
234 | | - "a retried cancel keeps a draft the user %s after the restore was shown", |
235 | | - async (_change, change) => { |
236 | | - let restoreAttempts = 0; |
| 253 | + const userChanges = [ |
| 254 | + ["edited", (drafts: Map<string, ComposerDraft>) => drafts.set("draft-a", edited), edited], |
| 255 | + ["deleted", (drafts: Map<string, ComposerDraft>) => drafts.delete("draft-a"), undefined], |
| 256 | + ] as const; |
| 257 | + |
| 258 | + it.each(userChanges)( |
| 259 | + "a retried cancel keeps a draft the user %s after the undo was shown", |
| 260 | + async (_change, change, expected) => { |
| 261 | + let undoAttempts = 0; |
237 | 262 | const { deps, drafts, watch, imports } = setup({ |
238 | 263 | consume: vi.fn(async () => { |
239 | 264 | throw new Error("disk full"); |
240 | 265 | }), |
241 | | - // Like restoreComposerDraftSnapshot: publishes, then fails to persist. |
242 | | - restoreDraft: vi.fn(async (draftKey: string, snapshot: ComposerDraft) => { |
243 | | - restoreAttempts += 1; |
244 | | - drafts.set(draftKey, snapshot); |
245 | | - if (restoreAttempts === 1) throw new Error("write failed"); |
246 | | - }), |
| 266 | + // Like undoComposerDraftMerge: publishes, then fails to persist. |
| 267 | + undoMerge: vi.fn( |
| 268 | + async (draftKey: string, snapshot: ComposerDraft, merged: ComposerDraft) => { |
| 269 | + undoAttempts += 1; |
| 270 | + undoInto(drafts, draftKey, snapshot, merged); |
| 271 | + if (undoAttempts === 1) throw new Error("write failed"); |
| 272 | + }, |
| 273 | + ), |
247 | 274 | }); |
248 | 275 | const target = importTarget(); |
249 | 276 | const watched = watch(target); |
@@ -252,15 +279,75 @@ describe("incoming share imports", () => { |
252 | 279 | expect(deps.onCancelFailure).toHaveBeenCalledWith(target, new Error("write failed")); |
253 | 280 |
|
254 | 281 | change(drafts); |
255 | | - const expected = drafts.get("draft-a"); |
256 | 282 | await imports.cancel(target); |
257 | 283 |
|
258 | 284 | expect(watched.phase()).toBe("cancelled"); |
259 | | - expect(drafts.get("draft-a")).toBe(expected); |
| 285 | + expect(drafts.get("draft-a")).toEqual(expected); |
260 | 286 | expect(deps.release).toHaveBeenCalledTimes(1); |
261 | 287 | }, |
262 | 288 | ); |
263 | 289 |
|
| 290 | + it("cancel undoes the import after an upload finished on its attachment", async () => { |
| 291 | + const file = fileAttachment("file"); |
| 292 | + const { drafts, inbox, watch, imports } = setup({ |
| 293 | + consume: vi.fn(async () => { |
| 294 | + throw new Error("disk full"); |
| 295 | + }), |
| 296 | + }); |
| 297 | + inbox.set("share-1", share([file])); |
| 298 | + const target = importTarget(); |
| 299 | + watch(target); |
| 300 | + await imports.settled(target); |
| 301 | + |
| 302 | + const merged = drafts.get("draft-a"); |
| 303 | + drafts.set("draft-a", { |
| 304 | + text: merged?.text ?? "", |
| 305 | + attachments: [{ ...file, uploadedAttachmentId: "uploaded-1" }], |
| 306 | + }); |
| 307 | + await imports.cancel(target); |
| 308 | + |
| 309 | + expect(drafts.get("draft-a")).toEqual({ text: "mine", attachments: [] }); |
| 310 | + }); |
| 311 | + |
| 312 | + it.each(userChanges)( |
| 313 | + "cancel keeps a draft the user %s after a merge that published but failed to persist", |
| 314 | + async (_change, change, expected) => { |
| 315 | + const { drafts, watch, imports } = setup({ |
| 316 | + // Like mergeComposerDraftContent: publishes, then fails to persist. |
| 317 | + mergeIntoDraft: vi.fn(async (draftKey, content) => { |
| 318 | + mergeInto(drafts, draftKey, content); |
| 319 | + throw new Error("write failed"); |
| 320 | + }), |
| 321 | + }); |
| 322 | + const target = importTarget(); |
| 323 | + const watched = watch(target); |
| 324 | + await imports.settled(target); |
| 325 | + expect(watched.phase()).toBe("failed"); |
| 326 | + |
| 327 | + change(drafts); |
| 328 | + await imports.cancel(target); |
| 329 | + |
| 330 | + expect(watched.phase()).toBe("cancelled"); |
| 331 | + expect(drafts.get("draft-a")).toEqual(expected); |
| 332 | + }, |
| 333 | + ); |
| 334 | + |
| 335 | + it("cancel after a merge that published but failed to persist removes the merged share", async () => { |
| 336 | + const { drafts, watch, imports } = setup({ |
| 337 | + mergeIntoDraft: vi.fn(async (draftKey, content) => { |
| 338 | + mergeInto(drafts, draftKey, content); |
| 339 | + throw new Error("write failed"); |
| 340 | + }), |
| 341 | + }); |
| 342 | + const target = importTarget(); |
| 343 | + watch(target); |
| 344 | + await imports.settled(target); |
| 345 | + |
| 346 | + await imports.cancel(target); |
| 347 | + |
| 348 | + expect(drafts.get("draft-a")).toEqual({ text: "mine", attachments: [] }); |
| 349 | + }); |
| 350 | + |
264 | 351 | it("a failed import keeps its cancel after its route left, and reports a failed undo", async () => { |
265 | 352 | const { calls, deps, imports, watch } = setup({ |
266 | 353 | consume: vi.fn(async () => { |
@@ -279,7 +366,7 @@ describe("incoming share imports", () => { |
279 | 366 |
|
280 | 367 | await imports.cancel(target); |
281 | 368 |
|
282 | | - expect(calls).toEqual(["reserve", "merge", "restore"]); |
| 369 | + expect(calls).toEqual(["reserve", "merge", "undo"]); |
283 | 370 | expect(deps.onCancelFailure).toHaveBeenCalledWith(target, new Error("locked")); |
284 | 371 | expect(deps.leave).not.toHaveBeenCalled(); |
285 | 372 | }); |
|