Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/lint/src/rules/slideshow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ describe("slideshow lint rule", () => {
expect(findings[0]!.code).toBe("slideshow_invalid");
});

it.each([null, {}, { id: "h1", label: 42, target: "deep" }])(
"reports malformed hotspot %j as a structure finding without crashing lint",
async (hotspot) => {
const html = `<div data-composition-id="c" data-width="1920" data-height="1080">
<div data-composition-id="a" data-start="0" data-duration="5"></div>
<script type="application/hyperframes-slideshow+json">${JSON.stringify({
slides: [{ sceneId: "a", hotspots: [hotspot] }],
slideSequences: [{ id: "deep", label: "Deep", slides: [{ sceneId: "a" }] }],
})}</script>
</div>`;
expect(await findSlideshow(html)).toEqual([
expect.objectContaining({ code: "slideshow_invalid", severity: "error" }),
]);
},
);

it("passes when sceneId resolves to a data-composition-id element (no .clip[id])", async () => {
const html = `<div data-composition-id="c" data-width="1920" data-height="1080">
<div data-composition-id="scene-a" data-start="0" data-duration="5"></div>
Expand Down
50 changes: 50 additions & 0 deletions packages/parsers/src/slideshow/parseSlideshow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,56 @@ describe("parseSlideshowManifest", () => {
</script>`;
expect(() => parseSlideshowManifest(html)).toThrow();
});

it.each([
null,
42,
"branch",
[],
{},
{ id: 1, label: "Go", target: "deep" },
{ id: "h1", label: null, target: "deep" },
{ id: "h1", label: "Go", target: [] },
{ id: "h1", label: "Go", target: "deep", region: null },
{ id: "h1", label: "Go", target: "deep", region: { x: 10, y: 20 } },
{ id: "h1", label: "Go", target: "deep", region: { x: "10", y: 20, w: 30, h: 40 } },
])("rejects malformed hotspot %j in main-line and branch slides", (hotspot) => {
for (const manifest of [
{ slides: [{ sceneId: "a", hotspots: [hotspot] }] },
{
slides: [{ sceneId: "a" }],
slideSequences: [
{ id: "deep", label: "Deep", slides: [{ sceneId: "c", hotspots: [hotspot] }] },
],
},
]) {
const html = `<script type="application/hyperframes-slideshow+json">${JSON.stringify(manifest)}</script>`;
expect(() => parseSlideshowManifest(html)).toThrow("not a valid SlideshowManifest");
}
});

it("preserves valid hotspot regions and optional hotspot arrays", () => {
const manifest = {
slides: [
{
sceneId: "a",
hotspots: [
{ id: "h1", label: "Go", target: "deep", region: { x: 0, y: 12.5, w: 100, h: 20 } },
],
},
],
slideSequences: [{ id: "deep", label: "Deep", slides: [{ sceneId: "c", hotspots: [] }] }],
};
const html = `<script type="application/hyperframes-slideshow+json">${JSON.stringify(manifest)}</script>`;
expect(parseSlideshowManifest(html)).toEqual(manifest);
});

it("rejects a hotspot region whose JSON number overflows to infinity", () => {
const html = `<script type="application/hyperframes-slideshow+json">
{"slides":[{"sceneId":"a","hotspots":[{"id":"h1","label":"Go","target":"deep","region":{"x":1e400,"y":0,"w":30,"h":20}}]}]}
</script>`;
expect(() => parseSlideshowManifest(html)).toThrow("not a valid SlideshowManifest");
});
});

describe("resolveSlideshow", () => {
Expand Down
34 changes: 31 additions & 3 deletions packages/parsers/src/slideshow/parseSlideshow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type {
SlideshowManifest,
SlideRef,
SlideHotspot,
ResolvedSlide,
ResolvedSlideshow,
ResolvedSlideSequence,
Expand Down Expand Up @@ -52,12 +53,39 @@ function isOptionalBoolean(v: unknown): v is boolean | undefined {
return v === undefined || typeof v === "boolean";
}

function isRecord(v: unknown): v is Record<string, unknown> {
return typeof v === "object" && v !== null && !Array.isArray(v);
}

function isHotspotRegion(v: unknown): boolean {
return (
isRecord(v) &&
[v["x"], v["y"], v["w"], v["h"]].every(
(coordinate) => typeof coordinate === "number" && Number.isFinite(coordinate),
)
);
}

function isSlideHotspot(v: unknown): v is SlideHotspot {
return (
isRecord(v) &&
typeof v["id"] === "string" &&
typeof v["label"] === "string" &&
typeof v["target"] === "string" &&
(v["region"] === undefined || isHotspotRegion(v["region"]))
);
}

function isSlideRef(v: unknown): v is SlideRef {
if (typeof v !== "object" || v === null) return false;
const r = v as Record<string, unknown>;
if (!isRecord(v)) return false;
const r = v;
if (typeof r["sceneId"] !== "string") return false;
if (!isOptionalNumberArray(r["fragments"])) return false;
if (r["hotspots"] !== undefined && !Array.isArray(r["hotspots"])) return false;
if (
r["hotspots"] !== undefined &&
(!Array.isArray(r["hotspots"]) || !r["hotspots"].every(isSlideHotspot))
)
return false;
if (!isOptionalBoolean(r["autoplay"])) return false;
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/player/src/slideshow/hyperframes-slideshow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,14 @@ describe("slideshow parts (pure)", () => {
expect(parts.player).toBe(root.querySelector("hyperframes-player"));
expect(parts.manifest.slides.map((s) => s.sceneId)).toEqual(["intro"]);
});

it("classifies a malformed hotspot as a malformed island before slideshow initialization", () => {
const root = subtree(`<hyperframes-player></hyperframes-player>
<script type="application/hyperframes-slideshow+json">${JSON.stringify({
slides: [{ sceneId: "intro", startTime: 0, endTime: 1, hotspots: [null] }],
})}</script>`);
expect(locateSlideshowParts(root)).toEqual({ kind: "incomplete", reason: "malformed-island" });
});
});

// ---------------------------------------------------------------------------
Expand Down
Loading