import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; const apiClientMocks = vi.hoisted(() => ({ requestJson: vi.fn(), })); vi.mock('../apiClient', async () => { const actual = await vi.importActual('../apiClient'); return { ...actual, requestJson: apiClientMocks.requestJson, }; }); import { restartJumpHopRuntimeRun, startJumpHopRuntimeRun, submitJumpHopJump, } from './jumpHopClient'; describe('jumpHopClient runtime requests', () => { beforeEach(() => { vi.clearAllMocks(); vi.spyOn(Date, 'now').mockReturnValue(1780000000000); apiClientMocks.requestJson.mockResolvedValue({ runId: 'run-1' }); }); afterEach(() => { vi.restoreAllMocks(); }); it('starts runs through the shared runtime request skeleton', async () => { await startJumpHopRuntimeRun('profile/1', { runtimeGuestToken: 'runtime-guest-token', }); expect(apiClientMocks.requestJson).toHaveBeenCalledWith( '/api/runtime/jump-hop/runs', expect.objectContaining({ method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer runtime-guest-token', }, body: JSON.stringify({ profileId: 'profile/1' }), }), '启动跳一跳运行态失败', expect.objectContaining({ skipAuth: true, skipRefresh: true, }), ); }); it('passes draft runtime mode in the start run request body', async () => { await startJumpHopRuntimeRun('profile/1', { runtimeMode: 'draft', }); expect(apiClientMocks.requestJson).toHaveBeenCalledWith( '/api/runtime/jump-hop/runs', expect.objectContaining({ method: 'POST', body: JSON.stringify({ profileId: 'profile/1', runtimeMode: 'draft', }), }), '启动跳一跳运行态失败', expect.anything(), ); }); it('submits jump input with a generated client event id', async () => { await submitJumpHopJump( 'run/1', { dragDistance: 320 }, { runtimeGuestToken: 'runtime-guest-token' }, ); expect(apiClientMocks.requestJson).toHaveBeenCalledWith( '/api/runtime/jump-hop/runs/run%2F1/jump', expect.objectContaining({ method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer runtime-guest-token', }, body: JSON.stringify({ dragDistance: 320, clientEventId: 'jump-run/1-1780000000000', }), }), '提交跳一跳起跳失败', expect.objectContaining({ skipAuth: true, skipRefresh: true, }), ); }); it('restarts runs with the same guest auth request skeleton', async () => { await restartJumpHopRuntimeRun('run/1', { runtimeGuestToken: 'runtime-guest-token', }); expect(apiClientMocks.requestJson).toHaveBeenCalledWith( '/api/runtime/jump-hop/runs/run%2F1/restart', expect.objectContaining({ method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer runtime-guest-token', }, body: JSON.stringify({ clientActionId: 'restart-run/1-1780000000000', }), }), '重新开始跳一跳失败', expect.objectContaining({ skipAuth: true, skipRefresh: true, }), ); }); });