/* 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 . For commercial licensing, please contact support@quantumnous.com */ import assert from 'node:assert/strict'; import { readFile, readdir } from 'node:fs/promises'; import test from 'node:test'; import React from 'react'; import { parse } from '@babel/parser'; import { transformWithEsbuild } from 'vite'; let pageKey = 'setting'; const token = { colorInfoBg: 'info-background', colorInfoBorder: 'info-border', colorInfo: 'info-icon', colorWarningBg: 'warning-background', colorWarningBorder: 'warning-border', }; const filename = new URL( '../../src/components/console/ConsolePrimitives.jsx', import.meta.url, ); const { code } = await transformWithEsbuild( await readFile(filename, 'utf8'), filename.pathname, { loader: 'jsx', format: 'cjs' }, ); const module = { exports: {} }; const mocks = { react: React, antd: { Alert: 'alert', Table: 'table', Empty: Object.assign(() => null, { PRESENTED_IMAGE_SIMPLE: 'simple' }), theme: { useToken: () => ({ token }) }, }, 'react-i18next': {}, '../ui/CenterActionTable': 'table', '../common/table/TableEmpty': () => null, './ConsoleContext': { useConsolePage: () => ({ page: { key: pageKey } }) }, }; new Function('require', 'module', 'exports', code)( (name) => { assert.ok(Object.hasOwn(mocks, name), name); return mocks[name]; }, module, module.exports, ); const { ConsoleAlert, ConsoleTable } = module.exports; test('settings alerts share the reference surface while retaining their semantic type', () => { pageKey = 'setting'; for (const type of ['info', 'warning', 'error', 'success']) { const element = ConsoleAlert({ type, description: 'Existing message' }); assert.equal(element.props.type, type); assert.equal(element.props.description, 'Existing message'); assert.equal(element.props.style.background, token.colorInfoBg); assert.equal(element.props.style.borderColor, token.colorInfoBorder); assert.equal( element.props.style['--settings-alert-icon-color'], token.colorInfo, ); } pageKey = 'token'; const element = ConsoleAlert({ type: 'warning' }); assert.equal(element.props.style.background, token.colorWarningBg); assert.equal(element.props.style.borderColor, token.colorWarningBorder); }); test('settings tables use token-list density and keep columns and callbacks intact', () => { const columns = [{ dataIndex: 'value', render: (value) => value }]; const onChange = () => {}; const props = { columns, onChange, size: 'small' }; pageKey = 'setting'; const element = ConsoleTable.render(props, null); assert.equal(element.props.size, 'middle'); assert.equal(element.props.columns, columns); assert.equal(element.props.onChange, onChange); pageKey = 'token'; assert.equal(ConsoleTable.render(props, null).props.size, 'small'); }); function walk(node, visit) { if (!node || typeof node !== 'object') return; visit(node); for (const [key, value] of Object.entries(node)) { if (key === 'loc') continue; if (Array.isArray(value)) value.forEach((child) => walk(child, visit)); else if (value && typeof value === 'object') walk(value, visit); } } async function sourceFiles(directory) { const entries = await readdir(directory, { withFileTypes: true }); const files = []; for (const entry of entries) { const child = new URL( entry.name + (entry.isDirectory() ? '/' : ''), directory, ); if (entry.isDirectory()) files.push(...(await sourceFiles(child))); else if (entry.name.endsWith('.jsx')) files.push(child); } return files; } test('every settings action column uses labeled links instead of icon buttons', async () => { const files = [ ...(await sourceFiles( new URL('../../src/pages/Setting/', import.meta.url), )), ...(await sourceFiles( new URL('../../src/components/settings/', import.meta.url), )), ]; let count = 0; for (const file of files) { const source = await readFile(file, 'utf8'); const ast = parse(source, { sourceType: 'module', plugins: ['jsx'] }); walk(ast, (node) => { if (node.type !== 'ObjectExpression') return; const title = node.properties.find( (property) => property.key?.name === 'title', ); const render = node.properties.find( (property) => property.key?.name === 'render', ); if ( !title || !render || !/t\(['"]\u64cd\u4f5c['"]\)/.test( source.slice(title.value.start, title.value.end), ) ) return; walk(render, (child) => { if ( child.type !== 'JSXElement' || !['Button', 'AntButton'].includes(child.openingElement.name.name) ) return; const attributes = child.openingElement.attributes; const type = attributes.find( (attribute) => attribute.name?.name === 'type', ); const location = `${file.pathname}:${child.loc.start.line}`; assert.equal(type?.value?.value, 'link', location); assert.ok( !attributes.some((attribute) => ['icon', 'theme'].includes(attribute.name?.name), ), location, ); assert.ok( child.children.some( (content) => content.type !== 'JSXText' || content.value.trim(), ), location, ); count++; }); }); } assert.ok(count > 0); });