Add Elo, pair selection and comparison metrics with tests

Ratings are replayed from an event log (undo = skip the target event). K decays
48 -> 16 over a school's first 15 games. Smart pairing favours rarely compared
schools, then close ratings, with 10% exploration and random sides. metrics.js
derives grant, out-of-pocket cost, language and subject fit and the duel rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gXkMmmRRKLSnY6vQN3
This commit is contained in:
agent 2026-11-12 20:55:35 +00:00
parent 783c4d2f9a
commit 226e5f46f8
6 changed files with 672 additions and 0 deletions

91
test/metrics.test.js Normal file
View file

@ -0,0 +1,91 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
DEFAULT_SETTINGS, languageFit, subjectFit, monthlyGrant, netCost, money, moneyRange, rowWinner, COMPARE_ROWS,
} from '../public/js/metrics.js';
const settings = { ...DEFAULT_SETTINGS };
const ag = (isced, languages = []) => ({ isced, languages, places: 2, scope: 'FEL', levels: ['N'] });
const L = (lang, level) => ({ lang, level });
test('language fit: one satisfied language is enough', () => {
const s = { agreements: [ag(['0714'], [L('de', 'B2'), L('en', 'B2')])] };
assert.equal(languageFit(s, settings.languages).status, 'ok');
});
test('language fit: German-only fails, Slovak is covered by Czech', () => {
assert.equal(languageFit({ agreements: [ag(['0533'], [L('de', 'B1')])] }, settings.languages).status, 'fail');
assert.equal(languageFit({ agreements: [ag(['0610'], [L('sk', 'B1')])] }, settings.languages).status, 'ok');
});
test('language fit: Spanish B1 fails with A2, unlisted is unknown, best agreement wins', () => {
assert.equal(languageFit({ agreements: [ag([], [L('es', 'B1')])] }, settings.languages).status, 'fail');
assert.equal(languageFit({ agreements: [ag([])] }, settings.languages).status, 'unknown');
const mixed = { agreements: [ag([], [L('de', 'B2')]), ag([])] };
assert.equal(languageFit(mixed, settings.languages).status, 'unknown');
});
test('subject fit for an Open Informatics AI student', () => {
assert.equal(subjectFit({ agreements: [ag(['0713', '0610'])] }).level, 'best');
assert.equal(subjectFit({ agreements: [ag(['0714'])] }).level, 'good');
assert.equal(subjectFit({ agreements: [ag([])] }).level, 'good', 'no codes = unrestricted');
assert.equal(subjectFit({ agreements: [ag(['0533'])] }).level, 'weak');
assert.equal(subjectFit({ agreements: [ag(['0730']), ag(['0000'])] }).level, 'good');
});
const school = (over = {}) => ({
code: 'X', country: 'Austria', agreements: [ag(['0610'])],
funding: { scheme: 'erasmus', group: 1, monthlyEUR: 660, label: 'Erasmus+ group 1' },
research: {
living: { totalEUR: [800, 1000] },
travel: { mode: 'train', roundTripEUR: [100, 200] },
fees: { mandatoryEUR: 25, hostFunding: { monthlyEUR: 100 } },
},
...over,
});
const data = { fx: { EURCZK: 25 }, funding: { erasmus: { fewerOpportunitiesTopUpEUR: 250 } }, countries: {} };
test('grant with optional top-ups', () => {
assert.equal(monthlyGrant(school(), settings, data).eur, 660);
assert.equal(monthlyGrant(school(), { ...settings, fewerOpportunities: true }, data).eur, 910);
assert.equal(monthlyGrant(school(), { ...settings, countHostFunding: true }, data).eur, 760);
assert.equal(monthlyGrant(school({ funding: null }), settings, data).eur, null);
});
test('net cost = living*months + travel + visa + fees - grant', () => {
const n = netCost(school(), settings, data);
// lo: 800*5 + 100 + 0 + 25 - 3300 = 825 ; hi: 1000*5 + 200 + 25 - 3300 = 1925
assert.deepEqual([n.lo, n.hi], [825, 1925]);
const withVisa = netCost(school({ country: 'Japan' }), settings,
{ ...data, countries: { Japan: { visa: { required: true, costEUR: 50 } } } });
assert.deepEqual([withVisa.lo, withVisa.hi], [875, 1975]);
const swap = netCost(school({ funding: { scheme: 'ctu', monthlyEUR: 616, extraPerSemesterEUR: 2260 } }), settings, data);
assert.equal(swap.lo, 800 * 5 + 100 + 25 + 2260 - 616 * 5);
assert.equal(netCost(school({ research: { living: null } }), settings, data), null);
});
test('money formatting in EUR and CZK', () => {
assert.equal(money(1234, settings, data), '€1,234');
assert.equal(money(1234, settings, data, { compact: true }), '€1.2k');
assert.equal(money(-300, settings, data), '−€300');
assert.equal(money(100, { ...settings, currency: 'CZK' }, data), '2,500 Kč');
assert.equal(moneyRange([750, 1050], settings, data), '€750–1,050');
assert.equal(moneyRange([750, 1050], { ...settings, currency: 'CZK' }, data, { compact: true }), '19k–26k Kč');
assert.equal(money(null, settings, data), '–');
});
test('row winner respects direction and unknowns', () => {
const high = { better: 'high' };
const low = { better: 'low' };
assert.equal(rowWinner(high, { v: 7 }, { v: 5 }), 'a');
assert.equal(rowWinner(low, { v: 7 }, { v: 5 }), 'b');
assert.equal(rowWinner(high, { v: 5 }, { v: 5 }), null);
assert.equal(rowWinner(high, null, { v: 5 }), null);
});
test('every comparison row tolerates a school without research', () => {
const bare = school({ research: null });
for (const row of COMPARE_ROWS) {
assert.doesNotThrow(() => row.get(bare, { settings, data }), row.id);
}
});