291 lines
9.0 KiB
TypeScript
291 lines
9.0 KiB
TypeScript
import { App } from '@inertiajs/react';
|
|
import { renderToStaticMarkup } from 'react-dom/server';
|
|
import { expect, test } from 'vitest';
|
|
import { ProcurementMenuResults } from '@/components/procurement-menu-results';
|
|
import {
|
|
procurementNavigation,
|
|
activeProcurementEntry,
|
|
type ProcurementGroup,
|
|
} from '@/lib/procurement-navigation';
|
|
import {
|
|
matchesMenuSearch,
|
|
searchProcurementMenu,
|
|
} from '@/lib/procurement-menu-search';
|
|
|
|
test.each([
|
|
'/compras/fornecimento',
|
|
'/compras/fornecimento?view=agreements&record_id=12',
|
|
'/compras/fornecimento?view=contracts&record_id=7#supply-record-7',
|
|
'/compras/acordos/vigencia?validity=current',
|
|
'/compras/acordos/vigencia?kind=contract&search=QA&page=2',
|
|
])(
|
|
'supply agreements and contracts retain authorised menu context: %s',
|
|
(path) => {
|
|
for (const role of [
|
|
'admin',
|
|
'supply_read',
|
|
'supply_prepare',
|
|
'supply_review',
|
|
]) {
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation([role], false),
|
|
path,
|
|
),
|
|
).toBe('supply_agreements');
|
|
}
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['requester'], false),
|
|
path,
|
|
),
|
|
).not.toBe('supply_agreements');
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['admin'], false, ['/dashboard']),
|
|
path,
|
|
),
|
|
).toBeUndefined();
|
|
},
|
|
);
|
|
|
|
test('supply menu synonyms find only the existing authorised destination', () => {
|
|
const groups = procurementNavigation(['supply_read'], false);
|
|
for (const term of [
|
|
'acordos',
|
|
'CONTRATOS',
|
|
'vigencia',
|
|
'fornecimento contrato',
|
|
]) {
|
|
expect(
|
|
searchProcurementMenu(groups, term).flatMap((group) =>
|
|
group.entries.map((entry) => entry.id),
|
|
),
|
|
).toEqual(['supply_agreements']);
|
|
expect(
|
|
searchProcurementMenu(
|
|
procurementNavigation(['requester'], false),
|
|
term,
|
|
),
|
|
).toEqual([]);
|
|
}
|
|
expect(
|
|
activeProcurementEntry(groups, '/compras/fornecimento?view=calendar'),
|
|
).toBe('supply_calendar');
|
|
});
|
|
|
|
test.each([
|
|
'/administracao/prazos-aprovacao',
|
|
'/administracao/revisao-urgencias?history=2',
|
|
])(
|
|
'approval policy subpages retain their authorised administration context: %s',
|
|
(path) => {
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['admin'], false),
|
|
path,
|
|
),
|
|
).toBe('organization_parameters');
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['requester'], false),
|
|
path,
|
|
),
|
|
).toBeUndefined();
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['finance'], false),
|
|
path,
|
|
),
|
|
).toBeUndefined();
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['admin'], false, ['/dashboard']),
|
|
path,
|
|
),
|
|
).toBeUndefined();
|
|
},
|
|
);
|
|
|
|
test.each(['classificacao-pautal', 'base-legal', 'estimativa-importacao'])(
|
|
'import process pages retain the authorised import menu context: %s',
|
|
(page) => {
|
|
const path = `/requisicoes/7/${page}`;
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['import_customs'], false),
|
|
path,
|
|
),
|
|
).toBe('supply_imports');
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['requester'], false),
|
|
path,
|
|
),
|
|
).not.toBe('supply_imports');
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['admin'], false, ['/dashboard']),
|
|
path,
|
|
),
|
|
).toBeUndefined();
|
|
},
|
|
);
|
|
|
|
test.each([
|
|
'documentos',
|
|
'documentos/18',
|
|
'suporte-bancario',
|
|
'acompanhamento',
|
|
'custos',
|
|
])(
|
|
'import documentary page highlights only an existing authorised import entry: %s',
|
|
(page) => {
|
|
const path = `/compras/importacoes/17/${page}`;
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['import_prepare'], false),
|
|
path,
|
|
),
|
|
).toBe('supply_imports');
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['requester'], false),
|
|
path,
|
|
),
|
|
).not.toBe('supply_imports');
|
|
expect(
|
|
activeProcurementEntry(
|
|
procurementNavigation(['admin'], false, ['/dashboard']),
|
|
path,
|
|
),
|
|
).toBeUndefined();
|
|
},
|
|
);
|
|
|
|
test('capital register menu is gated by readiness and native reading roles and highlights its history route', () => {
|
|
for (const permission of [
|
|
'admin',
|
|
'procurement',
|
|
'finance',
|
|
'department_head',
|
|
'warehouse',
|
|
'requester',
|
|
]) {
|
|
for (const ready of [false, true]) {
|
|
const groups = procurementNavigation(
|
|
[permission],
|
|
false,
|
|
undefined,
|
|
false,
|
|
false,
|
|
false,
|
|
ready,
|
|
);
|
|
const results = searchProcurementMenu(groups, 'projeto patrimonio');
|
|
expect(
|
|
results.flatMap((group) =>
|
|
group.entries.map((entry) => entry.id),
|
|
),
|
|
).toEqual(
|
|
ready && permission !== 'requester' ? ['capital_register'] : [],
|
|
);
|
|
if (ready && permission !== 'requester') {
|
|
expect(
|
|
activeProcurementEntry(
|
|
groups,
|
|
'/projectos-imobilizado?history=qa',
|
|
),
|
|
).toBe('capital_register');
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
test('menu search finds the authorised import route by singular name, accents and separate words', () => {
|
|
const groups = procurementNavigation(['import_customs'], false);
|
|
const before = JSON.stringify(groups);
|
|
for (const query of [
|
|
'IMPORTAÇÃO',
|
|
'aduaneira compras',
|
|
'transitario',
|
|
'pauta',
|
|
]) {
|
|
expect(
|
|
searchProcurementMenu(groups, query).flatMap((g) =>
|
|
g.entries.map((e) => e.id),
|
|
),
|
|
).toEqual(['supply_imports']);
|
|
}
|
|
expect(JSON.stringify(groups)).toBe(before);
|
|
});
|
|
|
|
test('synonyms cannot reveal audit, finance or operational routes outside the existing authority and flags', () => {
|
|
const groups = procurementNavigation(['requester'], false);
|
|
for (const query of ['auditoria privada', 'facturas', 'oficina'])
|
|
expect(searchProcurementMenu(groups, query)).toEqual([]);
|
|
const privateGroups = procurementNavigation(
|
|
['audit_private_stock_read'],
|
|
false,
|
|
undefined,
|
|
false,
|
|
false,
|
|
true,
|
|
);
|
|
expect(
|
|
searchProcurementMenu(privateGroups, 'exportar auditoria').flatMap(
|
|
(g) => g.entries.map((e) => e.id),
|
|
),
|
|
).toEqual(['audit_private']);
|
|
});
|
|
|
|
test('empty search preserves every current entry and dashboard matching accepts words in either order', () => {
|
|
const groups = procurementNavigation('admin', true);
|
|
expect(searchProcurementMenu(groups, ' ')).toEqual(groups);
|
|
expect(
|
|
matchesMenuSearch('Painel inicial Visão geral', 'GERAL painel'),
|
|
).toBe(true);
|
|
expect(
|
|
matchesMenuSearch('Painel inicial Visão geral', 'sem correspondência'),
|
|
).toBe(false);
|
|
});
|
|
|
|
function renderResults(groups: ProcurementGroup[], dashboard = false) {
|
|
const Screen = () => (
|
|
<ProcurementMenuResults
|
|
groups={groups}
|
|
dashboard={dashboard}
|
|
onNavigate={() => {}}
|
|
onEscape={() => {}}
|
|
/>
|
|
);
|
|
return renderToStaticMarkup(
|
|
<App
|
|
initialComponent={Screen}
|
|
initialPage={{
|
|
component: 'qa/menu',
|
|
url: '/dashboard',
|
|
version: null,
|
|
clearHistory: false,
|
|
encryptHistory: true,
|
|
props: { errors: {} },
|
|
}}
|
|
/>,
|
|
);
|
|
}
|
|
|
|
test('desktop results expose direct links, their area and a clear live empty state', () => {
|
|
const groups = searchProcurementMenu(
|
|
procurementNavigation(['import_customs'], false),
|
|
'importacao',
|
|
);
|
|
const html = renderResults(groups);
|
|
expect(html).toContain('1 opção disponível');
|
|
expect(html).toContain('href="/compras/fornecimento?view=imports"');
|
|
expect(html).toContain('Importação');
|
|
expect(html).toContain('Resultados da pesquisa do menu');
|
|
expect(renderResults([])).toContain('Nenhuma opção encontrada');
|
|
expect(renderResults([])).toContain('role="status"');
|
|
expect(renderResults(groups, true)).toContain('2 opções disponíveis');
|
|
});
|