import assert from "node:assert/strict"; import { test } from "node:test"; import { UniqueID } from "@repo/rdx-ddd"; import { Maybe } from "@repo/rdx-utils"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { createInstance } from "i18next"; import { renderToStaticMarkup } from "react-dom/server"; import { I18nextProvider } from "react-i18next"; import { buildCatalogResponse } from "../../../api/application/asset-catalog-response.builder"; import { AssetCatalog } from "../../../api/domain/asset-catalog.aggregate"; import { seedAssetCatalog } from "../../../api/domain/asset-catalog-seed"; import { InfrastructureValues } from "../../../api/domain/value-objects/infrastructure-values.vo"; import { InfrastructureSchema } from "../../../common/infrastructure"; import { AssetSchema } from "../../../common/inventory"; import { catalogKeys } from "../../catalog/shared/api"; import { assetsEnglish, assetsSpanish } from "../../shared"; import { inventoryKeys } from "../../shared/queries"; import { InfrastructurePage } from "../list/ui/infrastructure-page"; import { infrastructureKeys } from "./api/infrastructure-api"; import { infrastructureRoles } from "./helpers/infrastructure-roles"; import { AssetInfrastructurePanel } from "./ui/asset-infrastructure-panel"; const companyId = UniqueID.generateNewID().toPrimitive(); const catalog = buildCatalogResponse(seedAssetCatalog(AssetCatalog.create()), Maybe.none()); const router = catalog.items.find((item) => item.builtinKey === "template:router"); assert.ok(router && router.values.kind === "template"); const templateId = router.id; const values = { name: "Router principal", templateId, siteId: null, status: "operational", notes: "", characteristics: {}, }; const asset = AssetSchema.parse({ id: UniqueID.generateNewID().toPrimitive(), companyId, values, assigned: false, displayStatus: "available", specification: { templateId, templateName: "Router", fields: [ { fieldId: UniqueID.generateNewID().toPrimitive(), name: "Modelo", type: "text", unit: "", choices: [], required: false, }, ], }, history: [ { id: UniqueID.generateNewID().toPrimitive(), values, actorId: UniqueID.generateNewID().toPrimitive(), actorName: "Técnico", recordedAt: "2026-10-17T10:00:00.000Z", }, ], }); const networkValues = InfrastructureValues.create({ roles: ["network"], interfaces: [ { name: "LAN", kind: "ethernet", mac: "", notes: "", addresses: [{ address: "192.168.5.201" }], }, { name: "Wi-Fi", kind: "wifi", mac: "", notes: "", addresses: [{ address: "192.168.5.202" }] }, ], }); assert.ok(networkValues.isSuccess); const snapshot = networkValues.data.toPrimitive(); const config = InfrastructureSchema.parse({ id: UniqueID.generateNewID().toPrimitive(), assetId: asset.id, companyId, values: snapshot, history: [ { id: UniqueID.generateNewID().toPrimitive(), actorId: UniqueID.generateNewID().toPrimitive(), actorName: "Técnico", recordedAt: "2026-10-17T10:00:00.000Z", values: snapshot, }, ], }); test("infraestructura: clasificación de serie independiente del nombre/icono y selección manual prioritaria", () => { assert.ok(router.values.kind === "template"); const renamed = { ...router, values: { ...router.values, name: "Nombre propio", icon: "box" as const }, }; assert.deepEqual(infrastructureRoles(templateId, [renamed]), ["network"]); assert.deepEqual( infrastructureRoles(templateId, [renamed], { ...config, values: { ...config.values, roles: ["storage"] }, }), ["storage"] ); assert.deepEqual(infrastructureRoles(templateId, [{ ...renamed, builtinKey: null }]), []); }); test("infraestructura: listado y ficha presentan interfaces/IP en español e inglés, sin puertos", async () => { for (const [language, title, edit] of [ ["es", "Infraestructura", "Editar infraestructura"], ["en", "Infrastructure", "Edit infrastructure"], ] as const) { const i18n = createInstance(); await i18n.init({ lng: language, resources: { es: { assets: assetsSpanish }, en: { assets: assetsEnglish } }, }); const client = new QueryClient(); client.setQueryData(catalogKeys.scope(companyId), catalog); client.setQueryData(inventoryKeys.company(companyId), { assets: [asset], workstations: [], relationships: [], }); client.setQueryData(infrastructureKeys.company(companyId), { items: [config] }); try { const html = renderToStaticMarkup( ); assert.ok(html.includes(title)); assert.ok(html.includes(edit)); assert.ok(html.includes("192.168.5.201")); assert.ok(html.includes("192.168.5.202")); assert.ok(html.includes("Wi-Fi")); assert.ok(html.includes(`/companies/${companyId}/assets/${asset.id}`)); assert.ok(html.includes('aria-haspopup="dialog"')); assert.equal(html.includes("infrastructure.roles.network"), false); } finally { client.clear(); } } });