|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act } from 'react'; |
| 3 | +import { createRoot, type Root } from 'react-dom/client'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { activateSurface } from '@/infrastructure/peer-device/deviceSurface'; |
| 6 | +import MessageSendingSettingsSection from './MessageSendingSettingsSection'; |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ get: vi.fn(), set: vi.fn(), error: vi.fn(), listeners: new Set<() => void>() })); |
| 9 | +vi.mock('@/infrastructure/config/services/ConfigManager', () => ({ configManager: { |
| 10 | + hasCachedConfig: () => false, getCachedConfig: () => undefined, |
| 11 | + getOptionalConfig: mocks.get, setConfig: mocks.set, |
| 12 | + watch: (_path: string, listener: () => void) => { |
| 13 | + mocks.listeners.add(listener); |
| 14 | + return () => mocks.listeners.delete(listener); |
| 15 | + }, |
| 16 | +} })); |
| 17 | +vi.mock('@openbitfun/ui', async original => ({ |
| 18 | + ...await original<typeof import('@openbitfun/ui')>(), |
| 19 | + Select: ({ value, disabled, options, onValueChange }: { |
| 20 | + value: string; disabled: boolean; options: Array<{ value: string; label: string }>; |
| 21 | + onValueChange: (value: string) => void; |
| 22 | + }) => <select value={value} disabled={disabled} onChange={event => onValueChange(event.target.value)}> |
| 23 | + {options.map(option => <option key={option.value} value={option.value}>{option.label}</option>)} |
| 24 | + </select>, |
| 25 | +})); |
| 26 | +vi.mock('@/infrastructure/i18n', () => ({ useI18n: () => ({ t: (key: string) => key }) })); |
| 27 | +vi.mock('@/shared/notification-system', () => ({ notificationService: { error: mocks.error } })); |
| 28 | + |
| 29 | +describe('message sending settings persistence', () => { |
| 30 | + let root: Root; |
| 31 | + let container: HTMLDivElement; |
| 32 | + const select = () => container.querySelector('select')!; |
| 33 | + const choose = async (value: string) => act(async () => { |
| 34 | + select().value = value; |
| 35 | + select().dispatchEvent(new Event('change', { bubbles: true })); |
| 36 | + }); |
| 37 | + beforeEach(() => { |
| 38 | + globalThis.IS_REACT_ACT_ENVIRONMENT = true; |
| 39 | + activateSurface('local'); |
| 40 | + mocks.get.mockReset().mockResolvedValue(undefined); |
| 41 | + mocks.set.mockReset().mockImplementation(async (_path: string, value: string) => { |
| 42 | + mocks.get.mockResolvedValue(value); |
| 43 | + mocks.listeners.forEach(listener => listener()); |
| 44 | + }); |
| 45 | + mocks.error.mockReset(); |
| 46 | + mocks.listeners.clear(); |
| 47 | + container = document.createElement('div'); |
| 48 | + document.body.append(container); |
| 49 | + root = createRoot(container); |
| 50 | + }); |
| 51 | + afterEach(() => { act(() => root.unmount()); container.remove(); activateSurface('local'); }); |
| 52 | + |
| 53 | + it('starts with immediate and persists both choices using only the preference leaf', async () => { |
| 54 | + await act(async () => root.render(<MessageSendingSettingsSection />)); |
| 55 | + expect(select().value).toBe('immediate'); |
| 56 | + await choose('queue'); |
| 57 | + expect(mocks.set).toHaveBeenLastCalledWith('app.flow_chat.send_during_turn', 'queue'); |
| 58 | + expect(select().value).toBe('queue'); |
| 59 | + await choose('immediate'); |
| 60 | + expect(mocks.set).toHaveBeenLastCalledWith('app.flow_chat.send_during_turn', 'immediate'); |
| 61 | + expect(select().value).toBe('immediate'); |
| 62 | + expect(select().disabled).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + it('retains the saved behavior when persistence fails', async () => { |
| 66 | + mocks.set.mockRejectedValue(new Error('Write failed')); |
| 67 | + await act(async () => root.render(<MessageSendingSettingsSection />)); |
| 68 | + await choose('queue'); |
| 69 | + expect(select().value).toBe('immediate'); |
| 70 | + expect(mocks.error).toHaveBeenCalledWith('messageSending.saveFailed'); |
| 71 | + expect(select().disabled).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('keeps the picker unavailable after a failed read and allows recovery', async () => { |
| 75 | + mocks.get.mockRejectedValue(new Error('Host unavailable')); |
| 76 | + await act(async () => root.render(<MessageSendingSettingsSection />)); |
| 77 | + expect(select()).toBeNull(); |
| 78 | + expect(container.textContent).toContain('messageSending.loadFailed'); |
| 79 | + mocks.get.mockResolvedValue('queue'); |
| 80 | + await act(async () => container.querySelector('button')!.click()); |
| 81 | + expect(select().value).toBe('queue'); |
| 82 | + }); |
| 83 | +}); |
0 commit comments