/* eslint-disable no-restricted-syntax -- test fixtures use real display strings, not i18n UI descriptors */ import { Buffer } from "node:buffer"; import { describe, expect, test } from "vitest"; import type { DataTableModel } from "@hivegrid/view-models"; import { createMockPage, createVisualSnapshot, generateMockArtifacts, generateMockPageHtml, renderDataTableMock, type HotSwapDrillData, } from "./index.js"; describe("createVisualSnapshot byte accounting", () => { test("counts plain ASCII output as one byte per character", () => { const page = createMockPage({ id: "ascii", route: "/ascii", title: "Plain", description: "ascii only", state: { status: "ready", data: { cards: [{ labelKey: "mock.trend.up", value: "100", trend: "up" }], activity: [{ labelKey: "mock.trend.flat", meta: "x" }], }, }, }); const { html, module, snapshot } = generateMockArtifacts(page); // For pure ASCII, the custom byteLength must equal the JS string length. expect(snapshot.metrics.htmlBytes).toBe(html.length); expect(snapshot.metrics.moduleBytes).toBe(module.length); // And it must agree with the UTF-8 reference implementation. expect(snapshot.metrics.htmlBytes).toBe(Buffer.byteLength(html, "utf8")); expect(snapshot.metrics.moduleBytes).toBe(Buffer.byteLength(module, "utf8")); }); test("matches UTF-8 byte length for 2-, 3- and 4-byte code points", () => { // "é" = 2 bytes (U+00E9), "€" = 3 bytes (U+20AC), "🚀" = 4 bytes (surrogate pair). const page = createMockPage({ id: "utf8", route: "/utf8", title: "café €5 🚀", description: "Übersicht — 🚀 launch", state: { status: "ready", data: { cards: [{ labelKey: "mock.trend.up", value: "naïve €1.234,56 🚀", trend: "up" }], activity: [{ labelKey: "mock.trend.flat", meta: "déjà 🚀" }], }, }, }); const { html, module, snapshot } = generateMockArtifacts(page); // Reference UTF-8 byte length must be reproduced exactly across all branches. expect(snapshot.metrics.htmlBytes).toBe(Buffer.byteLength(html, "utf8")); expect(snapshot.metrics.moduleBytes).toBe(Buffer.byteLength(module, "utf8")); // Multi-byte chars make the byte count exceed the JS string length. expect(snapshot.metrics.htmlBytes).toBeGreaterThan(html.length); expect(snapshot.metrics.moduleBytes).toBeGreaterThan(module.length); }); test("reports zero shell metrics for a page without registries", () => { const page = createMockPage({ id: "bare", route: "/bare", state: { status: "empty" } }); const snapshot = createVisualSnapshot(page); expect(snapshot.metrics.sidebarItems).toBe(0); expect(snapshot.metrics.breadcrumbItems).toBe(0); expect(snapshot.metrics.mobileItems).toBe(0); expect(snapshot.metrics.portalItems).toBe(0); }); test("counts only the registry slots that are present", () => { const page = createMockPage({ id: "partial", route: "/partial", state: { status: "empty" }, registries: { sidebar: [ { id: "a", href: "/a", labelKey: "mock.root" }, { id: "b", href: "/b", labelKey: "mock.root" }, ], // breadcrumbs/mobile/portals deliberately omitted -> must fall back to 0. }, }); const snapshot = createVisualSnapshot(page); expect(snapshot.metrics.sidebarItems).toBe(2); expect(snapshot.metrics.breadcrumbItems).toBe(0); expect(snapshot.metrics.mobileItems).toBe(0); expect(snapshot.metrics.portalItems).toBe(0); }); }); describe("generateMockPageHtml title/description resolution", () => { test("prefers literal title/description over the id fallback", () => { const page = createMockPage({ id: "lit", route: "/lit", title: "Literal Title", description: "Literal Description", state: { status: "empty" }, }); const html = generateMockPageHtml(page); expect(html).toContain("

Literal Title

"); expect(html).toContain("

Literal Description

"); }); test("falls back to the page id and a static description key", () => { const page = createMockPage({ id: "fallback-id", route: "/f", state: { status: "empty" } }); const html = generateMockPageHtml(page); expect(html).toContain("

fallback-id

"); expect(html).toContain("mock.static.fallback-id"); }); }); describe("generateMockPageHtml hot-swap data detection", () => { test("omits cards/activity when state.data is absent", () => { const page = createMockPage({ id: "no-data", route: "/n", state: { status: "empty" } }); const html = generateMockPageHtml(page); expect(html).not.toContain('class="mock-card-value"'); // The serialized state summary (HTML-escaped) reports both counts as zero. expect(html).toContain(""cards": 0"); expect(html).toContain(""activity": 0"); }); test("rejects partial shapes where activity is not an array", () => { // cards is an array but activity is not -> not HotSwapDrillData, no cards render. const page = createMockPage({ id: "partial-data", route: "/p", state: { status: "ready", data: { cards: [{ labelKey: "mock.trend.up", value: "9", trend: "up" }], activity: "nope" } }, }); const html = generateMockPageHtml(page); expect(html).not.toContain('class="mock-card-value"'); expect(html).toContain(""cards": 0"); }); test("rejects partial shapes where cards is not an array", () => { const page = createMockPage({ id: "partial-data-2", route: "/p2", state: { status: "ready", data: { cards: "nope", activity: [{ labelKey: "mock.trend.flat", meta: "m" }] } }, }); const html = generateMockPageHtml(page); expect(html).not.toContain('class="mock-card-value"'); expect(html).toContain(""activity": 0"); }); }); describe("renderDataTableMock edge cases", () => { const base: DataTableModel = { columns: [ { key: "name", labelKey: "ui.data-table.column.name", align: "left" }, { key: "amount", labelKey: "ui.data-table.column.amount", align: "right" }, ], rows: [{ id: "row-1", cells: { name: "Alpha", amount: "10" } }], emptyLabelKey: "ui.data-table.empty", rowCount: 1, }; test("omits the caption row when captionKey is absent", () => { const html = renderDataTableMock(base); expect(html).not.toContain('data-row="caption"'); }); test("emits the caption row when captionKey is present", () => { const html = renderDataTableMock({ ...base, captionKey: "ui.data-table.caption" }); expect(html).toContain('data-row="caption" data-i18n-key="ui.data-table.caption"'); }); test("renders an empty cell when a row is missing a column value", () => { const html = renderDataTableMock({ ...base, rows: [{ id: "row-gap", cells: { name: "Alpha" } as Record }], }); expect(html).toContain('data-row-id="row-gap"'); // The missing "amount" cell collapses to an empty body span, not "undefined". expect(html).toContain(''); expect(html).not.toContain("undefined"); }); });