|
1 | 1 | import { describe, expect, it, vi } from "vitest"; |
2 | | -import { shouldUseSdkCutover, sdkCutoverPersist } from "./sdkCutover"; |
| 2 | +import { shouldUseSdkCutover, sdkCutoverPersist, sdkDeletePersist } from "./sdkCutover"; |
3 | 3 | import { openComposition } from "@hyperframes/sdk"; |
4 | 4 | import { createMemoryAdapter } from "@hyperframes/sdk/adapters/memory"; |
5 | 5 | import type { PatchOperation } from "./sourcePatcher"; |
@@ -298,6 +298,74 @@ describe("sdkCutoverPersist", () => { |
298 | 298 | }); |
299 | 299 | }); |
300 | 300 |
|
| 301 | +describe("sdkDeletePersist", () => { |
| 302 | + const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val }); |
| 303 | + const makeDeps = () => ({ |
| 304 | + editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) }, |
| 305 | + writeProjectFile: vi.fn().mockResolvedValue(undefined), |
| 306 | + reloadPreview: vi.fn(), |
| 307 | + domEditSaveTimestampRef: makeRef(0), |
| 308 | + }); |
| 309 | + |
| 310 | + const makeSession = (hasEl = true) => |
| 311 | + ({ |
| 312 | + getElement: vi.fn().mockReturnValue(hasEl ? { id: "hf-abc" } : null), |
| 313 | + removeElement: vi.fn(), |
| 314 | + serialize: vi.fn().mockReturnValue("<html>after</html>"), |
| 315 | + }) as unknown as Parameters<typeof sdkDeletePersist>[3]; |
| 316 | + |
| 317 | + it("returns false when session is null", async () => { |
| 318 | + expect(await sdkDeletePersist("hf-abc", "before", "/comp.html", null, makeDeps())).toBe(false); |
| 319 | + }); |
| 320 | + |
| 321 | + it("returns false when element not found in session", async () => { |
| 322 | + const session = makeSession(false); |
| 323 | + expect(await sdkDeletePersist("hf-abc", "before", "/comp.html", session, makeDeps())).toBe( |
| 324 | + false, |
| 325 | + ); |
| 326 | + }); |
| 327 | + |
| 328 | + it("calls removeElement and writes serialized content", async () => { |
| 329 | + const deps = makeDeps(); |
| 330 | + const session = makeSession(true); |
| 331 | + const result = await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps); |
| 332 | + expect(result).toBe(true); |
| 333 | + expect(session!.removeElement).toHaveBeenCalledWith("hf-abc"); |
| 334 | + expect(deps.writeProjectFile).toHaveBeenCalledWith("/comp.html", "<html>after</html>"); |
| 335 | + }); |
| 336 | + |
| 337 | + it("records edit history with before/after diff", async () => { |
| 338 | + const deps = makeDeps(); |
| 339 | + const session = makeSession(true); |
| 340 | + await sdkDeletePersist("hf-abc", "before-content", "/comp.html", session, deps); |
| 341 | + expect(deps.editHistory.recordEdit).toHaveBeenCalledWith( |
| 342 | + expect.objectContaining({ |
| 343 | + label: "Delete element", |
| 344 | + files: { "/comp.html": { before: "before-content", after: "<html>after</html>" } }, |
| 345 | + }), |
| 346 | + ); |
| 347 | + }); |
| 348 | + |
| 349 | + it("calls reloadPreview on success", async () => { |
| 350 | + const deps = makeDeps(); |
| 351 | + const session = makeSession(true); |
| 352 | + await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps); |
| 353 | + expect(deps.reloadPreview).toHaveBeenCalled(); |
| 354 | + }); |
| 355 | + |
| 356 | + it("returns false and does not write on removeElement error", async () => { |
| 357 | + const deps = makeDeps(); |
| 358 | + const session = makeSession(true); |
| 359 | + (session!.removeElement as ReturnType<typeof vi.fn>).mockImplementation(() => { |
| 360 | + throw new Error("remove failed"); |
| 361 | + }); |
| 362 | + const result = await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps); |
| 363 | + expect(result).toBe(false); |
| 364 | + expect(deps.writeProjectFile).not.toHaveBeenCalled(); |
| 365 | + expect(deps.reloadPreview).not.toHaveBeenCalled(); |
| 366 | + }); |
| 367 | +}); |
| 368 | + |
301 | 369 | describe("sdkCutoverPersist — GSAP script preservation (integration)", () => { |
302 | 370 | const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val }); |
303 | 371 | const makeDeps = () => ({ |
|
0 commit comments