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:
parent
0fece8cef5
commit
773e4426eb
4 changed files with 47 additions and 4 deletions
27
tests/helpers/distinctWordStartQuery.ts
Normal file
27
tests/helpers/distinctWordStartQuery.ts
Normal 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}"`)
|
||||
}
|
||||
|
|
@ -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([])
|
||||
|
|
|
|||
|
|
@ -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([])
|
||||
|
|
|
|||
14
tests/unit/distinctWordStartQuery.unit.spec.ts
Normal file
14
tests/unit/distinctWordStartQuery.unit.spec.ts
Normal 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')
|
||||
})
|
||||
})
|
||||
Loading…
Reference in a new issue