import { beforeEach, describe, expect, it, vi } from 'vitest'; const { requestJsonMock } = vi.hoisted(() => ({ requestJsonMock: vi.fn(), })); import { deleteRpgSaveSnapshot, getRpgSaveSnapshot, putRpgSaveSnapshot, } from './rpgSnapshotClient'; vi.mock('../apiClient', () => ({ requestJson: requestJsonMock, })); describe('rpgSnapshotClient routes', () => { beforeEach(() => { requestJsonMock.mockReset(); }); it('reads the current save snapshot from the runtime save route', async () => { requestJsonMock.mockResolvedValueOnce(null); await getRpgSaveSnapshot(); expect(requestJsonMock).toHaveBeenCalledWith( '/api/runtime/save/snapshot', expect.objectContaining({ method: 'GET' }), '读取存档失败', expect.objectContaining({ retry: expect.objectContaining({ maxRetries: 1 }), }), ); }); it('writes the current save snapshot through the runtime save route', async () => { requestJsonMock.mockResolvedValueOnce({ version: 2, savedAt: '2026-04-21T09:00:00.000Z', bottomTab: 'adventure', currentStory: null, gameState: { worldType: 'CUSTOM', }, }); await putRpgSaveSnapshot({ bottomTab: 'adventure', currentStory: null, gameState: { worldType: 'CUSTOM', } as never, }); expect(requestJsonMock).toHaveBeenCalledWith( '/api/runtime/save/snapshot', expect.objectContaining({ method: 'PUT', headers: { 'Content-Type': 'application/json' }, }), '保存存档失败', expect.objectContaining({ retry: expect.objectContaining({ maxRetries: 1, retryUnsafeMethods: true, }), }), ); }); it('deletes the current save snapshot through the runtime save route', async () => { requestJsonMock.mockResolvedValueOnce({ ok: true }); await deleteRpgSaveSnapshot(); expect(requestJsonMock).toHaveBeenCalledWith( '/api/runtime/save/snapshot', expect.objectContaining({ method: 'DELETE' }), '删除存档失败', expect.objectContaining({ retry: expect.objectContaining({ maxRetries: 1, retryUnsafeMethods: true, }), }), ); }); });