teqo-backup/tests/unit/wizardReturnPath.unit.spec.ts
Cursor Agent d5697bcfcb
B110 — wizard return path via allowlisted from query
Propagate return path from quick actions through the B98 wizard chain so
dismiss, skip, and chain end navigate back to the originating page instead
of always redirecting to Início.

Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
2026-08-01 20:39:24 +00:00

51 lines
2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
appendWizardReturnPath,
isWizardReturnPath,
parseWizardReturnPath,
WIZARD_RETURN_PATH_QUERY_KEY,
} from '@/lib/campaignActionRoutes'
import { CAMPAIGN_HOME } from '@/lib/campaignPaths'
describe('wizard return path (B110)', () => {
it('allowlists internal /campanha paths outside acoes and auth', () => {
expect(isWizardReturnPath('/campanha')).toBe(true)
expect(isWizardReturnPath('/campanha/municipios/cairu')).toBe(true)
expect(isWizardReturnPath('/campanha/liderancas/42')).toBe(true)
expect(isWizardReturnPath('/campanha/acoes/mudar-tendencia')).toBe(false)
expect(isWizardReturnPath('/campanha/login')).toBe(false)
expect(isWizardReturnPath('/campanha/convite/token')).toBe(false)
expect(isWizardReturnPath('https://evil.test/phish')).toBe(false)
expect(isWizardReturnPath('/public')).toBe(false)
})
it('parseWizardReturnPath fails closed on invalid values', () => {
expect(parseWizardReturnPath(undefined)).toBeUndefined()
expect(parseWizardReturnPath('/campanha/municipios/foo')).toBe('/campanha/municipios/foo')
expect(parseWizardReturnPath('/campanha/acoes/foo')).toBeUndefined()
expect(parseWizardReturnPath('//evil.test')).toBeUndefined()
expect(parseWizardReturnPath('/campanha/foo?x=1')).toBeUndefined()
})
it('appendWizardReturnPath adds the from query key', () => {
const href = appendWizardReturnPath(
'/campanha/acoes/mudar-tendencia?municipio=cairu',
'/campanha/municipios/cairu',
)
expect(href).toContain(`${WIZARD_RETURN_PATH_QUERY_KEY}=`)
expect(href).toContain(
`${WIZARD_RETURN_PATH_QUERY_KEY}=${encodeURIComponent('/campanha/municipios/cairu')}`,
)
})
it('appendWizardReturnPath ignores invalid return paths', () => {
expect(appendWizardReturnPath('/campanha/acoes/foo', '/campanha/acoes/bar')).toBe(
'/campanha/acoes/foo',
)
})
it('CAMPAIGN_HOME is a valid return path', () => {
expect(parseWizardReturnPath(CAMPAIGN_HOME)).toBe(CAMPAIGN_HOME)
})
})