160 lines
5.2 KiB
JavaScript
160 lines
5.2 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
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);
|
|
}
|
|
});
|
|
}
|