Files
accounted/extensions/general/whatsapp-inbox/__tests__/hardening.test.ts
T
88760ae6f6 fix(whatsapp-inbox): harden against adversarial review findings (#1342)
* fix(whatsapp-inbox): erase the WhatsApp channel on account deletion

whatsapp_phone_links relied on the auth.users ON DELETE CASCADE, but
Accounted never deletes auth.users: account deletion is
anonymize_user_account plus a ~100-year ban that keeps the auth row as a
tombstone, so the cascade never fires and nothing revokes the link. After
erasure the link stayed active with a decryptable phone_enc,
lookupActiveLink kept resolving the number, and every further inbound
message was persisted with body_text and the verbatim raw_payload while
the bot kept replying: GDPR Art 17 plus continued collection with no
lawful basis.

The RPC is re-created verbatim from 20260724150000 with one added block
that revokes and crypto-shreds the link, resets its conversation, nulls
body_text/raw_payload on that link's messages and deletes outstanding
link codes, plus a guarded repair pass for tombstones anonymized before
this migration. Covered by a pg-real test that fails against the previous
definition.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(whatsapp-inbox): pepper the link-code hash and bound code minting

hashLinkCode stored a bare sha256 over CODE_ALPHABET^6 = 30^6 values
behind a fixed 'AC-' prefix. The module cited the invite-token pattern,
but invite tokens are 256-bit random; this space enumerates offline in
about a second, so hashing at rest protected nothing. The sibling
phone-crypto.ts already states the team's own threat model for a LARGER
space ("a plain sha256 would be brute-forceable ... hence the pepper"),
so link codes now hash through the same env-mandated pepper.

/link/start was also an authenticated unbounded INSERT that left every
earlier code valid. Minting now burns the caller's unused codes (the code
the panel shows is the only one that works) and is capped per TTL window,
with the route answering 429 instead of throwing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(whatsapp-inbox): harden the conversation layer against the review findings

Pre-merge hardening of the unshipped chat layer. Every change below has a
test that fails without it.

Lifecycle and races:
- conversation writes go through updateConversation(), an optimistic
  compare-and-set on updated_at (the trigger makes it a revision counter).
  The ack winner, the answer worker, the pin refresh and the sweep hold
  different claims, so blind whole-jsonb writes resurrected answered
  questions, wiped pending_question and dropped queue entries.
- terminal markStatus writes are guarded on processing_status='processing'
  so a losing worker cannot overwrite the winner's 'done' and null its
  inbox_item_id.
- the message -> inbox item path is idempotent: a pre-check plus a 23505
  fallback adopt the item a concurrent worker created, instead of throwing
  after the WORM document is already committed.
- PROCESSING_STUCK_MS 90s -> 5 min. The enforced step budget of one media
  row already exceeds 90s, so the sweep was re-claiming live workers.
- sweep 2b re-arms only when the conversation itself has been quiet, not
  just the rows: pending_ack=false plus unacked rows is also the state of a
  live finalize, which produced a duplicate combined ack.
- pin expiry re-checks against fresh state instead of writing back a stale
  whole context, which reverted company choices applied mid-pass.
- askNextQueuedQuestion claims the pop before sending, so two answer
  workers cannot ask the same question twice.

Company question:
- the state is rolled back when the M6 send fails, so the next receipt
  re-asks instead of parking receipts behind a question nobody received.
- applyCompanyChoice claims the open question (company_options) rather
  than the state: a double tap confirms once, a transient membership-query
  error is no longer read as "not a member", and a LATE answer still lands.
- at the 48h TTL the parked receipts are kept, not discarded: options and
  staged rows survive so a late digit or tap still files them, and only
  rows past Meta's ~30-day media window get the terminal marker.
- an out-of-range digit or a typed company name now gets the options
  repeated instead of silence or the "I cannot answer questions" reply.

Inline dispositions:
- stop/start/byt/company answers run their side effect BEFORE the terminal
  wamid row, with a SELECT pre-check for dedupe. Writing the row 'done'
  first made them at-most-once: a crash in between lost the action forever.

Copy and answers:
- acks state the extracted currency instead of labelling every total 'kr'.
- M17 stops promising "about 10 minutes" when the daily quota tripped.
- M18 is sent once per message tracked by the outbound row, so a file
  whose first attempt died still reaches the sender, including from the
  max-attempts path.
- M11 no longer claims the number is disconnected: 'stopp' pauses, and
  muted senders now persist no chat content at all.
