|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { lintHyperframeHtml } from "../hyperframeLinter"; |
| 3 | + |
| 4 | +function findByCode(html: string, code: string) { |
| 5 | + return lintHyperframeHtml(html).findings.filter((f) => f.code === code); |
| 6 | +} |
| 7 | + |
| 8 | +describe("prefer_container_units", () => { |
| 9 | + it("flags px positioning on elements inside a composition", () => { |
| 10 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 11 | + <h1 style="position:absolute; left:96px; top:108px; font-size:64px;">Title</h1> |
| 12 | + </div>`; |
| 13 | + const findings = findByCode(html, "prefer_container_units"); |
| 14 | + expect(findings.length).toBeGreaterThanOrEqual(3); |
| 15 | + expect(findings.some((f) => f.message.includes("left"))).toBe(true); |
| 16 | + expect(findings.some((f) => f.message.includes("top"))).toBe(true); |
| 17 | + expect(findings.some((f) => f.message.includes("font-size"))).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it("suggests cqw for horizontal properties", () => { |
| 21 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 22 | + <div style="left:192px;">content</div> |
| 23 | + </div>`; |
| 24 | + const findings = findByCode(html, "prefer_container_units"); |
| 25 | + expect(findings[0].message).toContain("cqw"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("suggests cqh for vertical properties", () => { |
| 29 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 30 | + <div style="top:108px;">content</div> |
| 31 | + </div>`; |
| 32 | + const findings = findByCode(html, "prefer_container_units"); |
| 33 | + expect(findings[0].message).toContain("cqh"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("calculates correct container unit values", () => { |
| 37 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 38 | + <div style="left:96px;">content</div> |
| 39 | + </div>`; |
| 40 | + const findings = findByCode(html, "prefer_container_units"); |
| 41 | + expect(findings[0].message).toContain("5cqw"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("ignores small px values (borders, shadows)", () => { |
| 45 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 46 | + <div style="border-radius:2px; width:4px;">content</div> |
| 47 | + </div>`; |
| 48 | + const findings = findByCode(html, "prefer_container_units"); |
| 49 | + expect(findings).toHaveLength(0); |
| 50 | + }); |
| 51 | + |
| 52 | + it("ignores composition root elements", () => { |
| 53 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080" style="width:1920px; height:1080px;"> |
| 54 | + <p>content</p> |
| 55 | + </div>`; |
| 56 | + const findings = findByCode(html, "prefer_container_units"); |
| 57 | + expect(findings).toHaveLength(0); |
| 58 | + }); |
| 59 | + |
| 60 | + it("ignores script, style, and audio tags", () => { |
| 61 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 62 | + <script style="width:500px;"></script> |
| 63 | + <style>body { width: 1920px; }</style> |
| 64 | + <audio style="width:100px;" data-start="0" src="vo.mp3"></audio> |
| 65 | + </div>`; |
| 66 | + const findings = findByCode(html, "prefer_container_units"); |
| 67 | + expect(findings).toHaveLength(0); |
| 68 | + }); |
| 69 | + |
| 70 | + it("severity is info (suggestion, not error)", () => { |
| 71 | + const html = `<div data-composition-id="test" data-width="1920" data-height="1080"> |
| 72 | + <div style="left:200px;">content</div> |
| 73 | + </div>`; |
| 74 | + const findings = findByCode(html, "prefer_container_units"); |
| 75 | + expect(findings[0].severity).toBe("info"); |
| 76 | + }); |
| 77 | +}); |
0 commit comments