Files
Battle-for-Tismo/tests/inflation_test.js
adrien 31683be27d Rolled the commodity index over ninety days and gave the market an equilibrium
The central bank now charts one point a day for a rolling ninety days, shipped rebased to 100, with alternating month columns named once along the bottom so no two dates collide; short games leave future days blank and the axis floor is pinned at 0 with the 100 baseline labelled. The market no longer clamps a price to 0.5-1.8x base: the day's supply and demand set an equilibrium (base * demand/supply) and the price eases toward it, so a sustained shortage lifts a good well past its old band while a glut cheapens it, and the price settles instead of compounding to infinity. The nation modal holds each scroll position across a snapshot rebuild so the foreign reserves below the fold stay put.
2026-09-23 18:54:07 +02:00

85 lines
3.9 KiB
JavaScript

import { TestCase } from "./framework/test_case.js";
import { smallState } from "./framework/helpers.js";
import { RESOURCE_IDS, RESOURCE_MARKET_BASE, PRICE_INDEX_WINDOW_DAYS } from "../shared/data.js";
export class InflationHistoryTest extends TestCase {
test_the_index_starts_at_one_hundred() {
const state = smallState();
for (const id of RESOURCE_IDS) {
const info = state.resourceInflation(id);
this.assertApprox(info.index, 100, 1e-9, `${id} index`);
this.assertApprox(info.monthly, 0, 1e-9, `${id} monthly`);
this.assertApprox(info.yearly, 0, 1e-9, `${id} yearly`);
}
}
test_the_daily_series_rolls_over_the_window() {
const state = smallState();
const base = RESOURCE_MARKET_BASE.steel;
// One sample a day for longer than the window, each 1% dearer than the last.
for (let day = 1; day <= PRICE_INDEX_WINDOW_DAYS + 5; day++) {
state.setResourcePrice("steel", base * (1 + 0.01 * day));
state._sampleDailyResourcePrices();
}
const series = state.resourceDailyPrices.get("steel");
this.assertSize(series, PRICE_INDEX_WINDOW_DAYS, "the daily series holds the window");
this.assertApprox(series[0], base * 1.06, 1e-6, "the oldest five days have rolled off");
this.assertApprox(series[series.length - 1], base * 1.95, 1e-6, "the newest day is today");
}
test_the_snapshot_ships_daily_indices_rebased_on_the_opening_price() {
const state = smallState();
const base = RESOURCE_MARKET_BASE.food;
state.setResourcePrice("food", base * 1.25);
state._sampleDailyResourcePrices();
const shared = state.serializeShared();
const food = shared.resourcePriceHistory.find(([id]) => id === "food");
this.assertSize(food[1], 2, "the opening price and today's point");
this.assertApprox(food[1][0], 100, 1e-6, "the series opens at the index base");
this.assertApprox(food[1][1], 125, 1e-6, "today's point carries the move");
const inflation = shared.resourceInflation.find((entry) => entry.id === "food");
this.assertApprox(inflation.index, 125, 1e-6, "the table's live index agrees");
}
test_monthly_inflation_compares_with_last_month() {
const state = smallState();
const base = RESOURCE_MARKET_BASE.steel;
state.setResourcePrice("steel", base * 1.1);
state._sampleResourcePrices();
const info = state.resourceInflation("steel");
this.assertApprox(info.index, 110, 1e-6, "the index tracks the price");
this.assertApprox(info.monthly, 0.1, 1e-9, "a tenth dearer than last month");
this.assertApprox(info.yearly, 0.1, 1e-9, "and since the start, through 2000");
}
test_yearly_inflation_uses_the_same_month_a_year_earlier() {
const state = smallState();
const base = RESOURCE_MARKET_BASE.food;
// Twelve monthly openings, each 1% dearer than the last.
for (let month = 1; month <= 12; month++) {
state.setResourcePrice("food", base * (1 + 0.01 * month));
state._sampleResourcePrices();
}
// Then the live price a further step up.
state.setResourcePrice("food", base * 1.13);
const info = state.resourceInflation("food");
// The year-ago sample is January 2000 (the base); last month's opening is
// the eleventh sample.
this.assertApprox(info.yearly, 0.13, 1e-9, "growth since the same month last year");
this.assertApprox(info.monthly, 1.13 / 1.11 - 1, 1e-9, "growth since last month");
}
test_the_snapshot_ships_the_series_and_inflation() {
const state = smallState();
const shared = state.serializeShared();
this.assertSize(shared.resourcePriceHistory, RESOURCE_IDS.length);
this.assertSize(shared.resourceInflation, RESOURCE_IDS.length);
for (const [id, prices] of shared.resourcePriceHistory) {
this.assertTrue(RESOURCE_IDS.includes(id));
this.assertSize(prices, 1, "one opening index at the start");
}
const steel = shared.resourceInflation.find((entry) => entry.id === "steel");
this.assertApprox(steel.index, 100, 1e-9);
}
}