[ui] TwoTabs: The rose and the garden read again when another tab wrote, keeping their own
This commit is contained in:
@@ -29,7 +29,9 @@
|
|||||||
search: async (query) => (await loadTowns()).searchTowns(query),
|
search: async (query) => (await loadTowns()).searchTowns(query),
|
||||||
load: () => storage.loadSunsetPlace(),
|
load: () => storage.loadSunsetPlace(),
|
||||||
save: (choice) => storage.saveSunsetPlace(choice),
|
save: (choice) => storage.saveSunsetPlace(choice),
|
||||||
now: () => Date.now()
|
now: () => Date.now(),
|
||||||
|
// Another tab of the garden chose a town (two-tabs.md).
|
||||||
|
changes: (listener) => storage.onChange((what) => what === 'place' && listener())
|
||||||
};
|
};
|
||||||
|
|
||||||
// The player's key names their rose (rose.md). Kept once; if the device will not
|
// The player's key names their rose (rose.md). Kept once; if the device will not
|
||||||
@@ -97,6 +99,8 @@
|
|||||||
sentences,
|
sentences,
|
||||||
loadOpened: () => storage.loadOpened(),
|
loadOpened: () => storage.loadOpened(),
|
||||||
appendOpened: (event) => storage.appendOpened(event),
|
appendOpened: (event) => storage.appendOpened(event),
|
||||||
|
// Another tab gave care, opened a petal or brought her back (two-tabs.md).
|
||||||
|
changes: (listener) => storage.onChange((what) => what === 'log' && listener()),
|
||||||
clientVersion: version,
|
clientVersion: version,
|
||||||
deviceZone,
|
deviceZone,
|
||||||
now: () => Date.now(),
|
now: () => Date.now(),
|
||||||
|
|||||||
@@ -241,6 +241,45 @@ test.describe('the rose, in Prague at 08:00 on 1 March 2026', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --- Two tabs, one garden (.agents/features/two-tabs.md) ----------------------
|
||||||
|
test.describe('two tabs of the garden, in Prague at 08:00 on 1 March 2026', () => {
|
||||||
|
test.use({ timezoneId: 'Europe/Prague', locale: 'en-US' });
|
||||||
|
const MORNING = Date.parse('2026-03-01T08:00:00+01:00');
|
||||||
|
|
||||||
|
test('care given in one tab is seen in the other without a reload, so she is not watered twice', async ({ page, context }) => {
|
||||||
|
const other = await context.newPage();
|
||||||
|
for (const p of [page, other]) {
|
||||||
|
await p.clock.setFixedTime(MORNING);
|
||||||
|
await open(p);
|
||||||
|
await expect(p.getByText(en.rose.says.dry)).toBeVisible();
|
||||||
|
}
|
||||||
|
await page.getByRole('button', { name: en.rose.water }).hover();
|
||||||
|
await page.mouse.down();
|
||||||
|
await page.waitForTimeout(1500);
|
||||||
|
await page.mouse.up();
|
||||||
|
await expect(page.getByText(en.rose.says.thanks)).toBeVisible();
|
||||||
|
await storedCare(page, 1);
|
||||||
|
|
||||||
|
// The other tab, untouched and not reloaded, knows she was watered.
|
||||||
|
await expect(other.getByText(en.rose.says.thanks)).toBeVisible({ timeout: 3_000 });
|
||||||
|
await other.getByRole('button', { name: en.rose.numbers }).click();
|
||||||
|
await expect(other.locator('#rose-numbers')).toContainText(`${en.rose.daysOfCare}1`);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a town chosen in one tab is the town in the other', async ({ page, context }) => {
|
||||||
|
const other = await context.newPage();
|
||||||
|
for (const p of [page, other]) {
|
||||||
|
await p.clock.setFixedTime(MORNING);
|
||||||
|
await open(p);
|
||||||
|
await expect(p.getByRole('heading', { level: 2, name: en.sunset.question })).toBeVisible();
|
||||||
|
}
|
||||||
|
await page.getByRole('searchbox', { name: en.sunset.searchLabel }).fill('brno');
|
||||||
|
await page.getByRole('button', { name: /^Brno/ }).first().click();
|
||||||
|
await storedTown(page, 'Brno');
|
||||||
|
await expect(other.locator('.sun p').first()).toHaveText(/in Brno\.$/, { timeout: 3_000 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// --- Keeping her (.agents/features/keep-her-safe.md) --------------------------
|
// --- Keeping her (.agents/features/keep-her-safe.md) --------------------------
|
||||||
test.describe('keep her safe, in Prague at 08:00 on 1 March 2026', () => {
|
test.describe('keep her safe, in Prague at 08:00 on 1 March 2026', () => {
|
||||||
test.use({ timezoneId: 'Europe/Prague', locale: 'en-US' });
|
test.use({ timezoneId: 'Europe/Prague', locale: 'en-US' });
|
||||||
|
|||||||
@@ -72,6 +72,22 @@
|
|||||||
return () => clearInterval(timer);
|
return () => clearInterval(timer);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// A town chosen in another tab becomes the town here too (two-tabs.md).
|
||||||
|
$effect(() => {
|
||||||
|
const wiring = sunset;
|
||||||
|
return wiring?.changes?.(() => {
|
||||||
|
wiring.load().then(
|
||||||
|
(saved) => {
|
||||||
|
if (saved === null) return;
|
||||||
|
choice = saved;
|
||||||
|
status = 'set';
|
||||||
|
now = wiring.now();
|
||||||
|
},
|
||||||
|
(error: unknown) => console.error('sunset: could not read the town another tab chose', error)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
async function choose(next: SunsetChoice): Promise<void> {
|
async function choose(next: SunsetChoice): Promise<void> {
|
||||||
if (!sunset) return;
|
if (!sunset) return;
|
||||||
choice = next;
|
choice = next;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {
|
import {
|
||||||
careEvent, goodMoment, localDay, petalAt, pour, roseAt, ROSE, sentenceOpened, touch, TOUCH, UNTOUCHED,
|
careEvent, goodMoment, localDay, newCare, newOpened, petalAt, pour, roseAt, ROSE, sentenceOpened, touch, TOUCH, UNTOUCHED,
|
||||||
type Mood, type PetalNow, type Place, type RoseNow, type Stage, type TouchState
|
type Mood, type PetalNow, type Place, type RoseNow, type Stage, type TouchState
|
||||||
} from '@rosea/engine';
|
} from '@rosea/engine';
|
||||||
import type { CareEvent, SentenceOpened } from '@rosea/schema';
|
import type { CareEvent, SentenceOpened } from '@rosea/schema';
|
||||||
@@ -78,19 +78,25 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** After she is brought back from a file: read her log and keys again. */
|
/**
|
||||||
function restored(): void {
|
* Read her log and keys again: after she is brought back from a file, or when another
|
||||||
|
* tab wrote (two-tabs.md). What this tab already shows is kept: the log only grows,
|
||||||
|
* and this tab's own write may still be on its way when the read finishes.
|
||||||
|
*/
|
||||||
|
function reload(): void {
|
||||||
Promise.all([wiring.loadCare(), wiring.pastKeys(), wiring.loadOpened()]).then(
|
Promise.all([wiring.loadCare(), wiring.pastKeys(), wiring.loadOpened()]).then(
|
||||||
([log, past, petals]) => {
|
([log, past, petals]) => {
|
||||||
events = log;
|
events = [...log, ...newCare(log, events)];
|
||||||
pastKeys = past;
|
opened = [...petals, ...newOpened(petals, opened)];
|
||||||
opened = petals;
|
pastKeys = [...new Set([...past, ...pastKeys])];
|
||||||
now = wiring.now();
|
now = wiring.now();
|
||||||
},
|
},
|
||||||
(error: unknown) => console.error('rose: could not read her back after restoring', error)
|
(error: unknown) => console.error('rose: could not read her log again', error)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$effect(() => wiring.changes?.(reload));
|
||||||
|
|
||||||
function care(act: CareEvent['act'], amount?: number): void {
|
function care(act: CareEvent['act'], amount?: number): void {
|
||||||
if (seed === null) return;
|
if (seed === null) return;
|
||||||
now = wiring.now();
|
now = wiring.now();
|
||||||
@@ -260,7 +266,7 @@
|
|||||||
</dl>
|
</dl>
|
||||||
{/if}
|
{/if}
|
||||||
{#if showSafe && seed}
|
{#if showSafe && seed}
|
||||||
<KeepSafe {wiring} currentKey={seed} today={localDay(now, timeZone)} onrestored={restored} />
|
<KeepSafe {wiring} currentKey={seed} today={localDay(now, timeZone)} onrestored={reload} />
|
||||||
{/if}
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ export interface RoseWiring {
|
|||||||
/** The rose as a file's contents, and bringing one back. */
|
/** The rose as a file's contents, and bringing one back. */
|
||||||
readonly exportRose: (currentKey: string) => Promise<unknown>;
|
readonly exportRose: (currentKey: string) => Promise<unknown>;
|
||||||
readonly importRose: (file: unknown, currentKey: string) => Promise<{ readonly added: number; readonly petals: number }>;
|
readonly importRose: (file: unknown, currentKey: string) => Promise<{ readonly added: number; readonly petals: number }>;
|
||||||
|
/** Hears care, a petal or a rose brought back in another tab (two-tabs.md); returns the way to stop. */
|
||||||
|
readonly changes?: (listener: () => void) => () => void;
|
||||||
/** Hands the player a file to keep. */
|
/** Hands the player a file to keep. */
|
||||||
readonly saveFile: (name: string, text: string) => void;
|
readonly saveFile: (name: string, text: string) => void;
|
||||||
/** The browser's promise to keep the garden: true, false, or null when it cannot say. */
|
/** The browser's promise to keep the garden: true, false, or null when it cannot say. */
|
||||||
|
|||||||
@@ -16,4 +16,6 @@ export interface SunsetWiring {
|
|||||||
readonly save: (choice: SunsetChoice) => Promise<void>;
|
readonly save: (choice: SunsetChoice) => Promise<void>;
|
||||||
/** The clock. A parameter, so tests and the e2e suite can fix it. */
|
/** The clock. A parameter, so tests and the e2e suite can fix it. */
|
||||||
readonly now: () => number;
|
readonly now: () => number;
|
||||||
|
/** Hears a town chosen in another tab (two-tabs.md); returns the way to stop. */
|
||||||
|
readonly changes?: (listener: () => void) => () => void;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,107 @@
|
|||||||
|
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());
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user