[engine] Rose: Name a rose by her current and past keys, replay each key on its own, and add each act once

This commit is contained in:
2026-11-02 00:44:16 +01:00
parent a7d0fca31a
commit cc9cc11b0b
3 changed files with 112 additions and 17 deletions
+48 -1
View File
@@ -1,7 +1,7 @@
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 { careEvent, judgeCare, newCare, 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';
@@ -208,3 +208,50 @@ describe('careEvent — one act as the log keeps it', () => {
expect(validateCareEvent(s)).toMatchObject({ ok: true });
});
});
describe('a rose brought back under a new key (keep-her-safe.md)', () => {
const NEW = 'N'.repeat(43);
const careAs = (to: string, ts: number, amount = 0.5): CareEvent => ({ ...care(ts, 'water', amount), from: to, to });
it('keeps every day of care given under her old key, and starts her water afresh under the new one', () => {
const old = dailyWater(12);
const now = START + 12 * DAY;
const before = roseAt(old, ME, now, ZONE);
const moved = roseAt(old, [NEW, ME], now, ZONE);
expect(moved.daysOfCare).toBe(before.daysOfCare);
expect(moved.stage).toBe(before.stage);
expect(moved.lastDayOfCare).toBe(before.lastDayOfCare);
expect(moved.water).toBe(ROSE.waterStart); // no care under the new key yet
// Care under the new key adds to the old days.
const after = roseAt([...old, careAs(NEW, now)], [NEW, ME], now + 60_000, ZONE);
expect(after.daysOfCare).toBe(before.daysOfCare + 1);
});
it('a moved rose that is opening drops petals, and gathering them helps', () => {
const old = dailyWater(ROSE.stageDays[0]);
const from = START + ROSE.stageDays[0] * DAY;
let day = from;
while (!petalsOn(NEW, localDay(day, ZONE))) day += DAY;
expect(roseAt(old, [NEW, ME], day, ZONE).needs.petals).toBe(true);
const gathered = { ...care(day, 'petals'), from: NEW, to: NEW };
expect(judgeCare([...old, gathered], [NEW, ME], ZONE).at(-1)?.helped).toBe(true);
});
it('merging two logs never takes a day of care away from either', () => {
const act = fc.constantFrom<CareEvent['act']>('water', 'shade', 'weed', 'petals');
const logAs = (to: string) => fc.array(fc.tuple(fc.integer({ min: 0, max: 40 * 24 * 60 }), act, fc.double({ min: 0, max: 1, noNaN: true })), { maxLength: 40 })
.map((xs) => xs.map(([m, a, amount]) => ({ ...care(START + m * 60_000, a, amount), from: to, to })));
fc.assert(fc.property(logAs(ME), logAs(NEW), (a, b) => {
const end = START + 50 * DAY;
const merged = roseAt([...a, ...b], [NEW, ME], end, ZONE).daysOfCare;
return merged >= roseAt(a, ME, end, ZONE).daysOfCare && merged >= roseAt(b, NEW, end, ZONE).daysOfCare;
}), { numRuns: 150 });
});
it('newCare adds each act once, however often the same file is brought back', () => {
const log = dailyWater(3);
expect(newCare([], log)).toEqual(log);
expect(newCare(log, log)).toEqual([]);
expect(newCare(log.slice(0, 1), [...log, ...log])).toEqual(log.slice(1));
});
});