Skip to content

test(storybook): make the Charts stories' sample data deterministic - #12549

Merged
marie-lucas merged 1 commit into
mainfrom
charts/deterministic-story-data
Nov 18, 2026
Merged

marie-lucas merged 1 commit into
mainfrom
charts/deterministic-story-data

Conversation

@marie-lucas

Copy link
Copy Markdown
Contributor

Maintainer impact

Seven Charts stories build their sample data with Math.random:

  • Dot / Scatter
  • Candlestick / Basic
  • Candlestick / With Moving Average
  • WebGL / Scatter
  • WebGL / Interactive Scatter
  • WebGL / Heatmap
  • WebGL / Streaming

Every load renders different data. Two captures of the same story never match, so a maintainer reviewing a chart change sees noise instead of a diff, and no visual or geometry regression check can compare two renders of these stories.

Intended invariant

  • No package code changes. Only apps/storybook/stories/charts/_data.ts and apps/storybook/stories/charts/WebGL.stories.tsx change.
  • Story ids, export names and args are unchanged. The built index.json has the same 3124 entries before and after.
  • Every other dataset in _data.ts is byte-for-byte unchanged. That includes monthlyData, which the Tooltip stories and their play assertions read.
  • WebGL / Streaming still streams live by default.

Change

  • _data.ts gets a small module-private seeded generator (mulberry32).
    • The stock, scatter and heatmap datasets use it with fixed seeds (1, 2, 3) instead of Math.random. Their formulas don't change.
    • createStreamSamples(seed = 4) returns the next sample of the streaming story's noisy sine wave, with the same formula as before.
  • WebGL / Streaming starts from 60 seeded samples with an x window of [0, 60]. It then pushes one sample every 200 ms, as before.
    • Pause/Resume stops and restarts the stream.
    • Step pushes one sample. It is enabled only while the stream is paused.
    • Reset returns to the 60 initial samples.
    • Together they give a fixed frame for captures.
  • The stream's push handle exists only once the plot has mounted, which can be after the story's first effects. So the story's handle ref replays the samples so far (clear(), then push) each time a handle attaches. The initial samples reach the canvas, and a remounted canvas is filled again.

This is maintenance: it changes story fixtures only, not the charts package or any public API.

Evidence

