import { describe, expect, it } from 'vitest' import { activityTabs, buildActivityFiltersKey, buildActivityListHref, buildActivityListSearchParams, buildActivityListWhere, parseActivityListParams, resolveActivityListUrl, } from '@/utilities/activityUi' const NOW = new Date('2026-07-25T12:00:00.000Z') describe('parseActivityListParams', () => { it('defaults to the proximos tab on page 1', () => { expect(parseActivityListParams({})).toEqual({ page: 1, tab: 'proximos' }) }) it('falls back to proximos on an unknown tab', () => { expect(parseActivityListParams({ tab: 'passados' }).tab).toBe('proximos') }) it('accepts every declared tab', () => { for (const tab of activityTabs) { expect(parseActivityListParams({ tab }).tab).toBe(tab) } }) it('honors status only on the todos tab', () => { expect(parseActivityListParams({ status: 'realizado' }).status).toBeUndefined() expect(parseActivityListParams({ tab: 'todos', status: 'realizado' }).status).toBe('realizado') expect(parseActivityListParams({ tab: 'todos', status: 'inexistente' }).status).toBeUndefined() }) it('validates kind and municipality', () => { expect(parseActivityListParams({ kind: 'caminhada' }).kind).toBe('caminhada') expect(parseActivityListParams({ kind: 'festa' }).kind).toBeUndefined() expect(parseActivityListParams({ municipality: '12' }).municipality).toBe(12) expect(parseActivityListParams({ municipality: '0' }).municipality).toBeUndefined() }) }) describe('buildActivityListWhere (tab → where matrix)', () => { it('proximos = planejado/confirmado starting from now', () => { const where = buildActivityListWhere({ page: 1, tab: 'proximos' }, NOW) expect(where).toEqual({ and: [ { status: { in: ['planejado', 'confirmado'] } }, { startAt: { greater_than_equal: NOW.toISOString() } }, ], }) }) it('realizados and rascunhos filter by their status', () => { expect(buildActivityListWhere({ page: 1, tab: 'realizados' }, NOW)).toEqual({ and: [{ status: { equals: 'realizado' } }], }) expect(buildActivityListWhere({ page: 1, tab: 'rascunhos' }, NOW)).toEqual({ and: [{ status: { equals: 'rascunho' } }], }) }) it('todos is unfiltered unless a status is chosen', () => { expect(buildActivityListWhere({ page: 1, tab: 'todos' }, NOW)).toEqual({}) expect(buildActivityListWhere({ page: 1, tab: 'todos', status: 'cancelado' }, NOW)).toEqual({ and: [{ status: { equals: 'cancelado' } }], }) }) it('prepends kind and municipality filters on any tab', () => { const where = buildActivityListWhere( { page: 1, tab: 'realizados', kind: 'comicio', municipality: 3 }, NOW, ) expect(where).toEqual({ and: [ { kind: { equals: 'comicio' } }, { municipality: { equals: 3 } }, { status: { equals: 'realizado' } }, ], }) }) }) describe('buildActivityListSearchParams', () => { it('omits the default tab and page 1', () => { expect(buildActivityListSearchParams({ page: 1, tab: 'proximos' }).toString()).toBe('') expect(buildActivityListSearchParams({ page: 2, tab: 'todos' }).toString()).toBe( 'tab=todos&page=2', ) }) it('serializes filters in canonical order', () => { const params = buildActivityListSearchParams({ page: 1, tab: 'todos', kind: 'caminhada', status: 'cancelado', municipality: 9, }) expect(params.toString()).toBe('tab=todos&kind=caminhada&status=cancelado&municipality=9') }) }) describe('hrefs and canonical URL resolution', () => { it('builds hrefs against the atividades base path', () => { expect(buildActivityListHref({ page: 1, tab: 'proximos' }, 1)).toBe('/campanha/atividades') expect(buildActivityListHref({ page: 1, tab: 'realizados' }, 2)).toBe( '/campanha/atividades?tab=realizados&page=2', ) }) it('derives the filters key from the canonical serialization', () => { expect(buildActivityFiltersKey({ page: 1, tab: 'todos', kind: 'comicio' })).toBe( 'tab=todos&kind=comicio', ) }) it('redirects an explicit default tab out of the URL', () => { const result = resolveActivityListUrl({ tab: 'proximos' }) expect(result.href).toBe('/campanha/atividades') expect(result.redirectHref).toBe('/campanha/atividades') }) it('redirects on unsupported params and clamps the page', () => { expect(resolveActivityListUrl({ foo: 'bar' }).redirectHref).toBe('/campanha/atividades') const clamped = resolveActivityListUrl({ tab: 'todos', page: '50' }, 2) expect(clamped.state.page).toBe(2) expect(clamped.redirectHref).toBe('/campanha/atividades?tab=todos&page=2') }) })