import { describe, expect, it, vi } from 'vitest'; import { clearPuzzleRuntimeUrlState, readPuzzleRuntimeUrlState, writePuzzleRuntimeUrlState, } from './puzzleRuntimeUrlState'; describe('puzzleRuntimeUrlState', () => { it('writes puzzle runtime identity on runtime paths', () => { const replaceState = vi.fn(); const env = { location: { pathname: '/runtime/puzzle', search: '?clientRuntime=wechat_mini_program', }, history: { replaceState }, }; writePuzzleRuntimeUrlState( { runtimeSessionId: 'puzzle-session-1', runtimeProfileId: 'puzzle-profile-1', runtimeLevelId: 'puzzle-level-2', publicWorkCode: 'PZ-12345678', mode: 'draft', }, env, ); expect(replaceState).toHaveBeenCalledWith( null, '', '/runtime/puzzle?clientRuntime=wechat_mini_program&runtimeProfileId=puzzle-profile-1&runtimeSessionId=puzzle-session-1&runtimeLevelId=puzzle-level-2&work=PZ-12345678&mode=draft', ); expect( readPuzzleRuntimeUrlState({ location: { pathname: '/runtime/puzzle', search: '?runtimeProfileId=puzzle-profile-1&runtimeSessionId=puzzle-session-1&runtimeLevelId=puzzle-level-2&work=PZ-12345678&mode=draft', }, }), ).toEqual({ runtimeProfileId: 'puzzle-profile-1', runtimeSessionId: 'puzzle-session-1', runtimeLevelId: 'puzzle-level-2', publicWorkCode: 'PZ-12345678', mode: 'draft', }); }); it('ignores writes outside puzzle runtime paths', () => { const replaceState = vi.fn(); const env = { location: { pathname: '/creation/puzzle/result', search: '?sessionId=puzzle-session-1', }, history: { replaceState }, }; writePuzzleRuntimeUrlState({ runtimeSessionId: 'puzzle-session-2' }, env); expect(replaceState).not.toHaveBeenCalled(); }); it('can write runtime state to an explicit puzzle runtime pathname', () => { const replaceState = vi.fn(); const env = { location: { pathname: '/creation/puzzle/result', search: '?clientRuntime=wechat_mini_program', }, history: { replaceState }, }; writePuzzleRuntimeUrlState( { runtimeProfileId: 'puzzle-profile-1', mode: 'published', publicWorkCode: 'PZ-12345678', }, env, { pathname: '/runtime/puzzle' }, ); expect(replaceState).toHaveBeenCalledWith( null, '', '/runtime/puzzle?clientRuntime=wechat_mini_program&runtimeProfileId=puzzle-profile-1&work=PZ-12345678&mode=published', ); }); it('clears only puzzle runtime restore params on runtime paths', () => { const replaceState = vi.fn(); const env = { location: { pathname: '/runtime/puzzle', search: '?runtimeSessionId=puzzle-session-1&runtimeProfileId=puzzle-profile-1&runtimeLevelId=puzzle-level-1&work=PZ-12345678&mode=draft&clientRuntime=wechat', }, history: { replaceState }, }; clearPuzzleRuntimeUrlState(env); expect(replaceState).toHaveBeenCalledWith( null, '', '/runtime/puzzle?clientRuntime=wechat', ); }); });