commit compras3 2711
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-27 18:56:24 +02:00
parent 930064269e
commit 9cf9764710
87 changed files with 2856 additions and 195 deletions
+249
View File
@@ -0,0 +1,249 @@
import fs from 'node:fs';
import path from 'node:path';
import { App } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { expect, test } from 'vitest';
import {
ActionBar,
FormErrors,
PageTitle,
Panel,
TableViewport,
} from '@/components/procurement';
import RequisitionForm from '@/pages/requisitions/form';
import SupplierEdit from '@/pages/suppliers/edit';
import ProcurementReports from '@/pages/reports/procurement';
import type { Supplier } from '@/types/procurement';
function render(
component: Parameters<typeof App>[0]['initialComponent'],
props: object,
) {
const html = renderToStaticMarkup(
<App
initialComponent={component}
initialPage={{
component: 'qa',
url: '/qa',
version: 'presentation',
clearHistory: false,
encryptHistory: true,
props: { errors: {}, ...props },
}}
/>,
);
if (process.env.SGC_PRESENTATION_PREVIEW) {
const root = path.resolve(process.env.SGC_PRESENTATION_PREVIEW);
const allowed = path.resolve('storage/framework/testing') + path.sep;
if (!root.startsWith(allowed))
throw Error('Private preview path required');
fs.mkdirSync(root, { recursive: true });
const name =
component === RequisitionForm
? 'requisition'
: component === SupplierEdit
? 'supplier'
: 'report';
fs.writeFileSync(
path.join(root, name + '.html'),
'<!doctype html><html lang="pt-MZ"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>SGC - ' +
name +
' - dados sinteticos</title><link rel="stylesheet" href="/preview.css"><body><aside style="padding:12px;background:#fef3c7">Pre-visualizacao estatica isolada com dados sinteticos; sem gravacao.</aside>' +
html +
'</body></html>',
);
}
return html;
}
test('shared panels have distinct accessible titles and preserve their own content', () => {
const html = renderToStaticMarkup(
<>
<Panel title="Dados gerais">
<input name="reference" defaultValue="SINTETICO" />
</Panel>
<Panel title="Historico">
<p>Versao original</p>
</Panel>
</>,
);
const headings = [...html.matchAll(/<h2 id="([^"]+)"/g)].map(
(match) => match[1],
);
expect(new Set(headings).size).toBe(2);
for (const id of headings)
expect(html).toContain(`aria-labelledby="${id}"`);
expect(html).toContain('name="reference"');
expect(html).toContain('Versao original');
});
test('shared actions preserve submission and disabled semantics without adding commands', () => {
const html = renderToStaticMarkup(
<form>
<ActionBar align="end">
<a href="/requisicoes">Cancelar</a>
<button type="submit" disabled>
Guardar rascunho
</button>
</ActionBar>
</form>,
);
expect(html).toContain('role="group"');
expect(html).toContain('href="/requisicoes"');
expect(html).toContain('type="submit" disabled');
expect(html.match(/<button/g)).toHaveLength(1);
});
test('wide result viewport preserves table headers, cells and escaped user text', () => {
const html = renderToStaticMarkup(
<TableViewport label="Resultados autorizados">
<table>
<thead>
<tr>
<th>Referencia</th>
<th>Valor</th>
</tr>
</thead>
<tbody>
<tr>
<td>{'<script>synthetic</script>'}</td>
<td>100,00 MZN</td>
</tr>
</tbody>
</table>
</TableViewport>,
);
expect(html).toContain('tabindex="0"');
expect(html).toContain('aria-label="Resultados autorizados"');
expect(html).toContain('&lt;script&gt;synthetic&lt;/script&gt;');
expect(html).toContain('<td>100,00 MZN</td>');
expect(html.match(/<th>/g)).toHaveLength(2);
});
test('title keeps the single page heading without inserting an empty action area', () => {
const html = renderToStaticMarkup(
<PageTitle
eyebrow="Compras"
title="Nova requisicao"
description="Preparacao sintetica"
/>,
);
expect(html.match(/<h1 /g)).toHaveLength(1);
expect(html).not.toContain('sgc-page-actions');
});
test('shared validation keeps every error and the existing help action', () => {
const html = renderToStaticMarkup(
<FormErrors
errors={{
'items.0.quantity': 'Quantidade invalida',
currency: 'Moeda por indicar',
}}
/>,
);
expect(html).toContain('role="alert"');
expect(html).toContain('Quantidade invalida');
expect(html).toContain('Moeda por indicar');
expect(html).toContain('type="button"');
});
test.each([false, true])(
'requisition preserves existing save guard with incomplete drafts %s',
(incompleteDraftsAvailable) => {
const html = render(RequisitionForm, {
costCenters: [],
requisition: null,
incompleteDraftsAvailable,
});
expect(html).toContain('Guardar rascunho');
expect(html).toContain('sgc-action-bar');
const save = html.slice(
html.lastIndexOf('<button'),
html.lastIndexOf('</button>'),
);
expect(
/\sdisabled(?:=|[ >])/.test(save.slice(0, save.indexOf('>') + 1)),
).toBe(!incompleteDraftsAvailable);
},
);
test('supplier editing keeps the financial revalidation warning and cancellation destination', () => {
const supplier = {
id: 7,
name: 'Fornecedor SINTETICO',
nuit: '',
contact: '',
email: '',
phone: '',
address: '',
category: '',
beneficiaries: '',
document_references: '',
diligence_notes: '',
next_review: '2026-12-01',
blocked: false,
version: 3,
} as Supplier;
const html = render(SupplierEdit, { supplier });
expect(html).toContain('/fornecedores/7');
expect(html).toContain('Guardar alterações');
expect(html).toContain('retiram a validação actual');
expect(html).toContain('sgc-action-bar');
});
test('procurement report retains applied export filters, result scope and both pagination destinations', () => {
const html = render(ProcurementReports, {
records: {
data: [],
total: 0,
current_page: 2,
last_page: 3,
prev_page_url: '/relatorios?status=approved&page=1',
next_page_url: '/relatorios?status=approved&page=3',
},
filters: { status: 'approved', department_id: 9 },
departments: [{ id: 9, name: 'Departamento SINTETICO' }],
counts: {},
generatedAt: '2026-11-27T12:00:00Z',
});
expect(html).toContain(
'/relatorios/compras.csv?status=approved&amp;department_id=9',
);
expect(html).toContain('/relatorios?status=approved&amp;page=1');
expect(html).toContain('/relatorios?status=approved&amp;page=3');
expect(html).toContain('âmbito autorizado');
expect(html).toContain('sgc-table-viewport');
});
test('populated report keeps every authorised row and its process/report actions', () => {
const records = {
data: Array.from({ length: 25 }, (_, i) => ({
id: 100 + i,
number: `REQ-SINTETICA-${i + 1}`,
status: 'approved',
created_at: '2026-11-27T12:00:00Z',
needed_by: '2026-12-01',
department: 'Departamento sintetico de aprovisionamento',
requester: 'Utilizador sintetico',
total_minor: '10000',
quotations_count: 3,
order_number: null,
})),
total: 75,
current_page: 2,
last_page: 3,
prev_page_url: '/relatorios?page=1',
next_page_url: '/relatorios?page=3',
};
const html = render(ProcurementReports, {
records,
filters: {},
departments: [],
counts: {},
generatedAt: '2026-11-27T12:00:00Z',
});
expect(html.match(/Acompanhar processo/g)).toHaveLength(25);
expect(html.match(/Gerar relatório de conferência/g)).toHaveLength(25);
expect(html).toContain('REQ-SINTETICA-25');
expect(html).toContain('/requisicoes/124/relatorio');
});