Files
accounted/extensions/general/invoice-inbox/__tests__/resend-inbound.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

208 lines
6.8 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
extractLocalPartForDomain,
extractSharedRecipientsForDomain,
groupSharedRecipientsByInbox,
kindHintFromTag,
parseRecipients,
resolveKindHintForTags,
splitKnownInboxTags,
} from '@/extensions/general/invoice-inbox/lib/resend-inbound'
describe('extractLocalPartForDomain', () => {
it('returns the local part when a recipient matches the domain', () => {
const result = extractLocalPartForDomain(
['[email protected]', '[email protected]'],
'arcim.io'
)
expect(result).toEqual({ localPart: 'acme-ab-x7f2', tag: null })
})
it('lowercases the local part and matches domain case-insensitively', () => {
const result = extractLocalPartForDomain(
['[email protected]'],
'arcim.io'
)
expect(result).toEqual({ localPart: 'acme-ab-x7f2', tag: null })
})
it('splits a plus-address into local part and tag', () => {
expect(extractLocalPartForDomain(['[email protected]'], 'arcim.io')).toEqual({
localPart: 'acme-ab-x7f2',
tag: 'lev',
})
expect(extractLocalPartForDomain(['[email protected]'], 'arcim.io')).toEqual({
localPart: 'acme-ab-x7f2',
tag: 'ver',
})
})
it('lowercases the tag and splits at the first plus only', () => {
expect(extractLocalPartForDomain(['[email protected]'], 'arcim.io')).toEqual({
localPart: 'acme-ab-x7f2',
tag: 'lev+extra',
})
})
it('treats an empty tag as no tag', () => {
expect(extractLocalPartForDomain(['[email protected]'], 'arcim.io')).toEqual({
localPart: 'acme-ab-x7f2',
tag: null,
})
})
it('does not read a plus in a foreign-domain recipient', () => {
expect(extractLocalPartForDomain(['[email protected]', '[email protected]'], 'arcim.io')).toEqual({
localPart: 'acme-ab-x7f2',
tag: null,
})
})
it('returns null when no recipient matches', () => {
const result = extractLocalPartForDomain(
['[email protected]', '[email protected]'],
'arcim.io'
)
expect(result).toBeNull()
})
it('returns null for malformed addresses', () => {
const result = extractLocalPartForDomain(
['not-an-email', '@arcim.io', 'foo@'],
'arcim.io'
)
expect(result).toBeNull()
})
it('returns the first matching recipient when multiple match', () => {
const result = extractLocalPartForDomain(
['[email protected]', '[email protected]'],
'arcim.io'
)
expect(result?.localPart).toBe('first-abcd')
})
it('trims whitespace inside candidate addresses', () => {
const result = extractLocalPartForDomain(
[' [email protected] '],
'arcim.io'
)
expect(result?.localPart).toBe('acme-xxx')
})
})
describe('kindHintFromTag', () => {
it('maps the two documented tags', () => {
expect(kindHintFromTag('lev')).toBe('supplier_invoice')
expect(kindHintFromTag('ver')).toBe('receipt')
})
it('returns null for unknown, empty or missing tags', () => {
expect(kindHintFromTag('faktura')).toBeNull()
expect(kindHintFromTag('lev+extra')).toBeNull()
expect(kindHintFromTag('')).toBeNull()
expect(kindHintFromTag(null)).toBeNull()
expect(kindHintFromTag(undefined)).toBeNull()
})
})
describe('parseRecipients', () => {
it('splits recipients into lowercased localPart/domain pairs in order', () => {
expect(
parseRecipients(['[email protected]', '[email protected]'])
).toEqual([
{ localPart: 'faktura', domain: 'hansbolag.se' },
{ localPart: 'billing', domain: 'acme.se' },
])
})
it('skips malformed addresses', () => {
expect(parseRecipients(['not-an-email', '@x.se', 'foo@', '[email protected]'])).toEqual([
{ localPart: 'ok', domain: 'a.se' },
])
})
it('returns an empty array for no recipients', () => {
expect(parseRecipients([])).toEqual([])
})
})
describe('extractSharedRecipientsForDomain (#2181)', () => {
it('returns every shared-domain recipient in order with its tag', () => {
expect(
extractSharedRecipientsForDomain(
['[email protected]', '[email protected]', '[email protected]', '[email protected]'],
'arcim.io',
),
).toEqual([
{ localPart: 'acme-ab-x7f2', tag: 'lev' },
{ localPart: 'acme-ab-x7f2', tag: 'ver' },
{ localPart: 'other-1234', tag: null },
])
})
it('collapses the same address listed twice', () => {
expect(
extractSharedRecipientsForDomain(['[email protected]', '[email protected]'], 'arcim.io'),
).toEqual([{ localPart: 'acme-ab-x7f2', tag: 'lev' }])
})
it('skips foreign domains and malformed addresses', () => {
expect(
extractSharedRecipientsForDomain(['[email protected]', 'not-an-email', '@arcim.io', 'foo@'], 'arcim.io'),
).toEqual([])
})
it('keeps extractLocalPartForDomain as the first entry', () => {
const to = ['[email protected]', '[email protected]']
expect(extractLocalPartForDomain(to, 'arcim.io')).toEqual(extractSharedRecipientsForDomain(to, 'arcim.io')[0])
})
})
describe('groupSharedRecipientsByInbox (#2181)', () => {
it('groups recipients per local part with distinct tags in first-seen order', () => {
expect(
groupSharedRecipientsByInbox([
{ localPart: 'acme', tag: 'lev' },
{ localPart: 'other', tag: null },
{ localPart: 'acme', tag: 'ver' },
{ localPart: 'acme', tag: 'lev' },
]),
).toEqual([
{ localPart: 'acme', tags: ['lev', 'ver'] },
{ localPart: 'other', tags: [] },
])
})
})
describe('resolveKindHintForTags (#2181)', () => {
it('maps a single documented tag', () => {
expect(resolveKindHintForTags(['lev'])).toEqual({ kindHint: 'supplier_invoice', conflict: false })
expect(resolveKindHintForTags(['ver'])).toEqual({ kindHint: 'receipt', conflict: false })
})
it('returns no hint and no conflict for unknown or missing tags', () => {
expect(resolveKindHintForTags([])).toEqual({ kindHint: null, conflict: false })
expect(resolveKindHintForTags(['faktura'])).toEqual({ kindHint: null, conflict: false })
})
it('ignores unknown tags next to a documented one', () => {
expect(resolveKindHintForTags(['faktura', 'lev'])).toEqual({ kindHint: 'supplier_invoice', conflict: false })
})
it('resolves +lev and +ver on one mail to no hint, flagged as a conflict', () => {
expect(resolveKindHintForTags(['lev', 'ver'])).toEqual({ kindHint: null, conflict: true })
expect(resolveKindHintForTags(['ver', 'lev'])).toEqual({ kindHint: null, conflict: true })
})
})
describe('splitKnownInboxTags (#2181)', () => {
it('keeps the documented tags and only counts the rest', () => {
expect(splitKnownInboxTags(['lev', '8501011234', 'ver', 'anna-svensson'])).toEqual({
known: ['lev', 'ver'],
unknownCount: 2,
})
expect(splitKnownInboxTags([])).toEqual({ known: [], unknownCount: 0 })
})
})