refactor: 迁移视觉小说与木鱼 runtime 请求骨架

This commit is contained in:
2026-06-03 17:08:38 +08:00
parent 06fabd3eab
commit 4f59a0e791
7 changed files with 214 additions and 103 deletions

View File

@@ -10,10 +10,12 @@ vi.mock('../apiClient', () => ({
requestJson: requestJsonMock,
}));
import type { VisualNovelRunSnapshot } from '../../../packages/shared/src/contracts/visualNovel';
import {
buildVisualNovelRuntimeCheckpoint,
buildVisualNovelSaveArchiveState,
type VisualNovelRuntimeStreamOptions,
getVisualNovelHistory,
getVisualNovelRun,
listVisualNovelGallery,
listVisualNovelSaveArchives,
putVisualNovelRuntimeSnapshot,
@@ -21,8 +23,8 @@ import {
resumeVisualNovelSaveArchive,
startVisualNovelRun,
streamVisualNovelRuntimeAction,
type VisualNovelRuntimeStreamOptions,
} from './visualNovelRuntimeClient';
import type { VisualNovelRunSnapshot } from '../../../packages/shared/src/contracts/visualNovel';
function createMockRun(
overrides: Partial<VisualNovelRunSnapshot> = {},
@@ -108,6 +110,32 @@ test('startVisualNovelRun uses the visual novel runtime work route', async () =>
);
});
test('getVisualNovelRun and getVisualNovelHistory use encoded runtime run routes', async () => {
requestJsonMock
.mockResolvedValueOnce({ run: createMockRun() })
.mockResolvedValueOnce({ entries: [] });
await getVisualNovelRun('vn/run-1');
await getVisualNovelHistory('vn/run-1');
expect(requestJsonMock.mock.calls[0]).toEqual([
'/api/runtime/visual-novel/runs/vn%2Frun-1',
expect.objectContaining({ method: 'GET' }),
'读取视觉小说运行快照失败',
expect.objectContaining({
retry: expect.objectContaining({ maxRetries: 1 }),
}),
]);
expect(requestJsonMock.mock.calls[1]).toEqual([
'/api/runtime/visual-novel/runs/vn%2Frun-1/history',
expect.objectContaining({ method: 'GET' }),
'读取视觉小说历史失败',
expect.objectContaining({
retry: expect.objectContaining({ maxRetries: 1 }),
}),
]);
});
test('streamVisualNovelRuntimeAction posts to the SSE action stream route', async () => {
const response = createSseResponse(
[
@@ -146,6 +174,10 @@ test('streamVisualNovelRuntimeAction posts to the SSE action stream route', asyn
}),
signal: undefined,
}),
expect.objectContaining({
skipAuth: undefined,
skipRefresh: undefined,
}),
);
expect(result).toMatchObject({ runId: 'vn-run-route-1' });
});

View File

