[engine] Rose: Replay the care log into growth from days of care, drift, moods, needs and numbers
This commit is contained in:
@@ -1,2 +1,6 @@
|
|||||||
export { addDays, isLocalDay, localDay, type LocalDay } from './time/localDay.js';
|
export { addDays, daysBetween, isLocalDay, localDay, localMinutes, type LocalDay } from './time/localDay.js';
|
||||||
|
export {
|
||||||
|
careEvent, judgeCare, OWN_TRACE, petalsOn, pour, ROSE, roseAt, seeded, stageFor, weedsOn, windOn,
|
||||||
|
type Judged, type Mood, type Needs, type RoseNow, type Stage, type Trend
|
||||||
|
} from './rose/rose.js';
|
||||||
export { invitation, nextInvitation, sunDay, type NamedPlace, type Place, type SunDay } from './sun/sun.js';
|
export { invitation, nextInvitation, sunDay, type NamedPlace, type Place, type SunDay } from './sun/sun.js';
|
||||||
|
|||||||
@@ -0,0 +1,231 @@
|
|||||||
|
/**
|
||||||
|
* The rose (GAMEPLAY.md §1–2, .agents/features/rose.md). Pure: the rose at any
|
||||||
|
* moment is a replay of her care log. Time is `now`; the only "randomness" is a
|
||||||
|
* hash of her key and the day, so every replay on every device agrees.
|
||||||
|
*
|
||||||
|
* She grows from the number of local days on which care helped, never from a
|
||||||
|
* streak and never from minutes. She drifts when left, and drift never lowers
|
||||||
|
* what was grown.
|
||||||
|
*/
|
||||||
|
import type { CareEvent } from '@rosea/schema';
|
||||||
|
import { addDays, daysBetween, localDay, localMinutes, type LocalDay } from '../time/localDay.js';
|
||||||
|
|
||||||
|
/** Every tuning number in one place. Assumptions for the author: rose.md R2, R4, R5, R6. */
|
||||||
|
export const ROSE = {
|
||||||
|
/** A new rose's water: a little low, so the first pour helps and teaches. */
|
||||||
|
waterStart: 0.3,
|
||||||
|
waterFallPerDay: 0.3,
|
||||||
|
/** What a full can (amount 1) adds. */
|
||||||
|
fullPour: 0.6,
|
||||||
|
thirsty: 0.25,
|
||||||
|
/** Below this she needs water; at or above it her leaves lift. */
|
||||||
|
enough: 0.4,
|
||||||
|
tooFull: 0.95,
|
||||||
|
weedChance: 0.4,
|
||||||
|
petalChance: 0.3,
|
||||||
|
windChance: 0.25,
|
||||||
|
windEarliest: 9 * 60,
|
||||||
|
windLatest: 18 * 60,
|
||||||
|
windMinutes: 90,
|
||||||
|
/** Days of care to reach opening, half bloom, full bloom. */
|
||||||
|
stageDays: [3, 10, 25],
|
||||||
|
/** Days since the last day of care when she turns a little away, then closes a little. */
|
||||||
|
driftDays: [3, 7],
|
||||||
|
/** How long she blushes after a helpful act. */
|
||||||
|
blushMinutes: 20
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
const DAY_MS = 86_400_000;
|
||||||
|
|
||||||
|
export type Stage = 'bud' | 'opening' | 'half-bloom' | 'full-bloom';
|
||||||
|
export type Mood = 'content' | 'blushing' | 'too-full' | 'windblown' | 'waiting' | 'thirsty';
|
||||||
|
export type Trend = 'growing' | 'steady' | 'waiting';
|
||||||
|
|
||||||
|
export interface Needs {
|
||||||
|
readonly water: boolean;
|
||||||
|
readonly weeds: boolean;
|
||||||
|
readonly petals: boolean;
|
||||||
|
readonly wind: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RoseNow {
|
||||||
|
readonly stage: Stage;
|
||||||
|
/** 0: facing you; 1: turned a little away; 2: closed a little, waiting. Never lowers the stage. */
|
||||||
|
readonly drift: 0 | 1 | 2;
|
||||||
|
readonly mood: Mood;
|
||||||
|
/** Water level now: 0 is dry, from `ROSE.enough` her leaves lift, above `ROSE.tooFull` too full. */
|
||||||
|
readonly water: number;
|
||||||
|
readonly needs: Needs;
|
||||||
|
/** Local days on which care helped, in all. */
|
||||||
|
readonly daysOfCare: number;
|
||||||
|
/** Days of care in the last seven local days, today included. */
|
||||||
|
readonly warmth: number;
|
||||||
|
readonly trend: Trend;
|
||||||
|
readonly lastDayOfCare: LocalDay | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A number in [0, 1) from the rose's key, a day and a topic: the same on every replay. FNV-1a. */
|
||||||
|
export function seeded(seed: string, day: LocalDay, topic: string): number {
|
||||||
|
let h = 0x811c9dc5;
|
||||||
|
for (const ch of `${seed}|${day}|${topic}`) {
|
||||||
|
h ^= ch.codePointAt(0)!;
|
||||||
|
h = Math.imul(h, 0x01000193) >>> 0;
|
||||||
|
}
|
||||||
|
return h / 0x1_0000_0000;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const weedsOn = (seed: string, day: LocalDay): boolean => seeded(seed, day, 'weeds') < ROSE.weedChance;
|
||||||
|
export const petalsOn = (seed: string, day: LocalDay): boolean => seeded(seed, day, 'petals') < ROSE.petalChance;
|
||||||
|
|
||||||
|
/** The day's wind, as [first, last) minutes on the local clock, or null on a still day. */
|
||||||
|
export function windOn(seed: string, day: LocalDay): readonly [number, number] | null {
|
||||||
|
if (seeded(seed, day, 'wind') >= ROSE.windChance) return null;
|
||||||
|
const start = ROSE.windEarliest + Math.floor(seeded(seed, day, 'wind-start') * (ROSE.windLatest - ROSE.windEarliest));
|
||||||
|
return [start, start + ROSE.windMinutes];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stageFor(daysOfCare: number): Stage {
|
||||||
|
const [opening, half, full] = ROSE.stageDays;
|
||||||
|
return daysOfCare >= full ? 'full-bloom' : daysOfCare >= half ? 'half-bloom' : daysOfCare >= opening ? 'opening' : 'bud';
|
||||||
|
}
|
||||||
|
|
||||||
|
/** What a pour of `amount` (0–1) would do, for the can in the player's hand. */
|
||||||
|
export function pour(waterNow: number, amount: number): { readonly after: number; readonly leavesLift: boolean; readonly tooFull: boolean; readonly helps: boolean } {
|
||||||
|
const after = waterNow + Math.min(1, Math.max(0, amount)) * ROSE.fullPour;
|
||||||
|
const tooFull = after > ROSE.tooFull;
|
||||||
|
return { after, leavesLift: after >= ROSE.enough, tooFull, helps: waterNow < ROSE.enough && after >= ROSE.enough && !tooFull };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The petal a player's care leaves behind (CLAUDE.md §6.2, `trace`). Every player's
|
||||||
|
* is rose-coloured until friends can tell them apart (Phase 3).
|
||||||
|
*/
|
||||||
|
export const OWN_TRACE = 'petal:rose';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One act of care as the log keeps it. The day is computed here, by the one module
|
||||||
|
* that computes days, in the rose's zone (rose.md R8). `amount` only for water.
|
||||||
|
*/
|
||||||
|
export function careEvent(p: {
|
||||||
|
readonly now: number;
|
||||||
|
readonly timeZone: string;
|
||||||
|
readonly from: string;
|
||||||
|
readonly to: string;
|
||||||
|
readonly act: CareEvent['act'];
|
||||||
|
readonly amount?: number;
|
||||||
|
readonly clientVersion: string;
|
||||||
|
}): CareEvent {
|
||||||
|
const base = {
|
||||||
|
kind: 'care' as const, ts: Math.round(p.now), localDay: localDay(p.now, p.timeZone), from: p.from, to: p.to,
|
||||||
|
act: p.act, trace: OWN_TRACE, messageId: null, clientVersion: p.clientVersion
|
||||||
|
};
|
||||||
|
if (p.act !== 'water') return base;
|
||||||
|
// Two decimals are feel enough, and keep the log free of float noise.
|
||||||
|
return { ...base, amount: Math.round(Math.min(1, Math.max(0, p.amount ?? 0)) * 100) / 100 };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One act of the log, judged: did it meet a real need? */
|
||||||
|
export interface Judged {
|
||||||
|
readonly event: CareEvent;
|
||||||
|
readonly helped: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Replay {
|
||||||
|
readonly judged: readonly Judged[];
|
||||||
|
readonly days: ReadonlySet<LocalDay>;
|
||||||
|
readonly water: { readonly level: number; readonly at: number | null };
|
||||||
|
readonly met: ReadonlyMap<string, true>;
|
||||||
|
readonly firstDay: LocalDay | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fall = (level: number, from: number | null, to: number): number =>
|
||||||
|
from === null ? level : Math.max(0, level - (ROSE.waterFallPerDay * Math.max(0, to - from)) / DAY_MS);
|
||||||
|
|
||||||
|
/** The log in order, each act judged. Only care given to this rose counts. */
|
||||||
|
function replay(events: readonly CareEvent[], seed: string, timeZone: string): Replay {
|
||||||
|
const mine = events
|
||||||
|
.map((e, i) => ({ e, i }))
|
||||||
|
.filter(({ e }) => e.kind === 'care' && e.to === seed)
|
||||||
|
.sort((a, b) => a.e.ts - b.e.ts || a.i - b.i)
|
||||||
|
.map(({ e }) => e);
|
||||||
|
|
||||||
|
const days = new Set<LocalDay>();
|
||||||
|
const met = new Map<string, true>();
|
||||||
|
const judged: Judged[] = [];
|
||||||
|
let level: number = ROSE.waterStart;
|
||||||
|
let at: number | null = null;
|
||||||
|
|
||||||
|
for (const e of mine) {
|
||||||
|
const day = e.localDay; // the day recorded when it happened is the day that counts
|
||||||
|
let helped = false;
|
||||||
|
level = fall(level, at, e.ts);
|
||||||
|
at = e.ts;
|
||||||
|
switch (e.act) {
|
||||||
|
case 'water': {
|
||||||
|
const p = pour(level, e.amount ?? 0);
|
||||||
|
helped = p.helps;
|
||||||
|
level = p.after;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'weed':
|
||||||
|
helped = weedsOn(seed, day) && !met.has(`${day}|weeds`);
|
||||||
|
if (helped) met.set(`${day}|weeds`, true);
|
||||||
|
break;
|
||||||
|
case 'petals':
|
||||||
|
helped = stageFor(days.size) !== 'bud' && petalsOn(seed, day) && !met.has(`${day}|petals`);
|
||||||
|
if (helped) met.set(`${day}|petals`, true);
|
||||||
|
break;
|
||||||
|
case 'shade': {
|
||||||
|
const wind = windOn(seed, day);
|
||||||
|
const minute = localMinutes(e.ts, timeZone);
|
||||||
|
helped = wind !== null && minute >= wind[0] && minute < wind[1] && !met.has(`${day}|wind`);
|
||||||
|
if (helped) met.set(`${day}|wind`, true);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (helped) days.add(day);
|
||||||
|
judged.push({ event: e, helped });
|
||||||
|
}
|
||||||
|
return { judged, days, water: { level, at }, met, firstDay: mine[0]?.localDay ?? null };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Every act of the log with whether it helped, in order. */
|
||||||
|
export function judgeCare(events: readonly CareEvent[], seed: string, timeZone: string): readonly Judged[] {
|
||||||
|
return replay(events, seed, timeZone).judged;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The rose at `now`: a pure replay of `events` for the rose whose key is `seed`. */
|
||||||
|
export function roseAt(events: readonly CareEvent[], seed: string, now: number, timeZone: string): RoseNow {
|
||||||
|
const r = replay(events, seed, timeZone);
|
||||||
|
const today = localDay(now, timeZone);
|
||||||
|
const water = fall(r.water.level, r.water.at, now);
|
||||||
|
const stage = stageFor(r.days.size);
|
||||||
|
|
||||||
|
const wind = windOn(seed, today);
|
||||||
|
const minute = localMinutes(now, timeZone);
|
||||||
|
const needs: Needs = {
|
||||||
|
water: water < ROSE.enough,
|
||||||
|
weeds: weedsOn(seed, today) && !r.met.has(`${today}|weeds`),
|
||||||
|
petals: stage !== 'bud' && petalsOn(seed, today) && !r.met.has(`${today}|petals`),
|
||||||
|
wind: wind !== null && minute >= wind[0] && minute < wind[1] && !r.met.has(`${today}|wind`)
|
||||||
|
};
|
||||||
|
|
||||||
|
const careDays = [...r.days].filter((d) => d <= today).sort();
|
||||||
|
const lastDayOfCare = careDays.at(-1) ?? null;
|
||||||
|
const since = daysBetween(lastDayOfCare ?? r.firstDay ?? today, today);
|
||||||
|
const drift = since >= ROSE.driftDays[1] ? 2 : since >= ROSE.driftDays[0] ? 1 : 0;
|
||||||
|
const weekStart = addDays(today, -6);
|
||||||
|
const warmth = careDays.filter((d) => d >= weekStart).length;
|
||||||
|
const trend: Trend = drift > 0 ? 'waiting' : lastDayOfCare !== null && daysBetween(lastDayOfCare, today) <= 1 ? 'growing' : 'steady';
|
||||||
|
|
||||||
|
const lastHelp = [...r.judged].reverse().find((j) => j.helped && j.event.ts <= now);
|
||||||
|
const blushing = lastHelp !== undefined && now - lastHelp.event.ts < ROSE.blushMinutes * 60_000;
|
||||||
|
const mood: Mood = blushing ? 'blushing'
|
||||||
|
: water > ROSE.tooFull ? 'too-full'
|
||||||
|
: needs.wind ? 'windblown'
|
||||||
|
: drift > 0 ? 'waiting'
|
||||||
|
: water < ROSE.thirsty ? 'thirsty'
|
||||||
|
: 'content';
|
||||||
|
|
||||||
|
return { stage, drift, mood, water, needs, daysOfCare: r.days.size, warmth, trend, lastDayOfCare };
|
||||||
|
}
|
||||||
@@ -61,6 +61,31 @@ export function localDay(epochMs: number, timeZone: string): LocalDay {
|
|||||||
return day;
|
return day;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const clockFormatters = new Map<string, Intl.DateTimeFormat>();
|
||||||
|
|
||||||
|
/** Minutes after local midnight on the clock of `timeZone` (0–1439): what a clock there shows. */
|
||||||
|
export function localMinutes(epochMs: number, timeZone: string): number {
|
||||||
|
if (!Number.isFinite(epochMs)) throw new RangeError(`localMinutes: epochMs must be a finite number, got ${String(epochMs)}`);
|
||||||
|
let f = clockFormatters.get(timeZone);
|
||||||
|
if (f === undefined) {
|
||||||
|
f = new Intl.DateTimeFormat('en-GB', { timeZone, hour: '2-digit', minute: '2-digit', hourCycle: 'h23', numberingSystem: 'latn' });
|
||||||
|
clockFormatters.set(timeZone, f);
|
||||||
|
}
|
||||||
|
const parts = f.formatToParts(epochMs);
|
||||||
|
const n = (type: Intl.DateTimeFormatPartTypes): number => Number(parts.find((p) => p.type === type)?.value);
|
||||||
|
return n('hour') * 60 + n('minute');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Whole calendar days from `from` to `to`: positive when `to` is later. */
|
||||||
|
export function daysBetween(from: LocalDay, to: LocalDay): number {
|
||||||
|
const utc = (day: LocalDay): number => {
|
||||||
|
if (!isLocalDay(day)) throw new RangeError(`daysBetween: '${day}' is not a LocalDay`);
|
||||||
|
const [y, m, d] = day.split('-').map(Number) as [number, number, number];
|
||||||
|
return Date.UTC(y, m - 1, d);
|
||||||
|
};
|
||||||
|
return Math.round((utc(to) - utc(from)) / 86_400_000);
|
||||||
|
}
|
||||||
|
|
||||||
/** The calendar day `days` after `day` (before, if negative). A calendar step: no zone, no DST. */
|
/** The calendar day `days` after `day` (before, if negative). A calendar step: no zone, no DST. */
|
||||||
export function addDays(day: LocalDay, days: number): LocalDay {
|
export function addDays(day: LocalDay, days: number): LocalDay {
|
||||||
if (!isLocalDay(day)) throw new RangeError(`addDays: '${day}' is not a LocalDay`);
|
if (!isLocalDay(day)) throw new RangeError(`addDays: '${day}' is not a LocalDay`);
|
||||||
|
|||||||
@@ -0,0 +1,210 @@
|
|||||||
|
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 });
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user