import { strict as assert } from "node:assert"; import { test } from "node:test"; import { fetchTrendSeries, normalizeRange } from "../../api/src/lib/isp.js"; import { maxTrendRowsForSeries } from "../../shared/trend-preferences.mjs"; import { formatMeasurement, measurementNumber } from "../src/components/measurement.mjs"; import { assignTrendColors, trendDashPattern } from "../src/components/trend-colors.mjs"; test("missing measurements do not become zero", () => { for (const value of [null, undefined, "", " ", NaN, Infinity, {}, []]) { assert.equal(measurementNumber(value), null); assert.equal(formatMeasurement(value), "Kein Messwert"); } for (const value of [0, "0", false]) assert.equal(formatMeasurement(value), "0.00"); assert.equal(formatMeasurement(17.25), "17.25"); }); test("trend range preserves 20,000 by default and exposes the effective query cap", () => { assert.equal(normalizeRange({}, 1, 10).maxRows, 20_000); assert.equal(normalizeRange({ maxRows: 40_000 }, 1, 2).maxRows, 40_000); const capped = normalizeRange({ maxRows: 40_000 }, 1, 10); assert.equal(capped.requestedMaxRows, 40_000); assert.equal(capped.maxRows, maxTrendRowsForSeries(10)); assert.equal(capped.maxRows, 25_000); assert.equal(normalizeRange({ maxRows: -1 }).maxRows, 100); assert.equal(normalizeRange({ maxRows: 100_000 }).maxRows, 50_000); }); test("trend samples are capped after merging sources and keep null distinct from zero", async () => { const points = [ { isp: "isp_a", pointIndex: 1 }, { isp: "isp_b", pointIndex: 1 }, ]; const queriedLimits = []; const pool = { async query(sql, args) { if (sql.includes("information_schema.COLUMNS")) return [[{ columnName: "Wert1" }]]; if (sql.includes("COUNT(*)")) return [[{ rowCount: 60 }]]; if (sql.includes("source_datapoint_registry")) return [[]]; if (sql.includes("Wert1")) { queriedLimits.push(args.at(-1)); const offset = sql.includes("isp_a") ? 0 : 1; return [Array.from({ length: 60 }, (_, i) => ({ datum: new Date(1_700_000_000_000 + (59 - i) * 2000 + offset * 1000), Wert1: i % 2 ? null : 0, }))]; } throw new Error("Unexpected query in fixture: " + sql); }, }; const result = await fetchTrendSeries(pool, { get: async () => null, set: async () => {} }, points, normalizeRange({ maxRows: 100 }, 1, points.length)); assert.deepEqual(queriedLimits, [101, 101]); assert.equal(result.rows.length, 100); assert.equal(result.effectiveMaxRows, 100); assert.equal(result.rowLimitReached, true); assert.equal(result.rows.at(-1)["isp_b:1"], 0); assert.equal(result.rows.at(-3)["isp_b:1"], null); }); test("automatic series have contrast in both themes and manual color is preserved", () => { const luminance = (hex) => hex.slice(1).match(/.{2}/g).map((value) => parseInt(value, 16) / 255) .map((channel) => channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4) .reduce((sum, value, idx) => sum + value * [0.2126, 0.7152, 0.0722][idx], 0); const series = assignTrendColors(Array.from({ length: 100 }, (_, index) => ({ key: String(index), color: index ? "#000000" : "#232323" }))); assert.equal(series[0].color, "#232323"); for (const { color } of series.slice(1)) { for (const theme of ["#292e2c", "#f5f6f5"]) { const [min, max] = [luminance(color), luminance(theme)].sort((a, b) => a - b); assert.ok((max + 0.05) / (min + 0.05) >= 3.2, color + " on " + theme); } } assert.equal(new Set(series.map((_, index) => trendDashPattern(index))).size, series.length); });