Files
accounted/lib/parties/__tests__/suggest.test.ts
T
c6ca119e73 feat(parties): one suggestion per legal person, rename on rebuild, review list for SCB matches, model reading for memos (#2274)
* fix(parties): one suggestion per legal person, and a later run may rename an untouched one

Found while walking the queue end to end: two voucher keys naming the same
company ("TIC identity · … The Intelligence Company AB (publ)" and
"Utbetalning leverantörsfaktura …, The Intelligence Company AB (publ)")
became two suggestions and, after Lägg upp, two suppliers; and a suggestion
made before the legal-form anchoring kept its sentence-long name for good,
because apply_party_suggestions never touched a name.

- Suggestions whose display name is anchored on a legal form read out of
  the voucher text (name_anchored) are grouped: one item, both keys as
  aliases, stats summed. Such a name also attaches to an existing party
  called exactly that, legal form included, unless an org number on either
  side says otherwise. Registered company names are unique in Sweden; a
  bank memo never groups or attaches by name.
- Migration 20260904030000: apply_party_suggestions renames a suggestion
  nobody has touched (no decision, no user or registry fact) to an anchored
  name from a later run, and reports 'renamed'. Confirmed and decided
  parties keep their names.

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

* fix(parties): read legal_name for exact-name attach; say a row is foreign instead of offering SCB

next build: ExistingParty had no legal_name, so the exact-legal-name index
did not compile. The query now selects it.

Queue rows whose voucher text places the company abroad show
"Utländskt bolag (Nederländerna), finns inte i SCB" instead of a search
that cannot succeed; the promote dialog counts them separately from rows
that merely lack an org number; the dossier shows the country.

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

* fix(parties): carry country on the dossier row

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

* feat(parties): one review list for SCB matches, a model reading for bank memos, refresh demoted

- Review list ("Hitta org.nr (n)" in the queue toolbar): every suggestion
  SCB could hold but that lacks an org number is asked for, one row at a
  time under SCB's rate limit; rows with exactly one active match are
  shown ticked and approved in one click, the rest keep the per-row
  picker. Nothing is written before the click.
- Model reading (lib/parties/ai-name.ts, through getAiService): when the
  rules find no legal form or country in the texts, one call reads the
  counterpart out of the bank memo; kept as a 'model' fact, shown as
  "Läst ur verifikatet", used as the query, never as a hard key. On
  demand only, never when the queue builds.
- "Uppdatera förslag" moves from the page header to a ghost button in the
  toolbar: the queue builds itself now.

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

* fix(parties): review list passes the dialog overflow guard; plural for match counts

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

* fix(parties): gate the model reading on the company's AI capability

Same gate as every other model call on company data: the capability the
company holds by plan and can switch off. No call, no fact, no reading
without it.

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-04 17:01:10 +02:00

277 lines
14 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', 'voucher_text'])
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', 'voucher_text'])
expect(item.reason.org_number).toBe(ORG)
})
it('names the legal person in an assistant-written text and takes a foreign VAT number only on the expense side', () => {
const text = 'Utlägg Framer · Framer B.V. (NL), webbdesignverktyg. Säljaren debiterat svensk moms via OSS (NL VAT NL853695386B01 på fakturan).'
const expense = buildSuggestions({ observed: [observed({ key: 'utlägg framer', name: text, expense_sek: 500, revenue_sek: 0 })], evidence: [], existing: [] }).items[0]!
expect(expense.display_name).toBe('Framer B.V.')
expect(expense.vat_number).toBe('NL853695386B01')
expect(expense.facts.find((f) => f.field === 'country')).toMatchObject({ value: 'NL', source: 'ledger' })
expect(expense.facts.find((f) => f.field === 'voucher_text')).toMatchObject({ value: [text], source: 'ledger' })
const revenue = buildSuggestions({ observed: [observed({ key: 'framer intäkt', name: text, expense_sek: 0, revenue_sek: 500 })], evidence: [], existing: [] }).items[0]!
expect(revenue.vat_number).toBeUndefined()
expect(revenue.facts.some((f) => f.field === 'vat_number')).toBe(false)
})
it('groups keys that name the same legal person into one suggestion, and attaches to an existing party by exact legal name', () => {
const tic1 = 'TIC identity BG 0000005786439 Bg-bet. via internet · Faktura 20250746, The Intelligence Company AB (publ). TIC Identity-abonnemang.'
const tic2 = 'Utbetalning leverantörsfaktura 20250928, The Intelligence Company AB (publ)'
const r = buildSuggestions({
observed: [
observed({ key: 'tic identity', name: tic1, expense_sek: 2385, occurrences: 1, first_seen: '2026-02-01', last_seen: '2026-02-01' }),
observed({ key: 'utbetalning leverantörsfaktura the intelligence company publ', name: tic2, expense_sek: 2385, occurrences: 1, first_seen: '2026-03-01', last_seen: '2026-03-01' }),
],
evidence: [],
existing: [],
})
expect(r.items).toHaveLength(1)
const item = r.items[0]!
expect(item.display_name).toBe('The Intelligence Company AB (publ)')
expect(item.name_anchored).toBe(true)
expect(item.alias_keys).toEqual(['tic identity', 'utbetalning leverantörsfaktura the intelligence company publ'])
expect(item.reason.occurrences).toBe(2)
expect(item.reason.expense_sek).toBe(4770)
expect(item.reason.first_seen).toBe('2026-02-01')
expect(item.reason.last_seen).toBe('2026-03-01')
const confirmed: ExistingParty = { id: 'p-tic', display_name: 'The Intelligence Company AB (publ)', org_number: '5594871682', alias_keys: [], status: 'confirmed' }
const attached = buildSuggestions({ observed: [observed({ key: 'tic identity', name: tic1 })], evidence: [], existing: [confirmed] }).items[0]!
expect(attached.party_id).toBe('p-tic')
expect(attached.reason.attach).toBe('legal_name')
// A different org number on the key side is a different company with a confusable name.
const other = buildSuggestions({
observed: [observed({ key: 'tic identity', name: tic1 })],
evidence: [{ key: 'tic identity', docs: 1, self_docs: 0, orgs: [{ org: '5560125790', n: 1 }], vat_numbers: [], names: [], bankgiro: [], plusgiro: [] }],
existing: [confirmed],
}).items[0]!
expect(other.party_id).toBeUndefined()
// A bank memo head never groups or attaches by name.
const memo = buildSuggestions({ observed: [observed({ key: 'beijer byggmaterial', name: 'BEIJER BYGGMATERIAL 2089' })], evidence: [], existing: [{ id: 'p-b', display_name: 'BEIJER BYGGMATERIAL', org_number: null, alias_keys: [], status: 'confirmed' }] }).items[0]!
expect(memo.party_id).toBeUndefined()
expect(memo.name_anchored).toBeUndefined()
})
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'])
})
})