108 lines
4.2 KiB
TypeScript
108 lines
4.2 KiB
TypeScript
import { careEvent, type NamedPlace } from '@rosea/engine';
|
|
import type { CareEvent } from '@rosea/schema';
|
|
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte';
|
|
import { tick } from 'svelte';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import Shell from '../src/Shell.svelte';
|
|
import { en } from '../src/copy/en.js';
|
|
import Rose from '../src/rose/Rose.svelte';
|
|
import type { RoseWiring } from '../src/rose/types.js';
|
|
import type { SunsetChoice, SunsetWiring } from '../src/sunset/types.js';
|
|
|
|
const ZONE = 'Europe/Prague';
|
|
const ME = 'M'.repeat(87);
|
|
const MORNING = Date.parse('2026-03-01T08:00:00+01:00');
|
|
const water = (now: number): CareEvent => careEvent({ now, timeZone: ZONE, from: ME, to: ME, act: 'water', amount: 0.5, clientVersion: '0.1.0' });
|
|
|
|
/** The device's storage as another tab would change it, and the one listener the garden gave. */
|
|
function roseWiring(): RoseWiring & { stored: CareEvent[]; tellOtherTabChanged: () => void } {
|
|
const stored: CareEvent[] = [];
|
|
let listener: (() => void) | undefined;
|
|
return {
|
|
identity: async () => ({ publicKey: ME, kept: true }),
|
|
loadCare: async () => [...stored],
|
|
appendCare: async () => undefined, // this tab's own write is still on its way
|
|
sentences: [],
|
|
loadOpened: async () => [],
|
|
appendOpened: async () => undefined,
|
|
changes: (l) => {
|
|
listener = l;
|
|
return () => (listener = undefined);
|
|
},
|
|
clientVersion: '0.1.0',
|
|
deviceZone: ZONE,
|
|
now: () => MORNING,
|
|
pastKeys: async () => [],
|
|
exportRose: async () => ({}),
|
|
importRose: async () => ({ added: 0, petals: 0 }),
|
|
saveFile: () => undefined,
|
|
keeping: { persisted: async () => false, persist: async () => true },
|
|
installHint: false,
|
|
stored,
|
|
tellOtherTabChanged: () => listener?.()
|
|
};
|
|
}
|
|
|
|
async function daysOfCare(): Promise<string> {
|
|
const toggle = screen.getByRole('button', { name: en.rose.numbers });
|
|
if (toggle.getAttribute('aria-expanded') !== 'true') await fireEvent.click(toggle);
|
|
await tick();
|
|
return document.getElementById('rose-numbers')?.textContent ?? '';
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
cleanup();
|
|
});
|
|
|
|
describe('two tabs, one garden (two-tabs.md)', () => {
|
|
it('care given in another tab is seen here, without a reload', async () => {
|
|
const w = roseWiring();
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
await screen.findByText(en.rose.says.dry);
|
|
w.stored.push(water(MORNING - 60_000));
|
|
w.tellOtherTabChanged();
|
|
await screen.findByText(en.rose.says.thanks);
|
|
expect(await daysOfCare()).toContain(`${en.rose.daysOfCare}1`);
|
|
});
|
|
|
|
it("this tab's own care, still being written, is not lost when another tab's news arrives first", async () => {
|
|
const w = roseWiring();
|
|
render(Rose, { wiring: w, timeZone: ZONE });
|
|
const button = await screen.findByRole('button', { name: en.rose.water });
|
|
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
|
|
await fireEvent.pointerDown(button, { pointerId: 1 });
|
|
vi.advanceTimersByTime(1500);
|
|
await fireEvent.pointerUp(button, { pointerId: 1 });
|
|
vi.useRealTimers();
|
|
await screen.findByText(en.rose.says.thanks);
|
|
// Storage has not got it yet; the other tab's news makes this tab read anyway.
|
|
w.tellOtherTabChanged();
|
|
await new Promise((r) => setTimeout(r, 20));
|
|
expect(await daysOfCare()).toContain(`${en.rose.daysOfCare}1`);
|
|
expect(screen.getByText(en.rose.says.thanks)).toBeTruthy();
|
|
});
|
|
|
|
it('a town chosen in another tab becomes the town here', async () => {
|
|
const brno: NamedPlace = { name: 'Brno', region: '', country: 'Czechia', lat: 49.2, lon: 16.61, timeZone: ZONE };
|
|
let saved: SunsetChoice | null = null;
|
|
let listener: (() => void) | undefined;
|
|
const sunset: SunsetWiring = {
|
|
guess: null,
|
|
search: async () => [],
|
|
load: async () => saved,
|
|
save: async () => undefined,
|
|
now: () => MORNING,
|
|
changes: (l) => {
|
|
listener = l;
|
|
return () => (listener = undefined);
|
|
}
|
|
};
|
|
render(Shell, { sunset });
|
|
await screen.findByRole('heading', { level: 2, name: en.sunset.question });
|
|
saved = { kind: 'town', town: brno };
|
|
listener?.();
|
|
await waitFor(() => expect(screen.getByText(/in Brno\.$/)).toBeTruthy());
|
|
});
|
|
});
|