import test from 'node:test'; import assert from 'node:assert/strict'; import { catalogueDetailHref, catalogueReturnHref, catalogueQuery, } from '../../resources/js/lib/catalogue-context.ts'; test('return preserves deliberate destination edits, including cleared ancestors, without stale descendants', () => { const detail = catalogueDetailHref( '/carteira/a', 'q=energia&province_id=p&district_id=d&page=4', ); assert.equal( catalogueReturnHref('/carteira?q=turismo&province_id=', detail), '/carteira?q=turismo&province_id=&page=4', ); assert.equal( catalogueReturnHref('/carteira?province_id=other', detail), '/carteira?province_id=other&q=energia&page=4', ); }); test('different catalogue families share geography but not text, kind, price, page or private context', () => { const detail = '/carteira/a?catalogue_query=' + encodeURIComponent( 'q=airport&kind=project&page=4&province_id=p&context=private&min_amount=200', ); assert.equal( catalogueReturnHref('/zonas?category=zee', detail), '/zonas?category=zee&province_id=p', ); assert.equal(catalogueReturnHref('/documentos', detail), '/documentos'); }); test('resource-specific return queries do not inherit from other tabs or mutable storage', () => { const a = catalogueDetailHref( '/oportunidades/a', 'q=rice&facets[]=2&facets[]=3¤cy=MZN', ); const b = catalogueDetailHref('/oportunidades/b', 'q=gas¤cy=USD'); const back = catalogueReturnHref('/oportunidades', a); assert.deepEqual( new URL(back, 'https://portal.invalid').searchParams.getAll('facets[]'), ['2', '3'], ); assert.match(back, /q=rice/); assert.match(catalogueReturnHref('/oportunidades', b), /q=gas/); assert.equal( catalogueReturnHref('/oportunidades', '/oportunidades/c'), '/oportunidades', ); }); test('detail URL preserves existing parameters and fragment; unsafe and recursive filters are not carried', () => { const href = catalogueDetailHref( '/oportunidades/a?from_dossier=origin#details', 'q=rice&context=other&catalogue_query=recursive&password=secret&kind=cluster', ); const url = new URL(href, 'https://portal.invalid'); assert.equal(url.searchParams.get('from_dossier'), 'origin'); assert.equal(url.hash, '#details'); assert.equal(url.searchParams.get('catalogue_query'), 'q=rice'); assert.equal( catalogueDetailHref('https://outside.test/a', 'q=rice'), 'https://outside.test/a', ); }); test('only positive finite page numbers survive; repeated scalar criteria have one deliberate value', () => { assert.equal( catalogueQuery( '/carteira', 'q=old&q=new&page=-1&per_page=Infinity', ).toString(), 'q=new', ); assert.equal( catalogueReturnHref( '/carteira', '/carteira/a?catalogue_query=' + encodeURIComponent('https://outside.test'), ), '/carteira', ); });