You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sims-hq/apps/hq/test/import.test.ts

34 lines
1.6 KiB
TypeScript

// apps/hq/test/import.test.ts
import { describe, it, expect } from 'vitest'
import { openDb } from '../src/db'
import { seedIfEmpty } from '../src/seed'
import { stageCsv, verificationReport, commitImport } from '../src/import-apex'
const CLIENTS = `code,name,gstin,state_code,address,phone,email,status
AC001,Acme Traders,,32,Kochi,9744000001,acme@x.in,active
,No Code Shop,,32,,,,active`
const INVOICES = `client_code,doc_no,doc_date,taxable,tax,total,paid
AC001,TS/26-27/0411,2026-05-02,10000.00,1800.00,11800.00,1
AC001,TS/26-27/0412,2026-06-15,5000.00,900.00,5900.00,0`
describe('apex import', () => {
it('stages, reports problems, refuses commit until clean', async () => {
const db = openDb(':memory:'); await seedIfEmpty(db)
await stageCsv(db, 'clients', CLIENTS)
await stageCsv(db, 'invoices', INVOICES)
const rep = await verificationReport(db)
expect(rep.clients.staged).toBe(2)
expect(rep.clients.problems).toBe(1) // missing code
await expect(commitImport(db, 'u1')).rejects.toThrow(/problem/)
})
it('commits clean data and seeds the invoice series from the last APEX number', async () => {
const db = openDb(':memory:'); await seedIfEmpty(db)
await stageCsv(db, 'clients', CLIENTS.split('\n').slice(0, 2).join('\n'))
await stageCsv(db, 'invoices', INVOICES)
const out = await commitImport(db, 'u1')
expect(out).toMatchObject({ clients: 1, invoices: 2, seeded: { fy: '2026-27', lastSeq: 412 } })
const paid = await db.get(`SELECT status FROM document WHERE doc_no='TS/26-27/0411'`) as { status: string }
expect(paid.status).toBe('paid')
})
})