|
1 | 1 | import { describe, expect, it, vi } from "vitest"; |
2 | | -import { shouldUseSdkCutover, sdkCutoverPersist } from "./sdkCutover"; |
| 2 | +import { shouldUseSdkCutover, sdkCutoverPersist, sdkDeletePersist } from "./sdkCutover"; |
| 3 | +import { openComposition } from "@hyperframes/sdk"; |
| 4 | +import { createMemoryAdapter } from "@hyperframes/sdk/adapters/memory"; |
3 | 5 | import type { PatchOperation } from "./sourcePatcher"; |
4 | 6 | import type { MutableRefObject } from "react"; |
5 | 7 |
|
@@ -154,4 +156,148 @@ describe("sdkCutoverPersist", () => { |
154 | 156 | expect(result).toBe(false); |
155 | 157 | expect(deps.reloadPreview).not.toHaveBeenCalled(); |
156 | 158 | }); |
| 159 | + |
| 160 | + it("wraps all dispatches in session.batch() for atomic rollback", async () => { |
| 161 | + const deps = makeDeps(); |
| 162 | + const session = makeSession(true); |
| 163 | + const sel = { hfId: "hf-abc" } as never; |
| 164 | + await sdkCutoverPersist( |
| 165 | + sel, |
| 166 | + [styleOp("color", "red"), styleOp("opacity", "0.5")], |
| 167 | + "before", |
| 168 | + "/comp.html", |
| 169 | + session, |
| 170 | + deps, |
| 171 | + ); |
| 172 | + expect( |
| 173 | + (session as unknown as { batch: ReturnType<typeof vi.fn> }).batch, |
| 174 | + ).toHaveBeenCalledOnce(); |
| 175 | + }); |
| 176 | + |
| 177 | + it("returns false when second dispatch throws (batch prevents partial mutation)", async () => { |
| 178 | + // inline-style ops coalesce into one setStyle dispatch; use style+text to produce two dispatches. |
| 179 | + const deps = makeDeps(); |
| 180 | + const session = makeSession(true); |
| 181 | + let callCount = 0; |
| 182 | + (session!.dispatch as ReturnType<typeof vi.fn>).mockImplementation(() => { |
| 183 | + callCount++; |
| 184 | + if (callCount === 2) throw new Error("2nd op failed"); |
| 185 | + }); |
| 186 | + const sel = { hfId: "hf-abc" } as never; |
| 187 | + const result = await sdkCutoverPersist( |
| 188 | + sel, |
| 189 | + [styleOp("color", "red"), textOp("hello")], |
| 190 | + "before", |
| 191 | + "/comp.html", |
| 192 | + session, |
| 193 | + deps, |
| 194 | + ); |
| 195 | + expect(result).toBe(false); |
| 196 | + expect(deps.writeProjectFile).not.toHaveBeenCalled(); |
| 197 | + expect(deps.reloadPreview).not.toHaveBeenCalled(); |
| 198 | + }); |
| 199 | +}); |
| 200 | + |
| 201 | +describe("sdkDeletePersist", () => { |
| 202 | + const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val }); |
| 203 | + const makeDeps = () => ({ |
| 204 | + editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) }, |
| 205 | + writeProjectFile: vi.fn().mockResolvedValue(undefined), |
| 206 | + reloadPreview: vi.fn(), |
| 207 | + domEditSaveTimestampRef: makeRef(0), |
| 208 | + }); |
| 209 | + |
| 210 | + const makeSession = (hasEl = true) => |
| 211 | + ({ |
| 212 | + getElement: vi.fn().mockReturnValue(hasEl ? { id: "hf-abc" } : null), |
| 213 | + removeElement: vi.fn(), |
| 214 | + serialize: vi.fn().mockReturnValue("<html>after</html>"), |
| 215 | + }) as unknown as Parameters<typeof sdkDeletePersist>[3]; |
| 216 | + |
| 217 | + it("returns false when session is null", async () => { |
| 218 | + expect(await sdkDeletePersist("hf-abc", "before", "/comp.html", null, makeDeps())).toBe(false); |
| 219 | + }); |
| 220 | + |
| 221 | + it("returns false when element not found in session", async () => { |
| 222 | + const session = makeSession(false); |
| 223 | + expect(await sdkDeletePersist("hf-abc", "before", "/comp.html", session, makeDeps())).toBe( |
| 224 | + false, |
| 225 | + ); |
| 226 | + }); |
| 227 | + |
| 228 | + it("calls removeElement and writes serialized content", async () => { |
| 229 | + const deps = makeDeps(); |
| 230 | + const session = makeSession(true); |
| 231 | + const result = await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps); |
| 232 | + expect(result).toBe(true); |
| 233 | + expect(session!.removeElement).toHaveBeenCalledWith("hf-abc"); |
| 234 | + expect(deps.writeProjectFile).toHaveBeenCalledWith("/comp.html", "<html>after</html>"); |
| 235 | + }); |
| 236 | + |
| 237 | + it("records edit history with before/after diff", async () => { |
| 238 | + const deps = makeDeps(); |
| 239 | + const session = makeSession(true); |
| 240 | + await sdkDeletePersist("hf-abc", "before-content", "/comp.html", session, deps); |
| 241 | + expect(deps.editHistory.recordEdit).toHaveBeenCalledWith( |
| 242 | + expect.objectContaining({ |
| 243 | + label: "Delete element", |
| 244 | + files: { "/comp.html": { before: "before-content", after: "<html>after</html>" } }, |
| 245 | + }), |
| 246 | + ); |
| 247 | + }); |
| 248 | + |
| 249 | + it("calls reloadPreview on success", async () => { |
| 250 | + const deps = makeDeps(); |
| 251 | + const session = makeSession(true); |
| 252 | + await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps); |
| 253 | + expect(deps.reloadPreview).toHaveBeenCalled(); |
| 254 | + }); |
| 255 | + |
| 256 | + it("returns false and does not write on removeElement error", async () => { |
| 257 | + const deps = makeDeps(); |
| 258 | + const session = makeSession(true); |
| 259 | + (session!.removeElement as ReturnType<typeof vi.fn>).mockImplementation(() => { |
| 260 | + throw new Error("remove failed"); |
| 261 | + }); |
| 262 | + const result = await sdkDeletePersist("hf-abc", "before", "/comp.html", session, deps); |
| 263 | + expect(result).toBe(false); |
| 264 | + expect(deps.writeProjectFile).not.toHaveBeenCalled(); |
| 265 | + expect(deps.reloadPreview).not.toHaveBeenCalled(); |
| 266 | + }); |
| 267 | +}); |
| 268 | + |
| 269 | +describe("sdkCutoverPersist — GSAP script preservation (integration)", () => { |
| 270 | + const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val }); |
| 271 | + const makeDeps = () => ({ |
| 272 | + editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) }, |
| 273 | + writeProjectFile: vi.fn().mockResolvedValue(undefined), |
| 274 | + reloadPreview: vi.fn(), |
| 275 | + domEditSaveTimestampRef: makeRef(0), |
| 276 | + }); |
| 277 | + |
| 278 | + it("preserves GSAP <script> block and data-position-mode through setStyle dispatch", async () => { |
| 279 | + const html = `<!DOCTYPE html><html><head></head><body> |
| 280 | +<div data-hf-id="hf-layer" style="color: blue; opacity: 1"></div> |
| 281 | +<script data-hf-gsap data-position-mode="relative"> |
| 282 | +gsap.timeline().to('[data-hf-id="hf-layer"]', { duration: 1, x: 100 }); |
| 283 | +</script> |
| 284 | +</body></html>`; |
| 285 | + const comp = await openComposition(html, { persist: createMemoryAdapter() }); |
| 286 | + const deps = makeDeps(); |
| 287 | + const sel = { hfId: "hf-layer" } as never; |
| 288 | + const result = await sdkCutoverPersist( |
| 289 | + sel, |
| 290 | + [{ type: "inline-style", property: "color", value: "red" }], |
| 291 | + html, |
| 292 | + "/comp.html", |
| 293 | + comp, |
| 294 | + deps, |
| 295 | + ); |
| 296 | + expect(result).toBe(true); |
| 297 | + const written = (deps.writeProjectFile as ReturnType<typeof vi.fn>).mock |
| 298 | + .calls[0]?.[1] as string; |
| 299 | + expect(written).toContain("data-hf-gsap"); |
| 300 | + expect(written).toContain('data-position-mode="relative"'); |
| 301 | + expect(written).toContain("gsap.timeline()"); |
| 302 | + }); |
157 | 303 | }); |
0 commit comments