2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
141 lines
4.5 KiB
JavaScript
141 lines
4.5 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import test from 'node:test';
|
|
import React from 'react';
|
|
import { parse } from '@babel/parser';
|
|
import { runInNewContext } from 'node:vm';
|
|
import { transformWithEsbuild } from 'vite';
|
|
import { loadFrontendModule } from './frontend-module.js';
|
|
|
|
const react = {
|
|
...React,
|
|
useMemo: (callback) => callback(),
|
|
useEffect: () => {},
|
|
useState: (initial) => [initial, () => {}],
|
|
useContext: (context) => context,
|
|
};
|
|
const userState = { user: { quota: 100, used_quota: 20, request_count: 8 } };
|
|
async function loadStats(filename, mocks) {
|
|
const { code } = await transformWithEsbuild(
|
|
await readFile(filename, 'utf8'),
|
|
filename.pathname,
|
|
{
|
|
loader: 'jsx',
|
|
format: 'cjs',
|
|
},
|
|
);
|
|
const module = { exports: {} };
|
|
runInNewContext(code, {
|
|
React,
|
|
module,
|
|
exports: module.exports,
|
|
require: (name) => mocks[name],
|
|
});
|
|
return module.exports;
|
|
}
|
|
const stats = await loadStats(
|
|
new URL('../../src/hooks/dashboard/useDashboardStats.jsx', import.meta.url),
|
|
{
|
|
react,
|
|
'lucide-react': {
|
|
Wallet: 'Wallet',
|
|
Activity: 'Activity',
|
|
Gauge: 'Gauge',
|
|
Cpu: 'Cpu',
|
|
},
|
|
'@/components/ui/icons': Object.fromEntries(
|
|
[
|
|
'IconMoneyExchangeStroked',
|
|
'IconHistogram',
|
|
'IconPulse',
|
|
'IconStopwatchStroked',
|
|
'IconTypograph',
|
|
'IconSend',
|
|
].map((name) => [name, name]),
|
|
),
|
|
'../../helpers': { renderNumber: String, renderQuota: String },
|
|
'../../helpers/dashboard': { createSectionTitle: (_, title) => title },
|
|
},
|
|
);
|
|
|
|
function descendants(node) {
|
|
if (Array.isArray(node)) return node.flatMap(descendants);
|
|
if (!node || typeof node !== 'object') return [];
|
|
return [node, ...descendants(node.props?.children)];
|
|
}
|
|
|
|
for (const path of [
|
|
'pages/UserCenter/UserDashboard.jsx',
|
|
'components/dashboard/index.jsx',
|
|
]) {
|
|
test(`${path} renders real statistics without navigating during render`, async () => {
|
|
const filename = new URL(`../../src/${path}`, import.meta.url);
|
|
const source = await readFile(filename, 'utf8');
|
|
const ast = parse(source, { sourceType: 'module', plugins: ['jsx'] });
|
|
// Only data fetching and presentation children are stubbed; execute the page
|
|
// and the real statistics hook together to catch positional argument drift.
|
|
const mocks = Object.fromEntries(
|
|
ast.program.body
|
|
.filter((node) => node.type === 'ImportDeclaration')
|
|
.map((node) => [
|
|
node.source.value,
|
|
Object.fromEntries([
|
|
['__esModule', true],
|
|
...node.specifiers.map((specifier) => [
|
|
specifier.imported?.name || 'default',
|
|
specifier.local.name,
|
|
]),
|
|
]),
|
|
]),
|
|
);
|
|
const navigation = [];
|
|
Object.assign(mocks, {
|
|
react,
|
|
'../../context/User': { UserContext: [userState, () => {}] },
|
|
'../../context/Status': { StatusContext: [{}] },
|
|
'../../hooks/common/useEffectEvent': {
|
|
useEffectEvent: (callback) => callback,
|
|
},
|
|
'../../hooks/common/useIsMobile': { useIsMobile: () => false },
|
|
'../../hooks/dashboard/useDashboardStats': stats,
|
|
'../../hooks/dashboard/useDashboardCharts': {
|
|
useDashboardCharts: () => ({}),
|
|
},
|
|
'../../hooks/dashboard/useDashboardData': {
|
|
useDashboardData: () => ({
|
|
times: 7,
|
|
trendData: [{ deliberately: 'different from metrics' }],
|
|
performanceMetrics: { avgRPM: 12, avgTPM: 345 },
|
|
tokenUsage: {
|
|
input_tokens: 10,
|
|
output_tokens: 20,
|
|
cache_tokens: 30,
|
|
cache_creation_tokens: 40,
|
|
},
|
|
navigate: (...args) => navigation.push(args),
|
|
t: (value) => value,
|
|
}),
|
|
},
|
|
'../../constants/dashboard.constants': { UPTIME_STATUS_MAP: {} },
|
|
});
|
|
const Page = (await loadFrontendModule(filename, mocks)).default;
|
|
const tree = Page();
|
|
assert.deepEqual(navigation, []);
|
|
const groups = descendants(tree).find((node) => node.type === 'StatsCards')
|
|
.props.groupedStatsData;
|
|
assert.deepEqual(
|
|
Array.from(groups.at(-1).items, (item) => item.value),
|
|
[12, 345],
|
|
);
|
|
const tokens = groups.find((group) => group.title === 'Token 流量');
|
|
if (path.startsWith('pages/')) {
|
|
assert.deepEqual(
|
|
Array.from(tokens.items, (item) => item.value),
|
|
['10', '20', '30', '40'],
|
|
);
|
|
} else {
|
|
assert.equal(tokens, undefined);
|
|
}
|
|
});
|
|
}
|