const { ObjectId } = require('bson');
const Container = require('./container');

describe('Container', () => {
  const container = new Container();

  it('should return null normalize values if no units are passed in', () => {
    expect(container.normalizeSizeAndAmount(1, 1, null).normalizedSize).toEqual({ gram: null, liter: null });
  });

  it('should return null normalize values if unit isother ', () => {
    expect(container.normalizeSizeAndAmount(1, 1, 'other').normalizedSize).toEqual({ gram: null, liter: null });
  });

  it('should normalize to gram using conversion factory', () => {
    expect(container.normalizeSizeAndAmount(2, 1, 'μg').normalizedSize).toEqual({ gram: 0.000002, liter: 2e-9 });
  });

  it('should normalize units gram using density when unit is volume', () => {
    expect(container.normalizeSizeAndAmount(2, 1, 'μL', '.5').normalizedSize).toEqual({ gram: 0.001, liter: 0.000002 });
  });

  it('should normalize units gram defaulting density to 1 when density is not supplied when unit is volume', () => {
    expect(container.normalizeSizeAndAmount(2, 1, 'μL').normalizedSize).toEqual({ gram: 0.002, liter: 0.000002 });
  });

  it('should set container amount to container size if not passed in', () => {
    const cont = new Container({ size: 10, amount: null });
    expect(cont.amount).toEqual(10);
  });

  it('should allow container amount to be zero', () => {
    const cont = new Container({ amount: 0, size: 10 });
    expect(cont.amount).toEqual(0);
  });

  describe('barcode', () => {
    const containerData = {
      _id: new ObjectId('5abc0aca8bdad56d036adf48'),
      substance: {
        _id: null,
      },
      family: {
        _id: new ObjectId('5956e591bd74595f951d41bf'),
        density: {
          value: 0.71,
          unit: 'g/mL',
        },
      },
      inventory: {
        _id: new ObjectId('5adf97597ed8203054035908'),
        tenantCode: 'UC',
        campusCode: '03',
        name: 'Demo Lab',
      },
      location: {
        buildingId: 'MDN8RFYtMDEtMDAxMjMw',
        floorId: 'MDN8RFYtMDEtMDAxMjMwfERWLTAxLTAwMDE3NjQ',
        roomId: 'MDN8RFYtMDEtMDAxMjMwfERWLTAxLTAwMDE3NjR8RFYtMDEtMDAwMzUzMTI',
        sublocationId: '5adfab3949a95f33e82afe04',
        buildingName: '',
        floorName: '',
        roomNumber: '',
        sublocationName: 'Davis 202 Cousteau Place - 205100',
      },
      size: 3,
      units: 'g',
      physicalState: 'Liquid',
      type: 'GLASS_BOTTLE',
      openedDate: null,
      receivedDate: null,
      expirationDate: null,
      lastTestedDate: null,
      isPrivate: false,
      barcode: 'UC0000241146',
      customBarcode: null,
      audits: [],
      tags: [],
      comments: null,
      checkout: null,
      normalized: {
        gram: 3,
        liter: 0.004225352112676057,
      },
      solvent: null,
      concentration: null,
      concentrationUnits: null,
      createdByUserId: 'ian',
      _createdDate: new Date(),
    };

    it('should validate UC barcode ', () => {
      const containerToValidate = new Container(containerData);
      const validation = containerToValidate.validate();
      expect(validation.valid).toBeTruthy();
    });

    it('should invalidate non-UC barcode ', () => {
      const containerToValidate = new Container({ ...containerData, barcode: '99' });
      const validation = containerToValidate.validate();
      expect(validation.valid).toBeFalsy();
    });
  });
});
