import { InvalidLineError, UnbalancedEntryError } from '../../src/modules/gl/posting.types'; const transactionMock = jest.fn(); jest.mock('../../src/lib/prisma', () => ({ prisma: { $transaction: transactionMock }, })); // eslint-disable-next-line import/first import { postEntry } from '../../src/modules/gl/posting.service'; const baseDraft = { journalCode: 'GJ', entryDate: new Date('2026-01-15'), description: 'Test entry', sourceModule: 'GL' as const, createdById: 1, }; describe('PostingService.postEntry — pre-write validation (no DB required)', () => { beforeEach(() => { transactionMock.mockReset(); }); it('rejects an unbalanced entry before touching the database', async () => { await expect( postEntry({ ...baseDraft, lines: [ { glAccountId: 1, debit: 100, credit: 0 }, { glAccountId: 2, debit: 0, credit: 50 }, ], }), ).rejects.toBeInstanceOf(UnbalancedEntryError); expect(transactionMock).not.toHaveBeenCalled(); }); it('rejects a line with both debit and credit set', async () => { await expect( postEntry({ ...baseDraft, lines: [ { glAccountId: 1, debit: 100, credit: 100 }, { glAccountId: 2, debit: 0, credit: 0 }, ], }), ).rejects.toBeInstanceOf(InvalidLineError); expect(transactionMock).not.toHaveBeenCalled(); }); it('rejects a line with neither debit nor credit set', async () => { await expect( postEntry({ ...baseDraft, lines: [ { glAccountId: 1, debit: 0, credit: 0 }, { glAccountId: 2, debit: 100, credit: 100 }, ], }), ).rejects.toBeInstanceOf(InvalidLineError); expect(transactionMock).not.toHaveBeenCalled(); }); it('rejects a negative amount', async () => { await expect( postEntry({ ...baseDraft, lines: [ { glAccountId: 1, debit: -50, credit: 0 }, { glAccountId: 2, debit: 0, credit: -50 }, ], }), ).rejects.toBeInstanceOf(InvalidLineError); expect(transactionMock).not.toHaveBeenCalled(); }); it('rejects an entry with no lines', async () => { await expect(postEntry({ ...baseDraft, lines: [] })).rejects.toBeInstanceOf(InvalidLineError); expect(transactionMock).not.toHaveBeenCalled(); }); it('proceeds to the DB transaction once a balanced, valid draft is given', async () => { transactionMock.mockResolvedValue({ id: 1 }); await postEntry({ ...baseDraft, lines: [ { glAccountId: 1, debit: 100, credit: 0 }, { glAccountId: 2, debit: 0, credit: 100 }, ], }); expect(transactionMock).toHaveBeenCalledTimes(1); }); });