feat(pure): computeEstimate with safe division and target-reached handling
This commit is contained in:
+36
-1
@@ -1,6 +1,6 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { parseTarget } from '../src/pure.js';
|
||||
import { parseTarget, computeEstimate } from '../src/pure.js';
|
||||
|
||||
test('parseTarget: integer numbers', () => {
|
||||
assert.equal(parseTarget(25), 25);
|
||||
@@ -32,3 +32,38 @@ test('parseTarget: rejects invalid input', () => {
|
||||
assert.equal(parseTarget('1.5.5M'), null);
|
||||
assert.equal(parseTarget('0'), null);
|
||||
});
|
||||
|
||||
test('computeEstimate: typical case', () => {
|
||||
const r = computeEstimate(14_328_501, 25_000_000, 247, 4520);
|
||||
assert.equal(r.remaining, 10_671_499);
|
||||
assert.equal(r.trainsToGo, 43_205);
|
||||
assert.equal(r.days, 2_361);
|
||||
assert.ok(r.eta instanceof Date);
|
||||
});
|
||||
|
||||
test('computeEstimate: target reached', () => {
|
||||
const r = computeEstimate(25_000_000, 25_000_000, 247, 4520);
|
||||
assert.equal(r.remaining, 0);
|
||||
assert.equal(r.trainsToGo, 0);
|
||||
assert.equal(r.days, 0);
|
||||
assert.equal(r.eta, null);
|
||||
});
|
||||
|
||||
test('computeEstimate: target below current', () => {
|
||||
const r = computeEstimate(30_000_000, 25_000_000, 247, 4520);
|
||||
assert.equal(r.remaining, 0);
|
||||
assert.equal(r.trainsToGo, 0);
|
||||
assert.equal(r.days, 0);
|
||||
assert.equal(r.eta, null);
|
||||
});
|
||||
|
||||
test('computeEstimate: zero perTrain or perDay does not crash', () => {
|
||||
const a = computeEstimate(100, 200, 0, 50);
|
||||
assert.equal(a.trainsToGo, 0);
|
||||
assert.equal(a.days, 2);
|
||||
|
||||
const b = computeEstimate(100, 200, 50, 0);
|
||||
assert.equal(b.trainsToGo, 2);
|
||||
assert.equal(b.days, 0);
|
||||
assert.equal(b.eta, null);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user