211 lines
11 KiB
TypeScript
211 lines
11 KiB
TypeScript
import fc from 'fast-check';
|
|
import type { CareEvent } from '@rosea/schema';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { careEvent, judgeCare, OWN_TRACE, petalsOn, pour, ROSE, roseAt, stageFor, weedsOn, windOn, type Stage } from '../src/rose/rose.js';
|
|
import { addDays, localDay } from '../src/time/localDay.js';
|
|
|
|
const ZONE = 'Europe/Prague';
|
|
const ME = 'M'.repeat(43);
|
|
const SOMEONE = 'S'.repeat(43);
|
|
const HOUR = 3_600_000;
|
|
const DAY = 24 * HOUR;
|
|
/** 08:00 in Prague on 1 March 2026, before any wind can start. */
|
|
const START = Date.parse('2026-03-01T08:00:00+01:00');
|
|
|
|
function care(ts: number, act: CareEvent['act'], amount?: number, to = ME): CareEvent {
|
|
return {
|
|
kind: 'care', ts, localDay: localDay(ts, ZONE), from: ME, to, act,
|
|
trace: 'petal:pale-pink', messageId: null, clientVersion: '0.1.0',
|
|
...(act === 'water' ? { amount: amount ?? 0.5 } : {})
|
|
};
|
|
}
|
|
const at = (events: CareEvent[], now: number) => roseAt(events, ME, now, ZONE);
|
|
/** Water her helpfully once a day for `n` days, 08:00 each morning. */
|
|
const dailyWater = (n: number, from = START): CareEvent[] => Array.from({ length: n }, (_, i) => care(from + i * DAY, 'water', 0.5));
|
|
const RANK: Record<Stage, number> = { bud: 0, opening: 1, 'half-bloom': 2, 'full-bloom': 3 };
|
|
|
|
/** The first day from START on which `has` is true for this rose. */
|
|
function firstDayWith(has: (day: string) => boolean): number {
|
|
for (let i = 0; i < 400; i++) if (has(localDay(START + i * DAY, ZONE))) return START + i * DAY;
|
|
throw new Error('no such day in 400');
|
|
}
|
|
|
|
describe('a new rose', () => {
|
|
it('is a bud, a little low on water so the first pour helps, and not drifting', () => {
|
|
const r = at([], START);
|
|
expect(r).toMatchObject({ stage: 'bud', drift: 0, daysOfCare: 0, warmth: 0, lastDayOfCare: null });
|
|
expect(r.water).toBe(ROSE.waterStart);
|
|
expect(r.needs.water).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('water by feel', () => {
|
|
it('the can tells when the leaves lift, and when it is too much', () => {
|
|
expect(pour(0.3, 0.1)).toMatchObject({ leavesLift: false, helps: false });
|
|
expect(pour(0.3, 0.5)).toMatchObject({ leavesLift: true, tooFull: false, helps: true });
|
|
expect(pour(0.3, 1)).toMatchObject({ leavesLift: true, tooFull: false, helps: true });
|
|
expect(pour(0.5, 1)).toMatchObject({ tooFull: true, helps: false });
|
|
expect(pour(0.5, 0.1).helps, 'she did not need it').toBe(false);
|
|
// She needed it, but a full can from just below "enough" overshoots: too much helps no more than too little.
|
|
expect(pour(0.39, 1)).toMatchObject({ leavesLift: true, tooFull: true, helps: false });
|
|
});
|
|
|
|
it('a pour that meets her need is a day of care, and she blushes for a while', () => {
|
|
const r = at([care(START, 'water', 0.5)], START + 60_000);
|
|
expect(r).toMatchObject({ daysOfCare: 1, mood: 'blushing', trend: 'growing' });
|
|
expect(r.needs.water).toBe(false);
|
|
expect(at([care(START, 'water', 0.5)], START + 2 * HOUR).mood).toBe('content');
|
|
});
|
|
|
|
it('too much is as bad as too little: no day of care, and she is too full', () => {
|
|
const r = at([care(START, 'water', 1), care(START + 60_000, 'water', 1)], START + 2 * HOUR);
|
|
expect(r.daysOfCare).toBe(1); // the first pour helped; the second only overfilled her
|
|
expect(r.mood).toBe('too-full');
|
|
expect(judgeCare([care(START, 'water', 1), care(START + 60_000, 'water', 1)], ME, ZONE).map((j) => j.helped)).toEqual([true, false]);
|
|
});
|
|
|
|
it('water falls by ROSE.waterFallPerDay a day, never below zero', () => {
|
|
const events = [care(START, 'water', 0.5)];
|
|
expect(at(events, START + DAY).water).toBeCloseTo(0.6 - ROSE.waterFallPerDay, 6);
|
|
expect(at(events, START + 30 * DAY).water).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('weeds, petals, wind', () => {
|
|
it('pulling weeds helps on a day with weeds, once; on a day without, it does not', () => {
|
|
const weedy = firstDayWith((d) => weedsOn(ME, d));
|
|
const clear = firstDayWith((d) => !weedsOn(ME, d));
|
|
expect(judgeCare([care(weedy, 'weed'), care(weedy + 60_000, 'weed')], ME, ZONE).map((j) => j.helped)).toEqual([true, false]);
|
|
expect(judgeCare([care(clear, 'weed')], ME, ZONE)[0]?.helped).toBe(false);
|
|
expect(at([], weedy).needs.weeds).toBe(true);
|
|
expect(at([care(weedy, 'weed')], weedy + 60_000).needs.weeds).toBe(false);
|
|
});
|
|
|
|
it('shade helps only while the wind blows', () => {
|
|
const windy = firstDayWith((d) => windOn(ME, d) !== null);
|
|
const [from] = windOn(ME, localDay(windy, ZONE))!;
|
|
const midnight = windy - 8 * HOUR; // windy is 08:00 local
|
|
const during = midnight + (from + 10) * 60_000;
|
|
const before = midnight + (from - 30) * 60_000;
|
|
expect(at([], during).needs.wind).toBe(true);
|
|
expect(at([], during).mood).toBe('windblown');
|
|
expect(judgeCare([care(before, 'shade')], ME, ZONE)[0]?.helped).toBe(false);
|
|
expect(judgeCare([care(during, 'shade')], ME, ZONE)[0]?.helped).toBe(true);
|
|
expect(at([care(during, 'shade')], during + 60_000).needs.wind).toBe(false);
|
|
});
|
|
|
|
it('a bud drops no petals; from the opening stage, petals can be gathered', () => {
|
|
const grown = dailyWater(ROSE.stageDays[0]);
|
|
const after = START + ROSE.stageDays[0] * DAY;
|
|
const petalDay = firstDayWith((d) => petalsOn(ME, d) && d >= localDay(after, ZONE));
|
|
expect(at([], petalDay).needs.petals, 'a bud').toBe(false);
|
|
expect(at(grown, petalDay).stage).toBe('opening');
|
|
expect(at(grown, petalDay).needs.petals).toBe(true);
|
|
});
|
|
|
|
it('come at about their rates, and differ between two roses', () => {
|
|
const days = Array.from({ length: 2000 }, (_, i) => addDays('2026-01-01', i));
|
|
const rate = (f: (d: string) => boolean): number => days.filter(f).length / days.length;
|
|
expect(Math.abs(rate((d) => weedsOn(ME, d)) - ROSE.weedChance)).toBeLessThan(0.04);
|
|
expect(Math.abs(rate((d) => petalsOn(ME, d)) - ROSE.petalChance)).toBeLessThan(0.04);
|
|
expect(Math.abs(rate((d) => windOn(ME, d) !== null) - ROSE.windChance)).toBeLessThan(0.04);
|
|
expect(days.filter((d) => weedsOn(ME, d) !== weedsOn(SOMEONE, d)).length).toBeGreaterThan(500);
|
|
for (const d of days) {
|
|
const w = windOn(ME, d);
|
|
if (w) expect(w[0] >= ROSE.windEarliest && w[0] < ROSE.windLatest && w[1] - w[0] === ROSE.windMinutes).toBe(true);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('growth and drift', () => {
|
|
it('stages come from days of care: bud, opening, half bloom, full bloom', () => {
|
|
const [opening, half, full] = ROSE.stageDays;
|
|
expect([0, opening - 1, opening, half, full, 400].map(stageFor)).toEqual(['bud', 'bud', 'opening', 'half-bloom', 'full-bloom', 'full-bloom']);
|
|
expect(at(dailyWater(full), START + full * DAY).stage).toBe('full-bloom');
|
|
});
|
|
|
|
it('drifts a little after 3 days, more after 7, never losing her stage; one helpful touch brings her back', () => {
|
|
const events = dailyWater(12);
|
|
const last = START + 11 * DAY;
|
|
const r = (d: number) => at(events, last + d * DAY);
|
|
expect([0, 2, 3, 6, 7, 60].map((d) => r(d).drift)).toEqual([0, 0, 1, 1, 2, 2]);
|
|
expect(r(60).stage).toBe(r(0).stage);
|
|
expect(r(60).daysOfCare).toBe(12);
|
|
expect(r(60).mood).toBe('waiting');
|
|
expect(r(60).trend).toBe('waiting');
|
|
// Dry after two months, she needs a fuller pour before her leaves lift.
|
|
const back = [...events, care(last + 60 * DAY, 'water', 1)];
|
|
expect(at(back, last + 60 * DAY + HOUR).drift).toBe(0);
|
|
});
|
|
|
|
it('warmth is days of care in the last seven, and says nothing of the rest', () => {
|
|
const events = dailyWater(10);
|
|
expect(at(events, START + 9 * DAY).warmth).toBe(7);
|
|
expect(at(events, START + 12 * DAY).warmth).toBe(4);
|
|
expect(at(events, START + 40 * DAY).warmth).toBe(0);
|
|
});
|
|
|
|
it('the log is replayed in time order, however it was stored', () => {
|
|
const [first, second] = [care(START, 'water', 0.5), care(START + DAY, 'water', 0.5)];
|
|
expect(at([first, second], START + 2 * DAY).daysOfCare).toBe(2);
|
|
expect(at([second, first], START + 2 * DAY).daysOfCare).toBe(2);
|
|
});
|
|
|
|
it('only care given to this rose counts', () => {
|
|
expect(at([care(START, 'water', 0.5, SOMEONE)], START + HOUR).daysOfCare).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('properties (CLAUDE.md §5.5)', () => {
|
|
const act = fc.constantFrom<CareEvent['act']>('water', 'shade', 'weed', 'petals');
|
|
/** A log over two months: acts at random moments, in time order, as a real log is. */
|
|
const log = fc.array(fc.tuple(fc.integer({ min: 0, max: 60 * 24 * 60 - 1 }), act, fc.double({ min: 0, max: 1, noNaN: true })), { maxLength: 80 })
|
|
.map((xs) => xs.map(([minute, a, amount]) => care(START + minute * 60_000, a, amount)).sort((x, y) => x.ts - y.ts));
|
|
const END = START + 70 * DAY;
|
|
|
|
it('growth never decreases as the log grows', () => {
|
|
fc.assert(fc.property(log, (events) => {
|
|
let previous = -1;
|
|
for (let k = 0; k <= events.length; k++) {
|
|
const r = at(events.slice(0, k), END);
|
|
if (r.daysOfCare < previous) return false;
|
|
previous = r.daysOfCare;
|
|
}
|
|
return true;
|
|
}), { numRuns: 60 });
|
|
});
|
|
|
|
it('a gap never lowers the stage or the days of care', () => {
|
|
fc.assert(fc.property(log, fc.integer({ min: 1, max: 3650 }), (events, gap) => {
|
|
const a = at(events, END);
|
|
const b = at(events, END + gap * DAY);
|
|
return RANK[b.stage] === RANK[a.stage] && b.daysOfCare === a.daysOfCare;
|
|
}), { numRuns: 100 });
|
|
});
|
|
|
|
it('a long session is worth no more than a short one: days of care never exceed the days with care', () => {
|
|
fc.assert(fc.property(log, (events) => at(events, END).daysOfCare <= new Set(events.map((e) => e.localDay)).size), { numRuns: 200 });
|
|
});
|
|
|
|
it('replaying the same log twice, in any order it was stored, gives identical state', () => {
|
|
fc.assert(fc.property(log, fc.integer(), (events, seed) => {
|
|
const shuffled = [...events].sort((a, b) => ((a.ts * 31 + seed) % 7) - ((b.ts * 31 + seed) % 7));
|
|
return JSON.stringify(at(events, END)) === JSON.stringify(at(events, END)) && JSON.stringify(at(shuffled, END)) === JSON.stringify(at(events, END));
|
|
}), { numRuns: 100 });
|
|
});
|
|
});
|
|
|
|
describe('careEvent — one act as the log keeps it', () => {
|
|
it('is the schema\'s shape, with the day in the rose\'s zone and the amount only for water', async () => {
|
|
const { validateCareEvent } = await import('@rosea/schema/validate');
|
|
// 00:30 in Prague on 31 March is the 31st there, though UTC still says the 30th.
|
|
const late = Date.parse('2026-03-31T00:30:00+02:00');
|
|
const w = careEvent({ now: late, timeZone: ZONE, from: ME, to: ME, act: 'water', amount: 0.4567, clientVersion: '0.1.0' });
|
|
expect(w).toMatchObject({ localDay: '2026-03-31', amount: 0.46, trace: OWN_TRACE, messageId: null });
|
|
expect(validateCareEvent(w)).toMatchObject({ ok: true });
|
|
const s = careEvent({ now: late, timeZone: ZONE, from: ME, to: ME, act: 'shade', amount: 0.5, clientVersion: '0.1.0' });
|
|
expect('amount' in s).toBe(false);
|
|
expect(validateCareEvent(s)).toMatchObject({ ok: true });
|
|
});
|
|
});
|