Merge branch 'master' of https://git.genarrative.world/GenarrativeAI/Genarrative
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -300,6 +300,161 @@ function ActionCompleteHarness({
|
||||
);
|
||||
}
|
||||
|
||||
function SessionChangeHarness({
|
||||
onSessionChanged,
|
||||
}: {
|
||||
onSessionChanged: (session: TestSession | null) => void;
|
||||
}) {
|
||||
const flow = usePlatformCreationAgentFlowController<
|
||||
TestSession,
|
||||
Record<string, never>,
|
||||
{ session: TestSession },
|
||||
TestMessagePayload,
|
||||
{ action: string },
|
||||
{ session: TestSession }
|
||||
>({
|
||||
client: {
|
||||
createSession: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-open',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
getSession: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-restore',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
streamMessage: async () => ({
|
||||
sessionId: 'session-open',
|
||||
messages: [],
|
||||
}),
|
||||
executeAction: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-compile',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
selectSession: (response) => response.session,
|
||||
},
|
||||
createPayload: {},
|
||||
workspaceStage: 'match3d-agent-workspace',
|
||||
resultStage: 'match3d-result',
|
||||
platformStage: 'platform',
|
||||
isCompileAction: (payload) => payload.action === 'match3d_compile_draft',
|
||||
resolveErrorMessage: (error, fallback) =>
|
||||
error instanceof Error ? error.message : fallback,
|
||||
errorMessages: {
|
||||
open: '打开失败',
|
||||
restoreMissingSession: '缺少会话',
|
||||
restore: '恢复失败',
|
||||
submit: '发送失败',
|
||||
execute: '执行失败',
|
||||
},
|
||||
enterCreateTab: () => {},
|
||||
setSelectionStage: () => {},
|
||||
onSessionChanged,
|
||||
onActionComplete: ({ response, setSession }) => {
|
||||
setSession(response.session);
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
<button type="button" onClick={() => void flow.openWorkspace({})}>
|
||||
打开
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void flow.restoreDraft('session-restore')}
|
||||
>
|
||||
恢复
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
void flow.executeAction({ action: 'match3d_compile_draft' })
|
||||
}
|
||||
>
|
||||
编译
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function SessionSetterIdentityHarness({
|
||||
onSetterIdentity,
|
||||
}: {
|
||||
onSetterIdentity: (setter: unknown) => void;
|
||||
}) {
|
||||
const [renderCount, setRenderCount] = useState(0);
|
||||
const flow = usePlatformCreationAgentFlowController<
|
||||
TestSession,
|
||||
Record<string, never>,
|
||||
{ session: TestSession },
|
||||
TestMessagePayload,
|
||||
{ action: string },
|
||||
{ session: TestSession }
|
||||
>({
|
||||
client: {
|
||||
createSession: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-open',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
getSession: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-restore',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
streamMessage: async () => ({
|
||||
sessionId: 'session-open',
|
||||
messages: [],
|
||||
}),
|
||||
executeAction: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-compile',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
selectSession: (response) => response.session,
|
||||
},
|
||||
createPayload: {},
|
||||
workspaceStage: 'match3d-agent-workspace',
|
||||
resultStage: 'match3d-result',
|
||||
platformStage: 'platform',
|
||||
isCompileAction: () => false,
|
||||
resolveErrorMessage: (error, fallback) =>
|
||||
error instanceof Error ? error.message : fallback,
|
||||
errorMessages: {
|
||||
open: '打开失败',
|
||||
restoreMissingSession: '缺少会话',
|
||||
restore: '恢复失败',
|
||||
submit: '发送失败',
|
||||
execute: '执行失败',
|
||||
},
|
||||
enterCreateTab: () => {},
|
||||
setSelectionStage: () => {},
|
||||
onSessionChanged: () => {},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
onSetterIdentity(flow.setSession);
|
||||
});
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setRenderCount((current) => current + 1)}
|
||||
>
|
||||
重渲染 {renderCount}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
test('creation agent flow preserves streamed assistant text when stream fails', async () => {
|
||||
const streamMessage = vi.fn(async (_sessionId, _payload, options) => {
|
||||
options?.onUpdate?.('先把方洞万能的反差定住。');
|
||||
@@ -391,3 +546,48 @@ test('creation agent flow suppresses compile result stage for background complet
|
||||
'match3d-agent-workspace',
|
||||
);
|
||||
});
|
||||
|
||||
test('creation agent flow notifies session changes after open restore and compile', async () => {
|
||||
const onSessionChanged = vi.fn();
|
||||
|
||||
render(<SessionChangeHarness onSessionChanged={onSessionChanged} />);
|
||||
|
||||
await act(async () => {
|
||||
screen.getByRole('button', { name: '打开' }).click();
|
||||
});
|
||||
await act(async () => {
|
||||
screen.getByRole('button', { name: '恢复' }).click();
|
||||
});
|
||||
await act(async () => {
|
||||
screen.getByRole('button', { name: '编译' }).click();
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSessionChanged).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
expect(
|
||||
onSessionChanged.mock.calls.map(([session]) => session?.sessionId),
|
||||
).toEqual(['session-open', 'session-restore', 'session-compile']);
|
||||
});
|
||||
|
||||
test('creation agent flow keeps session setter stable across parent rerenders', async () => {
|
||||
const onSetterIdentity = vi.fn();
|
||||
|
||||
render(<SessionSetterIdentityHarness onSetterIdentity={onSetterIdentity} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSetterIdentity).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
const initialSetter = onSetterIdentity.mock.calls[0]?.[0];
|
||||
|
||||
await act(async () => {
|
||||
screen.getByRole('button', { name: /重渲染/u }).click();
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSetterIdentity).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
expect(onSetterIdentity.mock.calls[1]?.[0]).toBe(initialSetter);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useCallback, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import type { Dispatch, SetStateAction } from 'react';
|
||||
|
||||
import type { TextStreamOptions } from '../../services/aiTypes';
|
||||
import type { SelectionStage } from './platformEntryTypes';
|
||||
@@ -75,12 +76,13 @@ type PlatformCreationAgentFlowControllerOptions<
|
||||
enterCreateTab: () => void;
|
||||
setSelectionStage: (stage: SelectionStage) => void;
|
||||
onSessionOpened?: () => void;
|
||||
onSessionChanged?: (session: TSession | null) => void;
|
||||
onOpenError?: (params: { error: unknown; errorMessage: string }) => void;
|
||||
onActionComplete?: (params: {
|
||||
payload: TActionPayload;
|
||||
response: TActionResponse;
|
||||
session: TSession;
|
||||
setSession: (session: TSession) => void;
|
||||
setSession: Dispatch<SetStateAction<TSession | null>>;
|
||||
}) =>
|
||||
| Promise<{ openResult?: boolean } | void>
|
||||
| { openResult?: boolean }
|
||||
@@ -94,7 +96,7 @@ type PlatformCreationAgentFlowControllerOptions<
|
||||
error: unknown;
|
||||
errorMessage: string;
|
||||
session: TSession;
|
||||
setSession: (session: TSession) => void;
|
||||
setSession: Dispatch<SetStateAction<TSession | null>>;
|
||||
}) => void | Promise<void>;
|
||||
};
|
||||
|
||||
@@ -141,12 +143,27 @@ export function usePlatformCreationAgentFlowController<
|
||||
TActionResponse
|
||||
>,
|
||||
) {
|
||||
const [session, setSession] = useState<TSession | null>(null);
|
||||
const [session, rawSetSession] = useState<TSession | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [isBusy, setIsBusy] = useState(false);
|
||||
const [streamingReplyText, setStreamingReplyText] = useState('');
|
||||
const [isStreamingReply, setIsStreamingReply] = useState(false);
|
||||
const latestStreamingReplyTextRef = useRef('');
|
||||
const onSessionChangedRef = useRef(options.onSessionChanged);
|
||||
|
||||
useEffect(() => {
|
||||
onSessionChangedRef.current = options.onSessionChanged;
|
||||
}, [options.onSessionChanged]);
|
||||
|
||||
const setSession = useCallback(
|
||||
(nextSessionOrUpdater: SetStateAction<TSession | null>) => {
|
||||
rawSetSession(nextSessionOrUpdater);
|
||||
if (typeof nextSessionOrUpdater !== 'function') {
|
||||
onSessionChangedRef.current?.(nextSessionOrUpdater);
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const updateStreamingReplyText = useCallback((text: string) => {
|
||||
latestStreamingReplyTextRef.current = text;
|
||||
@@ -174,10 +191,10 @@ export function usePlatformCreationAgentFlowController<
|
||||
createPayload ?? options.createPayload,
|
||||
);
|
||||
const nextSession = options.client.selectSession(response);
|
||||
setSession(nextSession);
|
||||
options.enterCreateTab();
|
||||
options.onSessionOpened?.();
|
||||
options.setSelectionStage(options.workspaceStage);
|
||||
setSession(nextSession);
|
||||
return nextSession;
|
||||
} catch (caughtError) {
|
||||
const errorMessage = options.resolveErrorMessage(
|
||||
@@ -212,11 +229,11 @@ export function usePlatformCreationAgentFlowController<
|
||||
try {
|
||||
const response = await options.client.getSession(normalizedSessionId);
|
||||
const nextSession = options.client.selectSession(response);
|
||||
setSession(nextSession);
|
||||
options.enterCreateTab();
|
||||
options.setSelectionStage(
|
||||
nextSession.draft ? options.resultStage : options.workspaceStage,
|
||||
);
|
||||
setSession(nextSession);
|
||||
return nextSession;
|
||||
} catch (caughtError) {
|
||||
setError(
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,9 @@
|
||||
/* @vitest-environment jsdom */
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import {
|
||||
pushAppHistoryPath,
|
||||
resolvePathForSelectionStage,
|
||||
resolveSelectionStageFromPath,
|
||||
} from './appPageRoutes';
|
||||
@@ -117,4 +120,43 @@ describe('appPageRoutes', () => {
|
||||
'/creation/baby-object-match',
|
||||
);
|
||||
});
|
||||
|
||||
it('preserves creation restore query params within the same creation flow', () => {
|
||||
window.history.replaceState(
|
||||
null,
|
||||
'',
|
||||
'/creation/rpg?sessionId=session-1&profileId=profile-1&draftId=draft-1&workId=work-1&clientRuntime=wechat_mini_program',
|
||||
);
|
||||
|
||||
pushAppHistoryPath('/creation/rpg/result');
|
||||
|
||||
expect(window.location.pathname).toBe('/creation/rpg/result');
|
||||
expect(window.location.search).toBe(
|
||||
'?sessionId=session-1&profileId=profile-1&draftId=draft-1&workId=work-1',
|
||||
);
|
||||
});
|
||||
|
||||
it('clears creation restore query params when leaving the flow or switching flows', () => {
|
||||
window.history.replaceState(
|
||||
null,
|
||||
'',
|
||||
'/creation/rpg?sessionId=session-1&profileId=profile-1',
|
||||
);
|
||||
|
||||
pushAppHistoryPath('/creation/puzzle');
|
||||
|
||||
expect(window.location.pathname).toBe('/creation/puzzle');
|
||||
expect(window.location.search).toBe('');
|
||||
|
||||
window.history.replaceState(
|
||||
null,
|
||||
'',
|
||||
'/creation/rpg?sessionId=session-2&profileId=profile-2',
|
||||
);
|
||||
|
||||
pushAppHistoryPath('/');
|
||||
|
||||
expect(window.location.pathname).toBe('/');
|
||||
expect(window.location.search).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import type { SelectionStage } from '../components/platform-entry';
|
||||
import {
|
||||
buildCreationUrlSearchFromParams,
|
||||
isCreationRestorePath,
|
||||
isSameCreationFlowPath,
|
||||
} from '../services/creationUrlState';
|
||||
|
||||
export type RuntimePageRoute = 'rpg-character-select' | 'rpg-adventure';
|
||||
|
||||
@@ -130,7 +135,14 @@ export function isKnownMainAppPagePath(pathname: string) {
|
||||
export function pushAppHistoryPath(path: string) {
|
||||
const nextUrl = new URL(path, window.location.origin);
|
||||
const normalizedPath = normalizeAppPath(nextUrl.pathname);
|
||||
const nextRelativeUrl = `${normalizedPath}${nextUrl.search}`;
|
||||
const nextSearch =
|
||||
nextUrl.search ||
|
||||
buildPreservedAppSearch(
|
||||
window.location.pathname,
|
||||
normalizedPath,
|
||||
window.location.search,
|
||||
);
|
||||
const nextRelativeUrl = `${normalizedPath}${nextSearch}`;
|
||||
const currentRelativeUrl = `${normalizeAppPath(window.location.pathname)}${window.location.search}`;
|
||||
if (currentRelativeUrl === nextRelativeUrl) {
|
||||
return;
|
||||
@@ -139,3 +151,18 @@ export function pushAppHistoryPath(path: string) {
|
||||
// 页面阶段变化是用户可感知导航,写入 history 以支持前进后退。
|
||||
window.history.pushState(null, '', nextRelativeUrl);
|
||||
}
|
||||
|
||||
function buildPreservedAppSearch(
|
||||
currentPathname: string,
|
||||
normalizedPath: string,
|
||||
search: string,
|
||||
) {
|
||||
if (
|
||||
!isCreationRestorePath(normalizedPath) ||
|
||||
!isSameCreationFlowPath(currentPathname, normalizedPath)
|
||||
) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return buildCreationUrlSearchFromParams(search);
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,220 @@
|
||||
import {
|
||||
CUSTOM_WORLD_AGENT_OPERATION_QUERY_KEY,
|
||||
CUSTOM_WORLD_AGENT_SESSION_QUERY_KEY,
|
||||
CUSTOM_WORLD_GENERATION_SOURCE_QUERY_KEY,
|
||||
} from './customWorldAgentUiState';
|
||||
|
||||
export const CREATION_URL_SESSION_QUERY_KEY = 'sessionId';
|
||||
export const CREATION_URL_PROFILE_QUERY_KEY = 'profileId';
|
||||
export const CREATION_URL_DRAFT_QUERY_KEY = 'draftId';
|
||||
export const CREATION_URL_WORK_QUERY_KEY = 'workId';
|
||||
|
||||
export const CREATION_URL_RESTORE_QUERY_KEYS = [
|
||||
CUSTOM_WORLD_AGENT_SESSION_QUERY_KEY,
|
||||
CUSTOM_WORLD_AGENT_OPERATION_QUERY_KEY,
|
||||
CUSTOM_WORLD_GENERATION_SOURCE_QUERY_KEY,
|
||||
CREATION_URL_SESSION_QUERY_KEY,
|
||||
CREATION_URL_PROFILE_QUERY_KEY,
|
||||
CREATION_URL_DRAFT_QUERY_KEY,
|
||||
CREATION_URL_WORK_QUERY_KEY,
|
||||
] as const;
|
||||
|
||||
export type CreationUrlState = {
|
||||
sessionId?: string | null;
|
||||
profileId?: string | null;
|
||||
draftId?: string | null;
|
||||
workId?: string | null;
|
||||
};
|
||||
|
||||
type CreationUrlEnvironment = {
|
||||
location?: {
|
||||
pathname: string;
|
||||
search: string;
|
||||
} | null;
|
||||
history?: {
|
||||
replaceState: (
|
||||
data: unknown,
|
||||
unused: string,
|
||||
url?: string | URL | null,
|
||||
) => void;
|
||||
} | null;
|
||||
};
|
||||
|
||||
const CREATION_PATH_PREFIXES = [
|
||||
'/creation/rpg',
|
||||
'/creation/big-fish',
|
||||
'/creation/match3d',
|
||||
'/creation/square-hole',
|
||||
'/creation/jump-hop',
|
||||
'/creation/wooden-fish',
|
||||
'/creation/bark-battle',
|
||||
'/creation/visual-novel',
|
||||
'/creation/baby-object-match',
|
||||
'/creation/puzzle',
|
||||
] as const;
|
||||
|
||||
function resolveEnvironment(
|
||||
env?: CreationUrlEnvironment,
|
||||
): Required<CreationUrlEnvironment> {
|
||||
if (env) {
|
||||
return {
|
||||
location: env.location ?? null,
|
||||
history: env.history ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
return {
|
||||
location: null,
|
||||
history: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
location: window.location,
|
||||
history: window.history,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeValue(value: unknown) {
|
||||
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
||||
}
|
||||
|
||||
function normalizePathname(value: string | undefined) {
|
||||
const pathname = value?.trim().toLowerCase() ?? '';
|
||||
if (!pathname || pathname === '/') {
|
||||
return '/';
|
||||
}
|
||||
|
||||
return pathname.replace(/\/+$/u, '');
|
||||
}
|
||||
|
||||
export function isCreationRestorePath(pathname: string | undefined) {
|
||||
const normalizedPathname = normalizePathname(pathname);
|
||||
return CREATION_PATH_PREFIXES.some(
|
||||
(pathPrefix) =>
|
||||
normalizedPathname === pathPrefix ||
|
||||
normalizedPathname.startsWith(`${pathPrefix}/`),
|
||||
);
|
||||
}
|
||||
|
||||
export function isSameCreationFlowPath(
|
||||
currentPathname: string | undefined,
|
||||
nextPathname: string | undefined,
|
||||
) {
|
||||
const normalizedCurrentPath = normalizePathname(currentPathname);
|
||||
const normalizedNextPath = normalizePathname(nextPathname);
|
||||
if (
|
||||
!normalizedCurrentPath ||
|
||||
!normalizedNextPath ||
|
||||
normalizedCurrentPath === '/' ||
|
||||
normalizedNextPath === '/'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentCreationPrefix = CREATION_PATH_PREFIXES.find((pathPrefix) =>
|
||||
normalizedCurrentPath === pathPrefix ||
|
||||
normalizedCurrentPath.startsWith(`${pathPrefix}/`),
|
||||
);
|
||||
const nextCreationPrefix = CREATION_PATH_PREFIXES.find((pathPrefix) =>
|
||||
normalizedNextPath === pathPrefix ||
|
||||
normalizedNextPath.startsWith(`${pathPrefix}/`),
|
||||
);
|
||||
return Boolean(
|
||||
currentCreationPrefix &&
|
||||
nextCreationPrefix &&
|
||||
currentCreationPrefix === nextCreationPrefix,
|
||||
);
|
||||
}
|
||||
|
||||
export function buildCreationUrlSearchFromParams(search: string) {
|
||||
const params = new URLSearchParams(search);
|
||||
const preservedParams = new URLSearchParams();
|
||||
|
||||
CREATION_URL_RESTORE_QUERY_KEYS.forEach((key) => {
|
||||
const value = normalizeValue(params.get(key));
|
||||
if (value) {
|
||||
preservedParams.set(key, value);
|
||||
}
|
||||
});
|
||||
|
||||
const queryString = preservedParams.toString();
|
||||
return queryString ? `?${queryString}` : '';
|
||||
}
|
||||
|
||||
export function readCreationUrlState(
|
||||
env?: CreationUrlEnvironment,
|
||||
): CreationUrlState {
|
||||
const resolved = resolveEnvironment(env);
|
||||
const params = new URLSearchParams(resolved.location?.search ?? '');
|
||||
|
||||
return {
|
||||
sessionId: normalizeValue(params.get(CREATION_URL_SESSION_QUERY_KEY)),
|
||||
profileId: normalizeValue(params.get(CREATION_URL_PROFILE_QUERY_KEY)),
|
||||
draftId: normalizeValue(params.get(CREATION_URL_DRAFT_QUERY_KEY)),
|
||||
workId: normalizeValue(params.get(CREATION_URL_WORK_QUERY_KEY)),
|
||||
};
|
||||
}
|
||||
|
||||
export function writeCreationUrlState(
|
||||
state: CreationUrlState,
|
||||
env?: CreationUrlEnvironment,
|
||||
) {
|
||||
const resolved = resolveEnvironment(env);
|
||||
if (
|
||||
!resolved.location ||
|
||||
!resolved.history?.replaceState ||
|
||||
!isCreationRestorePath(resolved.location.pathname)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const params = new URLSearchParams(resolved.location.search);
|
||||
const entries = [
|
||||
[CREATION_URL_SESSION_QUERY_KEY, state.sessionId],
|
||||
[CREATION_URL_PROFILE_QUERY_KEY, state.profileId],
|
||||
[CREATION_URL_DRAFT_QUERY_KEY, state.draftId],
|
||||
[CREATION_URL_WORK_QUERY_KEY, state.workId],
|
||||
] as const;
|
||||
|
||||
entries.forEach(([key, rawValue]) => {
|
||||
const value = normalizeValue(rawValue);
|
||||
if (value) {
|
||||
params.set(key, value);
|
||||
} else {
|
||||
params.delete(key);
|
||||
}
|
||||
});
|
||||
|
||||
const search = params.toString();
|
||||
const nextUrl = search
|
||||
? `${resolved.location.pathname}?${search}`
|
||||
: resolved.location.pathname;
|
||||
resolved.history.replaceState(null, '', nextUrl);
|
||||
}
|
||||
|
||||
export function clearCreationUrlState(env?: CreationUrlEnvironment) {
|
||||
const resolved = resolveEnvironment(env);
|
||||
if (
|
||||
!resolved.location ||
|
||||
!resolved.history?.replaceState ||
|
||||
!isCreationRestorePath(resolved.location.pathname)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const params = new URLSearchParams(resolved.location.search);
|
||||
[
|
||||
CREATION_URL_SESSION_QUERY_KEY,
|
||||
CREATION_URL_PROFILE_QUERY_KEY,
|
||||
CREATION_URL_DRAFT_QUERY_KEY,
|
||||
CREATION_URL_WORK_QUERY_KEY,
|
||||
].forEach((key) => params.delete(key));
|
||||
|
||||
const search = params.toString();
|
||||
const nextUrl = search
|
||||
? `${resolved.location.pathname}?${search}`
|
||||
: resolved.location.pathname;
|
||||
resolved.history.replaceState(null, '', nextUrl);
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
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',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,155 @@
|
||||
export const PUZZLE_RUNTIME_WORK_QUERY_KEY = 'work';
|
||||
export const PUZZLE_RUNTIME_PROFILE_QUERY_KEY = 'runtimeProfileId';
|
||||
export const PUZZLE_RUNTIME_SESSION_QUERY_KEY = 'runtimeSessionId';
|
||||
export const PUZZLE_RUNTIME_LEVEL_QUERY_KEY = 'runtimeLevelId';
|
||||
export const PUZZLE_RUNTIME_MODE_QUERY_KEY = 'mode';
|
||||
|
||||
export type PuzzleRuntimeUrlMode = 'draft' | 'published';
|
||||
|
||||
export type PuzzleRuntimeUrlState = {
|
||||
runtimeProfileId?: string | null;
|
||||
runtimeSessionId?: string | null;
|
||||
runtimeLevelId?: string | null;
|
||||
publicWorkCode?: string | null;
|
||||
mode?: PuzzleRuntimeUrlMode | null;
|
||||
};
|
||||
|
||||
type PuzzleRuntimeUrlEnvironment = {
|
||||
location?: {
|
||||
pathname: string;
|
||||
search: string;
|
||||
} | null;
|
||||
history?: {
|
||||
replaceState: (
|
||||
data: unknown,
|
||||
unused: string,
|
||||
url?: string | URL | null,
|
||||
) => void;
|
||||
} | null;
|
||||
};
|
||||
|
||||
type WritePuzzleRuntimeUrlOptions = {
|
||||
pathname?: string;
|
||||
};
|
||||
|
||||
function resolveEnvironment(
|
||||
env?: PuzzleRuntimeUrlEnvironment,
|
||||
): Required<PuzzleRuntimeUrlEnvironment> {
|
||||
if (env) {
|
||||
return {
|
||||
location: env.location ?? null,
|
||||
history: env.history ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
return {
|
||||
location: null,
|
||||
history: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
location: window.location,
|
||||
history: window.history,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeValue(value: unknown) {
|
||||
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
||||
}
|
||||
|
||||
function normalizeRuntimeMode(value: unknown): PuzzleRuntimeUrlMode | null {
|
||||
const normalized = normalizeValue(value);
|
||||
return normalized === 'draft' || normalized === 'published'
|
||||
? normalized
|
||||
: null;
|
||||
}
|
||||
|
||||
function isPuzzleRuntimePath(pathname: string | undefined) {
|
||||
return pathname?.trim().toLowerCase().replace(/\/+$/u, '') === '/runtime/puzzle';
|
||||
}
|
||||
|
||||
export function readPuzzleRuntimeUrlState(
|
||||
env?: PuzzleRuntimeUrlEnvironment,
|
||||
): PuzzleRuntimeUrlState {
|
||||
const resolved = resolveEnvironment(env);
|
||||
const params = new URLSearchParams(resolved.location?.search ?? '');
|
||||
|
||||
return {
|
||||
runtimeProfileId: normalizeValue(
|
||||
params.get(PUZZLE_RUNTIME_PROFILE_QUERY_KEY),
|
||||
),
|
||||
runtimeSessionId: normalizeValue(
|
||||
params.get(PUZZLE_RUNTIME_SESSION_QUERY_KEY),
|
||||
),
|
||||
runtimeLevelId: normalizeValue(params.get(PUZZLE_RUNTIME_LEVEL_QUERY_KEY)),
|
||||
publicWorkCode: normalizeValue(params.get(PUZZLE_RUNTIME_WORK_QUERY_KEY)),
|
||||
mode: normalizeRuntimeMode(params.get(PUZZLE_RUNTIME_MODE_QUERY_KEY)),
|
||||
};
|
||||
}
|
||||
|
||||
export function writePuzzleRuntimeUrlState(
|
||||
state: PuzzleRuntimeUrlState,
|
||||
env?: PuzzleRuntimeUrlEnvironment,
|
||||
options: WritePuzzleRuntimeUrlOptions = {},
|
||||
) {
|
||||
const resolved = resolveEnvironment(env);
|
||||
const pathname = options.pathname ?? resolved.location?.pathname;
|
||||
if (
|
||||
!resolved.location ||
|
||||
!resolved.history?.replaceState ||
|
||||
!isPuzzleRuntimePath(pathname)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const params = new URLSearchParams(resolved.location.search);
|
||||
const entries = [
|
||||
[PUZZLE_RUNTIME_PROFILE_QUERY_KEY, state.runtimeProfileId],
|
||||
[PUZZLE_RUNTIME_SESSION_QUERY_KEY, state.runtimeSessionId],
|
||||
[PUZZLE_RUNTIME_LEVEL_QUERY_KEY, state.runtimeLevelId],
|
||||
[PUZZLE_RUNTIME_WORK_QUERY_KEY, state.publicWorkCode],
|
||||
[PUZZLE_RUNTIME_MODE_QUERY_KEY, state.mode],
|
||||
] as const;
|
||||
|
||||
entries.forEach(([key, rawValue]) => {
|
||||
const value = normalizeValue(rawValue);
|
||||
if (value) {
|
||||
params.set(key, value);
|
||||
} else {
|
||||
params.delete(key);
|
||||
}
|
||||
});
|
||||
|
||||
const search = params.toString();
|
||||
const nextPathname = pathname ?? resolved.location.pathname;
|
||||
const nextUrl = search ? `${nextPathname}?${search}` : nextPathname;
|
||||
resolved.history.replaceState(null, '', nextUrl);
|
||||
}
|
||||
|
||||
export function clearPuzzleRuntimeUrlState(env?: PuzzleRuntimeUrlEnvironment) {
|
||||
const resolved = resolveEnvironment(env);
|
||||
if (
|
||||
!resolved.location ||
|
||||
!resolved.history?.replaceState ||
|
||||
!isPuzzleRuntimePath(resolved.location.pathname)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const params = new URLSearchParams(resolved.location.search);
|
||||
[
|
||||
PUZZLE_RUNTIME_PROFILE_QUERY_KEY,
|
||||
PUZZLE_RUNTIME_SESSION_QUERY_KEY,
|
||||
PUZZLE_RUNTIME_LEVEL_QUERY_KEY,
|
||||
PUZZLE_RUNTIME_WORK_QUERY_KEY,
|
||||
PUZZLE_RUNTIME_MODE_QUERY_KEY,
|
||||
].forEach((key) => params.delete(key));
|
||||
|
||||
const search = params.toString();
|
||||
const nextUrl = search
|
||||
? `${resolved.location.pathname}?${search}`
|
||||
: resolved.location.pathname;
|
||||
resolved.history.replaceState(null, '', nextUrl);
|
||||
}
|
||||
Reference in New Issue
Block a user