Add RFM segmentation analytics
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 3m12s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 3m12s
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const test = require('node:test');
|
||||
|
||||
const { buildDateFilter, normalizeDateParam } = require('../services/analyticsService');
|
||||
const {
|
||||
buildRfmSegments,
|
||||
buildDateFilter,
|
||||
getRfmSegment,
|
||||
normalizeDateParam,
|
||||
scoreTertile
|
||||
} = require('../services/analyticsService');
|
||||
|
||||
test('normalizeDateParam accepts strict ISO dates', () => {
|
||||
assert.equal(normalizeDateParam('2026-05-28'), '2026-05-28');
|
||||
@@ -32,3 +38,42 @@ test('buildDateFilter ignores invalid bounds', () => {
|
||||
'WHERE data_pedido_date IS NOT NULL AND data_pedido_date <= $1::date'
|
||||
);
|
||||
});
|
||||
|
||||
test('scoreTertile scores higher values higher by default', () => {
|
||||
const values = [10, 20, 30, 40, 50];
|
||||
|
||||
assert.equal(scoreTertile(10, values), 1);
|
||||
assert.equal(scoreTertile(30, values), 2);
|
||||
assert.equal(scoreTertile(50, values), 3);
|
||||
});
|
||||
|
||||
test('scoreTertile can score lower values higher for recency', () => {
|
||||
const recencyDays = [2, 10, 20, 40, 80];
|
||||
|
||||
assert.equal(scoreTertile(2, recencyDays, false), 3);
|
||||
assert.equal(scoreTertile(20, recencyDays, false), 2);
|
||||
assert.equal(scoreTertile(80, recencyDays, false), 1);
|
||||
});
|
||||
|
||||
test('getRfmSegment maps the 3x3 RFM matrix', () => {
|
||||
assert.deepEqual(getRfmSegment(3, 3), { key: 'champions', label: 'Champions' });
|
||||
assert.deepEqual(getRfmSegment(2, 2), { key: 'need_attention', label: 'Precisam de Atenção' });
|
||||
assert.deepEqual(getRfmSegment(1, 1), { key: 'lost', label: 'Perdidos' });
|
||||
});
|
||||
|
||||
test('buildRfmSegments summarizes segment count and revenue', () => {
|
||||
const segments = buildRfmSegments([
|
||||
{ segmentKey: 'champions', monetary: 100 },
|
||||
{ segmentKey: 'champions', monetary: 50 },
|
||||
{ segmentKey: 'lost', monetary: 25 }
|
||||
]);
|
||||
|
||||
const champions = segments.find(segment => segment.key === 'champions');
|
||||
const lost = segments.find(segment => segment.key === 'lost');
|
||||
|
||||
assert.equal(champions.count, 2);
|
||||
assert.equal(champions.totalRevenue, 150);
|
||||
assert.equal(champions.averageRevenue, 75);
|
||||
assert.equal(lost.count, 1);
|
||||
assert.equal(lost.totalRevenue, 25);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user