Files
Genarrative/src/stores/usePlatformWalletStore.test.tsx
T
k88936 fc0dcb0782
Project CI / Repository checks (push) Successful in 50s
Project CI / Frontend tests (push) Has been cancelled
Project CI / Backend tests (push) Has been cancelled
Project CI / Native shell tests (push) Has been cancelled
修复泥点消耗刷新不及时 (#138)
refactor:
- 主站复用game agent的zustand store

fix:
- 修复旧请求覆盖问题
- 修复账号切换问题

- 主站原本已经每 4 秒轮询 external generation 任务状态, 在这里补充触发余额刷新的时机

---------

Co-authored-by: 段舒康 <kdletters@qq.com>
Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/138
Co-authored-by: 王德宇 <kvtodev@outlook.com>
Co-committed-by: 王德宇 <kvtodev@outlook.com>
2026-08-08 10:25:02 +08:00

86 lines
2.5 KiB
TypeScript

/* @vitest-environment jsdom */
import { act, renderHook, waitFor } from '@testing-library/react';
import { type ReactNode, StrictMode } from 'react';
import { beforeEach, expect, test, vi } from 'vitest';
const profileClientMocks = vi.hoisted(() => ({
getPlatformProfileRechargeCenter: vi.fn(),
}));
vi.mock('../services/platform-entry/platformProfileClient', () => ({
getPlatformProfileRechargeCenter:
profileClientMocks.getPlatformProfileRechargeCenter,
}));
import {
usePlatformWalletLifecycle,
usePlatformWalletStore,
} from './usePlatformWalletStore';
beforeEach(() => {
vi.clearAllMocks();
usePlatformWalletStore.getState().resetWalletBalance();
profileClientMocks.getPlatformProfileRechargeCenter.mockResolvedValue({
walletBalance: 42,
mudPointBalance: {
totalPoints: 42,
permanentPoints: 42,
limitedPoints: 0,
limitedExpiresAt: null,
dailyFreePoints: 0,
dailyFreeResetPoints: 20,
dailyFreeResetsAt: '2026-08-08T00:00:00+08:00',
},
});
});
test('the single lifecycle survives StrictMode replay and resets on root unmount', async () => {
const addWindowListener = vi.spyOn(window, 'addEventListener');
const removeWindowListener = vi.spyOn(window, 'removeEventListener');
const { unmount } = renderHook(
() => usePlatformWalletLifecycle('user-1', true),
{
wrapper: ({ children }: { children: ReactNode }) => (
<StrictMode>{children}</StrictMode>
),
},
);
await waitFor(() => {
expect(usePlatformWalletStore.getState()).toMatchObject({
ownerUserId: 'user-1',
mudPointBalance: expect.objectContaining({ totalPoints: 42 }),
legacyWalletBalance: 42,
mudPointBalanceStatus: 'ready',
});
});
const requestsBeforeFocus =
profileClientMocks.getPlatformProfileRechargeCenter.mock.calls.length;
await act(async () => {
window.dispatchEvent(new Event('focus'));
});
await waitFor(() => {
expect(
profileClientMocks.getPlatformProfileRechargeCenter.mock.calls.length,
).toBeGreaterThan(requestsBeforeFocus);
});
unmount();
expect(usePlatformWalletStore.getState()).toMatchObject({
ownerUserId: null,
mudPointBalance: null,
legacyWalletBalance: null,
mudPointBalanceStatus: 'idle',
});
expect(
addWindowListener.mock.calls.filter(([eventName]) => eventName === 'focus'),
).toHaveLength(
removeWindowListener.mock.calls.filter(
([eventName]) => eventName === 'focus',
).length,
);
});