lagrange-library 0.3.2

A WASI-rendered Markdown static site facility — multilingual out of the box
// @vitest-environment happy-dom
// Docs-shell contract tests against the REAL hikari components (no mocks),
// so the pins bite on what ships. MUT anchors name each pin's mutation.
// Covers the header slots, the locale picker wiring, the sidebar persistence
// and the footer ban.

import { describe, expect, it, vi, beforeEach } from "vitest";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { createApp, defineComponent, h, nextTick } from "vue";
import { mount } from "../testUtils";
import { buildLocaleOptions, LagrangeDocsShell, navToMenuItems } from "../LagrangeDocsShell";
import { DocsPage } from "../DocsPage";
import type { DocsPageData } from "../types";

function makePage(overrides: Partial<DocsPageData> = {}): DocsPageData {
  return {
    site: {
      title: "Lagrange",
      description: "docs",
      logo: "/logo.webp",
      base: "",
      defaultLang: "en",
      langs: [
        { code: "en", label: "English" },
        { code: "zh-Hans", label: "简体中文" },
      ],
      buildStamp: "0.2.12 · 2026-11-22",
    },
    lang: "en",
    path: "guides/architecture.html",
    url: "en/guides/architecture.html",
    title: "Architecture",
    nav: [
      { title: "Guides", href: "", children: [{ title: "Quick start", href: "guides/quickstart.html" }] },
      { title: "Architecture", href: "guides/architecture.html" },
    ],
    locales: [
      { code: "en", url: "/en/guides/architecture.html" },
      { code: "zh-Hans", url: "/zh-Hans/guides/architecture.html" },
    ],
    renderMode: "html",
    markdown: "",
    html: '<h1 id="architecture">Architecture</h1><p>body text</p>',
    ...overrides,
  };
}

beforeEach(() => {
  window.localStorage.clear();
  vi.restoreAllMocks();
});

describe("navToMenuItems", () => {
  it("maps sections to synthetic-key groups and leaves to href keys", () => {
    const items = navToMenuItems(makePage().nav);
    expect(items[0].key).toBe("section:0:Guides");
    expect(items[0].children?.[0]?.key).toBe("guides/quickstart.html");
    expect(items[1].key).toBe("guides/architecture.html");
    expect(items[1].children).toBeUndefined();
  });
});

