Files
Aura/crates/aura-cli/tests/chart_viewer_gaps.mjs
T
Brummel 45cc1c44c9 feat(trace-charts): ordinal x-axis default + zoom/pan/cursor-sync (viewer Tier-1)
The served trace chart's x-axis no longer plots against raw timestamps by
default — non-trading gaps (nights, weekends) left horizontal blanks that
dominated the view. The x spine is now ordinal by default: every recorded slot
sits at an even position, so gaps collapse and the signal fills the width. A
header toggle flips to continuous (real-time-proportional) spacing and back;
default ordinal honours the requested "x-axis should not be continuous", with
continuous as the one-click option.

Comfort, all on the read/render side (the engine and the Rust serve contract
are untouched — window.AURA_TRACES still carries the real xs, the ordinal
indices are derived client-side):
- ordinal mode keeps the axis honest: ticks and the legend value-readout map a
  collapsed index back to the real xs[i], never the index itself.
- wheel-zoom (x, cursor-anchored), drag-pan, double-click-reset on the plot
  (uPlot's default drag-box-zoom is disabled so drag pans).
- panels share a cursor.sync key, so the crosshair lines up across stacked
  panels.

Confined to chart-viewer.js (buildCharts gains an xMode param + a wheelZoomPlugin
factory; mount re-renders on the toggle) and render.rs (CHART_HEAD toggle button
+ a chart-only style). The two twin node guards (build/gaps) re-point to the
ordinal default in lockstep; a new x-mode guard pins ordinal-default / continuous
/ panel-sync / plugin wiring, and a label-honesty guard (gappy-traces fixture,
xs far from the index spine) pins that ordinal ticks report the real timestamp.

Verified: cargo test --workspace green (incl. the four chart guards + render
inline tests), clippy --all-targets -D warnings clean, e2e (aura run --trace +
aura chart) emits a self-contained page defaulting to ordinal with the toggle.

Spec docs/specs/0057, plan docs/plans/0057 (boss-signed; grounding-check PASS).
Derived-fork decision log in the issue comments.

closes #103
2026-06-19 19:00:33 +02:00

50 lines
2.8 KiB
JavaScript

// Headless guard for the trace-chart viewer (chart-viewer.js) gap-handling.
// Property protected: when taps fire at DIFFERENT cadences, the CLI's union-spine
// alignment (spec 0056 §6) null-fills the timestamps a tap did NOT fire at, and
// buildCharts must hand those nulls to uPlot VERBATIM as the data array — a null is
// a rendered gap, never dropped, never coerced to 0. This is the observable end of
// the "no tap's points dropped, no privileged spine" contract: a positional shift or
// a `null -> 0` coercion would silently misalign every series past the first gap.
//
// It loads the real viewer module (a regression there is observed here) and drives
// the exported pure `buildCharts` over a fixture whose two series have nulls at
// DIFFERENT positions; uPlot itself (which needs `document`) is never instantiated.
import { createRequire } from "node:module";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
const require = createRequire(import.meta.url);
const here = dirname(fileURLToPath(import.meta.url));
const traces = JSON.parse(readFileSync(join(here, "fixtures", "sparse-traces.json"), "utf8"));
const fail = (msg) => { console.error("FAIL: " + msg); process.exit(1); };
const sameArr = (a, b, msg) => {
// strict identity per slot, so a null that became 0 (or a shifted point) fails.
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length || a.some((v, i) => v !== b[i])) fail(msg);
};
// No `document` global -> the browser mount is skipped; we get only the pure export.
global.window = { AURA_TRACES: traces };
const { buildCharts } = require(join(here, "..", "assets", "chart-viewer.js"));
const eqPoints = traces.series[0].points; // [0.0, 0.001, null, 0.004]
const exPoints = traces.series[1].points; // [1.0, null, null, -1.0]
const idx = Array.from({ length: traces.xs.length }, (_, i) => i);
// overlay: the data array is [ordinal spine, ...each series' points], nulls preserved.
const overlay = buildCharts(traces, "overlay")[0];
sameArr(overlay.data[0], idx, "overlay x is the ordinal index spine (gaps collapsed)");
sameArr(overlay.data[1], eqPoints, "overlay equity points carry their null gap verbatim");
sameArr(overlay.data[2], exPoints, "overlay exposure points carry their null gaps verbatim");
// panels: each panel is [ordinal spine, its own series' points] — same nulls.
const panels = buildCharts(traces, "panels");
sameArr(panels[0].data[0], idx, "panel 0 shares the ordinal index spine");
sameArr(panels[0].data[1], eqPoints, "panel 0 carries equity's null gap verbatim");
sameArr(panels[1].data[1], exPoints, "panel 1 carries exposure's null gaps verbatim");
console.log("OK — null gaps preserved verbatim across overlay + panels, shared union xs.");
process.exit(0);