Files
co-lite/solvway-admin-portal/app/lib/frameHtml.test.ts
T

69 lines
4.2 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { FRAME_SANDBOX, frameAnchor, frameDocument, frameResponse, nextFrameHeight, START_HEIGHT } from "./frameHtml";
// add-custom-html-block: pasted HTML runs sealed, in its own document with a sandbox policy, and reports its height.
describe("frameHtml", () => {
it("grants scripts and pop-ups only", () => {
expect(FRAME_SANDBOX.split(" ").sort()).toEqual(["allow-popups", "allow-popups-to-escape-sandbox", "allow-scripts"]);
});
it("adds the height script before </body>, or at the end of a fragment", () => {
const doc = frameDocument("<html><body><h1>Hi</h1></body></html>");
expect(doc).toMatch(/<h1>Hi<\/h1><script>[\s\S]*colite-frame-height[\s\S]*<\/script><\/body><\/html>$/);
expect(frameDocument("<section>x</section>")).toMatch(/^<section>x<\/section><script>[\s\S]*colite-frame-height/);
expect(frameDocument("<BODY>x</BODY>")).toMatch(/x<script>[\s\S]*<\/script><\/BODY>$/);
});
it("serves the HTML sealed: sandbox policy, own framing only, no sniffing", async () => {
const res = frameResponse("<p>x</p>", { preview: false });
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toBe("text/html; charset=utf-8");
const csp = res.headers.get("content-security-policy") ?? "";
expect(csp).toBe(`sandbox ${FRAME_SANDBOX}; frame-ancestors 'self'`);
expect(csp).not.toContain("allow-same-origin");
expect(csp).not.toContain("allow-forms");
expect(csp).not.toContain("allow-top-navigation");
expect(res.headers.get("x-content-type-options")).toBe("nosniff");
expect(res.headers.get("referrer-policy")).toBe("no-referrer");
expect(await res.text()).toContain("<p>x</p>");
});
it("does not let a browser cache a draft", () => {
expect(frameResponse("<p>x</p>", { preview: true }).headers.get("cache-control")).toBe("no-store");
expect(frameResponse("<p>x</p>", { preview: false }).headers.get("cache-control")).not.toBe("no-store");
});
it("clamps a reported height between 100 and 20000 pixels and ignores junk", () => {
const start = { height: START_HEIGHT, steps: [] };
expect(START_HEIGHT).toBe(600);
expect(nextFrameHeight(start, { type: "colite-frame-height", height: 3000 }).height).toBe(3000);
expect(nextFrameHeight(start, { type: "colite-frame-height", height: 10 }).height).toBe(100);
expect(nextFrameHeight(start, { type: "colite-frame-height", height: 99999 }).height).toBe(20000);
for (const junk of [null, "600", { type: "other", height: 900 }, { type: "colite-frame-height", height: "900" }, { type: "colite-frame-height", height: NaN }]) {
expect(nextFrameHeight(start, junk)).toBe(start);
}
});
it("stops growing when the content grows with the frame (100vh sections)", () => {
let s = { height: START_HEIGHT, steps: [] as number[] };
// Each report is the frame height plus the same 400 px below a full-height section: a loop.
for (let i = 0; i < 10; i++) s = nextFrameHeight(s, { type: "colite-frame-height", height: s.height + 400 });
expect(s.height).toBeLessThan(START_HEIGHT + 400 * 4);
// Ordinary growth with varying steps (images loading) keeps following the content.
let t = { height: START_HEIGHT, steps: [] as number[] };
for (const h of [900, 1250, 1900, 2000, 2600]) t = nextFrameHeight(t, { type: "colite-frame-height", height: h });
expect(t.height).toBe(2600);
});
// add-page-anchor-links: a frame asks its page to scroll to a #name target.
it("reads a same-page link request: a valid name and the in-frame offset, or nothing", () => {
expect(frameAnchor({ type: "colite-frame-anchor", name: "signup", y: null })).toEqual({ name: "signup", y: null });
expect(frameAnchor({ type: "colite-frame-anchor", name: "top", y: 120.4 })).toEqual({ name: "top", y: 120 });
expect(frameAnchor({ type: "colite-frame-anchor", name: "top", y: -5 })).toEqual({ name: "top", y: 0 });
for (const junk of [null, "x", { type: "colite-frame-height", height: 5 }, { type: "colite-frame-anchor", name: "Sign Up", y: null },
{ type: "colite-frame-anchor", name: "a", y: "1" }, { type: "colite-frame-anchor", name: "a", y: Infinity }, { type: "colite-frame-anchor", y: null }]) {
expect(frameAnchor(junk), JSON.stringify(junk)).toBeNull();
}
});
});