|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { patchOpsToSdkEditOps, SdkShadowMismatch } from "./sdkShadow"; |
| 3 | +import type { PatchOperation } from "./sourcePatcher"; |
| 4 | +import { openComposition } from "@hyperframes/sdk"; |
| 5 | + |
| 6 | +const BASE_HTML = /* html */ `<!DOCTYPE html> |
| 7 | +<html><body> |
| 8 | + <div data-hf-id="hf-box" style="color: red; width: 100px;" data-name="box">Hello</div> |
| 9 | +</body></html>`; |
| 10 | + |
| 11 | +describe("patchOpsToSdkEditOps", () => { |
| 12 | + it("maps inline-style ops to a single setStyle EditOp", () => { |
| 13 | + const ops: PatchOperation[] = [ |
| 14 | + { type: "inline-style", property: "color", value: "#00f" }, |
| 15 | + { type: "inline-style", property: "opacity", value: "0.5" }, |
| 16 | + ]; |
| 17 | + const result = patchOpsToSdkEditOps("hf-box", ops); |
| 18 | + expect(result).toHaveLength(1); |
| 19 | + expect(result[0]).toEqual({ |
| 20 | + type: "setStyle", |
| 21 | + target: "hf-box", |
| 22 | + styles: { color: "#00f", opacity: "0.5" }, |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it("maps text-content op to setText EditOp", () => { |
| 27 | + const ops: PatchOperation[] = [{ type: "text-content", property: "text", value: "World" }]; |
| 28 | + const result = patchOpsToSdkEditOps("hf-box", ops); |
| 29 | + expect(result).toHaveLength(1); |
| 30 | + expect(result[0]).toEqual({ type: "setText", target: "hf-box", value: "World" }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("maps attribute op to setAttribute with data- prefix", () => { |
| 34 | + const ops: PatchOperation[] = [{ type: "attribute", property: "name", value: "hero" }]; |
| 35 | + const result = patchOpsToSdkEditOps("hf-box", ops); |
| 36 | + expect(result).toHaveLength(1); |
| 37 | + expect(result[0]).toEqual({ |
| 38 | + type: "setAttribute", |
| 39 | + target: "hf-box", |
| 40 | + name: "data-name", |
| 41 | + value: "hero", |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("maps html-attribute op to setAttribute without prefix", () => { |
| 46 | + const ops: PatchOperation[] = [ |
| 47 | + { type: "html-attribute", property: "contenteditable", value: "true" }, |
| 48 | + ]; |
| 49 | + const result = patchOpsToSdkEditOps("hf-box", ops); |
| 50 | + expect(result).toHaveLength(1); |
| 51 | + expect(result[0]).toEqual({ |
| 52 | + type: "setAttribute", |
| 53 | + target: "hf-box", |
| 54 | + name: "contenteditable", |
| 55 | + value: "true", |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("handles null value for attribute removal", () => { |
| 60 | + const ops: PatchOperation[] = [{ type: "html-attribute", property: "hidden", value: null }]; |
| 61 | + const result = patchOpsToSdkEditOps("hf-box", ops); |
| 62 | + expect(result[0]).toEqual({ |
| 63 | + type: "setAttribute", |
| 64 | + target: "hf-box", |
| 65 | + name: "hidden", |
| 66 | + value: null, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns empty array for unknown op types", () => { |
| 71 | + const ops = [{ type: "unknown-op", property: "x", value: "y" }] as unknown as PatchOperation[]; |
| 72 | + expect(patchOpsToSdkEditOps("hf-box", ops)).toHaveLength(0); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe("sdkShadowDispatch (integration)", () => { |
| 77 | + it("applies ops and returns no mismatches when SDK matches expected values", async () => { |
| 78 | + const { sdkShadowDispatch } = await import("./sdkShadow"); |
| 79 | + const session = await openComposition(BASE_HTML); |
| 80 | + |
| 81 | + const ops: PatchOperation[] = [{ type: "inline-style", property: "color", value: "#00f" }]; |
| 82 | + const result = sdkShadowDispatch(session, "hf-box", ops); |
| 83 | + |
| 84 | + expect(result.dispatched).toBe(true); |
| 85 | + expect(result.mismatches).toHaveLength(0); |
| 86 | + expect(session.getElement("hf-box")?.inlineStyles.color).toBe("#00f"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("returns dispatched:false when hfId not found in session", async () => { |
| 90 | + const { sdkShadowDispatch } = await import("./sdkShadow"); |
| 91 | + const session = await openComposition(BASE_HTML); |
| 92 | + |
| 93 | + const ops: PatchOperation[] = [{ type: "inline-style", property: "color", value: "#00f" }]; |
| 94 | + const result = sdkShadowDispatch(session, "hf-missing", ops); |
| 95 | + |
| 96 | + expect(result.dispatched).toBe(false); |
| 97 | + expect(result.mismatches).toHaveLength(1); |
| 98 | + expect(result.mismatches[0]).toMatchObject<SdkShadowMismatch>({ |
| 99 | + kind: "element_not_found", |
| 100 | + hfId: "hf-missing", |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it("applies text op and reads back via session.getElement", async () => { |
| 105 | + const { sdkShadowDispatch } = await import("./sdkShadow"); |
| 106 | + const session = await openComposition(BASE_HTML); |
| 107 | + |
| 108 | + const ops: PatchOperation[] = [{ type: "text-content", property: "text", value: "Updated" }]; |
| 109 | + sdkShadowDispatch(session, "hf-box", ops); |
| 110 | + |
| 111 | + expect(session.getElement("hf-box")?.text).toBe("Updated"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("applies attribute op and reads back via session.getElement", async () => { |
| 115 | + const { sdkShadowDispatch } = await import("./sdkShadow"); |
| 116 | + const session = await openComposition(BASE_HTML); |
| 117 | + |
| 118 | + const ops: PatchOperation[] = [{ type: "attribute", property: "name", value: "hero" }]; |
| 119 | + sdkShadowDispatch(session, "hf-box", ops); |
| 120 | + |
| 121 | + expect(session.getElement("hf-box")?.attributes["data-name"]).toBe("hero"); |
| 122 | + }); |
| 123 | +}); |
0 commit comments