fix(test): queries distintas em specs de portfolio de município

Nova Soure vs Nova Canaã compartilham o primeiro token; distinctWordStartQuery
escolhe palavra que casa só com o alvo e evita falso positivo no assessor.
This commit is contained in:
Francisco Ignácio Fontoura Solla 2026-07-30 22:28:50 -03:00
parent 0fece8cef5
commit 773e4426eb
4 changed files with 47 additions and 4 deletions

View file

@ -0,0 +1,27 @@
import { homeSearchQueryIsActive } from '@/lib/campaignHomeSearchContract'
import { normalizeHomeSearchName } from '@/lib/homeSearchMunicipalityMatch'
import { matchesNormalizedAtWordStart } from '@/lib/wordStartFilter'
/**
* Picks a word-start query that matches `targetName` but not `excludeName`.
* Portfolio scoping specs allocate two municipalities per test; sharing the
* first token (e.g. Nova Soure vs Nova Canaã) makes advisor assertions flaky.
*/
export function distinctWordStartQuery(targetName: string, excludeName: string): string {
const normalizedTarget = normalizeHomeSearchName(targetName)
const normalizedExclude = normalizeHomeSearchName(excludeName)
for (const word of targetName.split(/\s+/)) {
const query = word.trim()
if (!homeSearchQueryIsActive(normalizeHomeSearchName(query))) continue
const normalizedQuery = normalizeHomeSearchName(query)
if (
matchesNormalizedAtWordStart(normalizedTarget, normalizedQuery) &&
!matchesNormalizedAtWordStart(normalizedExclude, normalizedQuery)
) {
return query
}
}
throw new Error(`No word-start query matches "${targetName}" without matching "${excludeName}"`)
}

View file

@ -9,6 +9,7 @@ import config from '@/payload.config'
import { searchHomeMunicipalities } from '@/utilities/homeSearch/searchHomeMunicipalities'
import { installCampaignFixtures } from '../helpers/campaignFixtures'
import { distinctWordStartQuery } from '../helpers/distinctWordStartQuery'
let payload: Payload
const campaignFixtures = installCampaignFixtures({
@ -51,8 +52,8 @@ describe('searchHomeMunicipalities (B48)', () => {
const other = await fixtures.getMunicipality()
await fixtures.assignMunicipalityAdvisors(administered.id, [advisor.id])
const otherQuery = other.name.split(/\s+/)[0]!
const administeredQuery = administered.name.split(/\s+/)[0]!
const otherQuery = distinctWordStartQuery(other.name, administered.name)
const administeredQuery = distinctWordStartQuery(administered.name, other.name)
const advisorResult = await searchHomeMunicipalities(payload, advisor, otherQuery)
expect(advisorResult.municipalities.map((hit) => hit.slug)).toEqual([])

View file

@ -9,6 +9,7 @@ import config from '@/payload.config'
import { searchStaffMunicipalityHits } from '@/utilities/homeSearch/searchStaffMunicipalityHits'
import { installCampaignFixtures } from '../helpers/campaignFixtures'
import { distinctWordStartQuery } from '../helpers/distinctWordStartQuery'
let payload: Payload
const campaignFixtures = installCampaignFixtures({
@ -50,8 +51,8 @@ describe('searchStaffMunicipalityHits (B60)', () => {
const other = await fixtures.getMunicipality()
await fixtures.assignMunicipalityAdvisors(administered.id, [advisor.id])
const otherQuery = other.name.split(/\s+/)[0]!
const administeredQuery = administered.name.split(/\s+/)[0]!
const otherQuery = distinctWordStartQuery(other.name, administered.name)
const administeredQuery = distinctWordStartQuery(administered.name, other.name)
const advisorResult = await searchStaffMunicipalityHits(payload, advisor, otherQuery)
expect(advisorResult.map((hit) => hit.slug)).toEqual([])

View file

@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest'
import { distinctWordStartQuery } from '../helpers/distinctWordStartQuery'
describe('distinctWordStartQuery', () => {
it('returns a word that matches only the target name', () => {
expect(distinctWordStartQuery('Nova Canaã', 'Nova Soure')).toBe('Canaã')
expect(distinctWordStartQuery('Nova Soure', 'Nova Canaã')).toBe('Soure')
})
it('falls back to the first word when names do not share a prefix token', () => {
expect(distinctWordStartQuery('Cairu', 'Feira de Santana')).toBe('Cairu')
})
})