import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import { createRequire } from 'node:module'; import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import ts from 'typescript'; const require = createRequire(import.meta.url); function load(path, dependencies = {}) { const source = fs.readFileSync(new URL(path, import.meta.url), 'utf8'); const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX, }, }).outputText; const module = { exports: {} }; new Function('require', 'module', 'exports', code)( (name) => dependencies[name] ?? require(name), module, module.exports, ); return module.exports; } const lib = load('../../resources/js/lib/cluster-competitiveness.ts'); const Page = load('../../resources/js/pages/structures/competitiveness.tsx', { '@/lib/cluster-competitiveness': lib, '@/lib/portal-api': { portalRequest() { throw new Error('SSR must not send requests'); }, PortalApiError: Error, }, '../management/shared': { button: '', control: '' }, '@/lib/editorial-states': { states: { draft: 'Rascunho', approved: 'Aprovado', published: 'Publicado', }, }, }).default; const draft = lib.newCompetitivenessDraft('qa-cluster', 'Energia'); draft.region = 'Local sintético'; for (const force of lib.forces) { draft.forces[force] = { pressure: 'medium', evidence: 'Dado sintético', source: 'Fonte QA', period: '2026', rationale: 'Hipótese', limitations: 'Exemplo', opportunity: 'Ideia', }; } const initial = { version: 4, draft, approved: { draft, version: 4, at: '2026-11-13' }, published: null, editorial_enabled: true, history: [], workflow: { state: 'approved', edition: 1, hash: 'a'.repeat(64), fresh: true, contributors: [1], independent_required: true, }, }; const render = (state = initial, viewerId = 2) => renderToStaticMarkup( React.createElement(Page, { clusterId: 'qa-cluster', sector: 'Energia', initial: state, viewerId, }), ); test('approved cluster edition requires explicit publication and retains a prior public version', () => { const html = render(); assert.match(html, /Publicar edição aprovada/); assert.doesNotMatch(html, /Aprovar torna a versão guardada/); assert.match(html, /Sem análise aprovada para consulta pública/); const later = render({ ...initial, published: { draft, version: 2, at: '2026-11-13' }, workflow: { ...initial.workflow, state: 'draft' }, }); assert.match(later, /Consulta pública: versão 2/); assert.doesNotMatch(later, /Publicar edição aprovada/); assert.match(later, /Submeter para revisão/); }); test('changed context disables publication and a disabled workflow remains read only', () => { const stale = render({ ...initial, workflow: { ...initial.workflow, fresh: false }, }); assert.match(stale, /O contexto do cluster mudou/); assert.match( stale, /]*disabled=""[^>]*>Publicar edição aprovada<\/button>/, ); const disabled = render({ ...initial, editorial_enabled: false }); assert.match(disabled, /