fix: restore puzzle runtime url state

This commit is contained in:
2026-05-25 22:52:38 +08:00
parent 30cf8abbf7
commit eb6ab404e2
12 changed files with 1917 additions and 177 deletions

View File

@@ -0,0 +1,85 @@
import { describe, expect, it, vi } from 'vitest';
import {
clearCreationUrlState,
readCreationUrlState,
writeCreationUrlState,
} from './creationUrlState';
describe('creationUrlState', () => {
it('writes and reads restore state on creation restore paths', () => {
const replaceState = vi.fn();
const env = {
location: {
pathname: '/creation/puzzle/result',
search: '?clientRuntime=wechat_mini_program',
},
history: { replaceState },
};
writeCreationUrlState(
{
sessionId: ' session-1 ',
profileId: 'profile-1',
draftId: 'draft-1',
workId: 'work-1',
},
env,
);
expect(replaceState).toHaveBeenCalledWith(
null,
'',
'/creation/puzzle/result?clientRuntime=wechat_mini_program&sessionId=session-1&profileId=profile-1&draftId=draft-1&workId=work-1',
);
expect(
readCreationUrlState({
location: {
pathname: '/creation/puzzle/result',
search:
'?sessionId=session-1&profileId=profile-1&draftId=draft-1&workId=work-1',
},
}),
).toEqual({
sessionId: 'session-1',
profileId: 'profile-1',
draftId: 'draft-1',
workId: 'work-1',
});
});
it('ignores writes and clears outside creation restore paths', () => {
const replaceState = vi.fn();
const env = {
location: {
pathname: '/works/detail',
search: '?work=PZ-123&sessionId=session-1',
},
history: { replaceState },
};
writeCreationUrlState({ sessionId: 'session-2' }, env);
clearCreationUrlState(env);
expect(replaceState).not.toHaveBeenCalled();
});
it('clears only private restore params on creation restore paths', () => {
const replaceState = vi.fn();
const env = {
location: {
pathname: '/creation/bark-battle/result',
search: '?draftId=draft-1&workId=work-1&clientRuntime=wechat',
},
history: { replaceState },
};
clearCreationUrlState(env);
expect(replaceState).toHaveBeenCalledWith(
null,
'',
'/creation/bark-battle/result?clientRuntime=wechat',
);
});
});