) => {
+ event.preventDefault()
+ const form = event.currentTarget
+ const estimatedVotes = readScenarioFromForm(form)
+ const estimateNoteRaw = new FormData(form).get('estimateNote')
+ const estimateNote =
+ typeof estimateNoteRaw === 'string' && estimateNoteRaw.trim() !== ''
+ ? estimateNoteRaw.trim()
+ : null
+
+ setIsSubmitting(true)
+ setFormError(null)
+
+ void enqueueEstimateVotes({
+ pledge: pledgeID,
+ estimatedVotes,
+ estimateNote,
+ baseEstimatedAt,
+ }).then(
+ () => {
+ setIsSubmitting(false)
+ setFormError(null)
+ },
+ (error: unknown) => {
+ setIsSubmitting(false)
+ setFormError(
+ error instanceof Error
+ ? error.message
+ : 'Não foi possível enfileirar a estimativa.',
+ )
+ },
+ )
+ }
+
+ return (
+
+ )
+}
diff --git a/tests/e2e/campaignOpsEstimateOutbox.e2e.spec.ts b/tests/e2e/campaignOpsEstimateOutbox.e2e.spec.ts
new file mode 100644
index 00000000..ecaa8ae3
--- /dev/null
+++ b/tests/e2e/campaignOpsEstimateOutbox.e2e.spec.ts
@@ -0,0 +1,61 @@
+import { expect, test } from './fixtures/campaignE2EFixtures.js'
+
+/**
+ * OH6 — flaky-network outbox for estimateVotes. Requires `OPS_HYBRID=1` on the
+ * Playwright webServer (compile-time). Default CI keeps the flag off so
+ * `campaignMunicipalities` continues to pin the legacy formAction path.
+ */
+const opsHybridEnabled =
+ process.env.OPS_HYBRID === '1' || process.env.OPS_HYBRID?.toLowerCase() === 'true'
+
+test.describe('OH6 estimate outbox (OPS_HYBRID)', () => {
+ test.skip(!opsHybridEnabled, 'Set OPS_HYBRID=1 on the e2e webServer to run this tracer.')
+
+ test('offline edit stays queued across reload and flushes when online', async ({
+ campaign,
+ page,
+ }) => {
+ const { fixtures } = campaign
+ const municipality = await fixtures.claimMunicipality()
+ const advisor = await fixtures.createCampaignUser('advisor', {
+ name: fixtures.value('Assessor Outbox'),
+ })
+ await campaign.payload.update({
+ collection: 'municipality',
+ id: municipality.id,
+ data: { advisors: [advisor.id] },
+ depth: 0,
+ })
+ fixtures.touchMunicipality(municipality.id)
+
+ const { leadershipId } = await fixtures.createStaffLeadership({
+ namePrefix: 'Liderança Outbox',
+ municipalities: [municipality],
+ supportStatus: 'engajado',
+ })
+ await campaign.payload.create({
+ collection: 'votePledge',
+ data: {
+ leadership: leadershipId,
+ municipality: municipality.id,
+ declaredVotes: 120,
+ },
+ depth: 0,
+ })
+
+ await campaign.login(page, advisor.email!, advisor.password)
+ await page.goto(`${campaign.baseURL}/campanha/municipios/${municipality.slug}`)
+
+ await page.context().setOffline(true)
+ await page.getByLabel('Média', { exact: true }).fill('55')
+ await page.getByLabel('Justificativa').fill('Editado na estrada.')
+ await page.getByRole('button', { name: 'Salvar estimativa' }).click()
+ await expect(page.getByText('Pendente', { exact: true })).toBeVisible()
+
+ await page.reload()
+ await expect(page.getByText('Pendente', { exact: true })).toBeVisible()
+
+ await page.context().setOffline(false)
+ await expect(page.getByText('Média: 55')).toBeVisible({ timeout: 30_000 })
+ })
+})
diff --git a/tests/unit/opsEstimateOutbox.unit.spec.ts b/tests/unit/opsEstimateOutbox.unit.spec.ts
new file mode 100644
index 00000000..64e66f51
--- /dev/null
+++ b/tests/unit/opsEstimateOutbox.unit.spec.ts
@@ -0,0 +1,69 @@
+import { describe, expect, it } from 'vitest'
+
+import {
+ collapseEstimateOutboxByPledge,
+ resolveEstimateTransactionPledgeKey,
+ toOpsEstimateOutboxRow,
+} from '@/components/campaign/opsSync/opsEstimateOutboxModel'
+import { resolveOpsHybridEnabled } from '@/components/campaign/opsSync/opsHybridEnabled'
+import type { OfflineTransaction } from '@tanstack/offline-transactions'
+
+const tx = (
+ id: string,
+ pledgeId: number,
+ createdAtMs: number,
+): OfflineTransaction =>
+ ({
+ id,
+ mutationFnName: 'estimateVotes',
+ mutations: [{ key: pledgeId } as OfflineTransaction['mutations'][number]],
+ keys: [String(pledgeId)],
+ idempotencyKey: id,
+ createdAt: new Date(createdAtMs),
+ retryCount: 0,
+ nextAttemptAt: createdAtMs,
+ metadata: { pledgeId },
+ version: 1,
+ }) as OfflineTransaction
+
+describe('opsEstimateOutboxModel (OH6)', () => {
+ it('collapses superseded transactions for the same pledge', () => {
+ const collapsed = collapseEstimateOutboxByPledge([
+ tx('a', 1, 1000),
+ tx('b', 1, 3000),
+ tx('c', 2, 2000),
+ tx('d', 1, 2500),
+ ])
+
+ expect(collapsed.map((entry) => entry.id)).toEqual(['c', 'b'])
+ expect(resolveEstimateTransactionPledgeKey(collapsed[1]!)).toBe('1')
+ })
+
+ it('builds a pending outbox row from an enqueue payload', () => {
+ expect(
+ toOpsEstimateOutboxRow({
+ pledge: 9,
+ estimatedVotes: { pessimistic: 1, central: 2, optimistic: 3 },
+ estimateNote: 'nota',
+ baseEstimatedAt: '2026-08-01T00:00:00.000Z',
+ }),
+ ).toEqual({
+ pledgeId: 9,
+ estimatedVotes: { pessimistic: 1, central: 2, optimistic: 3 },
+ estimateNote: 'nota',
+ baseEstimatedAt: '2026-08-01T00:00:00.000Z',
+ status: 'pending',
+ })
+ })
+})
+
+describe('resolveOpsHybridEnabled (OH6)', () => {
+ it('accepts 1/true and rejects absent/other values', () => {
+ expect(resolveOpsHybridEnabled({ OPS_HYBRID: '1' })).toBe(true)
+ expect(resolveOpsHybridEnabled({ OPS_HYBRID: 'true' })).toBe(true)
+ expect(resolveOpsHybridEnabled({ OPS_HYBRID: 'TRUE' })).toBe(true)
+ expect(resolveOpsHybridEnabled({ OPS_HYBRID: '0' })).toBe(false)
+ expect(resolveOpsHybridEnabled({ OPS_HYBRID: '' })).toBe(false)
+ expect(resolveOpsHybridEnabled({})).toBe(false)
+ })
+})