|
| 1 | +// End-to-end: a real project on disk -> lintProject -> the exact PostHog |
| 2 | +// payloads. Unit tests cover the streak arithmetic; this proves the wiring |
| 3 | +// and pins the event shape a dashboard will be built against. |
| 4 | + |
| 5 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 6 | +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; |
| 7 | +import { tmpdir } from "node:os"; |
| 8 | +import { join } from "node:path"; |
| 9 | + |
| 10 | +const HOME = mkdtempSync(join(tmpdir(), "hf-lintrun-")); |
| 11 | +vi.mock("node:os", async (importOriginal) => { |
| 12 | + const actual = await importOriginal<typeof import("node:os")>(); |
| 13 | + return { ...actual, homedir: () => HOME }; |
| 14 | +}); |
| 15 | + |
| 16 | +// Capture at the transport boundary so everything client.ts adds |
| 17 | +// (cli_version, invocation_id, ...) is visible in the assertions. |
| 18 | +const enqueued: Array<{ event: string; properties: Record<string, unknown> }> = []; |
| 19 | +vi.mock("./transport.js", () => ({ |
| 20 | + enqueue: (event: string, properties: Record<string, unknown>) => |
| 21 | + enqueued.push({ event, properties }), |
| 22 | + flush: () => Promise.resolve(), |
| 23 | +})); |
| 24 | +// shouldTrack() consults these two. A dev build disables telemetry by default, |
| 25 | +// which would make this test assert on an empty queue and pass for the wrong |
| 26 | +// reason. |
| 27 | +vi.mock("./policy.js", async (importOriginal) => { |
| 28 | + const actual = await importOriginal<typeof import("./policy.js")>(); |
| 29 | + return { ...actual, telemetryRuntimeOverride: () => null }; |
| 30 | +}); |
| 31 | +vi.mock("./config.js", async (importOriginal) => { |
| 32 | + const actual = await importOriginal<typeof import("./config.js")>(); |
| 33 | + return { ...actual, readConfig: () => ({ ...actual.readConfig(), telemetryEnabled: true }) }; |
| 34 | +}); |
| 35 | + |
| 36 | +const { trackLintRun } = await import("./lintRun.js"); |
| 37 | +const { lintProject } = await import("@hyperframes/lint"); |
| 38 | + |
| 39 | +const COMPOSITION = `<html><body> |
| 40 | + <div id="scene" data-composition-id="main" data-width="1920" data-height="1080" |
| 41 | + data-start="0" data-duration="4"> |
| 42 | + <video id="clip" src="clip.mp4"></video> |
| 43 | + </div> |
| 44 | + <script src="gsap.min.js"></script> |
| 45 | + <script> |
| 46 | + window.__timelines = window.__timelines || {}; |
| 47 | + window.__timelines["main"] = gsap.timeline({ paused: true }); |
| 48 | + </script> |
| 49 | +</body></html>`; |
| 50 | + |
| 51 | +function makeProject(html: string): string { |
| 52 | + const dir = mkdtempSync(join(tmpdir(), "hf-proj-")); |
| 53 | + mkdirSync(join(dir, "compositions"), { recursive: true }); |
| 54 | + writeFileSync(join(dir, "index.html"), html, "utf-8"); |
| 55 | + return dir; |
| 56 | +} |
| 57 | + |
| 58 | +beforeEach(() => { |
| 59 | + enqueued.length = 0; |
| 60 | + rmSync(join(HOME, ".hyperframes"), { recursive: true, force: true }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("trackLintRun end to end", () => { |
| 64 | + it("emits one lint_report carrying codes, timings, and the ruleset fingerprint", async () => { |
| 65 | + const dir = makeProject(COMPOSITION); |
| 66 | + const result = await lintProject(dir); |
| 67 | + trackLintRun(dir, result, { command: "lint", durationMs: 12 }); |
| 68 | + |
| 69 | + const reports = enqueued.filter((e) => e.event === "lint_report"); |
| 70 | + expect(reports).toHaveLength(1); |
| 71 | + const props = reports[0]!.properties; |
| 72 | + |
| 73 | + expect(props["command"]).toBe("lint"); |
| 74 | + expect(props["files_scanned"]).toBe(1); |
| 75 | + expect(props["duration_ms"]).toBe(12); |
| 76 | + // The real linter found real problems in this composition. |
| 77 | + expect((props["codes"] as string[]).length).toBeGreaterThan(0); |
| 78 | + expect(props["error_count"]).toBeGreaterThan(0); |
| 79 | + // Timings are attributed per rule group and a slowest rule is identified. |
| 80 | + expect(Object.keys(props["rule_group_ms"] as object)).toContain("gsap"); |
| 81 | + expect(props["slowest_rule"]).toMatch(/^[a-z]+#\d+$/); |
| 82 | + // Version and ruleset fingerprint ride along. |
| 83 | + expect(props["cli_version"]).toBeTruthy(); |
| 84 | + expect(props["rule_count"]).toBeGreaterThan(0); |
| 85 | + // Per-group sizes make the positional `slowest_rule` index comparable |
| 86 | + // across builds: a group that changed size renumbered its rules. |
| 87 | + const groupCounts = props["rule_group_counts"] as Record<string, number>; |
| 88 | + expect(groupCounts["gsap"]).toBeGreaterThan(0); |
| 89 | + expect(Object.values(groupCounts).reduce((a, b) => a + b, 0)).toBe(props["rule_count"]); |
| 90 | + // code_counts sums to the number of findings. |
| 91 | + const counts = Object.values(props["code_counts"] as Record<string, number>); |
| 92 | + expect(counts.reduce((a, b) => a + b, 0)).toBe( |
| 93 | + result.results.flatMap((r) => r.result.findings).length, |
| 94 | + ); |
| 95 | + |
| 96 | + rmSync(dir, { recursive: true, force: true }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("emits lint_rule_streak with cleared:true once an edit removes the finding", async () => { |
| 100 | + const dir = makeProject(COMPOSITION); |
| 101 | + |
| 102 | + const first = await lintProject(dir); |
| 103 | + trackLintRun(dir, first, { command: "lint", durationMs: 1 }); |
| 104 | + const codes = first.results[0]!.result.findings.map((f) => f.code); |
| 105 | + expect(codes).toContain("media_missing_data_start"); |
| 106 | + |
| 107 | + // Fix exactly that finding and re-lint. |
| 108 | + writeFileSync( |
| 109 | + join(dir, "index.html"), |
| 110 | + COMPOSITION.replace('<video id="clip"', '<video id="clip" data-start="0" data-duration="4"'), |
| 111 | + "utf-8", |
| 112 | + ); |
| 113 | + enqueued.length = 0; |
| 114 | + const second = await lintProject(dir); |
| 115 | + trackLintRun(dir, second, { command: "lint", durationMs: 1 }); |
| 116 | + |
| 117 | + const streaks = enqueued.filter((e) => e.event === "lint_rule_streak"); |
| 118 | + const cleared = streaks.find((e) => e.properties["code"] === "media_missing_data_start"); |
| 119 | + expect(cleared?.properties).toMatchObject({ |
| 120 | + code: "media_missing_data_start", |
| 121 | + cleared: true, |
| 122 | + edits: 1, |
| 123 | + command: "lint", |
| 124 | + }); |
| 125 | + |
| 126 | + rmSync(dir, { recursive: true, force: true }); |
| 127 | + }); |
| 128 | + |
| 129 | + it("never throws when the lint result is malformed", () => { |
| 130 | + expect(() => |
| 131 | + trackLintRun("/nope", { results: [] } as never, { command: "lint", durationMs: 0 }), |
| 132 | + ).not.toThrow(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments