~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/stream

jetstream v2 in zig · mirror of https://tangled.org/zat.dev/stream

0
0
133 lines · 7.0 KB Raw History
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
const page = await readFile(new URL('../deploy/site/stats.html', import.meta.url), 'utf8');
const source = page.match(/<script type="module" id="rate-history-script">([\s\S]*?)<\/script>/)?.[1];
assert.ok(source, 'the deployed stats page includes the history module');
const { parseHistory, parseProfile, summarize, ranges, selectedRange, fullCoverage, minObservedFraction, upstream } = await import('data:text/javascript;base64,' + Buffer.from(source).toString('base64'));
const start = Date.parse('2026-11-01T00:00:00Z');
const hour = 3600000;
const profile = Array.from({ length: 24 }, (_, h) => ({ med: h * 10, low: h * 5, high: h * 15, samples: 2 }));

test('rates weight event counts by the actual measured duration', () => {
  const points = parseHistory({ name: upstream, points: [
    { ts: '2026-11-01T00:00:00Z', events: 600, window_seconds: 60, connected: true, coverage_pct: 99.7 },
    { ts: '2026-11-01T00:30:00Z', events: 9000, window_seconds: 300, connected: true, coverage_pct: 99.8 },
  ] });
  const bins = summarize(points, profile, start, start + 2 * hour, 1);
  assert.equal(bins[0].actual, 9600 / 360);
  assert.equal(bins[0].samples, 2);
  assert.equal(bins[1].actual, null);
});

test('disconnected and out-of-window observations do not become zero traffic', () => {
  const points = [
    { time: start - 1, events: 900, window: 60, observed: null, connected: true, coverage: 100 },
    { time: start, events: 0, window: 60, observed: null, connected: false, coverage: 0 },
    { time: start + hour, events: 0, window: 60, observed: null, connected: true, coverage: 100 },
    { time: start + 2 * hour, events: 900, window: 60, observed: null, connected: true, coverage: 100 },
  ];
  const bins = summarize(points, profile, start, start + 2 * hour, 1);
  assert.equal(bins[0].actual, null);
  assert.equal(bins[1].actual, 0);
  assert.equal(bins[1].samples, 1);
});

test('windows where the reference connection dropped early are gaps, not traffic dips', () => {
  // Nov 9 2026: bsky.network closed relay-eval's socket ~3 minutes into
  // each peak-hour window; the collector kept counting only what arrived.
  const points = parseHistory({ name: upstream, points: [
    { ts: '2026-11-01T00:06:00Z', events: 150000, window_seconds: 300, connected: true, coverage_pct: 99.8 },
    { ts: '2026-11-01T00:36:00Z', events: 25585, window_seconds: 300, connected: true, coverage_pct: 31.29 },
    { ts: '2026-11-01T01:06:00Z', events: 22000, window_seconds: 300, connected: true, coverage_pct: fullCoverage - 0.01 },
  ] });
  const bins = summarize(points, profile, start, start + 2 * hour, 1);
  assert.equal(bins[0].actual, 500);
  assert.equal(bins[0].samples, 1);
  assert.equal(bins[0].partial, 1);
  assert.equal(bins[1].actual, null);
  assert.equal(bins[1].samples, 0);
  assert.equal(bins[1].partial, 1);
});

test('reconnecting collectors are measured over the time they were subscribed', () => {
  const points = parseHistory({ name: upstream, points: [
    { ts: '2026-11-01T00:06:00Z', events: 144000, window_seconds: 300, connected: true, coverage_pct: 94.2,
      connected_seconds: 288, disconnects: 1 },
    { ts: '2026-11-01T00:36:00Z', events: 20000, window_seconds: 300, connected: true, coverage_pct: 30.1,
      connected_seconds: minObservedFraction * 300 - 1, disconnects: 6 },
    { ts: '2026-11-01T01:06:00Z', events: 150000, window_seconds: 300, connected: true, coverage_pct: 99.9,
      connected_seconds: null, disconnects: null },
  ] });
  const bins = summarize(points, profile, start, start + 2 * hour, 1);
  assert.equal(bins[0].actual, 500);
  assert.equal(bins[0].samples, 1);
  assert.equal(bins[0].partial, 1);
  assert.equal(bins[1].actual, 500);
  assert.throws(() => parseHistory({ name: upstream, points: [
    { ts: '2026-11-01T00:06:00Z', events: 1, window_seconds: 300, connected: true, coverage_pct: 99, connected_seconds: -1 },
  ] }));
});

test('typical buckets weight partial UTC hours and wrap through midnight', () => {
  const [bin] = summarize([], profile, start - hour / 2, start + hour / 2, 1);
  assert.equal(bin.med, 115);
  assert.equal(bin.low, 57.5);
  assert.equal(bin.high, 172.5);
});

test('missing hourly reference data leaves a gap rather than a zero baseline', () => {
  const sparse = profile.map(p => ({ ...p })); sparse[1].samples = 0;
  const [bin] = summarize([], sparse, start, start + 3 * hour, 3);
  assert.equal(bin.med, null);
});

test('all ranges retain the entire requested period with bounded bucket counts', () => {
  for (const { days, bucketHours } of Object.values(ranges)) {
    const end = start + days * 24 * hour;
    const bins = summarize([], profile, start, end, bucketHours);
    assert.equal(bins[0].time, start);
    assert.equal(bins.at(-1).until, end);
    assert.ok(bins.length <= 365);
  }
});

test('history refuses missing durations and malformed values instead of assuming five minutes', () => {
  const point = { ts: '2026-11-01T00:00:00Z', events: 600, window_seconds: 60, connected: true, coverage_pct: 99.7 };
  for (const patch of [{ window_seconds: undefined }, { window_seconds: 0 }, { events: -1 }, { ts: 'bad' }, { connected: 'true' },
    { coverage_pct: undefined }, { coverage_pct: -1 }, { coverage_pct: 101 }]) {
    assert.throws(() => parseHistory({ name: upstream, points: [{ ...point, ...patch }] }));
  }
});

test('profile rejects misordered hours and invalid percentile bands', () => {
  const data = { host: upstream, hours: profile.map((p, h) => ({ h, n: p.samples, med: p.med, p10: p.low, p90: p.high })) };
  assert.equal(parseProfile(data).length, 24);
  data.hours[1].h = 0;
  assert.throws(() => parseProfile(data));
  data.hours[1].h = 1; data.hours[1].p10 = 900;
  assert.throws(() => parseProfile(data));
});


test('the original typical-day view is the default, with history explicitly selected', () => {
  for (const search of ['', '?range=day', '?range=unknown', '?other=value']) {
    assert.equal(selectedRange(search), 'day');
  }
  for (const range of Object.keys(ranges)) assert.equal(selectedRange('?range=' + range), range);
  assert.match(page, /<canvas id="profile"[^>]*>/);
  assert.doesNotMatch(page.match(/<canvas id="profile"[^>]*>/)[0], /hidden/);
  assert.match(page, /<canvas id="rate-history"[^>]* hidden>/);
  assert.match(page, /data-range="day" aria-pressed="true"/);
  assert.ok(!page.includes('Rate measurements as a table'));
  assert.ok(!page.includes('it is a reference, not a saved forecast'));
});

test('history and typical rates follow the relay stream subscribes to', () => {
  // Nov 10 2026: bsky.network delivered ~100/s to relay-eval from 17:00 to
  // 21:00 UTC while stream's upstream, relay1.us-east, measured ~500/s.
  assert.equal(upstream, 'relay1.us-east.bsky.network');
  assert.ok(page.includes('relay-eval.waow.tech/api/rate-profile?host=' + upstream), 'the live ingest baseline reads the same relay');
  assert.throws(() => parseHistory({ name: 'bsky.network', points: [] }));
  assert.throws(() => parseProfile({ host: 'bsky.network', hours: [] }));
});