'use strict'

const convert = require('../lib')
const { test } = require('tap')

const ACCURACY = 1 / 1000
const percentError = require('../lib/percentError')

test('l/s to l/s', (t) => {
  t.plan(1)
  t.equals(convert(2).from('l/s').to('l/s'), 2)
})

test('mm3/s to l/s', (t) => {
  t.plan(1)
  t.equals(convert(1000000).from('mm3/s').to('l/s'), 1)
})

test('cm3/s to l/s', (t) => {
  t.plan(1)
  t.equals(convert(100).from('cm3/s').to('l/s'), 1 / 10)
})

test('dl/s to l/s', (t) => {
  t.plan(1)
  t.equals(convert(2).from('dl/s').to('l/s'), 0.2)
})

test('cl/s to l/s', (t) => {
  t.plan(1)
  t.equals(convert(25).from('cl/s').to('l/s'), 0.25)
})

test('ml/s to l/s', (t) => {
  t.plan(1)
  t.equals(convert(100).from('ml/s').to('l/s'), 1 / 10)
})

test('m3/s to l/s', (t) => {
  t.plan(1)
  t.equals(convert(1).from('m3/s').to('l/s'), 1000)
})

test('km3/s to l/s', (t) => {
  t.plan(1)
  t.equals(convert(1).from('km3/s').to('l/s'), 1000000000000)
})

test('l/s to ml/s', (t) => {
  t.plan(1)
  t.equals(convert(1).from('l/s').to('ml/s'), 1000)
})

test('dl/s to ml/s', (t) => {
  t.plan(1)
  t.equals(convert(10).from('dl/s').to('ml/s'), 1000)
})

test('cl/s to ml/s', (t) => {
  t.plan(1)
  t.equals(convert(100).from('cl/s').to('ml/s'), 1000)
})

test('ml/s to ml/s', (t) => {
  t.plan(1)
  t.equals(convert(13).from('ml/s').to('ml/s'), 13)
})

test('fl-oz/s to fl-oz/s', (t) => {
  t.plan(1)
  t.equals(convert(62).from('fl-oz/s').to('fl-oz/s'), 62)
})

test('fl-oz/s to Tbs/s', (t) => {
  t.plan(1)
  t.equals(convert(4).from('fl-oz/s').to('Tbs/s'), 8)
})

test('Tbs/s to fl-oz/s', (t) => {
  t.plan(1)
  t.equals(convert(2).from('Tbs/s').to('fl-oz/s'), 1)
})

test('Tbs/s to Tbs/s', (t) => {
  t.plan(1)
  t.equals(convert(140).from('Tbs/s').to('Tbs/s'), 140)
})

test('l/s to l/min', (t) => {
  t.plan(1)
  t.equals(convert(1).from('l/s').to('l/min'), 60)
})

test('l/s to l/h', (t) => {
  t.plan(1)
  t.equals(convert(1).from('l/s').to('l/h'), 3600)
})

test('kl/s to kl/h', (t) => {
  t.plan(1)
  t.equals(convert(1).from('kl/s').to('kl/h'), 3600)
})

test('l/s to kl/h', (t) => {
  t.plan(1)

  const expected = 3.6
  const actual = convert(1).from('l/s').to('kl/h')
  t.ok(percentError(expected, actual) < ACCURACY, 'Expected: ' + expected + ', Actual: ' + actual)
})

test('l/s to m3/s', (t) => {
  t.plan(1)
  t.equals(convert(1).from('l/s').to('m3/s'), 0.001)
})

test('m3/s to m3/h', (t) => {
  t.plan(1)
  t.equals(convert(1).from('kl/s').to('kl/h'), 3600)
})

// When converting between systems, expect < 0.1% error
test('tsp/s to l/s', (t) => {
  t.plan(1)
  const expected = 1.75
  const actual = convert(355).from('tsp/s').to('l/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('in3/s to l/s', (t) => {
  t.plan(1)
  const expected = 0.0163871
  const actual = convert(1).from('in3/s').to('l/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('in3/s to fl-oz/s', (t) => {
  t.plan(1)
  const expected = 0.554113
  const actual = convert(1).from('in3/s').to('fl-oz/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('m3/s to yd3/s', (t) => {
  t.plan(1)
  const expected = 1.30795
  const actual = convert(1).from('m3/s').to('yd3/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('ft3/s to cm3/s', (t) => {
  t.plan(1)
  const expected = 28316.8
  const actual = convert(1).from('ft3/s').to('cm3/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('pnt/s to ml/s', (t) => {
  t.plan(1)
  const expected = 2366
  const actual = convert(5).from('pnt/s').to('ml/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('ml/s to gal/s', (t) => {
  t.plan(1)
  const expected = 2.609
  const actual = convert(9876).from('ml/s').to('gal/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('gal/s to l/s', (t) => {
  t.plan(1)
  const expected = 3.78541178
  const actual = convert(1).from('gal/s').to('l/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('kl/s to kl/min', (t) => {
  t.plan(1)
  const expected = 60
  const actual = convert(1).from('kl/s').to('kl/min')
  t.ok(percentError(expected, actual) < ACCURACY, 'Expected: ' + expected + ', Actual: ' + actual)
})

test('l/s to kl/h', (t) => {
  t.plan(1)
  const expected = 3.6
  const actual = convert(1).from('l/s').to('kl/h')
  t.ok(percentError(expected, actual) < ACCURACY, 'Expected: ' + expected + ', Actual: ' + actual)
})

test('kl/min to l/h', (t) => {
  t.plan(1)
  const expected = 60000
  const actual = convert(1).from('kl/min').to('l/h')
  t.ok(percentError(expected, actual) < ACCURACY, 'Expected: ' + expected + ', Actual: ' + actual)
})

test('l/s to m3/h', (t) => {
  t.plan(1)
  const expected = 3.6
  const actual = convert(1).from('l/s').to('m3/h')
  t.ok(percentError(expected, actual) < ACCURACY, 'Expected: ' + expected + ', Actual: ' + actual)
})

test('m3/s to kl/h', (t) => {
  t.plan(1)
  const expected = 3600
  const actual = convert(1).from('m3/s').to('kl/h')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('fl-oz/s to m3/min', (t) => {
  t.plan(1)
  const expected = 0.00177441177
  const actual = convert(1).from('fl-oz/s').to('m3/min')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('ft3/min to l/s', (t) => {
  t.plan(1)
  const expected = 0.471947443
  const actual = convert(1).from('ft3/min').to('l/s')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('pnt/min to kl/h', (t) => {
  t.plan(1)
  const expected = 0.0283905884
  const actual = convert(1).from('pnt/min').to('kl/h')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})

test('yd3/h to m3/min', (t) => {
  t.plan(1)
  const expected = 0.012742581
  const actual = convert(1).from('yd3/h').to('m3/min')
  t.ok(percentError(expected, actual) < ACCURACY
    , 'Expected: ' + expected + ', Actual: ' + actual)
})
