teqo-backup/tests/int/campaignMunicipalityAllocator.int.spec.ts
Francisco Solla 89bf9d0854 ops(OPS46): e2e local coleta via hook ESM (next/cache + server-only) e allocator de município com registry anti-wrap (Closes #733)
Fase 1: playwright bare (--list, extensão VS Code, runs ad-hoc) quebrava na
coleta — next@15 sem exports não resolve 'next/cache' em ESM nativo e
server-only exige a condição react-server; o script test:e2e carregava ambas
via NODE_OPTIONS (por isso o CI passava). Hook de resolução ESM vitest-free
(e2eEsmResolve.mjs) registrada no main via module.register + propagada aos
workers via NODE_OPTIONS quando invocada bare; path do script intocado.

Fase 2: a sequência persistente campaign_fixture_municipality_alloc nunca é
resetada e um ciclo completo claima ~505 do catálogo de 435 — o wrap em todo
ciclo em DB de worktree fazia dois runs concorrentes receberem o mesmo índice
e purgarem as linhas vivas um do outro. Registry de claims compartilhado
vitest-free (campaignMunicipalityAllocator.ts): INSERT ON CONFLICT DO NOTHING
com skip de slot vivo, steal de claim stale (TTL 2h), release no cleanup dos
dois fixtures, erro claro de exaustão. Guard estático no
testMunicipalityAllocatorConventions + spec int de stress.
2026-08-23 14:02:19 -03:00

154 lines
5.7 KiB
TypeScript

// @vitest-environment node
import { randomUUID } from 'node:crypto'
import { sql } from '@payloadcms/db-postgres'
import { getPayload, type Payload } from 'payload'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import config from '@/payload.config'
import {
claimMunicipalityIndex,
releaseMunicipalityClaims,
} from '../helpers/campaignMunicipalityAllocator'
/**
* Behavioral stress of the allocator's claims registry (OPS46). The real
* catalog has 435 slots and a full int + e2e cycle claims ~505 of them, so
* the sequence wraps every cycle on persistent worktree databases; these
* tests exercise the wrap with a small synthetic `catalogSize` so the
* properties are observable in one run. They run under the PARALLEL int
* suite, so assertions never depend on the exact index a claim lands on —
* only on the registry invariants (live exclusivity through the wrap,
* exhaustion, release, stale steal) that hold regardless of what other
* specs claim concurrently.
*/
let payload: Payload
const releasedRunIDs: string[] = []
const freshRunID = (): string => {
const runID = randomUUID()
releasedRunIDs.push(runID)
return runID
}
const claimCount = async (runID: string): Promise<number> => {
const result = await payload.db.drizzle.execute(sql`
SELECT count(*)::int AS "count"
FROM "campaign_fixture_municipality_claims"
WHERE "run_id" = ${runID}
`)
return Number((result.rows[0] as { count: number }).count)
}
describe('campaign municipality allocator registry', () => {
beforeAll(async () => {
payload = await getPayload({ config: await config })
})
afterAll(async () => {
await Promise.all(releasedRunIDs.map((runID) => releaseMunicipalityClaims(payload, runID)))
})
it('never hands a live run an index another live run holds, across many sequence wraps', async () => {
const catalogSize = 40
// The anchor run keeps ONE slot held for the whole test; the residue
// space (40 slots) wraps ~3 times over the 120 claims below, and the
// registry must skip the held slot every time the cursor lands on it.
const anchor = freshRunID()
const anchorIndex = await claimMunicipalityIndex(payload, catalogSize, anchor)
for (let cycle = 0; cycle < 60; cycle += 1) {
const runID = freshRunID()
const first = await claimMunicipalityIndex(payload, catalogSize, runID)
const second = await claimMunicipalityIndex(payload, catalogSize, runID)
expect(second).not.toBe(first)
expect([first, second]).not.toContain(anchorIndex)
await releaseMunicipalityClaims(payload, runID)
}
expect(await claimCount(anchor)).toBe(1)
})
it('fails loudly with an exhaustion error instead of colliding, then recovers on release', async () => {
const catalogSize = 8
const holder = freshRunID()
let exhausted = false
for (let attempt = 0; attempt < 100 && !exhausted; attempt += 1) {
try {
await claimMunicipalityIndex(payload, catalogSize, holder)
} catch (error) {
expect((error as Error).message).toMatch(/exhausted/i)
exhausted = true
}
}
expect(exhausted, 'all slots held must surface as an exhaustion error').toBe(true)
await releaseMunicipalityClaims(payload, holder)
expect(await claimCount(holder)).toBe(0)
const recovered = freshRunID()
let claimed = false
for (let attempt = 0; attempt < 20 && !claimed; attempt += 1) {
try {
await claimMunicipalityIndex(payload, catalogSize, recovered)
claimed = true
} catch (error) {
expect((error as Error).message).toMatch(/exhausted/i)
}
}
expect(claimed, 'released slots must be claimable again').toBe(true)
})
it('steals claims of a crashed run once they age past the stale window', async () => {
const catalogSize = 40
const crashed = freshRunID()
const survivor = freshRunID()
const crashedIndex = await claimMunicipalityIndex(payload, catalogSize, crashed)
// Simulate a run killed before cleanup: age its claim past the 2h window.
await payload.db.drizzle.execute(sql`
UPDATE "campaign_fixture_municipality_claims"
SET "claimed_at" = now() - interval '3 hours'
WHERE "run_id" = ${crashed}
`)
// The stale slot is reclaimable: keep claiming until the survivor lands
// on the crashed run's index (the sequence advances one slot per claim,
// so a bounded loop covers every slot of the residue space).
let reclaimed = false
for (let attempt = 0; attempt < catalogSize * 3 && !reclaimed; attempt += 1) {
if ((await claimMunicipalityIndex(payload, catalogSize, survivor)) === crashedIndex) {
reclaimed = true
}
}
expect(reclaimed, 'stale claim must be stealable').toBe(true)
expect(await claimCount(crashed)).toBe(0)
// The survivor may hold up to catalogSize slots at this point; release
// inline so the later tests keep free space (the afterAll is the net).
await releaseMunicipalityClaims(payload, survivor)
})
it('releases every slot of a run on release and lets later runs reuse the space', async () => {
const catalogSize = 40
const runA = freshRunID()
const runB = freshRunID()
const claimedA = new Set<number>()
for (let i = 0; i < 5; i += 1) {
claimedA.add(await claimMunicipalityIndex(payload, catalogSize, runA))
}
expect(claimedA.size).toBe(5)
expect(await claimCount(runA)).toBe(5)
await releaseMunicipalityClaims(payload, runA)
expect(await claimCount(runA)).toBe(0)
const claimedB = new Set<number>()
for (let i = 0; i < 5; i += 1) {
claimedB.add(await claimMunicipalityIndex(payload, catalogSize, runB))
}
expect(claimedB.size).toBe(5)
})
})