Setup. Headless Chrome 157, with Figtree and JetBrains Mono installed.

  • Before = a production Storybook build of 2ff1afc9f8 (main before this change).
  • After = a production Storybook build of 6424b7e5b8 (this pull request's head).
  • Desktop is 1280×900 at DPR 1. Phone is 390×844 at DPR 3. Light and dark use &globals=colorMode:dark.
  • Each comparison is two fresh page loads of the same story.
  • Before each capture, the color scheme, direction (ltr), loaded fonts, DPR and viewport width were asserted, with 0 violations.

Oracle. Expected values come from an independent Python implementation of mulberry32 and the dataset formulas, not from the story code.

Failure before the change

Two fresh loads differ in viewport pixels, for each story across the 4 combinations (desktop/phone × light/dark):

Story Differing viewport pixels between two loads
Dot / Scatter 23,678–166,761
Candlestick / Basic 12,096–96,102
Candlestick / With Moving Average 50,719–101,319
WebGL / Scatter 10,449–76,760
WebGL / Interactive Scatter 10,911–91,583
WebGL / Heatmap 255,263–522,251
WebGL / Streaming 302–736. It starts empty, so only its first random points differ.
  • The SVG geometry of the three SVG-data stories also differs between loads.
  • Fitting the oracle data to the rendered marks fails on the before build.

Success after the change

Determinism. 27 of 28 load pairs (7 stories × 4 combinations) have 0 differing viewport pixels and identical SVG geometry.

  • The 28th pair (WebGL / Streaming, phone, dark) differed in 333 pixels, by at most 1 in a single channel. It had the same ticks and the same sample window, so this is rasterization noise.
  • 4 repeated pairs of that combination had 0 differing pixels.
  • The WebGL stories draw their data on a canvas, so their SVG holds only axes. The pixel diff and the pixel oracle below are the real checks for them.

Oracle checks (desktop, light):

Story Check Expected Measured
Dot / Scatter Point 0 (73.425, 32.500) → predicted (863.479, 196.334) circle at (863.479, 196.334)
Dot / Scatter All 200 points each circle's cx and cy is an affine map of its data point max residual 0.00055 px
Candlestick / Basic Day 1 O 101 / C 96 / H 105.3 / L 90.2 rendered wick, body and color match
Candlestick (both stories) All 30 candles wick high, wick low and body top are affine in the data; up/down color follows close ≥ open max residual 0.0005 px; colors 30/30
WebGL / Scatter and Interactive Scatter Sampled points 20 oracle points are painted; 20 control points at least 12 px from any point are background 20/20 painted; 20/20 background
WebGL / Heatmap All 168 cells cell luminance falls as traffic rises Spearman ρ −0.9995. Darkest is Fri 06h (traffic 108), lightest is Sat 20h (traffic 1).
WebGL / Streaming (Pause → Reset frame) Line position ticks 0–60; the line passes through the sampled oracle samples and not through points 25 y-units away 10/10 on the line; 0/10 off it

WebGL / Streaming still advances when not paused. This sequence ran on one page, never reloaded:

  1. Live: x ticks went 10–60, then 20–70 at +2 s, then 30–80 at +4 s. The canvas changed each time.
  2. Step stayed aria-disabled while the stream was live.
  3. Pause: the ticks and canvas stayed identical for 2 s.
  4. Step, twice: the window moved by one sample each time.
  5. Reset: the window returned to 0–60.
  6. Resume: the stream advanced again (20–70 after 2 s).

Seeding red arm. The check loads the story with no clicks and pauses virtual time for one read. It asks whether the seeded initial samples are on the canvas.

  • This head: 18/18 sampled oracle points painted, in 3 of 3 runs.
  • A variant of this head that seeds from a mount useEffect instead of the handle ref: 0/18, in 3 of 3 runs.
  • main: 0/19, because it has no seed.

Product/runtime behavior verified unchanged

  • Only files under apps/storybook/stories/charts/ change. No package source changes.
  • The diff of the built story id set before vs after is empty (3124 entries, 43 under charts-).
  • The story play guard for charts-chrome-tooltip--modal-layering passes.
  • The curated RTL D2 targets pass:
    • charts-chart--playground (legendPosition: 'start'): LTR 48 < 590; RTL 1052 > 510.
    • charts-chrome-legend--default: LTR 48 < 186; RTL 1052 > 914.
  • Chart / Playground doesn't change. Its SVG geometry and captures are identical before and after.
  • When the full story play guard ran in this headless setup, 4 targets outside this change failed: core-powersearch--near-full-token-row, core-powersearch--near-full-token-row-rtl, and core-layer--taller-than-the-viewport (desktop and phone). They fail the same way on the main build in the same setup.

CI impact

None. No workflow, action or CI script changes.

Scope

Components: Storybook stories only (Charts: Dot, Candlestick, dotGL, dotGLInteractive, heatmapGL, streamGL)

  • No intended public API, product behavior, visual direction, or policy change.
  • Product changes discovered during the work were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

All of these ran at 6424b7e5b8:

  • pnpm exec prettier --check and ASTRYX_STRICT_LINT=1 pnpm exec eslint --max-warnings=0 on the 2 changed files: pass
  • node .github/scripts/build-packages-and-storybook.mjs, then pnpm typecheck: pass, 0 errors
  • pnpm exec vitest run --project node apps/storybook/.storybook/story-tree.test.ts against the built index: 5/5 pass
  • pnpm specs:for --diff origin/main: no record links to the changed paths
  • pnpm review:pre: 0 findings
  • pnpm check:changesets: pass. This is a story-only change, so it has no changeset.
  • node .github/scripts/story-play-guard.js --storybook-dir apps/storybook/dist: charts-chrome-tooltip--modal-layering passes. The unrelated failures are listed above.
  • node apps/storybook/rtl-audit/rtl-audit.mjs --storybook-dir dist --curated-only --filter charts/Chart,charts/ChartLegend: D2 passes for both
  • Determinism, oracle, live-advance and seeding checks: headless Chrome against production Storybook builds of 2ff1afc9f8 (before) and 6424b7e5b8 (after), as described above. The red-arm variant was injected into this head.

The stock, scatter and heatmap sample data now come from a small seeded
pseudo-random generator instead of Math.random, so Dot/Scatter, both
Candlestick stories and the WebGL scatter and heatmap stories draw the
same geometry on every load.

WebGL/Streaming starts from 60 seeded samples with a fixed window and
still streams live by default. Pause, Step and Reset give a fixed frame
for captures. No story id changes.
@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 18, 2026 2:09am UTC

Request Review

@marie-lucas marie-lucas added the ci:run-rtl Run the RTL suite on this PR; write access, next push (AST-030 FR12) label Nov 18, 2026
@astracat-bot

astracat-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 18, 2026, 2:29 AM UTC

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 18, 2026
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 6424b7e

Storybook · Sandbox · built from this PR's head

Not run on this PR: add ci:run-visual for Visual, ci:run-a11y for Accessibility.


Generated by PR Enrichment workflow | View full report

@marie-lucas marie-lucas reopened this Nov 18, 2026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI: the required checks are still running on this head (the first CI run was refused for lack of capacity), so let them finish before merging.

The seven Charts stories now render the same data on every load. Two loads of each story on this head give identical frames, where two loads on the base differ in every story, and the seeded values match an independent implementation. No package code, story id or public API changes.

Claims and files checked

Review mode: full; reason: no-earlier-review

Claims:

  • Seven Charts stories (Dot / Scatter, both Candlestick stories, four WebGL stories) drew Math.random data: verified (base _data.ts stock, scatter and heatmap data; base WebGL.stories.tsx:118).
  • Every load rendered different data, so two captures never matched: verified (two base loads differ in 7 of 7 stories).
  • Only the two story files change, with no package code: verified (diff).
  • Story ids, export names and args are unchanged; the index keeps 3124 entries, 43 under charts-: verified (base and head story index).
  • Every other dataset in _data.ts, monthlyData included, is unchanged: verified (the diff touches only the stock, scatter and heatmap data).
  • WebGL / Streaming still streams live by default: verified (the window slides while live).
  • A module-private mulberry32 generator with seeds 1, 2 and 3 replaces Math.random, with unchanged formulas: verified (_data.ts:13-22, :86-124).
  • createStreamSamples (seed 4) yields the same noisy sine wave formula as before: verified (_data.ts:28-36).
  • Streaming starts from 60 samples in a 0–60 window and pushes one sample every 200 ms: verified (WebGL.stories.tsx:112-121, :183-191).
  • Pause/Resume, Step (enabled only while paused) and Reset behave as described: verified in the browser; Step is aria-disabled while live, and each Step moves the window by one sample.
  • The push handle can attach after the story's first effects, so the handle ref replays the samples on every attach: verified (the chart renders its series only after it measures its width, and the seeded line is on the canvas at load with no clicks; WebGL.stories.tsx:145-159).
  • Two head loads give identical frames: verified at desktop width in light mode for 7 of 7 stories; phone and dark were not re-run.
  • Oracle values for Dot point 0, Candlestick day 1 and the heatmap extremes: verified with an independent implementation; the lightest heatmap value (1) is shared by Sat 20h and Sun 18h.
  • The before captures came from an earlier main: verified that both story files are identical there and on the current base.
  • The seeding red-arm variant fails without the handle-ref replay: not reproduced; consistent with when the chart mounts its stream mark.
  • Prettier and strict ESLint pass on both files: verified.
  • Typecheck passes: verified for the changed story files.
  • Story play guard, RTL audit, story-tree test, pre-review and changeset checks pass: not re-run; the stories they name read only datasets this change leaves untouched, and a story-only change needs no changeset.
  • No workflow or CI script changes: verified.
  • Public text carries no internal context: verified.

Files:

  • apps/storybook/stories/charts/WebGL.stories.tsx: clear.
  • apps/storybook/stories/charts/_data.ts: clear.

Automated review (maintainer link)

@marie-lucas
marie-lucas merged commit 3035263 into main Nov 18, 2026
142 of 148 checks passed
@github-actions
github-actions Bot deleted the charts/deterministic-story-data branch November 18, 2026 06:04

This branch was successfully deployed

1 active deployment
Preview — 6424b7e5 Deployed Nov 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run-rtl Run the RTL suite on this PR; write access, next push (AST-030 FR12) CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant