Files
accounted/lib/parties/__tests__/suggest.test.ts
T
b996da60ee feat(parties): Förslag från bokföringen, confirmed straight into Leverantörer and Kunder (#2206)
* feat(parties): Kontakter register, suggestion queue, dossier and merge

Phase 1's two surfaces on top of the parties substrate:

- /parties page: one list with the five-way switch (Alla, Kunder,
  Leverantörer, Förslag, Bara i bokföringen), search, a 12-month/all
  period picker, and at most one attention line. Confirmed rows show
  roles as muted text, rhythm, underlag, dominant account and money.
  Observed rows are computed and never stored; a generic band keeps
  unattributed spend visible.
- Suggestion queue: a reason per row, hard-key rows pre-ticked, bulk
  confirm behind one dialog, dismiss on hover, undo on the toast.
- Dossier slide-over: Pengar, Bokföring, Vad Accounted vet (facts and
  identities with source and count), Underlag och verifikat, Historik.
- Merge dialog with a visible, swappable survivor and undo.
- API: GET /api/parties, GET /api/parties/[id], POST suggest, decide,
  decide/undo, merge, merge/undo (withRouteContext, Zod, 15 tests).
- Migration 20260903090000: decide_parties snapshots the reason it
  clears; undo_party_decisions reverses confirm/dismiss within 30 days;
  decision kind 'undo'.
- The pipeline runs after SIE import and provider migration (non-blocking)
  so a migrant's register is full on arrival.
- Nav entry under Register; sv/en strings.

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

* fix(parties): pass explicit interpolation values to next-intl

next build's type check rejects a typed interface where the translator
wants an index-signature record.

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

* fix(parties): retry label on the load-failed state

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

* fix(parties): hard keys for companies without org number, readable names, look-alikes at read time

- get_ledger_key_evidence dropped every document for a company whose own
  org number is NULL (the self check compared against NULL). Replaced in
  20260903100000 with a coalesced comparison; pg test covers it.
- Display names come from the printed name on documents, otherwise from
  the voucher text with the AP/AR prefix and supplier number removed.
- Look-alike parties (same core, or one core extending the other by whole
  words: Fortnox / Fortnox Finans) are detected when the register is read,
  never stored, and feed the Dubblett? chip and the merge dialog.
- Queue shows Intäkt beside Kostnad; dossier hides zero money rows and
  formats bankgiro/plusgiro; merge dialog cancels with Avbryt; no
  synchronous setState inside effects.

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

* feat(parties): link every new supplier and customer to a party on write

The backfill covered the rows that existed on 2026-09-02; 108 rows
created since had no party and never reached the register. A BEFORE
INSERT/UPDATE trigger on customers and suppliers now calls ensure_party
on every write path at once: find-or-create by org number inside the
company, never by name; a private customer gets a kind=person party
without any number; a nameless row stays unlinked; a foreign party id is
refused with the same error as the composite foreign key; a link to a
merged party follows the chain to the survivor; the clear that ON DELETE
SET NULL performs is kept. ensure_party lets the trigger act for the
row's owner (pg_trigger_depth() > 0); the RPC path is unchanged. The
migration also links the rows created since the backfill.

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

* fix(parties): dossier hides dismissed parties and follows merges to the survivor

The register hid archived parties while the dossier still served them by
id, and a merged party's dossier pointed at a dead row. Superagent P2 on
#2206; three unit tests.

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

* chore(parties): move the role-link migration past main's 20260903110000

Two files with one version would collide in schema_migrations.

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

* feat(parties): confirm suggestions into Leverantörer and Kunder, no third noun

Founder decision after the walkthrough: users know two words. The page
becomes the queue 'Förslag från bokföringen' with 'Bara i bokföringen'
beside it; the Kontakter nav entry and the Alla/Kunder/Leverantörer
views go. Each suggestion shows what it becomes (Blir), read from the
ledger side and changeable per row; confirming calls promote_parties,
which creates the supplier and/or customer row from the party's facts,
never a duplicate, and is undoable for 30 days through
undo_party_promotions (the created rows are archived, the party returns
to the queue). Leverantörer and Kunder carry the one attention line that
leads here. The dossier offers Lägg upp som leverantör / som kund.

Migration 20260903130000, 5 pg tests, route and unit tests updated.

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

* fix(parties): write bankgiro and plusgiro the way the supplier form does

Identities are stored as digits; suppliers carry 5317-0900.

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

* chore(parties): move the four queue migrations past main's 20260903170000

Main merged 20260903120000_skattekonto_transactions_realtime_publication
with the same version as the role-link trigger; the preview database
refused the duplicate key. All four now sit after main's newest so the
set applies in one ordered run on prod.

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

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-03 19:49:25 +02:00

226 lines
11 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { coreKey, displayNameFromVoucherText } from '../ledger-key'
import type { ObservedParty } from '../observed'
import { buildSuggestions, suggestPartiesForCompany, type ExistingParty, type LedgerKeyEvidence } from '../suggest'
function observed(over: Partial<ObservedParty> & { key: string }): ObservedParty {
return {
name: over.key.toUpperCase(),
variants: [],
variant_count: 1,
occurrences: 3,
expense_sek: 3000,
revenue_sek: 0,
first_seen: '2026-01-10',
last_seen: '2026-03-10',
cadence_days: 30,
dominant_account_number: '4000',
dominant_account_share: 0.6,
dominant_account_count: 2,
dominant_account_total: 3,
label: 'party',
rhythm: 'monthly',
...over,
}
}
function evidence(over: Partial<LedgerKeyEvidence> & { key: string }): LedgerKeyEvidence {
return { docs: 0, self_docs: 0, orgs: [], vat_numbers: [], names: [], bankgiro: [], plusgiro: [], ...over }
}
const ORG = '5564300142'
describe('coreKey', () => {
it('strips AP prefixes, digit runs and legal forms', () => {
expect(coreKey('levfakt beijer byggmaterial ab 2089')).toBe('beijer byggmaterial')
expect(coreKey('Fortnox Finans AB')).toBe('fortnox finans')
expect(coreKey('inköp av varor')).toBe('av varor')
})
})
describe('displayNameFromVoucherText', () => {
it('drops AP/AR prefixes and supplier numbers but keeps casing and legal form', () => {
expect(displayNameFromVoucherText('Levfakt BEIJER BYGGMATERIAL AB (2089)')).toBe('BEIJER BYGGMATERIAL AB')
expect(displayNameFromVoucherText('Levfakt Beijer Byggmaterial AB, 097')).toBe('Beijer Byggmaterial AB')
expect(displayNameFromVoucherText('Kundbet Acme Konsult AB')).toBe('Acme Konsult AB')
expect(displayNameFromVoucherText('Leverantörsfaktura från 18 Loopia')).toBe('Loopia')
expect(displayNameFromVoucherText('UBER *TRIP HELP.UBER.COM')).toBe('UBER *TRIP HELP.UBER.COM')
expect(displayNameFromVoucherText('Inköp av varor')).toBe('Inköp av varor')
})
})
describe('buildSuggestions', () => {
it('skips keys the pre-classifier does not call party', () => {
const r = buildSuggestions({
observed: [observed({ key: 'inköp av varor', label: 'category' }), observed({ key: 'lön mars', label: 'payroll' })],
evidence: [],
existing: [],
})
expect(r.items).toHaveLength(0)
expect(r.skipped).toEqual([
{ key: 'inköp av varor', label: 'category' },
{ key: 'lön mars', label: 'payroll' },
])
})
it('creates a new suggested party from the ledger alone, with ledger facts and a reason', () => {
const r = buildSuggestions({ observed: [observed({ key: 'beijer byggmaterial' })], evidence: [], existing: [] })
expect(r.items).toHaveLength(1)
const item = r.items[0]!
expect(item.party_id).toBeUndefined()
expect(item.org_number).toBeUndefined()
expect(item.origin).toBe('ledger')
expect(item.display_name).toBe('BEIJER BYGGMATERIAL')
expect(item.alias_keys).toEqual(['beijer byggmaterial'])
expect(item.reason.attach).toBe('new')
expect(item.reason.occurrences).toBe(3)
expect(item.facts.map((f) => f.field)).toEqual(['dominant_account', 'cadence_days'])
expect(item.identities).toEqual([])
})
it('uses the document hard key: org number, printed name, VAT and identities', () => {
const r = buildSuggestions({
observed: [observed({ key: 'beijer byggmaterial' })],
evidence: [
evidence({
key: 'beijer byggmaterial',
docs: 3,
orgs: [{ org: ORG, n: 3 }],
vat_numbers: [{ vat: `SE${ORG}01`, n: 3 }],
names: [{ name: 'Beijer Byggmaterial AB', n: 3 }],
bankgiro: [{ value: '53170900', n: 3, first_seen: '2026-01-10', last_seen: '2026-03-10' }],
}),
],
existing: [],
})
const item = r.items[0]!
expect(item.org_number).toBe(ORG)
expect(item.origin).toBe('document')
expect(item.display_name).toBe('Beijer Byggmaterial AB')
expect(item.legal_name).toBe('Beijer Byggmaterial AB')
expect(item.vat_number).toBe(`SE${ORG}01`)
expect(item.identities).toEqual([
{ scheme: 'bankgiro', value: '53170900', first_seen: '2026-01-10', last_seen: '2026-03-10', seen_count: 3 },
])
expect(item.facts.map((f) => f.field)).toEqual(['dominant_account', 'cadence_days', 'org_number', 'legal_name'])
expect(item.reason.org_number).toBe(ORG)
})
it('withholds the hard key and identities when a key mixes two org numbers', () => {
const r = buildSuggestions({
observed: [observed({ key: 'vattenfall' })],
evidence: [
evidence({
key: 'vattenfall',
docs: 4,
orgs: [
{ org: ORG, n: 2 },
{ org: '5560125790', n: 2 },
],
bankgiro: [{ value: '51108348', n: 4, first_seen: '2026-01-01', last_seen: '2026-04-01' }],
}),
],
existing: [],
})
const item = r.items[0]!
expect(item.org_number).toBeUndefined()
expect(item.identities).toEqual([])
expect(item.reason.ambiguous_orgs).toEqual([ORG, '5560125790'])
})
it('attaches to an existing party by org number, then by exact alias key, never by name', () => {
const byOrg: ExistingParty = { id: 'p-org', display_name: 'Beijer AB', org_number: ORG, alias_keys: [], status: 'confirmed' }
const byAlias: ExistingParty = { id: 'p-alias', display_name: 'Loopia', org_number: null, alias_keys: ['loopia'], status: 'suggested' }
const lookalike: ExistingParty = { id: 'p-fortnox', display_name: 'Fortnox AB', org_number: '5566661012', alias_keys: [], status: 'confirmed' }
const r = buildSuggestions({
observed: [observed({ key: 'beijer byggmaterial' }), observed({ key: 'loopia' }), observed({ key: 'fortnox finans' })],
evidence: [evidence({ key: 'beijer byggmaterial', docs: 1, orgs: [{ org: ORG, n: 1 }] })],
existing: [byOrg, byAlias, lookalike],
})
const [beijer, loopia, fortnox] = r.items
expect(beijer!.party_id).toBe('p-org')
expect(beijer!.reason.attach).toBe('org_number')
expect(loopia!.party_id).toBe('p-alias')
expect(loopia!.reason.attach).toBe('alias_key')
// Same trade name is a question for a person, not a merge.
expect(fortnox!.party_id).toBeUndefined()
expect(fortnox!.reason.attach).toBe('new')
expect(fortnox!.reason.similar_to).toBeUndefined()
})
it('reports same-core live parties as similar_to on new suggestions', () => {
const existing: ExistingParty = { id: 'p1', display_name: 'Levfakt Beijer Byggmaterial AB 2089', org_number: null, alias_keys: [], status: 'suggested' }
const r = buildSuggestions({ observed: [observed({ key: 'beijer byggmaterial' })], evidence: [], existing: [existing] })
expect(r.items[0]!.party_id).toBeUndefined()
expect(r.items[0]!.reason.similar_to).toEqual([{ party_id: 'p1', display_name: 'Levfakt Beijer Byggmaterial AB 2089' }])
})
})
describe('suggestPartiesForCompany', () => {
function stubClient(opts: { observed: unknown[]; evidence: unknown[]; existing: unknown[]; apply: unknown }) {
const rpc = vi.fn(async (name: string, _args?: Record<string, unknown>) => {
if (name === 'get_observed_parties') return { data: opts.observed, error: null }
if (name === 'get_ledger_key_evidence') return { data: opts.evidence, error: null }
if (name === 'apply_party_suggestions') return { data: opts.apply, error: null }
return { data: null, error: { message: `unexpected rpc ${name}` } }
})
const range = vi.fn(async () => ({ data: opts.existing, error: null }))
const chain: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'is', 'order']) chain[m] = vi.fn(() => chain)
chain.range = range
const from = vi.fn(() => chain)
return { client: { rpc, from } as never, rpc, from }
}
it('runs observed -> evidence -> existing -> apply and sums the RPC summary', async () => {
const { client, rpc } = stubClient({
observed: [
{ key: 'beijer byggmaterial', name: 'BEIJER', variants: [], variant_count: 1, occurrences: 3, expense_sek: 3000, revenue_sek: 0, first_seen: '2026-01-10', last_seen: '2026-03-10', cadence_days: 30, dominant_account_number: '4000', dominant_account_share: 0.6, dominant_account_count: 2, dominant_account_total: 3 },
{ key: 'inköp av varor', name: 'Inköp av varor', variants: [], variant_count: 1, occurrences: 1, expense_sek: 300, revenue_sek: 0, first_seen: '2026-03-15', last_seen: '2026-03-15', cadence_days: null, dominant_account_number: '4010', dominant_account_share: 0.5, dominant_account_count: 1, dominant_account_total: 1 },
],
evidence: [],
existing: [],
apply: { created: 1, attached: 0, identities: 0, facts: 2 },
})
const summary = await suggestPartiesForCompany(client, 'co', 'user')
expect(summary).toEqual({ observed: 2, suggested: 1, skipped: 1, created: 1, attached: 0, identities: 0, facts: 2 })
const applyCall = rpc.mock.calls.find((c) => c[0] === 'apply_party_suggestions')!
const args = applyCall[1] as unknown as { p_company_id: string; p_user_id: string; p_items: Array<{ key: string }> }
expect(args.p_company_id).toBe('co')
expect(args.p_user_id).toBe('user')
expect(args.p_items.map((i) => i.key)).toEqual(['beijer byggmaterial'])
})
it('does not call apply when nothing is a party', async () => {
const { client, rpc } = stubClient({ observed: [], evidence: [], existing: [], apply: null })
const summary = await suggestPartiesForCompany(client, 'co', 'user')
expect(summary.suggested).toBe(0)
expect(rpc.mock.calls.map((c) => c[0])).not.toContain('apply_party_suggestions')
})
it('surfaces RPC errors', async () => {
const rpc = vi.fn(async () => ({ data: null, error: { message: 'boom' } }))
await expect(suggestPartiesForCompany({ rpc } as never, 'co', 'user')).rejects.toThrow(/get_observed_parties failed: boom/)
})
})
describe('similarAmong', () => {
it('pairs same-core and whole-word-extended names, never unrelated ones', async () => {
const { similarAmong } = await import('../register')
const m = similarAmong([
{ id: 'a', display_name: 'Fortnox AB', alias_keys: ['fortnox'] },
{ id: 'b', display_name: 'Fortnox Finans AB', alias_keys: ['fortnox finans'] },
{ id: 'c', display_name: 'Rikshem Uppsala KB', alias_keys: [] },
{ id: 'd', display_name: 'Rikshem', alias_keys: [] },
{ id: 'e', display_name: 'Fortum Markets AB', alias_keys: [] },
{ id: 'f', display_name: 'Levfakt Beijer Byggmaterial AB 2089', alias_keys: ['beijer byggmaterial'] },
{ id: 'g', display_name: 'BEIJER BYGGMATERIAL', alias_keys: [] },
])
expect(m.get('a')!.map((s) => s.id)).toEqual(['b'])
expect(m.get('b')!.map((s) => s.id)).toEqual(['a'])
expect(m.get('c')!.map((s) => s.id)).toEqual(['d'])
expect(m.get('e')).toEqual([])
expect(m.get('f')!.map((s) => s.id)).toEqual(['g'])
})
})