- 'byt' is recognized in every state but awaiting_company (m6-confirm
  teaches the word, and it was being stored as answer data instead).
- text sent while a re-send question is open is kept as a note on THAT
  receipt with the question left open, instead of binding to another
  receipt's question.
- a quoted reply wins over the pending question and is appended when the
  quoted question is already answered, so corrections stop landing on the
  wrong receipt.
- context answers keep raw_answer + answered_at like representation does.
- finalizeBurst checks the send result: on failure it rolls the question
  back and leaves the rows unacked for the sweep.

PII:
- the sender's plaintext number is stripped from raw_payload before it is
  persisted; replies decrypt the link's phone_enc instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(whatsapp-inbox): record the erasure path and the hardening decisions

RoPA gains the account-deletion row (immediate, not via the cron: the
auth.users cascade never fires because the row is tombstoned) plus the
two new security measures, and its "never in the clear" phone claim is
now true of the stored payload. DECISIONS.md records the non-obvious
calls: revoke-not-delete on erasure, commit-then-roll-back for the
company question, keeping expired company choices answerable, the
compare-and-set conversation write, effect-before-terminal-row for inline
dispositions, honest M11 copy, and the raw_payload redaction.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(whatsapp-inbox): stop the answer re-claim from following a confirm with M16

A worker that died after applying an answer and sending its confirmation
leaves the row 'processing'. The sweep re-runs it, resolveAnswerTarget
finds the question already answered, and the user got "I did not
understand" immediately after the confirmation they had just received.
The fallback is now first-attempt only.

The catch comment claiming the sweep retries these rows is corrected
too: 'error' is terminal for the sweep, and nothing on the answer path
throws anyway (interpretChatAnswer degrades, sends never throw,
supabase-js returns errors), so the catch is a programming-error net.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(whatsapp-inbox): drop the amount floor on the representation question

The Swedish compliance review on #1340 caught a real error in the trigger
rules: the representation question only fired above 150 kr, but the duty to
document deltagare and syfte is what makes the expense deductible at all
(BFL 5 kap 6-7 §) and it is not conditioned on any amount. The 300 kr per
person figure I had in mind is the VAT-deduction base cap, a different rule.
A 120 kr business lunch would have been booked with no participant trail,
which is exactly the deduction Skatteverket denies later.

Noise stays bounded by the triggers that were already there: the question
fires only for receipt-shaped documents from restaurant, cafe or hotel
merchants, at most once per receipt, twice per burst and six times per
sender per day, and a single "nej" dismisses it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <jakob.wennberg@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
2026-08-05 15:47:23 +02:00

553 lines
19 KiB
TypeScript

