'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');
const { buildCustodyWorkbook } = require('../src/excel');

test('ملف Excel يحتوي المعادلات الصحيحة', async () => {
  const buffer = await buildCustodyWorkbook(
    { custody_number: 'VEG-TEST', custody_date: '2026-08-02', created_at: new Date() },
    [{ product_name: 'طماطم', unit_name: 'صندوق', quantity: 2, pack_size: 10, total_units: 20, unit_cost: 30, total_cost: 60, cost_per_unit: 3, sale_price: 4, note: '' }],
  );
  assert.equal(buffer.subarray(0, 2).toString(), 'PK');
  const raw = buffer.toString('utf8');
  assert.match(raw, /IFERROR\(C2\*D2,&quot;&quot;\)/);
  assert.match(raw, /IFERROR\(F2\*C2,&quot;&quot;\)/);
  assert.match(raw, /IFERROR\(G2\/E2,&quot;&quot;\)/);
  assert.match(raw, /طماطم/);
});
