import { describe, expect, it } from 'vitest'; import { canLeaveCurrentPage, getSettingsReturnPath, getSettingsReturnState } from '@/lib/settings-navigation'; describe('global settings return navigation', () => { it.each(['/module-select', '/chat?session=story', '/cloud-agents?view=channels#account', '/image-canvas?workspace=canvas'])( 'returns to the full local origin %s', path => { const url = new URL(path, 'https://makelore.local'); const state = getSettingsReturnState({ pathname: url.pathname, search: url.search, hash: url.hash }); expect(getSettingsReturnPath(state)).toBe(path); }); it.each([null, {}, { settingsReturnTo: '/settings' }, { settingsReturnTo: '//outside.example' }, { settingsReturnTo: 'https://outside.example/chat' }, { settingsReturnTo: '/unknown' }, { settingsReturnTo: '/\\outside.example' }])( 'uses the module homepage for an absent or invalid origin', state => { expect(getSettingsReturnPath(state)).toBe('/module-select'); }); it('keeps the origin when settings is opened again from its own account menu', () => { expect(getSettingsReturnState({ pathname: '/settings', state: { settingsReturnTo: '/cloud-agents?view=mine' } })) .toEqual({ settingsReturnTo: '/cloud-agents?view=mine' }); }); it('honors the page leave guard without discarding or saving input', () => { const protect = (event: Event) => event.preventDefault(); window.addEventListener('beforeunload', protect); try { expect(canLeaveCurrentPage()).toBe(false); } finally { window.removeEventListener('beforeunload', protect); } expect(canLeaveCurrentPage()).toBe(true); }); });