/**
* Regression tests for the adversarial-review hardening pass.
*
* Every case here fails against the pre-fix code: a failed send stranding the
* company question, an ignored ack send result, SEK-labelled foreign amounts,
* unguarded terminal status writes, quoted corrections landing on the wrong
* receipt, blind whole-context conversation writes, the plaintext phone in
* raw_payload, text answers to the re-send question, and the 48h expiry
* discarding parked receipts.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createQueuedMockSupabase } from '@/tests/helpers'
import type { SupabaseClient } from '@supabase/supabase-js'
vi.mock('@/extensions/general/whatsapp-inbox/lib/graph-api', async () => {
const actual = await vi.importActual<
typeof import('@/extensions/general/whatsapp-inbox/lib/graph-api')
>('@/extensions/general/whatsapp-inbox/lib/graph-api')
return {
...actual,
sendText: vi.fn().mockResolvedValue({ ok: true, wamid: 'wamid.OUT' }),
sendReplyButtons: vi.fn().mockResolvedValue({ ok: true, wamid: 'wamid.OUT' }),
sendList: vi.fn().mockResolvedValue({ ok: true, wamid: 'wamid.OUT' }),
markReadWithTyping: vi.fn().mockResolvedValue(undefined),
downloadMedia: vi.fn(),
}
})
vi.mock('@/lib/processing-history/append', () => ({
appendProcessingHistory: vi.fn().mockResolvedValue('event-1'),
}))
vi.mock('@/lib/rate-limits/agent', () => ({
checkAgentRateLimit: vi.fn().mockResolvedValue({ ok: true }),
}))
vi.mock('@/extensions/general/whatsapp-inbox/lib/interpret-answer', () => ({
interpretChatAnswer: vi.fn().mockResolvedValue({ ok: false }),
}))
import { sendText, sendReplyButtons } from '@/extensions/general/whatsapp-inbox/lib/graph-api'
import { askCompanyQuestion } from '@/extensions/general/whatsapp-inbox/lib/company-question'
import {
finalizeBurst,
processInboundMessage,
} from '@/extensions/general/whatsapp-inbox/lib/process-inbound'
import {
redactRawPayload,
resolveAnswerTarget,
resolveRecipient,
updateConversation,
} from '@/extensions/general/whatsapp-inbox/lib/conversation'
import { encryptPhone } from '@/extensions/general/whatsapp-inbox/lib/phone-crypto'
import { runSweep } from '@/extensions/general/whatsapp-inbox/lib/sweep'
import { botCopy, TEMPLATE } from '@/extensions/general/whatsapp-inbox/lib/messages'
const sendTextMock = vi.mocked(sendText)
const sendButtonsMock = vi.mocked(sendReplyButtons)
function makeConversation(overrides: Record<string, unknown> = {}) {
return {
id: 'conv-1',
phone_link_id: 'link-1',
state: 'idle',
context: {},
company_id: 'company-1',
service_window_expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
debounce_until: new Date(Date.now() - 1000).toISOString(),
pending_ack: true,
last_inbound_at: null,
last_outbound_at: null,
created_at: '2026-08-01T09:00:00Z',
updated_at: '2026-08-01T09:00:00Z',
...overrides,
}
}
function makeLink(overrides: Record<string, unknown> = {}) {
return {
id: 'link-1',
user_id: 'user-1',
phone_hash: 'hash-1',
phone_enc: 'enc',
phone_masked: '+46 70 *** ** 67',
default_company_id: null,
last_company_id: null,
verified_at: '2026-08-01T09:00:00Z',
revoked_at: null,
muted_at: null,
...overrides,
}
}
function makeRow(overrides: Record<string, unknown> = {}) {
return {
id: 'msg-1',
direction: 'inbound',
wamid: 'wamid.IN1',
sender_phone_hash: 'hash-1',
phone_link_id: 'link-1',
conversation_id: 'conv-1',
message_type: 'image',
body_text: null,
media_id: 'media-1',
media_mime: 'image/jpeg',
media_sha256: 'abc',
media_filename: 'kvitto.jpg',
raw_payload: { from: '46701234567' },
processing_status: 'received',
attempts: 0,
error_message: null,
inbox_item_id: null,
delivery_status: null,
correlation_id: 'corr-1',
acked_at: null,
created_at: '2026-08-01T10:00:00Z',
updated_at: '2026-08-01T10:00:00Z',
...overrides,
}
}
beforeEach(() => {
vi.clearAllMocks()
process.env.WHATSAPP_PHONE_HASH_KEY = 'test-pepper'
process.env.WHATSAPP_PHONE_ENCRYPTION_KEY = 'a'.repeat(64)
sendTextMock.mockResolvedValue({ ok: true, wamid: 'wamid.OUT' })
sendButtonsMock.mockResolvedValue({ ok: true, wamid: 'wamid.OUT' })
})
describe('company question is not one-shot when the send fails', () => {
function enqueueAsk(mock: ReturnType<typeof createQueuedMockSupabase>) {
mock.enqueue({ data: [{ company_id: 'company-1' }, { company_id: 'company-2' }] })
mock.enqueue({
data: [
{ id: 'company-1', name: 'Bolag A AB' },
{ id: 'company-2', name: 'Bolag B AB' },
],
})
mock.enqueue({ data: [{ ...makeConversation(), state: 'awaiting_company' }] }) // guarded commit
}
it('rolls the question back so the next receipt re-asks', async () => {
sendButtonsMock.mockResolvedValue({ ok: false, wamid: null })
const mock = createQueuedMockSupabase()
enqueueAsk(mock)
mock.enqueue({
data: [
{
...makeConversation(),
state: 'awaiting_company',
context: { company_options: [{ id: 'company-1', name: 'Bolag A AB' }] },
},
],
})
const asked = await askCompanyQuestion(mock.supabase as unknown as SupabaseClient, {
conversation: makeConversation() as never,
link: makeLink() as never,
to: '46701234567',
replyBase: {},
stagedCount: 3,
})
expect(asked).toBe(false)
const updates = mock
.findCalls('whatsapp_conversations', 'update')
.map((args) => args[0] as { state?: string; context?: Record<string, unknown> })
// First write arms the question, the second undoes it.
expect(updates[0].state).toBe('awaiting_company')
expect(updates[1].state).toBe('idle')
expect(updates[1].context?.company_options).toBeUndefined()
expect(updates[1].context?.pending_question).toBeUndefined()
})
it('keeps the question when the send succeeded', async () => {
const mock = createQueuedMockSupabase()
enqueueAsk(mock)
const asked = await askCompanyQuestion(mock.supabase as unknown as SupabaseClient, {
conversation: makeConversation() as never,
link: makeLink() as never,
to: '46701234567',
replyBase: {},
stagedCount: 1,
})
expect(asked).toBe(true)
expect(mock.findCalls('whatsapp_conversations', 'update')).toHaveLength(1)
})
})
describe('combined ack', () => {
function enqueueBurst(
mock: ReturnType<typeof createQueuedMockSupabase>,
extracted: Record<string, unknown>,
) {
mock.enqueue({ data: [{ id: 'conv-1' }] }) // claimAck
mock.enqueue({ data: makeConversation() })
mock.enqueue({
data: [
{
...makeRow(),
processing_status: 'done',
inbox_item_id: 'item-1',
},
],
})
mock.enqueue({
data: [
{
id: 'item-1',
company_id: 'company-1',
extracted_data: extracted,
channel_context: { channel: 'whatsapp' },
document_id: null,
},
],
})
mock.enqueue({ data: [makeConversation()] }) // guarded state write
}
it('states a foreign total in its own currency, never as kronor', async () => {
const mock = createQueuedMockSupabase()
enqueueBurst(mock, {
documentKind: 'receipt',
legibility: 'good',
supplier: { name: 'Hotel Adlon' },
invoice: { invoiceDate: '2026-07-30', currency: 'EUR' },
totals: { total: 250 },
lineItems: [],
})
await finalizeBurst(mock.supabase as unknown as SupabaseClient, 'conv-1')
expect(sendTextMock).toHaveBeenCalledTimes(1)
const body = sendTextMock.mock.calls[0][1].body
expect(body).toContain('250 EUR')
expect(body).not.toContain('250 kr')
})
it('still says kr for SEK and for an unstated currency', async () => {
const mock = createQueuedMockSupabase()
enqueueBurst(mock, {
documentKind: 'receipt',
legibility: 'good',
supplier: { name: 'ICA Maxi' },
invoice: { invoiceDate: '2026-07-30' },
totals: { total: 234 },
lineItems: [],
})
await finalizeBurst(mock.supabase as unknown as SupabaseClient, 'conv-1')
expect(sendTextMock.mock.calls[0][1].body).toContain('234 kr')
})
it('leaves the rows unacked when the send failed so the sweep retries', async () => {
sendTextMock.mockResolvedValue({ ok: false, wamid: null })
const mock = createQueuedMockSupabase()
enqueueBurst(mock, {
documentKind: 'receipt',
legibility: 'good',
supplier: { name: 'ICA Maxi' },
invoice: { invoiceDate: '2026-07-30' },
totals: { total: 234 },
lineItems: [],
})
await finalizeBurst(mock.supabase as unknown as SupabaseClient, 'conv-1')
const messageUpdates = mock
.findCalls('whatsapp_messages', 'update')
.map((args) => args[0] as Record<string, unknown>)
expect(messageUpdates.some((patch) => 'acked_at' in patch)).toBe(false)
})
})
describe('terminal status writes hold the claim', () => {
it('guards markStatus on processing_status=processing', async () => {
const mock = createQueuedMockSupabase()
mock.enqueue({ data: makeRow() })
mock.enqueue({ data: { id: 'msg-1' } }) // claim
mock.enqueue({ data: makeLink({ revoked_at: '2026-08-01T10:00:00Z' }) })
mock.enqueue({ data: null }) // markStatus skipped
await processInboundMessage(mock.supabase as unknown as SupabaseClient, 'msg-1')
const guards = mock
.findCalls('whatsapp_messages', 'eq')
.filter((args) => args[0] === 'processing_status')
// The claim guards on 'received'; the terminal write must guard on
// 'processing' so a losing worker cannot clobber the winner's row.
expect(guards.map((args) => args[1])).toContain('processing')
})
})
describe('answer targeting', () => {
const askedAt = new Date(Date.now() - 60 * 60 * 1000).toISOString()
function conversationWith(recent: Record<string, unknown>[], overrides = {}) {
return makeConversation({
state: 'awaiting_context',
context: {
pending_question: { type: 'context', inbox_item_id: 'item-B', asked_at: askedAt },
recent_questions: recent,
},
...overrides,
})
}
it('binds a quoted follow-up to the quoted receipt, not to another open question', async () => {
const mock = createQueuedMockSupabase()
mock.enqueue({ data: { inbox_item_id: 'item-A' } }) // quoted wamid resolves
const target = await resolveAnswerTarget(
mock.supabase as unknown as SupabaseClient,
conversationWith([
{ type: 'representation', inbox_item_id: 'item-A', asked_at: askedAt, status: 'answered' },
{ type: 'context', inbox_item_id: 'item-B', asked_at: askedAt, status: 'open' },
]) as never,
'wamid.QUOTED',
)
expect(target?.inboxItemId).toBe('item-A')
expect(target?.followUp).toBe(true)
})
it('quoting the open question is still an ordinary answer, not a late one', async () => {
const mock = createQueuedMockSupabase()
mock.enqueue({ data: { inbox_item_id: 'item-B' } })
const target = await resolveAnswerTarget(
mock.supabase as unknown as SupabaseClient,
conversationWith([
{ type: 'context', inbox_item_id: 'item-B', asked_at: askedAt, status: 'open' },
]) as never,
'wamid.QUOTED',
)
expect(target).toEqual({ type: 'context', inboxItemId: 'item-B', late: false })
})
})
describe('conversation writes are compare-and-set', () => {
it('re-applies the mutation against fresh state after losing a race', async () => {
const mock = createQueuedMockSupabase()
mock.enqueue({ data: [] }) // guard did not match: someone else wrote first
mock.enqueue({ data: makeConversation({ context: { budget: { day_key: 'x', count: 2 } } }) })
mock.enqueue({ data: [makeConversation()] })
const seen: number[] = []
const result = await updateConversation(
mock.supabase as unknown as SupabaseClient,
makeConversation() as never,
(_current, context) => {
seen.push(context.budget?.count ?? 0)
return { context: { ...context, question_queue: [] } }
},
)
expect(result).not.toBeNull()
// Second pass saw the concurrent writer's state, not the stale snapshot.
expect(seen).toEqual([0, 2])
})
it('gives up rather than looping forever', async () => {
const mock = createQueuedMockSupabase()
for (let i = 0; i < 12; i++) {
mock.enqueue({ data: [] })
mock.enqueue({ data: makeConversation() })
}
const result = await updateConversation(
mock.supabase as unknown as SupabaseClient,
makeConversation() as never,
(_current, context) => ({ context }),
)
expect(result).toBeNull()
})
})
describe('phone PII', () => {
it('strips the sender number from the persisted payload but keeps the quote id', () => {
const redacted = redactRawPayload({
from: '46701234567',
id: 'wamid.IN1',
type: 'text',
context: { id: 'wamid.QUOTED' },
})
expect(redacted).not.toHaveProperty('from')
expect(redacted).toMatchObject({ id: 'wamid.IN1', context: { id: 'wamid.QUOTED' } })
})
it('resolves the reply address from the encrypted link when the payload is redacted', () => {
const row = makeRow({ raw_payload: { id: 'wamid.IN1', type: 'text' } })
const link = { phone_enc: encryptPhone('46701234567') }
expect(resolveRecipient(row as never, link)).toBe('46701234567')
})
it('returns null instead of throwing on a shredded link', () => {
const row = makeRow({ raw_payload: {} })
expect(resolveRecipient(row as never, { phone_enc: '' })).toBeNull()
})
})
describe('text reply while a re-send question is open', () => {
it('keeps it as a note on that receipt and leaves the question open', async () => {
const mock = createQueuedMockSupabase()
mock.enqueue({
data: makeRow({
id: 'msg-t1',
message_type: 'text',
body_text: 'det var lunch på Ica, 240 kr',
media_id: null,
}),
})
mock.enqueue({ data: { id: 'msg-t1' } }) // claim
mock.enqueue({ data: makeLink() })
mock.enqueue({
data: makeConversation({
state: 'awaiting_resend',
context: {
pending_question: {
type: 'resend',
inbox_item_id: 'item-9',
asked_at: new Date().toISOString(),
},
},
}),
})
mock.enqueue({ data: { channel_context: { channel: 'whatsapp' } } }) // load item context
mock.enqueue({ data: null }) // item update
mock.enqueue({ data: null }) // markStatus done
await processInboundMessage(mock.supabase as unknown as SupabaseClient, 'msg-t1')
expect(sendTextMock).toHaveBeenCalledTimes(1)
expect(sendTextMock.mock.calls[0][1].template).toBe(TEMPLATE.m9NoteSaved)
const itemPatch = mock.findCall('invoice_inbox_items', 'update') as [
{ channel_context: Record<string, unknown> },
]
expect(itemPatch[0].channel_context.user_note).toContain('lunch på Ica')
// The question is untouched: only a sharper file can answer it.
expect(mock.findCalls('whatsapp_conversations', 'update')).toHaveLength(0)
})
})
describe('answer worker re-claims', () => {
it('does not follow a delivered confirm with the M16 fallback', async () => {
// A worker that died after applying the answer leaves the row
// 'processing'; the sweep re-runs it and the question now resolves to
// nothing, which used to send "I did not understand" right after the
// confirmation the user already received.
const mock = createQueuedMockSupabase()
mock.enqueue({
data: makeRow({
id: 'msg-t1',
message_type: 'text',
body_text: 'Lunch med Anna Berg',
media_id: null,
attempts: 1,
}),
})
mock.enqueue({ data: { id: 'msg-t1' } }) // claim
mock.enqueue({ data: makeLink() })
mock.enqueue({ data: makeConversation({ state: 'idle', context: {} }) })
mock.enqueue({ data: null }) // markStatus done
await processInboundMessage(mock.supabase as unknown as SupabaseClient, 'msg-t1')
expect(sendTextMock).not.toHaveBeenCalled()
})
it('still explains itself on a first attempt with nothing to answer', async () => {
const mock = createQueuedMockSupabase()
mock.enqueue({
data: makeRow({
id: 'msg-t1',
message_type: 'text',
body_text: 'kan du bokföra allt åt mig?',
media_id: null,
attempts: 0,
}),
})
mock.enqueue({ data: { id: 'msg-t1' } })
mock.enqueue({ data: makeLink() })
mock.enqueue({ data: makeConversation({ state: 'idle', context: {} }) })
mock.enqueue({ data: null })
await processInboundMessage(mock.supabase as unknown as SupabaseClient, 'msg-t1')
expect(sendTextMock).toHaveBeenCalledTimes(1)
expect(sendTextMock.mock.calls[0][1].template).toBe(TEMPLATE.m16Fallback)
})
})
describe('48h company-question expiry', () => {
it('keeps the parked receipts recoverable by a late answer', async () => {
const mock = createQueuedMockSupabase()
mock.enqueue({ data: [] }) // 1a stuck received
mock.enqueue({ data: [] }) // 1b stuck processing
mock.enqueue({ data: [] }) // 2a stale pending_ack
mock.enqueue({ data: [] }) // 2b unacked rows
mock.enqueue({
data: [
makeConversation({
state: 'awaiting_company',
context: {
company_options: [{ id: 'company-1', name: 'Bolag A AB' }],
pending_question: {
type: 'company',
inbox_item_id: null,
asked_at: new Date(Date.now() - 72 * 60 * 60 * 1000).toISOString(),
},
},
}),
],
}) // 3 question TTL
mock.enqueue({ data: null }) // stamp only rows past the media window
mock.enqueue({ count: 2 }) // rows still staged
mock.enqueue({ data: null }) // conversation back to idle
mock.enqueue({ data: [] }) // 4 pins
await runSweep(mock.supabase as unknown as SupabaseClient)
const expiryStamp = mock
.findCalls('whatsapp_messages', 'update')
.map((args) => args[0] as Record<string, unknown>)
.find((patch) => patch.error_message === 'company_choice_expired')
// The stamp is now bounded by how long Meta still serves the media.
expect(expiryStamp).toBeTruthy()
const stampFilters = mock.findCalls('whatsapp_messages', 'lt')
expect(stampFilters.some((args) => args[0] === 'created_at')).toBe(true)
const idleUpdate = mock
.findCalls('whatsapp_conversations', 'update')
.map((args) => args[0] as { state?: string; context?: Record<string, unknown> })
.find((patch) => patch.state === 'idle')
// company_options survive, so a late digit still maps to a company.
expect(idleUpdate?.context?.company_options).toBeTruthy()
expect(idleUpdate?.context?.pending_question).toBeUndefined()
})
})
describe('stop copy', () => {
it('does not claim the number is disconnected: stopp only pauses', () => {
for (const locale of ['sv', 'en'] as const) {
const body = botCopy(locale).m11Stop()
expect(body).not.toMatch(/kopplas från|is disconnected/)
expect(body).toMatch(/pausar|pausing/)
}
})
})