110 lines
4.3 KiB
TypeScript
110 lines
4.3 KiB
TypeScript
import { careEvent, TOUCH } 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';
|
|
|
|
const ZONE = 'Europe/Prague';
|
|
const ME = 'M'.repeat(87);
|
|
const MORNING = Date.parse('2026-03-01T08:00:00+01:00');
|
|
const DAY = 86_400_000;
|
|
|
|
/** A clock the test moves, and every write the rose tries. */
|
|
function wiring(log: CareEvent[] = []): RoseWiring & { clock: { now: number }; writes: unknown[] } {
|
|
const clock = { now: MORNING };
|
|
const writes: unknown[] = [];
|
|
return {
|
|
identity: async () => ({ publicKey: ME, kept: true }),
|
|
loadCare: async () => log,
|
|
appendCare: async (e) => void writes.push(e),
|
|
sentences: [],
|
|
loadOpened: async () => [],
|
|
appendOpened: async (e) => void writes.push(e),
|
|
clientVersion: '0.1.0',
|
|
deviceZone: ZONE,
|
|
now: () => clock.now,
|
|
pastKeys: async () => [],
|
|
exportRose: async () => ({}),
|
|
importRose: async () => ({ added: 0, petals: 0 }),
|
|
saveFile: () => undefined,
|
|
keeping: { persisted: async () => false, persist: async () => true },
|
|
installHint: false,
|
|
clock,
|
|
writes
|
|
};
|
|
}
|
|
|
|
const bubble = (): string => document.querySelector('.bubble')?.textContent ?? '';
|
|
|
|
async function touchAt(w: ReturnType<typeof wiring>, at: number): Promise<void> {
|
|
w.clock.now = at;
|
|
await fireEvent.click(screen.getByRole('button', { name: en.rose.touch.label }));
|
|
await tick();
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
cleanup();
|
|
});
|
|
|
|
describe('touching her (thorns.md)', () => {
|
|
it('a touch is answered fondly, then her usual line returns; nothing is written', async () => {
|
|
const w = wiring();
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
await screen.findByRole('button', { name: en.rose.touch.label });
|
|
const usual = bubble();
|
|
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
|
|
await touchAt(w, MORNING);
|
|
expect(bubble()).toBe(en.rose.touch.fond[0]);
|
|
await touchAt(w, MORNING + 5000);
|
|
expect(bubble()).toBe(en.rose.touch.fond[1]);
|
|
vi.advanceTimersByTime(TOUCH.restMs);
|
|
await tick();
|
|
expect(bubble()).toBe(usual);
|
|
expect(w.writes).toEqual([]);
|
|
});
|
|
|
|
it('three quick taps: Gently. I have thorns. Taps while she rests change nothing', async () => {
|
|
const w = wiring();
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
await screen.findByRole('button', { name: en.rose.touch.label });
|
|
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
|
|
await touchAt(w, MORNING);
|
|
await touchAt(w, MORNING + 500);
|
|
await touchAt(w, MORNING + 1000);
|
|
expect(bubble()).toBe(en.rose.touch.thorns);
|
|
await touchAt(w, MORNING + 2000);
|
|
expect(bubble()).toBe(en.rose.touch.thorns);
|
|
expect(w.writes).toEqual([]);
|
|
});
|
|
|
|
it('a held key is one touch, not a rush', async () => {
|
|
const w = wiring();
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
const button = await screen.findByRole('button', { name: en.rose.touch.label });
|
|
const repeat = new KeyboardEvent('keydown', { key: 'Enter', repeat: true, cancelable: true, bubbles: true });
|
|
button.dispatchEvent(repeat);
|
|
expect(repeat.defaultPrevented).toBe(true);
|
|
const first = new KeyboardEvent('keydown', { key: 'Enter', cancelable: true, bubbles: true });
|
|
button.dispatchEvent(first);
|
|
expect(first.defaultPrevented).toBe(false);
|
|
});
|
|
|
|
it('in full bloom the first touch of a visit makes her blush; her picture keeps its description', async () => {
|
|
// 25 days of helpful morning watering: full bloom.
|
|
const log = Array.from({ length: 25 }, (_, i) =>
|
|
careEvent({ now: MORNING + i * DAY, timeZone: ZONE, from: ME, to: ME, act: 'water', amount: 0.5, clientVersion: '0.1.0' })
|
|
);
|
|
const w = wiring(log);
|
|
w.clock.now = MORNING + 25 * DAY + 3 * 3_600_000;
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
const img = await screen.findByRole('img', { name: /full bloom/ });
|
|
expect(img.getAttribute('alt')).not.toMatch(/blushing/);
|
|
await touchAt(w, w.clock.now);
|
|
expect(screen.getByRole('img', { name: /full bloom, blushing/ })).toBeTruthy();
|
|
});
|
|
});
|