From e948f99f9e8ec90a00e8b8f97dc769ea382c9f55 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 23:42:48 +0100 Subject: [PATCH] Sky menu: Auto, six named moments and a free moment picker Seven radio items plus Pick a moment, which opens a date input and a 5-minute time slider that show the place-local time and zone and commit on the native change event (slider release), never per input tick. The picker opens from the moment being drawn, in the place zone (seedFree). Co-Authored-By: Claude Sonnet 5.5 --- .../features/scene/SkyStateControl.test.tsx | 173 +++++++++++++++ src/app/features/scene/SkyStateControl.tsx | 205 +++++++++++++++--- .../features/scene/ridge/skyMoment.test.ts | 109 +++++++--- src/app/features/scene/ridge/skyMoment.ts | 30 ++- src/app/features/scene/skyTheme.test.ts | 2 +- src/app/hooks/useSkyState.ts | 11 +- 6 files changed, 456 insertions(+), 74 deletions(-) create mode 100644 src/app/features/scene/SkyStateControl.test.tsx diff --git a/src/app/features/scene/SkyStateControl.test.tsx b/src/app/features/scene/SkyStateControl.test.tsx new file mode 100644 index 00000000..df06280b --- /dev/null +++ b/src/app/features/scene/SkyStateControl.test.tsx @@ -0,0 +1,173 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import type { SkyChoice } from './ridge/skyMoment'; +import { FALLBACK_VIEW } from './ridge/view'; +import { + MENU_CHOICES, + SkyMomentPicker, + SkyStateControl, + choiceLabel, + useNativeChange, +} from './SkyStateControl'; + +// The component's hooks are held still, as in RoomOutbox.actions.test.tsx: state is its initial value, refs are plain boxes, effects are +// kept to be run once the test has put an element in each ref. The element tree it returns is read and its native events are dispatched +// on a fake input, so no DOM is needed. +const held = vi.hoisted(() => ({ + effects: [] as Array<() => unknown>, + choice: { kind: 'auto' } as unknown, +})); +vi.mock('react', async (importOriginal) => { + const actual = await importOriginal(); + const useState = (init: unknown) => [ + typeof init === 'function' ? (init as () => unknown)() : init, + () => undefined, + ]; + const useRef = (init: unknown) => ({ current: init }); + const useEffect = (fn: () => unknown) => { + held.effects.push(fn); + }; + return { + ...actual, + default: { ...actual, useState, useRef, useEffect }, + useState, + useRef, + useEffect, + }; +}); +vi.mock('../../hooks/useSkyState', () => ({ + useSkyChoice: () => held.choice, + useDrawnPlace: () => FALLBACK_VIEW.place, +})); +vi.mock('../../hooks/useClientConfig', () => ({ useClientConfig: () => ({}) })); + +type El = { type?: unknown; ref?: unknown; props?: Record }; +/** Every element reachable through any prop (children, `content`, `after`). */ +const walk = (node: unknown): El[] => { + if (Array.isArray(node)) return node.flatMap(walk); + if (node && typeof node === 'object' && 'props' in node) { + const el = node as El; + return [el, ...Object.values(el.props ?? {}).flatMap(walk)]; + } + return []; +}; +const textOf = (node: unknown): string => { + if (typeof node === 'string' || typeof node === 'number') return String(node); + if (Array.isArray(node)) return node.map(textOf).join(''); + if (node && typeof node === 'object' && 'props' in node) { + return textOf((node as El).props?.children); + } + return ''; +}; +/** A stand-in input: a real EventTarget with a value. */ +const fakeInput = (value: string) => Object.assign(new EventTarget(), { value }); +const fire = (el: EventTarget, type: 'input' | 'change') => el.dispatchEvent(new Event(type)); + +beforeEach(() => { + held.effects.length = 0; + held.choice = { kind: 'auto' }; +}); + +describe('the sky menu choices', () => { + const tree = () => walk(SkyStateControl()); + const radios = () => tree().filter((e) => e.props?.role === 'menuitemradio'); + + it('lists Auto and the six named moments, then Pick a moment', () => { + expect(MENU_CHOICES.map(choiceLabel)).toEqual([ + 'Auto', + 'Dawn', + 'Morning', + 'Noon', + 'Golden hour', + 'Dusk', + 'Night', + ]); + expect(radios().map((e) => textOf(e))).toEqual([ + 'Auto', + 'Dawn', + 'Morning', + 'Noon', + 'Golden hour', + 'Dusk', + 'Night', + 'Pick a moment', + ]); + }); + + it.each([ + [{ kind: 'auto' }, 0], + [{ kind: 'named', id: 'dawn' }, 1], + [{ kind: 'named', id: 'night' }, 6], + [{ kind: 'free', date: '2026-12-21', minutes: 400 }, 7], + ] as Array<[SkyChoice, number]>)('aria-checked follows the choice %j', (choice, checked) => { + held.choice = choice; + radios().forEach((e, i) => expect(e.props?.['aria-checked'], `item ${i}`).toBe(i === checked)); + }); + + it("the button reads 'Sky: Auto', 'Sky: Dawn' or 'Sky: