teqo-backup/tests/int/campaignVotePledge.int.spec.ts
Francisco Ignácio Fontoura Solla aa12e173d8 Ship A10 vote estimate scenarios (pessimistic/central/optimistic) for Praças.
Staff surfaces share one Cenário control; leaders keep a single declaredVotes value. Unlocks E8.
2026-07-23 19:12:22 -03:00

206 lines
6.9 KiB
TypeScript

// @vitest-environment node
import type { Payload } from 'payload'
import { getPayload } from 'payload'
import { beforeAll, describe, expect, it } from 'vitest'
import {
declareVotesRecord,
estimateVotesRecord,
} from '@/app/(campaign)/campanha/actions/votePledge'
import config from '@/payload.config'
import { aggregatePledgesByPlaza, loadLeaderPledges } from '@/utilities/votePledgeData'
import { installCampaignFixtures } from '../helpers/campaignFixtures'
let payload: Payload
const campaignFixtures = installCampaignFixtures({
getPayload: () => payload,
setPayload: (nextPayload) => {
payload = nextPayload
},
})
describe('vote pledges (declared by the leader, estimated by staff)', () => {
beforeAll(async () => {
payload = await getPayload({ config: await config })
})
const createEngagedLeader = async () => {
const fixtures = campaignFixtures()
const plaza = await fixtures.getPlaza()
const account = await fixtures.createCampaignUser('leader')
const contact = await fixtures.createContact()
const leadership = await fixtures.createLeadership({
contact: contact.id,
plazas: [plaza.id],
user: account.id,
supportStatus: 'engajado',
})
return { fixtures, plaza, account, contact, leadership }
}
it('lets an engaged leader declare and update votes in a linked plaza', async () => {
const { plaza, account } = await createEngagedLeader()
const pledge = await declareVotesRecord(payload, account, {
plaza: plaza.id,
declaredVotes: 150,
})
expect(pledge.declaredVotes).toBe(150)
expect(pledge.declaredAt).toBeTruthy()
campaignFixtures().own('votePledge', pledge.id)
const updated = await declareVotesRecord(payload, account, {
plaza: plaza.id,
declaredVotes: 220,
})
expect(updated.id).toBe(pledge.id)
expect(updated.declaredVotes).toBe(220)
})
it('rejects a declaration in a plaza the leadership is not linked to', async () => {
const { account, fixtures } = await createEngagedLeader()
const otherPlaza = await fixtures.getPlaza()
await expect(
declareVotesRecord(payload, account, { plaza: otherPlaza.id, declaredVotes: 10 }),
).rejects.toThrow('vinculada à Praça')
})
it('never serializes staff estimates to the leader', async () => {
const { fixtures, plaza, account, leadership } = await createEngagedLeader()
const advisor = await fixtures.createCampaignUser('advisor')
await fixtures.assignPlazaAdvisors(plaza.id, [advisor.id])
const pledge = await declareVotesRecord(payload, account, {
plaza: plaza.id,
declaredVotes: 300,
})
campaignFixtures().own('votePledge', pledge.id)
const estimated = await estimateVotesRecord(payload, advisor, {
pledge: pledge.id,
estimatedVotes: { pessimistic: null, central: 120, optimistic: null },
estimateNote: 'Base histórica indica menos da metade.',
})
expect(estimated.estimatedVotes?.central).toBe(120)
expect(estimated.estimatedBy).toBeTruthy()
// Leader read path (access enforced): estimated fields must be stripped.
const leaderRead = await payload.find({
collection: 'votePledge',
where: { leadership: { equals: leadership.id } },
depth: 0,
pagination: false,
user: account,
overrideAccess: false,
})
expect(leaderRead.docs).toHaveLength(1)
const doc = leaderRead.docs[0] as unknown as Record<string, unknown>
expect(doc.declaredVotes).toBe(300)
expect(doc.estimatedVotes ?? null).toBeNull()
expect(doc.estimateNote ?? null).toBeNull()
expect(doc.estimatedBy ?? null).toBeNull()
expect(doc.estimatedAt ?? null).toBeNull()
const leaderRows = await loadLeaderPledges(payload, account)
expect(leaderRows).toHaveLength(1)
expect(leaderRows[0]).not.toHaveProperty('estimatedVotes')
expect(leaderRows[0]?.declaredVotes).toBe(300)
})
it('blocks a leader from writing the estimated fields through the Local API', async () => {
const { fixtures, plaza, account } = await createEngagedLeader()
const pledge = await declareVotesRecord(payload, account, {
plaza: plaza.id,
declaredVotes: 90,
})
fixtures.own('votePledge', pledge.id)
await payload.update({
collection: 'votePledge',
id: pledge.id,
data: { estimatedVotes: { central: 9_999 } } as never,
depth: 0,
user: account,
overrideAccess: false,
})
const raw = await payload.findByID({
collection: 'votePledge',
id: pledge.id,
depth: 0,
overrideAccess: true,
})
expect(raw.estimatedVotes?.central ?? null).toBeNull()
expect(raw.estimatedVotes?.pessimistic ?? null).toBeNull()
expect(raw.estimatedVotes?.optimistic ?? null).toBeNull()
})
it('scopes an advisor to pledges of administered plazas only', async () => {
const { fixtures, plaza, account } = await createEngagedLeader()
const pledge = await declareVotesRecord(payload, account, {
plaza: plaza.id,
declaredVotes: 50,
})
fixtures.own('votePledge', pledge.id)
const outsideAdvisor = await fixtures.createCampaignUser('advisor')
await expect(
estimateVotesRecord(payload, outsideAdvisor, {
pledge: pledge.id,
estimatedVotes: { pessimistic: null, central: 10, optimistic: null },
estimateNote: null,
}),
).rejects.toThrow()
const outsideRead = await payload.find({
collection: 'votePledge',
where: { id: { equals: pledge.id } },
depth: 0,
pagination: false,
user: outsideAdvisor,
overrideAccess: false,
})
expect(outsideRead.docs).toHaveLength(0)
})
it('aggregates effective votes as estimated ?? declared', async () => {
const { fixtures, plaza, account } = await createEngagedLeader()
const advisor = await fixtures.createCampaignUser('advisor')
await fixtures.assignPlazaAdvisors(plaza.id, [advisor.id])
const firstPledge = await declareVotesRecord(payload, account, {
plaza: plaza.id,
declaredVotes: 200,
})
fixtures.own('votePledge', firstPledge.id)
await estimateVotesRecord(payload, advisor, {
pledge: firstPledge.id,
estimatedVotes: { pessimistic: null, central: 80, optimistic: null },
estimateNote: null,
})
const secondContact = await fixtures.createContact()
const secondLeadership = await fixtures.createLeadership({
contact: secondContact.id,
plazas: [plaza.id],
supportStatus: 'engajado',
})
const secondPledge = await fixtures.createVotePledge({
leadership: secondLeadership.id,
plaza: plaza.id,
declaredVotes: 40,
})
const aggregates = await aggregatePledgesByPlaza(payload, [plaza.id])
const aggregate = aggregates.get(plaza.id)
expect(aggregate?.pledgeCount).toBe(2)
expect(aggregate?.declaredTotal).toBe(240)
expect(aggregate?.effectiveByScenario.central).toBe(120)
expect(aggregate?.missingEstimateCount).toBe(1)
expect(secondPledge.declaredVotes).toBe(40)
})
})