205 lines
9.5 KiB
TypeScript
205 lines
9.5 KiB
TypeScript
import { careEvent, localDay, petalsOn, petalsToday, weedsOn, weedsToday, windOn } from '@rosea/engine';
|
|
import type { CareEvent } from '@rosea/schema';
|
|
import { cleanup, fireEvent, render, screen } from '@testing-library/svelte';
|
|
import { tick } from 'svelte';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { en } from '../src/copy/en.js';
|
|
import Rose from '../src/rose/Rose.svelte';
|
|
import type { RoseWiring } from '../src/rose/types.js';
|
|
|
|
// jsdom has no PointerEvent: without it a test pointer carries no coordinates. Every browser has one.
|
|
if (typeof window.PointerEvent !== 'function') {
|
|
class PointerEventPolyfill extends MouseEvent {
|
|
readonly pointerId: number;
|
|
readonly pointerType: string;
|
|
constructor(type: string, init: PointerEventInit = {}) {
|
|
super(type, init);
|
|
this.pointerId = init.pointerId ?? 1;
|
|
this.pointerType = init.pointerType ?? 'touch';
|
|
}
|
|
}
|
|
Object.assign(window, { PointerEvent: PointerEventPolyfill });
|
|
}
|
|
|
|
const ZONE = 'Europe/Prague';
|
|
const ME = 'M'.repeat(87);
|
|
const DAY = 86_400_000;
|
|
const MINUTE = 60_000;
|
|
/** 1 March 2026, local midnight in Prague (CET, before the clocks change). */
|
|
const MARCH = Date.parse('2026-03-01T00:00:00+01:00');
|
|
|
|
function wiring(at: number, log: CareEvent[] = []): RoseWiring & { appended: CareEvent[] } {
|
|
const appended: CareEvent[] = [];
|
|
return {
|
|
identity: async () => ({ publicKey: ME, kept: true }),
|
|
loadCare: async () => log,
|
|
appendCare: async (e) => void appended.push(e),
|
|
sentences: [],
|
|
loadOpened: async () => [],
|
|
appendOpened: async () => undefined,
|
|
clientVersion: '0.1.0',
|
|
deviceZone: ZONE,
|
|
now: () => at,
|
|
pastKeys: async () => [],
|
|
exportRose: async () => ({}),
|
|
importRose: async () => ({ added: 0, petals: 0 }),
|
|
saveFile: () => undefined,
|
|
keeping: { persisted: async () => false, persist: async () => true },
|
|
installHint: false,
|
|
appended
|
|
};
|
|
}
|
|
|
|
/** The first morning in March for which `has` holds. */
|
|
function morningWhen(has: (day: string) => boolean): number {
|
|
for (let i = 0; i < 27; i++) {
|
|
const at = MARCH + i * DAY + 9 * 60 * MINUTE;
|
|
if (has(localDay(at, ZONE))) return at;
|
|
}
|
|
throw new Error('no such day in March');
|
|
}
|
|
|
|
/**
|
|
* A pointer moving from a to b on `target`, then let go on the scene, as a finger does. jsdom stamps
|
|
* events with the real clock, so `ms` is waited for real when a gesture's speed matters.
|
|
*/
|
|
async function gesture(target: Element, a: [number, number], b: [number, number], ms = 0, steps = 6): Promise<void> {
|
|
const scene = screen.getByRole('group', { name: en.scene.planet });
|
|
await fireEvent.pointerDown(target, { pointerId: 1, clientX: a[0], clientY: a[1] });
|
|
for (let i = 1; i <= steps; i++) {
|
|
if (ms > 0) await new Promise((r) => setTimeout(r, ms / steps));
|
|
const x = a[0] + ((b[0] - a[0]) * i) / steps, y = a[1] + ((b[1] - a[1]) * i) / steps;
|
|
await fireEvent.pointerMove(scene, { pointerId: 1, clientX: x, clientY: y });
|
|
}
|
|
await fireEvent.pointerUp(scene, { pointerId: 1, clientX: b[0], clientY: b[1] });
|
|
await tick();
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
vi.unstubAllGlobals();
|
|
cleanup();
|
|
});
|
|
|
|
describe('her planet (planet-garden.md)', () => {
|
|
it('tapping her opens the ring of what she needs, with Touch her; Escape closes it and focus comes back to her', async () => {
|
|
render(Rose, { wiring: wiring(morningWhen((d) => !weedsOn(ME, d) && !petalsOn(ME, d))), timeZone: ZONE });
|
|
const her = await screen.findByRole('button', { name: en.rose.care });
|
|
expect(her.getAttribute('aria-expanded')).toBe('false');
|
|
await fireEvent.keyDown(her, { key: 'Enter' });
|
|
await tick();
|
|
const ring = screen.getByRole('group', { name: en.scene.ring });
|
|
expect(her.getAttribute('aria-expanded')).toBe('true');
|
|
expect(document.activeElement?.textContent?.trim()).toBe(en.rose.water);
|
|
expect(screen.getByRole('button', { name: en.rose.touch.label })).toBeTruthy();
|
|
expect(screen.queryByRole('button', { name: en.rose.weed })).toBeNull();
|
|
await fireEvent.keyDown(ring, { key: 'Escape' });
|
|
await tick();
|
|
expect(screen.queryByRole('group', { name: en.scene.ring })).toBeNull();
|
|
expect(document.activeElement).toBe(her);
|
|
});
|
|
|
|
it('weeds: each one swiped up comes out; only when the last is out is it one act of care', async () => {
|
|
const at = morningWhen((d) => weedsOn(ME, d));
|
|
const w = wiring(at);
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
await screen.findByRole('button', { name: en.rose.care });
|
|
const n = weedsToday(ME, localDay(at, ZONE));
|
|
expect(screen.getAllByRole('img', { name: en.scene.weed })).toHaveLength(n);
|
|
|
|
// A sideways swipe does not pull it.
|
|
await gesture(screen.getAllByRole('img', { name: en.scene.weed })[0]!, [200, 180], [280, 175]);
|
|
expect(screen.getAllByRole('img', { name: en.scene.weed })).toHaveLength(n);
|
|
|
|
for (let pulled = 1; pulled <= n; pulled++) {
|
|
await gesture(screen.getAllByRole('img', { name: en.scene.weed })[0]!, [200, 180], [202, 110]);
|
|
expect(w.appended).toHaveLength(pulled === n ? 1 : 0);
|
|
}
|
|
expect(w.appended[0]?.act).toBe('weed');
|
|
});
|
|
|
|
it('fallen petals: one sweep across all of them gathers them; a short swipe does not', async () => {
|
|
const at = morningWhen((d) => petalsOn(ME, d) && !weedsOn(ME, d));
|
|
// Petals fall only once she has opened: four earlier days of helpful watering.
|
|
const opened = [4, 3, 2, 1].map((d) => careEvent({ now: at - d * DAY, timeZone: ZONE, from: ME, to: ME, act: 'water', amount: 0.5, clientVersion: '0.1.0' }));
|
|
const w = wiring(at, opened);
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
await screen.findByRole('button', { name: en.rose.care });
|
|
expect(screen.getAllByRole('img', { name: en.scene.petals })).toHaveLength(petalsToday(ME, localDay(at, ZONE)));
|
|
const scene = screen.getByRole('group', { name: en.scene.planet });
|
|
await gesture(scene, [200, 160], [230, 160]);
|
|
expect(w.appended).toEqual([]);
|
|
await gesture(scene, [180, 160], [320, 170]);
|
|
expect(w.appended.map((e) => e.act)).toEqual(['petals']);
|
|
});
|
|
|
|
it('wind: the windbreak dragged to her windward side is shade; dropped elsewhere it goes back', async () => {
|
|
let at = 0;
|
|
for (let i = 0; i < 27 && !at; i++) {
|
|
const day = localDay(MARCH + i * DAY + 12 * 60 * MINUTE, ZONE);
|
|
const wind = windOn(ME, day);
|
|
if (wind && !petalsOn(ME, day)) at = MARCH + i * DAY + (wind[0] + 10) * MINUTE;
|
|
}
|
|
expect(at).toBeGreaterThan(0);
|
|
const w = wiring(at);
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
const windbreak = await screen.findByRole('img', { name: en.scene.windbreak });
|
|
await gesture(windbreak, [258, 250], [330, 380]);
|
|
expect(w.appended).toEqual([]);
|
|
await gesture(screen.getByRole('img', { name: en.scene.windbreak }), [258, 250], [140, 150]);
|
|
expect(w.appended.map((e) => e.act)).toEqual(['shade']);
|
|
});
|
|
|
|
it('the can: pressed and tilted far, it pours faster than a steady hold', async () => {
|
|
const at = morningWhen(() => true);
|
|
const w = wiring(at);
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
const can = await screen.findByRole('img', { name: en.scene.can });
|
|
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
|
|
const scene = screen.getByRole('group', { name: en.scene.planet });
|
|
await fireEvent.pointerDown(can, { pointerId: 1, clientX: 100, clientY: 220 });
|
|
await fireEvent.pointerMove(scene, { pointerId: 1, clientX: 260, clientY: 220 });
|
|
vi.advanceTimersByTime(1000);
|
|
await fireEvent.pointerUp(scene, { pointerId: 1, clientX: 260, clientY: 220 });
|
|
vi.useRealTimers();
|
|
await tick();
|
|
expect(w.appended[0]?.act).toBe('water');
|
|
// A steady hold pours a third of a can in a second; fully tilted pours half again as fast.
|
|
expect(w.appended[0]?.amount).toBeGreaterThan(0.45);
|
|
});
|
|
|
|
it('a slow stroke across her petals touches her; nothing is written', async () => {
|
|
const w = wiring(morningWhen(() => true));
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
const her = await screen.findByRole('button', { name: en.rose.care });
|
|
await gesture(her, [170, 100], [200, 104], 500);
|
|
expect(document.querySelector('.bubble')?.textContent).toBe(en.rose.touch.fond[0]);
|
|
expect(w.appended).toEqual([]);
|
|
expect(screen.queryByRole('group', { name: en.scene.ring })).toBeNull();
|
|
});
|
|
|
|
/** Where the gardener stands now, in degrees round the world. */
|
|
const gardenerAt = (): number => Number(/rotate\((-?[\d.]+)/.exec(document.querySelector('.walker')?.getAttribute('transform') ?? '')?.[1]);
|
|
|
|
/** Asks the gardener to touch her (from the ring), and reads where they are straight after. */
|
|
async function touchFromRing(): Promise<number> {
|
|
await fireEvent.keyDown(await screen.findByRole('button', { name: en.rose.care }), { key: 'Enter' });
|
|
await tick();
|
|
await fireEvent.click(screen.getByRole('button', { name: en.rose.touch.label }));
|
|
await tick();
|
|
return gardenerAt();
|
|
}
|
|
|
|
it('with reduced motion the gardener is at the work at once; otherwise still on the way', async () => {
|
|
render(Rose, { wiring: wiring(morningWhen(() => true)), timeZone: ZONE });
|
|
expect(await touchFromRing()).toBeCloseTo(-28, 0);
|
|
cleanup();
|
|
|
|
vi.stubGlobal('matchMedia', (q: string) => ({ matches: q.includes('reduce'), media: q, addEventListener: () => undefined, removeEventListener: () => undefined }));
|
|
render(Rose, { wiring: wiring(morningWhen(() => true)), timeZone: ZONE });
|
|
await screen.findByRole('button', { name: en.rose.care });
|
|
expect(document.querySelector('.planet')?.classList.contains('reduced')).toBe(true);
|
|
expect(await touchFromRing()).toBe(-10);
|
|
});
|
|
});
|