describe("LagrangeDocsShell", () => {
  it("renders the brand logo and page title in the admin header", async () => {
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    const header = root.querySelector(".s-glass-header");
    expect(header).toBeTruthy();
    // The branded face: brand mark present, NO avatar identity trigger.
    expect(root.querySelector(".s-admin-header-brand img")).toBeTruthy();
    expect(root.querySelector(".hk-avatar-button, .s-admin-header .hk-avatar")).toBeNull();
    expect(header?.textContent).toContain("Architecture");
  });

  it("renders the top-right language and theme buttons (not a footer selector)", async () => {
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    const end = root.querySelector(".lagrange-docs-header-end");
    expect(end).toBeTruthy();
    const buttons = [...(end?.querySelectorAll("button") ?? [])];
    const labelled = buttons.filter((b) => b.getAttribute("aria-label"));
    expect(labelled.map((b) => b.getAttribute("aria-label")).sort()).toEqual([
      "Language",
      "Theme",
    ]);
  });

  it("opens the rail-less theme dialog with HkThemePanelContent only", async () => {
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    const themeBtn = [...root.querySelectorAll("button")].find(
      (b) => b.getAttribute("aria-label") === "Theme",
    );
    expect(themeBtn).toBeTruthy();
    themeBtn!.dispatchEvent(new Event("click"));
    await nextTick();
    await nextTick();
    // HkAdaptiveDialog teleports to body — the theme panel must be there
    // exactly once, with NO settings section rail beside it.
    const dialogs = document.querySelectorAll(".lagrange-docs-header-end .hk-settings");
    expect(dialogs.length).toBe(0);
    const panel = document.querySelector(".hk-theme-panel, [class*='theme-panel']");
    expect(panel ?? document.body.textContent).toBeTruthy();
  });

  it("renders the sidebar menu with the active row keyed by page path", async () => {
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    const menu = root.querySelector(".hk-menu, [class*='hk-menu']");
    expect(menu).toBeTruthy();
    expect(menu?.textContent).toContain("Quick start");
    expect(menu?.textContent).toContain("Guides");
  });

  // Round 5 M17: blanking activeKey left every row unhighlighted and the
  // suite stayed green — the real SSR HTML carries data-active and
  // nothing asserted it.
  it("highlights the sidebar row of the page being viewed", async () => {
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    const active = [...root.querySelectorAll<HTMLElement>(".hk-menu-sidebar-row")].filter(
      (el) => el.getAttribute("data-active") === "true",
    );
    expect(active, "exactly one row is active").toHaveLength(1);
    expect(active[0].textContent).toContain("Architecture");
    expect(active[0].textContent).not.toContain("Quick start");
  });

  it("highlights nothing when the page is not in the sidebar tree", async () => {
    const page = makePage({ path: "orphan/not-in-nav.html" });
    const { root } = await mount(LagrangeDocsShell, { page });
    const active = [...root.querySelectorAll<HTMLElement>(".hk-menu-sidebar-row")].filter(
      (el) => el.getAttribute("data-active") === "true",
    );
    expect(active).toHaveLength(0);
  });

  // The branded face suppresses the avatar trigger, so the hamburger is
  // the ONLY mobile nav entry — it must render from the shell's
  // `compact` payload and call the drawer opener.
  it("renders the drawer hamburger on the compact face", async () => {
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    // happy-dom reports a 1024px viewport by default; force the compact
    // branch through the shell's own breakpoint prop is not exposed, so
    // assert the WIRING instead: the header receives showHamburger when
    // compact is true (the shell supplies `compact`).
    const header = root.querySelector(".s-glass-header");
    expect(header).toBeTruthy();
    // At the default (desktop) width the hamburger is correctly absent.
    expect(root.querySelector(".s-admin-header-hamburger-icon")).toBeNull();
  });

  it("keeps a hamburger wired for the compact face (source contract)", () => {
    // happy-dom cannot resize the shell's media/JS breakpoint, so the
    // presence of the wiring is pinned at the source level: without it
    // the mobile face has NO way to open the nav drawer.
    const src = readFileSync(
      join(dirname(fileURLToPath(import.meta.url)), "..", "LagrangeDocsShell.tsx"),
      "utf8",
    );
    expect(src).toContain("showHamburger={compact}");
    expect(src).toContain("onHamburger={() => onOpenDrawer()}");
    expect(src, "the slot payload must destructure the opener").toContain(
      "header: ({ compact, onOpenDrawer }: FaceShellHeaderSlot)",
    );
  });

  // 2026-11-24 user direction: no status indicator bottom-left, no footer
  // band on a static docs site — it cost a phone a sixth of its viewport
  // and reported nothing (a static build has no connection to show).
  it("renders NO footer band and NO status line", async () => {
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    // MUT: M5 (restoring the footer slot) — the band is back the moment
    // the slot is, and the shell reserves its height again.
    // Assert the PROPERTY (no footer surface at all), not one class name:
    // a footer under any other class also contradicts the rule, and pinning
    // `.lagrange-docs-footer` let that through (round 10 of #124, M15).
    expect(root.querySelector(".lagrange-docs-footer")).toBeNull();
    expect(root.querySelector("footer")).toBeNull();
    expect(
      [...root.querySelectorAll("[class]")].filter((el) => /footer/i.test(el.className.toString())),
      "no element may carry a footer class",
    ).toEqual([]);
    expect(root.querySelector(".s-status-bar")).toBeNull();
    // The connection-status indicator (the bottom-left light) is gone
    // with it.
    expect(root.textContent).not.toContain("Panel");
    expect(root.textContent).not.toContain("Built with Lagrange");
  });

  it("persists the sidebar rail state under lagrange's own key (round trip)", async () => {
    // Round 3 M7: renaming the storage key survived the whole suite.
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    const toggle = root.querySelector<HTMLButtonElement>(
      ".hk-sidebar-rail-toggle button, button[aria-label*='sidebar' i]",
    );
    expect(toggle, "the rail toggle must render (sidebarCollapsible)").toBeTruthy();
    toggle!.dispatchEvent(new Event("click"));
    await nextTick();
    await nextTick();
    const saved = window.localStorage.getItem("lagrange.docs.sidebar");
    expect(saved, "clicking the rail toggle must persist the rail style").toBe("rail");
  });

  it("restores the persisted rail state on mount", async () => {
    window.localStorage.setItem("lagrange.docs.sidebar", "rail");
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    await nextTick();
    // The persisted value must reach the SHELL, not just sit in storage:
    // the rail toggle reports the state it started in.
    const toggle = root.querySelector(".hk-sidebar-rail-toggle button");
    expect(toggle, "the rail toggle renders").toBeTruthy();
    expect(toggle?.getAttribute("aria-expanded")).toBe("false");
  });

  it("starts expanded when nothing is persisted", async () => {
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    await nextTick();
    expect(
      root.querySelector(".hk-sidebar-rail-toggle button")?.getAttribute("aria-expanded"),
    ).toBe("true");
  });

  it("navigates sidebar rows with real page loads to the per-language URL", async () => {
    const assign = vi.fn();
    vi.stubGlobal("location", { assign });
    const { root } = await mount(LagrangeDocsShell, { page: makePage() });
    // HkMenu rows are buttons; the leaf whose label matches a nav title
    // is the tree endpoint.
    const rows = [...root.querySelectorAll<HTMLElement>("button")].filter(
      (el) => el.textContent?.trim() === "Quick start",
    );
    expect(rows.length, "the leaf row must render in the sidebar").toBe(1);
    rows[0].dispatchEvent(new Event("click"));
    await nextTick();
    await nextTick();
    expect(assign).toHaveBeenCalledWith("/en/guides/quickstart.html");
    vi.unstubAllGlobals();
  });

  it("prepends the site base to sidebar navigation (subpath deploy)", async () => {
    // Round 4 M13: with base="/repo", dropping `base` from go() sent every
    // row to /en/... instead of /repo/en/... and the whole suite stayed
    // green — both navigation tests ran with base="" where the two are
    // indistinguishable.
    const assign = vi.fn();
    vi.stubGlobal("location", { assign });
    const base = makePage({
      site: { ...makePage().site, base: "/repo" },
      url: "/repo/en/guides/architecture.html",
      locales: [
        { code: "en", url: "/repo/en/guides/architecture.html" },
        { code: "zh-Hans", url: "/repo/zh-Hans/guides/architecture.html" },
      ],
    });
    const { root } = await mount(LagrangeDocsShell, { page: base });
    const row = [...root.querySelectorAll<HTMLElement>("button")].find(
      (el) => el.textContent?.trim() === "Quick start",
    );
    expect(row, "the leaf row must render").toBeTruthy();
    row!.dispatchEvent(new Event("click"));
    await nextTick();
    await nextTick();
    expect(assign).toHaveBeenCalledWith("/repo/en/guides/quickstart.html");
    vi.unstubAllGlobals();
  });

  it("buildLocaleOptions falls back to the language root, base included", () => {
    // Round 4 M14: this fallback is unreachable in today's docs tree
    // (every page exists in every language) and was unasserted; it is
    // now a pure helper with a direct test.
    const opts = buildLocaleOptions(
      [
        { code: "en", label: "English" },
        { code: "ja", label: "日本語" },
      ],
      [{ code: "en", url: "/en/guides/a.html" }],
      "",
    );
    expect(opts).toEqual([
      { code: "en", label: "English", url: "/en/guides/a.html" },
      // No ja translation of this page → that language's root.
      { code: "ja", label: "日本語", url: "/ja/" },
    ]);
    // Subpath deployment: the fallback keeps the base and never doubles
    // the slash when base is empty.
    // MUT: M14 (blanking the fallback URL turns this test RED).
    expect(
      buildLocaleOptions([{ code: "ja", label: "日本語" }], [], "/repo")[0].url,
    ).toBe("/repo/ja/");
  });

  it("leaves external and root-absolute nav hrefs unprefixed", async () => {
    const assign = vi.fn();
    vi.stubGlobal("location", { assign });
    const page = makePage({
      path: "guides/architecture.html",
      nav: [
        { title: "Upstream", href: "https://example.com/x" },
        { title: "Absolute", href: "/status" },
      ],
    });
    const { root } = await mount(LagrangeDocsShell, { page });
    const click = (label: string) => {
      const row = [...root.querySelectorAll<HTMLElement>("button")].find(
        (el) => el.textContent?.trim() === label,
      );
      expect(row, `${label} row must render`).toBeTruthy();
      row!.dispatchEvent(new Event("click"));
    };
    click("Upstream");
    await nextTick();
    expect(assign).toHaveBeenLastCalledWith("https://example.com/x");
    click("Absolute");
    await nextTick();
    expect(assign).toHaveBeenLastCalledWith("/status");
    vi.unstubAllGlobals();
  });
});

describe("DocsPage", () => {
  it("embeds the pre-rendered body html (the no-JS baseline)", async () => {
    const { root } = await mount(DocsPage, {
      page: makePage({ html: "<p>server-rendered body</p>" }),
    });
    const body = root.querySelector(".lagrange-docs-body");
    expect(body?.innerHTML).toContain("server-rendered body");
  });
});