// The pages and components, rendered the way the shell renders them, from the loader's data: the platform's // refusals are the page's own states, a control is offered to whoever may use it, every table has a // caption and scoped headers. import { AppError, buildRegistry, Container, type TransportCaller } from "@conusai-kit/core" import { builtInCatalogues, createI18n, uiLabels } from "@conusai-kit/i18n" import { tenancyModule } from "@conusai-kit/tenancy" import { createDataClient, DataProvider, type PermissionKey, PermissionsProvider } from "@conusai-kit/ui/data" import { UiLabelsProvider } from "@conusai-kit/ui/primitives" import type { ReactNode } from "react" import { renderToStaticMarkup as renderMarkup } from "react-dom/server" import { describe, expect, it, vi } from "vitest" import { modulePages } from "../../pages.js" import { catalogModule } from "../module.js" import { catalogPage } from "../ui/pages.js" const catalogues = buildRegistry([tenancyModule, catalogModule]).catalogues const i18n = createI18n("en", { en: { catalog: catalogues.en?.catalog ?? {}, app: { failed: "That did not work: {detail}" } }, }) const lithuanian = createI18n("lt", { lt: { catalog: catalogues.lt?.catalog ?? {}, app: { failed: "Nepavyko: {detail}" } }, }) const slugs = { org: "acme", workspace: "ws", project: "p" } const EVERYTHING: readonly PermissionKey[] = ["catalog.product.read", "catalog.product.write"] const kitStrings = uiLabels(createI18n("en", builtInCatalogues)) const renderToStaticMarkup = (node: ReactNode) => renderMarkup({node}) const inProvider = (node: ReactNode, permissions: readonly PermissionKey[] = EVERYTHING) => renderToStaticMarkup( new Response(null, { status: 501 }) })}> {node} , ) const callerOf = (answers: Record): TransportCaller => ({ call: async (contract: { name: string }) => { const answer = answers[contract.name] if (answer instanceof Error) throw answer return answer }, }) as unknown as TransportCaller /** The page as the shell renders it: the loader's data through JSON, as the browser receives it, then the view. */ const render = async (segments: string[], answers: Record, permissions?: readonly PermissionKey[]) => { const data = await catalogPage.load({ slugs, segments, search: {}, caller: callerOf(answers), i18n, container: new Container(), }) expect(JSON.parse(JSON.stringify(data))).toEqual(data) const { View } = catalogPage return inProvider( , permissions, ) } const product = { productId: "prd_1", code: "BOLT-M8", name: "Bolt M8", description: "A bolt", baseUnit: "C62", status: "active", groupCode: null, identifiers: [{ kind: "gtin", value: "4006381333931" }], revision: "3", } const escaped = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") /** Every label of a form names a control the page holds, and every hint is in its control's description. */ const expectLabelled = (html: string, labels: number): void => { const targets = [...html.matchAll(/]* for="([^"]+)"/g)].map((match) => match[1] ?? "") expect(targets).toHaveLength(labels) for (const id of targets) expect(html).toMatch(new RegExp(`<(?:input|select|button)[^>]* id="${escaped(id)}"`)) for (const [, id = ""] of html.matchAll(/

{ it("lists the products, with the way to the imports", async () => { const html = await render([], { "catalog.product.list": { rows: [product] } }) expect(html).toMatch(/]*>Products<\/h1>/) expect(html).toMatch( /]*href="\/acme\/ws\/p\/catalog\/prd_1")(?=[^>]*aria-label="Open Bolt M8")[^>]*>Bolt M8<\/a>/, ) expect(html).toMatch(/]*aria-label="Products"/) expect(html).toMatch(/]*role="columnheader"[^>]*>Code<\/th>/) expect(html).toContain('href="/acme/ws/p/catalog/imports"') }) it("shows one product with its identifiers and revision", async () => { const html = await render(["prd_1"], { "catalog.product.get": product }) expect(html).toContain("

Bolt M8

") expect(html).toContain("
BOLT-M8
") expect(html).toContain("
3
") expect(html).toContain("4006381333931") expect(html).toContain('href="/acme/ws/p/catalog"') }) it("lists the imports, with the form for whoever may apply one", async () => { const answers = { "catalog.import.list": { rows: [applied] } } const html = await render(["imports"], answers) expect(html).toMatch(/]*>Imports<\/h1>/) expect(html).toMatch(/]*scope="row"[^>]*>products\.csv<\/th>/) expect(html).toMatch(/]*>Imports<\/caption>/) expect(html).toContain('type="file"') expectLabelled(html, 1) expect(html.match(/data-slot="field-description"/g)).toHaveLength(1) expect(await render(["imports"], answers, ["catalog.product.read"])).not.toContain('type="file"') }) it.each([ ["service_not_enabled", "This service is not enabled for your organization."], ["service_permission_missing", "This app is not allowed to use this service. Ask an administrator to grant it."], ["business_unavailable", "The service did not answer. Try again later."], ])("shows the kit's state %s", async (code, text) => { const refused = new AppError({ type: `/problems/${code}`, title: "Refused", status: 503, code, detail: "why" }) const html = await render([], { "catalog.product.list": refused }) expect(html).toContain(`data-state="${code}"`) expect(html).toContain('role="status"') expect(html).toContain(text) expect(html).not.toContain(" { const code = "service_not_enabled" const refused = new AppError({ type: `/problems/${code}`, title: "Refused", status: 404, code }) const data = await catalogPage.load({ slugs, segments: [], search: {}, caller: callerOf({ "catalog.product.list": refused }), i18n: lithuanian, container: new Container(), }) expect(data).toMatchObject({ kind: "state", notice: { code, text: "Ši paslauga jūsų organizacijai neįjungta." }, }) }) it("leaves a product that is not there, and a path it does not have, to the shell", async () => { const missing = new AppError({ type: "/problems/business_not_found", title: "Not found", status: 404, code: "business_not_found", }) await expect(render(["prd_2"], { "catalog.product.get": missing })).rejects.toMatchObject({ problem: { code: "business_not_found" }, }) await expect(render(["prd_1", "x"], {})).rejects.toMatchObject({ problem: { status: 404 } }) await expect(render(["imports", "x"], {})).rejects.toMatchObject({ problem: { status: 404 } }) await expect(render(["%2Fetc"], {})).rejects.toMatchObject({ problem: { status: 404 } }) await expect(render(["%E0%A4%A"], {})).rejects.toMatchObject({ problem: { status: 404 } }) }) it("is listed for the shell", async () => { await expect(modulePages.catalog?.()).resolves.toBe(catalogPage) }) }) // Explicit synthetic admission: module-local fixtures exercise the selected module. vi.mock("../../inclusion.json", () => ({ default: { schemaVersion: 1, selected: ["catalog"], everAdopted: ["catalog"] }, }))