169 lines
7.9 KiB
TypeScript
169 lines
7.9 KiB
TypeScript
import { addDays, careEvent, invitation, petalOrder, roseAt, sentenceOpened, sunDay, type Place } from '@rosea/engine';
|
|
import type { CareEvent, HopeSentence, SentenceOpened } from '@rosea/schema';
|
|
import { cleanup, fireEvent, render, screen, within } from '@testing-library/svelte';
|
|
import { tick } from 'svelte';
|
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
import { en } from '../src/copy/en.js';
|
|
import Rose from '../src/rose/Rose.svelte';
|
|
import type { RoseWiring } from '../src/rose/types.js';
|
|
|
|
const ZONE = 'Europe/Prague';
|
|
const ME = 'M'.repeat(87);
|
|
const HOUR = 3_600_000;
|
|
const DAY = 24 * HOUR;
|
|
const prague: Place = { lat: 50.0755, lon: 14.4378, timeZone: ZONE };
|
|
|
|
const line = (id: string, text: string, over: Partial<HopeSentence['source']> = {}): HopeSentence => ({
|
|
id,
|
|
text: { en: text },
|
|
source: { work: `Fixture ${id}`, author: null, tradition: 'fixture', translation: null, publicDomain: true, verifiedBy: 'Test', verifiedAt: '2026-11-01', ...over },
|
|
use: ['sunset'],
|
|
tone: 'gentle',
|
|
promisesOutcome: false
|
|
});
|
|
const LIBRARY: HopeSentence[] = [
|
|
line('fx-one', 'The first fixture line.', { author: 'A Writer', translation: 'A Translator, 1900' }),
|
|
line('fx-two', 'The second fixture line.'),
|
|
line('fx-three', 'The third fixture line.')
|
|
];
|
|
const order = petalOrder(LIBRARY);
|
|
const textOf = (id: string): string => LIBRARY.find((s) => s.id === id)!.text.en;
|
|
|
|
const water = (now: number): CareEvent => careEvent({ now, timeZone: ZONE, from: ME, to: ME, act: 'water', amount: 0.5, clientVersion: '0.1.0' });
|
|
const opening = (now: number, messageId: string): SentenceOpened => sentenceOpened({ now, timeZone: ZONE, from: ME, messageId, clientVersion: '0.1.0' });
|
|
|
|
const quiet = (log: CareEvent[], now: number): boolean => {
|
|
const r = roseAt(log, ME, now, ZONE);
|
|
return !r.needs.water && !r.needs.wind && !r.needs.weeds && !r.needs.petals && r.mood === 'content';
|
|
};
|
|
|
|
/**
|
|
* A daytime moment, an hour or more after a helpful 08:00 pour on the same local day,
|
|
* at which she needs nothing and is not blushing: then the petal is hers to offer.
|
|
* Found with the engine, so the test does not guess the day's weeds and wind.
|
|
*/
|
|
function quietMoment(from: number): { log: CareEvent[]; now: number } {
|
|
for (let day = 0; day < 30; day++) {
|
|
const pour = from + day * DAY;
|
|
const log = [water(pour)];
|
|
for (let later = pour + HOUR; later <= pour + 10 * HOUR; later += 10 * 60_000) {
|
|
if (quiet(log, later)) return { log, now: later };
|
|
}
|
|
}
|
|
throw new Error('no quiet moment in 30 days');
|
|
}
|
|
|
|
/** A day's sunset at which she needs nothing, after a pour that afternoon. */
|
|
function quietSunset(): { log: CareEvent[]; now: number } {
|
|
for (let day = 0; day < 60; day++) {
|
|
const sunset = invitation(sunDay(addDays('2026-03-02', day), prague));
|
|
for (let hours = 2; hours <= 6; hours++) {
|
|
const log = [water(sunset - hours * HOUR)];
|
|
if (quiet(log, sunset)) return { log, now: sunset };
|
|
}
|
|
}
|
|
throw new Error('no quiet sunset in 60 days');
|
|
}
|
|
|
|
function wiring(p: { log?: CareEvent[]; opened?: SentenceOpened[]; at: number; sentences?: HopeSentence[] }): RoseWiring & { appendedOpened: SentenceOpened[] } {
|
|
const appendedOpened: SentenceOpened[] = [];
|
|
return {
|
|
identity: async () => ({ publicKey: ME, kept: true }),
|
|
loadCare: async () => p.log ?? [],
|
|
appendCare: async () => undefined,
|
|
sentences: p.sentences ?? LIBRARY,
|
|
loadOpened: async () => p.opened ?? [],
|
|
appendOpened: async (e) => {
|
|
appendedOpened.push(e);
|
|
},
|
|
clientVersion: '0.1.0',
|
|
deviceZone: ZONE,
|
|
now: () => p.at,
|
|
pastKeys: async () => [],
|
|
exportRose: async () => ({}),
|
|
importRose: async () => ({ added: 0, petals: 0 }),
|
|
saveFile: () => undefined,
|
|
keeping: { persisted: async () => false, persist: async () => true },
|
|
installHint: false,
|
|
appendedOpened
|
|
};
|
|
}
|
|
|
|
const MORNING = Date.parse('2026-03-02T08:00:00+01:00');
|
|
const bubble = (): string => document.querySelector('.bubble')?.textContent ?? '';
|
|
|
|
afterEach(() => cleanup());
|
|
|
|
describe('the folded petal (day-sentence.md)', () => {
|
|
it('with an empty library: no petal, and she says nothing about one', async () => {
|
|
const q = quietMoment(MORNING);
|
|
render(Rose, { wiring: wiring({ log: q.log, at: q.now, sentences: [] }), timeZone: ZONE });
|
|
await screen.findByRole('button', { name: en.rose.care });
|
|
expect(screen.queryByRole('button', { name: en.rose.petal.open })).toBeNull();
|
|
expect(bubble()).not.toContain('petal');
|
|
});
|
|
|
|
it('when she needs nothing, she says she keeps it, and offers it', async () => {
|
|
const q = quietMoment(MORNING);
|
|
render(Rose, { wiring: wiring({ log: q.log, at: q.now }), timeZone: ZONE, place: prague });
|
|
expect(await screen.findByRole('button', { name: en.rose.petal.open })).toBeTruthy();
|
|
expect(screen.getByRole('img', { name: en.rose.petal.folded })).toBeTruthy();
|
|
expect(bubble()).toBe(en.rose.petal.waiting);
|
|
});
|
|
|
|
it('when she needs something, the need is what she says, and the petal still waits', async () => {
|
|
render(Rose, { wiring: wiring({ at: MORNING }), timeZone: ZONE });
|
|
expect(await screen.findByRole('button', { name: en.rose.petal.open })).toBeTruthy();
|
|
// A new rose's soil is dry.
|
|
expect(bubble()).toBe(en.rose.says.dry);
|
|
});
|
|
|
|
it('opening it keeps one event, shows the words and where they come from, and moves focus to them', async () => {
|
|
const q = quietMoment(MORNING);
|
|
const w = wiring({ log: q.log, at: q.now });
|
|
render(Rose, { wiring: w, timeZone: ZONE, place: prague });
|
|
await fireEvent.click(await screen.findByRole('button', { name: en.rose.petal.open }));
|
|
await tick();
|
|
expect(w.appendedOpened).toEqual([opening(q.now, order[0]!)]);
|
|
expect(screen.queryByRole('button', { name: en.rose.petal.open })).toBeNull();
|
|
const words = document.getElementById(screen.getByRole('button', { name: en.rose.petal.today }).getAttribute('aria-controls')!)!;
|
|
expect(within(words).getByText(textOf(order[0]!))).toBeTruthy();
|
|
const s = LIBRARY.find((x) => x.id === order[0])!.source;
|
|
expect(within(words).getByText(en.rose.petal.source(s.work, s.author, s.translation))).toBeTruthy();
|
|
expect(document.activeElement).toBe(words);
|
|
// Away from sunset, opening it changes nothing in what she says.
|
|
expect(bubble()).toBe('');
|
|
});
|
|
|
|
it('opened earlier today: the same words come back, and can be folded away and back', async () => {
|
|
const q = quietMoment(MORNING);
|
|
render(Rose, { wiring: wiring({ log: q.log, at: q.now, opened: [opening(q.now - 60_000, 'fx-two')] }), timeZone: ZONE });
|
|
const today = await screen.findByRole('button', { name: en.rose.petal.today });
|
|
expect(today.getAttribute('aria-expanded')).toBe('true');
|
|
expect(screen.getByText(textOf('fx-two'))).toBeTruthy();
|
|
expect(screen.queryByRole('button', { name: en.rose.petal.open })).toBeNull();
|
|
await fireEvent.click(today);
|
|
expect(today.getAttribute('aria-expanded')).toBe('false');
|
|
expect(screen.queryByText(textOf('fx-two'))).toBeNull();
|
|
});
|
|
|
|
it('on a nearby day she also remembers an earlier one', async () => {
|
|
const q = quietMoment(MORNING);
|
|
const opened = [opening(q.now - DAY, order[0]!), opening(q.now - 60_000, order[1]!)];
|
|
render(Rose, { wiring: wiring({ log: q.log, at: q.now, opened }), timeZone: ZONE });
|
|
expect(await screen.findByText(en.rose.petal.remember)).toBeTruthy();
|
|
expect(screen.getByText(textOf(order[0]!))).toBeTruthy();
|
|
expect(screen.getByText(textOf(order[1]!))).toBeTruthy();
|
|
});
|
|
|
|
it('at sunset she says the sun is low, and once it is opened, sends the player to the sky', async () => {
|
|
const q = quietSunset();
|
|
render(Rose, { wiring: wiring({ log: q.log, at: q.now }), timeZone: ZONE, place: prague });
|
|
await screen.findByRole('button', { name: en.rose.petal.open });
|
|
expect(bubble()).toBe(en.rose.petal.goodMoment);
|
|
await fireEvent.click(screen.getByRole('button', { name: en.rose.petal.open }));
|
|
await tick();
|
|
expect(bubble()).toBe(en.rose.petal.lookUp);
|
|
});
|
|
});
|