# `PhxDemo.Drift`
[🔗](https://github.com/thenatespack/phoenix_demo/blob/main/lib/phx_demo/drift.ex#L1)

Visual drift checks. Warns, never fails.

While recording, the end state of each scene is screenshotted to
`<demo dir>/screens/NN-<scene>.png`. `mix demo.record` then compares each
screenshot with a baseline by the share of pixels that changed noticeably
(a per-pixel difference above 24/255). Whole-image SSIM was too forgiving:
a changed heading on a mostly blank page still scored 0.994.

  * baselines live in `test/demos/__baselines__/<os>/<module>/<demo>/`
    (`config :phx_demo, :baseline_dir, ...` to change the root). They're
    per OS because fonts render differently on macOS and Linux.
  * more than the threshold (default 0.1% of pixels, set with
    `config :phx_demo, :drift_threshold, 0.5`) is reported as drift, with a
    diff image written next to the screenshot (`NN-<scene>.diff.png`)
  * on GitHub Actions, drift becomes a `::warning` annotation
  * `mix demo.record --update-baselines` copies the new screenshots over

Missing baselines are reported, not treated as drift.

# `baseline_dir`

Baseline directory for a rendered demo directory. Baselines are kept per OS
(`darwin`, `linux`, …) because font rendering differs between platforms.

# `capture`

Screenshots the page into `<dir>/screens/` as the next numbered file.

# `changed_percent!`

Percentage of pixels whose difference exceeds 24/255. The candidate is
scaled to the baseline's size in case the viewport changed.

# `compare`

Compares a demo's screenshots with its baselines. Returns a list of
`{:ok | :drift | :new, screenshot, details}`.

# `update_baselines`

Copies a demo's screenshots into its baseline directory.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
