|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from "vitest"; |
| 2 | +import { execFileSync } from "child_process"; |
| 3 | +import { join, dirname } from "path"; |
| 4 | +import { fileURLToPath } from "url"; |
| 5 | +import { mkdirSync, writeFileSync, rmSync, existsSync, readdirSync } from "fs"; |
| 6 | + |
| 7 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 8 | +const SCRIPT = join(__dirname, "..", "audit-cross-browser-css.sh"); |
| 9 | + |
| 10 | +let counter = 0; |
| 11 | + |
| 12 | +function createTmpDir() { |
| 13 | + counter++; |
| 14 | + const dir = join(__dirname, "fixtures", `audit-css-${counter}-${Date.now()}`); |
| 15 | + mkdirSync(dir, { recursive: true }); |
| 16 | + return dir; |
| 17 | +} |
| 18 | + |
| 19 | +function run(cwd, args = []) { |
| 20 | + try { |
| 21 | + const stdout = execFileSync("bash", [SCRIPT, ...args], { |
| 22 | + encoding: "utf-8", |
| 23 | + timeout: 15000, |
| 24 | + cwd, |
| 25 | + }); |
| 26 | + return { stdout, exitCode: 0 }; |
| 27 | + } catch (err) { |
| 28 | + return { |
| 29 | + stdout: err.stdout || "", |
| 30 | + stderr: err.stderr || "", |
| 31 | + exitCode: err.status, |
| 32 | + }; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +afterAll(() => { |
| 37 | + const fixturesDir = join(__dirname, "fixtures"); |
| 38 | + if (existsSync(fixturesDir)) { |
| 39 | + try { |
| 40 | + for (const entry of readdirSync(fixturesDir)) { |
| 41 | + if (entry.startsWith("audit-css-")) { |
| 42 | + rmSync(join(fixturesDir, entry), { recursive: true, force: true }); |
| 43 | + } |
| 44 | + } |
| 45 | + } catch { |
| 46 | + // Ignore cleanup errors |
| 47 | + } |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +describe("audit-cross-browser-css.sh — clean project", () => { |
| 52 | + let dir; |
| 53 | + |
| 54 | + beforeAll(() => { |
| 55 | + dir = createTmpDir(); |
| 56 | + mkdirSync(join(dir, "src"), { recursive: true }); |
| 57 | + writeFileSync( |
| 58 | + join(dir, "src", "styles.css"), |
| 59 | + `.button { |
| 60 | + background: var(--primary); |
| 61 | + border-radius: 4px; |
| 62 | +}`, |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it("reports no issues on clean CSS", () => { |
| 67 | + const result = run(dir, [join(dir, "src")]); |
| 68 | + expect(result.exitCode).toBe(0); |
| 69 | + expect(result.stdout).toContain("Issues found: 0"); |
| 70 | + expect(result.stdout).toContain("All clear"); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("audit-cross-browser-css.sh — webkit prefix detection", () => { |
| 75 | + let dir; |
| 76 | + |
| 77 | + beforeAll(() => { |
| 78 | + dir = createTmpDir(); |
| 79 | + mkdirSync(join(dir, "src"), { recursive: true }); |
| 80 | + writeFileSync( |
| 81 | + join(dir, "src", "app.css"), |
| 82 | + `.gradient { |
| 83 | + -webkit-linear-gradient(top, red, blue); |
| 84 | + background: linear-gradient(top, red, blue); |
| 85 | +}`, |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it("detects -webkit- prefixed properties", () => { |
| 90 | + const result = run(dir, [join(dir, "src")]); |
| 91 | + expect(result.stdout).toContain("-webkit-"); |
| 92 | + expect(result.stdout).toContain("Vendor prefix"); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe("audit-cross-browser-css.sh — backdrop-filter without prefix", () => { |
| 97 | + let dir; |
| 98 | + |
| 99 | + beforeAll(() => { |
| 100 | + dir = createTmpDir(); |
| 101 | + mkdirSync(join(dir, "src"), { recursive: true }); |
| 102 | + writeFileSync( |
| 103 | + join(dir, "src", "overlay.css"), |
| 104 | + `.overlay { |
| 105 | + backdrop-filter: blur(10px); |
| 106 | +}`, |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it("detects backdrop-filter without -webkit- prefix", () => { |
| 111 | + const result = run(dir, [join(dir, "src")]); |
| 112 | + expect(result.stdout).toContain("backdrop-filter"); |
| 113 | + expect(result.stdout).toContain("Safari"); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe("audit-cross-browser-css.sh — :focus without :focus-visible", () => { |
| 118 | + let dir; |
| 119 | + |
| 120 | + beforeAll(() => { |
| 121 | + dir = createTmpDir(); |
| 122 | + mkdirSync(join(dir, "src"), { recursive: true }); |
| 123 | + writeFileSync( |
| 124 | + join(dir, "src", "forms.css"), |
| 125 | + `input:focus { |
| 126 | + outline: 2px solid blue; |
| 127 | +}`, |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it("flags :focus usage without :focus-visible", () => { |
| 132 | + const result = run(dir, [join(dir, "src")]); |
| 133 | + expect(result.stdout).toContain(":focus"); |
| 134 | + expect(result.stdout).toContain(":focus-visible"); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe("audit-cross-browser-css.sh — summary with issue count", () => { |
| 139 | + let dir; |
| 140 | + |
| 141 | + beforeAll(() => { |
| 142 | + dir = createTmpDir(); |
| 143 | + mkdirSync(join(dir, "src"), { recursive: true }); |
| 144 | + writeFileSync( |
| 145 | + join(dir, "src", "mixed.css"), |
| 146 | + `.a { -webkit-transition: all 0.3s; } |
| 147 | +.b:focus { outline: none; } |
| 148 | +.c { backdrop-filter: blur(5px); }`, |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it("counts total issues in summary", () => { |
| 153 | + const result = run(dir, [join(dir, "src")]); |
| 154 | + expect(result.stdout).toContain("Issues found:"); |
| 155 | + // Should have at least 2 issues (webkit prefix + focus or backdrop) |
| 156 | + const match = result.stdout.match(/Issues found: (\d+)/); |
| 157 | + expect(match).not.toBeNull(); |
| 158 | + expect(parseInt(match[1], 10)).toBeGreaterThanOrEqual(2); |
| 159 | + }); |
| 160 | +}); |
0 commit comments