72 lines
3.0 KiB
TypeScript
72 lines
3.0 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
METRIC_CATALOG,
|
|
isMetricDomain,
|
|
listMetricDefinitions,
|
|
} from './catalog.js';
|
|
|
|
test('metric ids are unique and namespaced by domain', () => {
|
|
const ids = METRIC_CATALOG.map(metric => metric.id);
|
|
|
|
assert.equal(new Set(ids).size, ids.length);
|
|
assert.ok(METRIC_CATALOG.every(metric => metric.id.startsWith(`${metric.domain}.`)));
|
|
});
|
|
|
|
test('lists mileage metrics without exposing the catalog array', () => {
|
|
const metrics = listMetricDefinitions('mileage');
|
|
|
|
assert.ok(metrics.length >= 5);
|
|
assert.notEqual(metrics, METRIC_CATALOG);
|
|
assert.ok(metrics.every(metric => metric.domain === 'mileage'));
|
|
assert.equal(
|
|
metrics.find(metric => metric.id === 'mileage.cumulative_odometer_sum')?.timeSemantics,
|
|
'snapshot',
|
|
);
|
|
assert.equal(
|
|
metrics.find(metric => metric.id === 'mileage.average_per_vehicle')?.formula,
|
|
'mileage.period_total / NULLIF(mileage.vehicle_count, 0)',
|
|
);
|
|
});
|
|
|
|
test('publishes hydrogen cost, revenue, and gross profit as separate metrics', () => {
|
|
const metrics = listMetricDefinitions('hydrogen');
|
|
const byId = new Map(metrics.map(metric => [metric.id, metric]));
|
|
|
|
assert.ok(metrics.length >= 8);
|
|
assert.equal(byId.get('hydrogen.refuel_cost')?.formula, "SUM(cost_total) WHERE del_flag = '0'");
|
|
assert.equal(byId.get('hydrogen.customer_revenue')?.formula, "SUM(fee_total) WHERE del_flag = '0'");
|
|
assert.equal(byId.get('hydrogen.customer_order_gross_profit')?.aggregation, 'derived');
|
|
});
|
|
|
|
test('publishes electric fee components, blended cost, and freshness semantics', () => {
|
|
const metrics = listMetricDefinitions('electric');
|
|
const byId = new Map(metrics.map(metric => [metric.id, metric]));
|
|
|
|
assert.ok(metrics.length >= 7);
|
|
assert.equal(byId.get('electric.charge_total_fee')?.formula, 'SUM(fee)');
|
|
assert.equal(byId.get('electric.electricity_fee')?.formula, 'SUM(e_fee)');
|
|
assert.equal(byId.get('electric.service_fee')?.formula, 'SUM(service_fee)');
|
|
assert.equal(byId.get('electric.blended_cost_intensity')?.aggregation, 'weighted-ratio');
|
|
assert.equal(byId.get('electric.data_freshness')?.timeSemantics, 'freshness');
|
|
});
|
|
|
|
test('publishes ETC passage and billing metrics as separate grains', () => {
|
|
const metrics = listMetricDefinitions('etc');
|
|
const byId = new Map(metrics.map(metric => [metric.id, metric]));
|
|
|
|
assert.ok(metrics.length >= 9);
|
|
assert.equal(byId.get('etc.total_amount')?.sources[0], 'etc_toll_record');
|
|
assert.equal(byId.get('etc.bill_receivable')?.sources[0], 'energy_etc_bill');
|
|
assert.equal(byId.get('etc.company_borne_amount')?.formula.includes('cost_type = 2'), true);
|
|
assert.equal(byId.get('etc.customer_borne_amount')?.formula.includes('cost_type = 1'), true);
|
|
});
|
|
|
|
test('validates only published metric domains', () => {
|
|
assert.equal(isMetricDomain('mileage'), true);
|
|
assert.equal(isMetricDomain('hydrogen'), true);
|
|
assert.equal(isMetricDomain('electric'), true);
|
|
assert.equal(isMetricDomain('etc'), true);
|
|
assert.equal(isMetricDomain('unknown'), false);
|
|
});
|