feat: 恢复服务监控菜单入口
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
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 test from 'node:test';
|
||||
import React from 'react';
|
||||
import * as icons from 'lucide-react';
|
||||
import * as modules from '../../src/constants/sidebarModules.js';
|
||||
import * as visibility from '../../src/helpers/navigationVisibility.js';
|
||||
import { loadFrontendModule } from './frontend-module.js';
|
||||
|
||||
const StatusContext = {};
|
||||
const UserContext = {};
|
||||
let config;
|
||||
let userGroup = 'default';
|
||||
const mocks = {
|
||||
react: {
|
||||
...React,
|
||||
useContext: (context) =>
|
||||
context === StatusContext
|
||||
? [{ status: { SidebarModulesAdmin: config } }]
|
||||
: [{ user: { group: userGroup } }],
|
||||
useMemo: (factory) => factory(),
|
||||
},
|
||||
'react-router-dom': {
|
||||
Routes: 'routes',
|
||||
Route: 'route',
|
||||
Navigate: 'navigate',
|
||||
Link: 'link',
|
||||
useLocation: () => ({ pathname: '/user-center/service-dashboard' }),
|
||||
useNavigate: () => () => {},
|
||||
},
|
||||
'react-i18next': { useTranslation: () => ({ t: (value) => value }) },
|
||||
'lucide-react': icons,
|
||||
'../../context/Status': { StatusContext },
|
||||
'../../context/User': { UserContext },
|
||||
'../../constants/sidebarModules': modules,
|
||||
'../../helpers/navigationVisibility': visibility,
|
||||
'../../helpers/miniProgram': {
|
||||
isMiniProgramWebView: () => false,
|
||||
isMiniProgramPaymentPath: () => false,
|
||||
},
|
||||
'../../hooks/common/useMarquee': { useMarquee: () => ({ visible: false }) },
|
||||
'../../hooks/common/useSidebarCollapsed': {
|
||||
useSidebarCollapsed: () => [false, () => {}],
|
||||
},
|
||||
'../../hooks/common/useWeyAiWebEntry': {
|
||||
useWeyAiDownloadEntry: () => ({ visible: false }),
|
||||
},
|
||||
};
|
||||
for (const path of [
|
||||
'../../components/common/PageBackground',
|
||||
'./UserCenterSidebar',
|
||||
'./UserDashboard',
|
||||
'./UsageRecord',
|
||||
'./Invite',
|
||||
'./Invoice',
|
||||
'../TopUp',
|
||||
'../../components/settings/PersonalSetting',
|
||||
'../WeyAiDownload',
|
||||
'../KeyManagement',
|
||||
])
|
||||
mocks[path] = { __esModule: true, default: () => null };
|
||||
const ServiceDashboard = () => null;
|
||||
mocks['../ServiceDashboard'] = { __esModule: true, default: ServiceDashboard };
|
||||
const UserCenter = (
|
||||
await loadFrontendModule(
|
||||
new URL('../../src/pages/UserCenter/index.jsx', import.meta.url),
|
||||
mocks,
|
||||
)
|
||||
).default;
|
||||
const Sidebar = (
|
||||
await loadFrontendModule(
|
||||
new URL(
|
||||
'../../src/pages/UserCenter/UserCenterSidebar.jsx',
|
||||
import.meta.url,
|
||||
),
|
||||
mocks,
|
||||
)
|
||||
).default;
|
||||
|
||||
function descendants(node) {
|
||||
if (!React.isValidElement(node)) return [];
|
||||
return [
|
||||
node,
|
||||
...React.Children.toArray(node.props.children).flatMap(descendants),
|
||||
];
|
||||
}
|
||||
|
||||
for (const [name, userCenter, group, visible] of [
|
||||
['defaults', {}, 'default', true],
|
||||
['enabled', { serviceDashboard: true }, 'default', true],
|
||||
['disabled', { serviceDashboard: false }, 'default', false],
|
||||
['section disabled', { enabled: false }, 'default', false],
|
||||
['allowed section group', { groups: ['vip'] }, 'vip', true],
|
||||
['denied section group', { groups: ['vip'] }, 'default', false],
|
||||
[
|
||||
'allowed module group',
|
||||
{ moduleGroups: { serviceDashboard: ['vip'] } },
|
||||
'vip',
|
||||
true,
|
||||
],
|
||||
[
|
||||
'denied module group',
|
||||
{ moduleGroups: { serviceDashboard: ['vip'] } },
|
||||
'default',
|
||||
false,
|
||||
],
|
||||
]) {
|
||||
test(`service dashboard desktop, mobile and route agree: ${name}`, () => {
|
||||
config = JSON.stringify({ userCenter });
|
||||
userGroup = group;
|
||||
const tree = UserCenter({});
|
||||
const nodes = descendants(tree);
|
||||
const route = nodes.find((node) => node.props.path === 'service-dashboard');
|
||||
const mountsPage = route
|
||||
? descendants(route.props.element).some(
|
||||
(node) => node.type === ServiceDashboard,
|
||||
)
|
||||
: false;
|
||||
assert.equal(mountsPage, visible);
|
||||
if (route && !visible) {
|
||||
assert.equal(route.props.element.type, 'navigate');
|
||||
assert.notEqual(
|
||||
route.props.element.props.to,
|
||||
'/user-center/service-dashboard',
|
||||
);
|
||||
}
|
||||
const desktop = descendants(Sidebar()).some(
|
||||
(node) => node.props.to === '/user-center/service-dashboard',
|
||||
);
|
||||
assert.equal(desktop, visible);
|
||||
const mobileComponent = nodes.find(
|
||||
(node) => node.props.sidebarConfig !== undefined,
|
||||
);
|
||||
const mobile = mobileComponent
|
||||
? descendants(mobileComponent.type(mobileComponent.props)).some(
|
||||
(node) => node.type === 'span' && node.props.children === '监控',
|
||||
)
|
||||
: false;
|
||||
assert.equal(mobile, visible);
|
||||
});
|
||||
}
|
||||
|
||||
test('service dashboard can be the only enabled default route without a redirect loop', () => {
|
||||
config = JSON.stringify({
|
||||
userCenter: {
|
||||
...Object.fromEntries(
|
||||
Object.keys(modules.getDefaultSidebarModulesAdmin().userCenter).map(
|
||||
(key) => [key, false],
|
||||
),
|
||||
),
|
||||
enabled: true,
|
||||
serviceDashboard: true,
|
||||
},
|
||||
});
|
||||
const nodes = descendants(UserCenter({}));
|
||||
const route = nodes.find((node) => node.props.path === '/');
|
||||
assert.equal(route.props.element.props.to, '/user-center/service-dashboard');
|
||||
const service = nodes.find((node) => node.props.path === 'service-dashboard');
|
||||
assert.ok(
|
||||
descendants(service.props.element).some(
|
||||
(node) => node.type === ServiceDashboard,
|
||||
),
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user