From 7f2ac2936814ac8a476ea9773859b41f8fa914df Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 1 Aug 2026 04:09:35 +0000 Subject: [PATCH] =?UTF-8?q?B81=20=E2=80=94=20simplify:=20path=20constant,?= =?UTF-8?q?=20role=20cache,=20href=20parity=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Francisco Solla --- src/lib/campaignHomeActions.ts | 17 ++++++++++++--- src/lib/campaignPaths.ts | 2 ++ src/lib/campaignQuickActionRegistry.ts | 4 +++- tests/unit/campaignHomeActions.unit.spec.ts | 23 +++++++++++++++++---- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/lib/campaignHomeActions.ts b/src/lib/campaignHomeActions.ts index cd04fe35..f1255cfa 100644 --- a/src/lib/campaignHomeActions.ts +++ b/src/lib/campaignHomeActions.ts @@ -16,8 +16,7 @@ import { type CampaignWizardActionId, } from '@/lib/campaignActionRoutes' import { LEADER_CONTACTS_HOME } from '@/lib/campaignPaths' -import type { CampaignRole } from '@/lib/campaignRoles' -import { isStaffCampaignRole } from '@/lib/campaignRoles' +import { isStaffCampaignRole, type CampaignRole } from '@/lib/campaignRoles' type CampaignHomeActionId = | 'update-votes' @@ -129,11 +128,23 @@ export const UNCOVERED_MUNICIPALITIES_LIST_HREF = '/campanha/municipios?coverage=sem_assessor&sort=votos' as const /** Staff Início catalog with hrefs — shared by quick-action registries (B81+). */ +const staffHomeQuickActionsByRole = new Map< + CampaignRole, + readonly ResolvedCampaignHomeAction[] +>() + export const resolveStaffHomeQuickActions = ( role: CampaignRole, ): readonly ResolvedCampaignHomeAction[] => { if (!isStaffCampaignRole(role)) return [] - return toHomeActionButtonProps(homeActionsForRole(role), UNCOVERED_MUNICIPALITIES_LIST_HREF) + const cached = staffHomeQuickActionsByRole.get(role) + if (cached) return cached + const actions = toHomeActionButtonProps( + homeActionsForRole(role), + UNCOVERED_MUNICIPALITIES_LIST_HREF, + ) + staffHomeQuickActionsByRole.set(role, actions) + return actions } export const toHomeActionButtonProps = ( diff --git a/src/lib/campaignPaths.ts b/src/lib/campaignPaths.ts index cec8b080..03d540f5 100644 --- a/src/lib/campaignPaths.ts +++ b/src/lib/campaignPaths.ts @@ -1,4 +1,6 @@ /** Client-safe `/campanha` route paths shared by nav, gates, and home actions. */ export const CAMPAIGN_HOME = '/campanha' as const +export const CAMPAIGN_TERRITORIES_HOME = '/campanha/territorios' as const + export const LEADER_CONTACTS_HOME = '/campanha/contatos' as const diff --git a/src/lib/campaignQuickActionRegistry.ts b/src/lib/campaignQuickActionRegistry.ts index d3960cd9..cbfe95f1 100644 --- a/src/lib/campaignQuickActionRegistry.ts +++ b/src/lib/campaignQuickActionRegistry.ts @@ -3,10 +3,12 @@ import { type ResolvedCampaignHomeAction, } from '@/lib/campaignHomeActions' import type { CampaignQuickActionContext } from '@/lib/campaignQuickActionContext' +import { CAMPAIGN_TERRITORIES_HOME } from '@/lib/campaignPaths' import type { CampaignRole } from '@/lib/campaignRoles' const isTerritoriesListPath = (pathname: string): boolean => - pathname === '/campanha/territorios' || pathname.startsWith('/campanha/territorios/') + pathname === CAMPAIGN_TERRITORIES_HOME || + pathname.startsWith(`${CAMPAIGN_TERRITORIES_HOME}/`) /** * Pathname + role + page context → contextual quick actions. diff --git a/tests/unit/campaignHomeActions.unit.spec.ts b/tests/unit/campaignHomeActions.unit.spec.ts index 627b1bb1..0caae0a1 100644 --- a/tests/unit/campaignHomeActions.unit.spec.ts +++ b/tests/unit/campaignHomeActions.unit.spec.ts @@ -1,7 +1,12 @@ import { describe, expect, it } from 'vitest' -import { homeActionsForRole, toHomeActionButtonProps } from '@/lib/campaignHomeActions' +import { + homeActionsForRole, + toHomeActionButtonProps, + UNCOVERED_MUNICIPALITIES_LIST_HREF, +} from '@/lib/campaignHomeActions' import { LEADER_CONTACTS_HOME } from '@/lib/campaignPaths' +import { buildMunicipalityListHref } from '@/utilities/municipality/municipalityListUrl' const staffActionIds = [ 'update-votes', @@ -48,10 +53,12 @@ describe('toHomeActionButtonProps', () => { }) it('wires uncovered-municipalities when href is provided', () => { - const href = '/campanha/municipios?coverage=sem_assessor&sort=votos' - const props = toHomeActionButtonProps(homeActionsForRole('coordinator'), href) + const props = toHomeActionButtonProps( + homeActionsForRole('coordinator'), + UNCOVERED_MUNICIPALITIES_LIST_HREF, + ) const uncovered = props.find((action) => action.id === 'uncovered-municipalities') - expect(uncovered?.href).toBe(href) + expect(uncovered?.href).toBe(UNCOVERED_MUNICIPALITIES_LIST_HREF) const updateVotes = props.find((action) => action.id === 'update-votes') expect(updateVotes?.href).toBe('/campanha/acoes/atualizar-votos') const registerSignal = props.find((action) => action.id === 'register-signal') @@ -63,3 +70,11 @@ describe('toHomeActionButtonProps', () => { expect(props.find((action) => action.id === 'uncovered-municipalities')?.href).toBeUndefined() }) }) + +describe('UNCOVERED_MUNICIPALITIES_LIST_HREF', () => { + it('matches canonical municipality list builder (client/server parity)', () => { + expect( + buildMunicipalityListHref({ page: 1, coverage: 'sem_assessor', sort: 'votos' }, 1), + ).toBe(UNCOVERED_MUNICIPALITIES_LIST_HREF) + }) +})