Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
58 lines
3.4 KiB
TypeScript
58 lines
3.4 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { FRAME_SANDBOX, 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);
|
|
});
|
|
});
|