@@ -23,12 +23,13 @@ import {
fetchWithApiAuth,
requestJson,
} from '../apiClient';
import { readVisualNovelRuntimeRunFromSse } from './visualNovelRuntimeSse';
import {
buildRuntimeGuestAuthOptions,
buildRuntimeGuestHeaders,
type RuntimeGuestRequestOptions,
} from '../runtimeGuestAuth';
import { buildRuntimeApiPath, requestRuntimeJson } from '../runtimeRequest';
import { readVisualNovelRuntimeRunFromSse } from './visualNovelRuntimeSse';
const VISUAL_NOVEL_RUNTIME_API_BASE = '/api/runtime/visual-novel';
const VISUAL_NOVEL_RUNTIME_READ_RETRY: ApiRetryOptions = {
@@ -57,17 +58,13 @@ export type VisualNovelSaveArchiveResumeResponse =
>;
export async function listVisualNovelGallery() {
return requestJson<VisualNovelWorksResponse>(
`${VISUAL_NOVEL_RUNTIME_API_BASE}/gallery`,
{ method: 'GET' },
'读取视觉小说公开作品列表失败',
{
retry: VISUAL_NOVEL_RUNTIME_READ_RETRY,
// 中文注释:公开广场是游客可读入口,避免未登录态先触发 refresh 再读取公开列表。
skipAuth: true,
skipRefresh: true,
},
);
return requestRuntimeJson<VisualNovelWorksResponse>({
url: buildRuntimeApiPath(VISUAL_NOVEL_RUNTIME_API_BASE, 'gallery'),
fallbackMessage: '读取视觉小说公开作品列表失败',
retry: VISUAL_NOVEL_RUNTIME_READ_RETRY,
// 中文注释:公开广场是游客可读入口,避免未登录态先触发 refresh 再读取公开列表。
requestOptions: { skipAuth: true, skipRefresh: true },
});
}
function buildJsonInit(method: 'POST' | 'PUT', payload: unknown): RequestInit {
@@ -117,7 +114,12 @@ export async function startVisualNovelRun(
) {
const requestOptions = buildRuntimeGuestAuthOptions(options);
return requestJson<VisualNovelRunResponse>(
`${VISUAL_NOVEL_RUNTIME_API_BASE}/works/${encodeURIComponent(profileId)}/runs`,
buildRuntimeApiPath(
VISUAL_NOVEL_RUNTIME_API_BASE,
'works',
profileId,
'runs',
),
{
...buildJsonInit('POST', payload),
headers: buildRuntimeGuestHeaders(options, {
@@ -134,25 +136,24 @@ export async function startVisualNovelRun(
}
export async function getVisualNovelRun(runId: string) {
return requestJson<VisualNovelRunResponse>(
`${VISUAL_NOVEL_RUNTIME_API_BASE}/runs/${encodeURIComponent(runId)}`,
{ method: 'GET' },
'读取视觉小说运行快照失败',
{
retry: VISUAL_NOVEL_RUNTIME_READ_RETRY,
},
);
return requestRuntimeJson<VisualNovelRunResponse>({
url: buildRuntimeApiPath(VISUAL_NOVEL_RUNTIME_API_BASE, 'runs', runId),
fallbackMessage: '读取视觉小说运行快照失败',
retry: VISUAL_NOVEL_RUNTIME_READ_RETRY,
});
}
export async function getVisualNovelHistory(runId: string) {
return requestJson<VisualNovelHistoryResponse>(
`${VISUAL_NOVEL_RUNTIME_API_BASE}/runs/${encodeURIComponent(runId)}/history`,
{ method: 'GET' },
'读取视觉小说历史失败',
{
retry: VISUAL_NOVEL_RUNTIME_READ_RETRY,
},
);
return requestRuntimeJson<VisualNovelHistoryResponse>({
url: buildRuntimeApiPath(
VISUAL_NOVEL_RUNTIME_API_BASE,
'runs',
runId,
'history',
),
fallbackMessage: '读取视觉小说历史失败',
retry: VISUAL_NOVEL_RUNTIME_READ_RETRY,
});
}
export async function streamVisualNovelRuntimeAction(
@@ -161,7 +162,13 @@ export async function streamVisualNovelRuntimeAction(
options: VisualNovelRuntimeStreamOptions = {},
) {
const response = await openVisualNovelRuntimeSsePost(
`${VISUAL_NOVEL_RUNTIME_API_BASE}/runs/${encodeURIComponent(runId)}/actions/stream`,
buildRuntimeApiPath(
VISUAL_NOVEL_RUNTIME_API_BASE,
'runs',
runId,
'actions',
'stream',
),
payload,
'推进视觉小说失败',
options.signal,
@@ -179,14 +186,18 @@ export async function regenerateVisualNovelRun(
runId: string,
payload: VisualNovelRegenerateRequest,
) {
return requestJson<VisualNovelRunResponse>(
`${VISUAL_NOVEL_RUNTIME_API_BASE}/runs/${encodeURIComponent(runId)}/regenerate`,
buildJsonInit('POST', payload),
'重生成视觉小说历史失败',
{
retry: VISUAL_NOVEL_RUNTIME_WRITE_RETRY,
},
);
return requestRuntimeJson<VisualNovelRunResponse>({
url: buildRuntimeApiPath(
VISUAL_NOVEL_RUNTIME_API_BASE,
'runs',
runId,
'regenerate',
),
method: 'POST',
jsonBody: payload,
fallbackMessage: '重生成视觉小说历史失败',
retry: VISUAL_NOVEL_RUNTIME_WRITE_RETRY,
});
}
export async function listVisualNovelSaveArchives(profileId?: string | null) {