Files
Genarrative/src/services/runtimeRequest.test.ts

89 lines
2.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const apiClientMocks = vi.hoisted(() => ({
requestJson: vi.fn(),
}));
vi.mock('./apiClient', async () => {
const actual =
await vi.importActual<typeof import('./apiClient')>('./apiClient');
return {
...actual,
requestJson: apiClientMocks.requestJson,
};
});
import {
buildRuntimeApiPath,
requestRuntimeJson,
} from './runtimeRequest';
describe('runtimeRequest', () => {
beforeEach(() => {
vi.clearAllMocks();
apiClientMocks.requestJson.mockResolvedValue({ ok: true });
});
it('builds encoded runtime api paths', () => {
expect(buildRuntimeApiPath('/api/runtime/demo/', 'work/a b', 'run/1')).toBe(
'/api/runtime/demo/work%2Fa%20b/run%2F1',
);
});
it('sends json runtime requests with guest auth and retry options', async () => {
const retry = { maxRetries: 1, retryUnsafeMethods: true };
await requestRuntimeJson({
url: '/api/runtime/demo/runs',
method: 'POST',
jsonBody: { profileId: 'profile-1' },
fallbackMessage: '启动失败',
retry,
requestOptions: {
runtimeGuestToken: 'runtime-guest-token',
},
});
expect(apiClientMocks.requestJson).toHaveBeenCalledWith(
'/api/runtime/demo/runs',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer runtime-guest-token',
},
body: JSON.stringify({ profileId: 'profile-1' }),
},
'启动失败',
{
retry,
authImpact: undefined,
skipAuth: true,
skipRefresh: true,
notifyAuthStateChange: undefined,
clearAuthOnUnauthorized: undefined,
},
);
});
it('omits empty headers and body for plain runtime reads', async () => {
await requestRuntimeJson({
url: '/api/runtime/demo/runs/run-1',
fallbackMessage: '读取失败',
});
expect(apiClientMocks.requestJson).toHaveBeenCalledWith(
'/api/runtime/demo/runs/run-1',
{ method: 'GET' },
'读取失败',
{
authImpact: undefined,
skipAuth: undefined,
skipRefresh: undefined,
notifyAuthStateChange: undefined,
clearAuthOnUnauthorized: undefined,
},
);
});
});