From b67fa3603ddaa778270dcc1832b30146a2f8eb78 Mon Sep 17 00:00:00 2001 From: mnovak Date: Sat, 7 Nov 2026 15:06:44 +0100 Subject: [PATCH] [ui] TwoTabs: The rose and the garden read again when another tab wrote, keeping their own --- apps/rosea/src/App.svelte | 6 +- e2e/shell.spec.ts | 39 +++++++++++ packages/ui/src/Shell.svelte | 16 +++++ packages/ui/src/rose/Rose.svelte | 22 +++--- packages/ui/src/rose/types.ts | 2 + packages/ui/src/sunset/types.ts | 2 + packages/ui/test/two-tabs.test.ts | 107 ++++++++++++++++++++++++++++++ 7 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 packages/ui/test/two-tabs.test.ts diff --git a/apps/rosea/src/App.svelte b/apps/rosea/src/App.svelte index cdbb75d..ab70679 100644 --- a/apps/rosea/src/App.svelte +++ b/apps/rosea/src/App.svelte @@ -29,7 +29,9 @@ search: async (query) => (await loadTowns()).searchTowns(query), load: () => storage.loadSunsetPlace(), 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 @@ -97,6 +99,8 @@ sentences, loadOpened: () => storage.loadOpened(), 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, deviceZone, now: () => Date.now(), diff --git a/e2e/shell.spec.ts b/e2e/shell.spec.ts index 034ac5e..763ab09 100644 --- a/e2e/shell.spec.ts +++ b/e2e/shell.spec.ts @@ -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) -------------------------- test.describe('keep her safe, in Prague at 08:00 on 1 March 2026', () => { test.use({ timezoneId: 'Europe/Prague', locale: 'en-US' }); diff --git a/packages/ui/src/Shell.svelte b/packages/ui/src/Shell.svelte index d11e73b..6996b68 100644 --- a/packages/ui/src/Shell.svelte +++ b/packages/ui/src/Shell.svelte @@ -72,6 +72,22 @@ 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 { if (!sunset) return; choice = next; diff --git a/packages/ui/src/rose/Rose.svelte b/packages/ui/src/rose/Rose.svelte index 7be3f8e..0118dde 100644 --- a/packages/ui/src/rose/Rose.svelte +++ b/packages/ui/src/rose/Rose.svelte @@ -1,6 +1,6 @@