* feat(invoice-inbox): store HTML mails as underlag, expandable field editor Body-only mails and .html attachments (including forwarded .eml bodies) no longer dead-end as "Fel vid bearbetning": the mail body is wrapped into a self-contained text/html document, stored through the normal upload/extract pipeline, and extracted via a new HTML-to-text Bedrock path, so the mail itself can serve as bookable underlag. Empty mails keep the error row, unsupported types are still rejected, and webhook retries dedupe on resend_email_id. Mail HTML is attacker-controlled, so rendering is fully sandboxed: iframe sandbox in the workspace preview and a CSP sandbox header on /api/documents/:id/inline for text/html. The type is accepted only from the email pipeline (EMAIL_ALLOWED_MIME_TYPES), never from manual upload. The "Extraherade falt" rail gains an expand button opening a centered dialog with the same autosaving field editor at a readable size (two columns), which also gives every failed or skipped extraction a manual fallback. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bookkeeping): sortable verifikat list headers with amount sort - clickable sort toggles on the verifikat list headers (asc -> desc -> default) - total_amount computed column + sort_by total/description on the list route - failed list loads render an error card with retry, never the empty-ledger state Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(import): decode bank CSV as Windows-1252 fallback in column mapping The client read the uploaded file with file.text(), which is UTF-8-only, so Windows-1252 exports (e.g. Handelsbanken) rendered and re-parsed with U+FFFD in place of Swedish characters. Decode from bytes with the shared decodeFileContent() helper, matching what the server parse route does. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bookkeeping): stackable sort keys on verifikat list headers - shift-click adds a column as secondary/tertiary sort key (max 3), plain click keeps the single-key tri-state cycle - sort_by accepts a comma-separated priority list; single tokens stay valid - voucher tiebreak follows the last key's direction (#972 parity) - priority numbers on stacked headers; hint text in the filter dialog Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(invoices): keep ROT/RUT deduction fields positive on credit notes Crediting an invoice with a ROT/RUT deduction failed 100% of the time: the credit-note path negated deduction_total (and per-item deduction_amount) like the other amounts, but both columns carry CHECK (>= 0), so Postgres rejected the insert and the user only saw 'Kunde inte skapa kreditfaktura'. Store the deduction fields as positive magnitudes, matching the convention everywhere else. The stored sign is inert on credit notes: the reversing verifikat recomputes the ROT/RUT split from the items, and the PDF and amount-to-pay logic skip deductions on credit notes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor(transactions): share the source chip across inbox and history modes Move SourceFilter to transaction-types.ts (widened with 'bank:other' and 'acct:<id>'), render the one toolbar ContextPicker in both view modes, and drop the narrower duplicate chip inside TransactionHistoryList. The history list now applies the acct:/bank:other narrowing itself and hides skattekonto rows under any bank-side selection. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore(deps): bump js-yaml to 4.3.1 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(schema): recognize PostgREST computed columns in the migration parser The verifikat amount sort orders by total_amount, a PostgREST computed column (a function on the journal_entries row type, migration 20260811100000). The schema guard only modeled real columns, so no-phantom-columns flagged the order as a phantom. Teach the parser that a function whose only argument is a table's row type joins that table's column set, with DROP FUNCTION retraction when the signature names the row type. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: resolve PR #1523 review findings - journal-entries route: append the globally unique id tiebreak to every direct-query sort; voucher series+number repeat across fiscal years, so the all-years scope could duplicate or drop rows at page boundaries. Existing order assertions updated, new all-years tiebreak test. - documents inline route: CSP source policy on HTML previews; sandbox alone still loads remote resources, letting a tracking pixel notify the sender on open. New route test asserts the full header. - JournalEntryList: catch rejected list requests so loading cannot stick forever, and gate every post-await state write behind a request generation so a slow earlier request cannot overwrite the current sort. - TransactionHistoryList: pagination follows the selected source scope (reachable with zero matches on the current page, hidden for the skattekonto scope it cannot affect). - transactions page: bank:other picker availability derives from history rows too, not only the pending inbox dataset. - DECISIONS.md: mark the superseded single-sort decision; record the credit-note deduction positive-magnitude invariant and its verified reader inventory (Swedish review flag). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: guard metadata refetches behind the list request generation fetchAttachmentCounts and fetchRattelseFlags write state after their own awaits; a stale list request's late completion could overwrite attachment counts and rattelse flags for rows a newer request just rendered, showing false missing-underlag warnings. Both helpers now take the caller's generation guard and discard stale completions, including the attachment-counts loaded flag. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
101 lines
3.9 KiB
TypeScript
101 lines
3.9 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
ensureHtmlDocument,
|
|
buildEmailBodyHtmlDocument,
|
|
EMAIL_ALLOWED_MIME_TYPES,
|
|
UPLOAD_ALLOWED_MIME_TYPES,
|
|
} from '@/extensions/general/invoice-inbox/lib/upload-and-extract'
|
|
import { htmlToText } from '@/extensions/general/invoice-inbox/lib/extract-invoice-fields'
|
|
import { validateDocumentMagicBytes } from '@/lib/core/documents/document-service'
|
|
|
|
const decode = (buf: ArrayBuffer) => new TextDecoder().decode(new Uint8Array(buf))
|
|
|
|
describe('EMAIL_ALLOWED_MIME_TYPES', () => {
|
|
it('is the upload set plus text/html, and only reachable from the email pipeline', () => {
|
|
for (const type of UPLOAD_ALLOWED_MIME_TYPES) {
|
|
expect(EMAIL_ALLOWED_MIME_TYPES.has(type)).toBe(true)
|
|
}
|
|
expect(EMAIL_ALLOWED_MIME_TYPES.has('text/html')).toBe(true)
|
|
// The manual upload surface must stay strict.
|
|
expect(UPLOAD_ALLOWED_MIME_TYPES.has('text/html')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('ensureHtmlDocument', () => {
|
|
it('wraps a fragment-shaped mail body into a full document', () => {
|
|
const out = decode(ensureHtmlDocument('<div>Faktura 123</div>'))
|
|
expect(out.toLowerCase().startsWith('<!doctype html')).toBe(true)
|
|
expect(out).toContain('<meta charset="utf-8">')
|
|
expect(out).toContain('<div>Faktura 123</div>')
|
|
})
|
|
|
|
it('passes a full document through unchanged', () => {
|
|
const full = '<!DOCTYPE html>\n<html><body>Hej</body></html>'
|
|
expect(decode(ensureHtmlDocument(full))).toBe(full)
|
|
const rooted = '<html><body>Hej</body></html>'
|
|
expect(decode(ensureHtmlDocument(rooted))).toBe(rooted)
|
|
})
|
|
|
|
it('recognises a full document behind leading whitespace', () => {
|
|
const full = '\n <!doctype html><html></html>'
|
|
expect(decode(ensureHtmlDocument(full))).toBe(full)
|
|
})
|
|
|
|
it('produces bytes that pass the document-service magic check', () => {
|
|
expect(
|
|
validateDocumentMagicBytes(ensureHtmlDocument('<td>999 kr</td>'), 'text/html'),
|
|
).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('buildEmailBodyHtmlDocument', () => {
|
|
it('prefers the HTML part over the text part', () => {
|
|
const out = decode(buildEmailBodyHtmlDocument('<p>Belopp: 100 kr</p>', 'plain fallback')!)
|
|
expect(out).toContain('<p>Belopp: 100 kr</p>')
|
|
expect(out).not.toContain('plain fallback')
|
|
})
|
|
|
|
it('escapes a plain-text-only body into a <pre>', () => {
|
|
const out = decode(
|
|
buildEmailBodyHtmlDocument(null, 'Total <script>alert(1)</script> & 100 kr')!,
|
|
)
|
|
expect(out).toContain('<pre>')
|
|
expect(out).toContain('Total <script>alert(1)</script> & 100 kr')
|
|
expect(out).not.toContain('<script>')
|
|
})
|
|
|
|
it('returns null when the mail has no body worth storing', () => {
|
|
expect(buildEmailBodyHtmlDocument(null, null)).toBeNull()
|
|
expect(buildEmailBodyHtmlDocument(' ', ' \n ')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('htmlToText', () => {
|
|
it('drops script/style/head blocks and comments', () => {
|
|
const text = htmlToText(
|
|
'<html><head><title>x</title><style>.a{color:red}</style></head>' +
|
|
'<body><script>evil()</script><!-- hidden --><p>Faktura 42</p></body></html>',
|
|
)
|
|
expect(text).toBe('Faktura 42')
|
|
})
|
|
|
|
it('keeps block boundaries as newlines so labels and amounts stay separated', () => {
|
|
const text = htmlToText('<table><tr><td>Att betala</td></tr><tr><td>1 234,56 kr</td></tr></table>')
|
|
expect(text).toContain('Att betala')
|
|
expect(text).toContain('1 234,56 kr')
|
|
expect(text.split('\n').length).toBeGreaterThan(1)
|
|
})
|
|
|
|
it('decodes entities, ampersand strictly last', () => {
|
|
expect(htmlToText('Moms & frakt: 25 kr')).toBe('Moms & frakt: 25 kr')
|
|
// Double-decoding would turn this into a real "<" instead of the literal.
|
|
expect(htmlToText('visar &lt; som text')).toBe('visar < som text')
|
|
expect(htmlToText('€100 och 'citat'')).toBe("€100 och 'citat'")
|
|
})
|
|
|
|
it('caps the output length', () => {
|
|
const long = `<p>${'a'.repeat(80_000)}</p>`
|
|
expect(htmlToText(long).length).toBeLessThanOrEqual(50_000)
|
|
})
|
|
})
|