Files
monkey-ai-web/src/components/common/LobeHubIcon.test.js
T
2026-10-28 11:37:15 +08:00

86 lines
2.8 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 { runInNewContext } from 'node:vm';
import test from 'node:test';
import React from 'react';
import { transformWithEsbuild } from 'vite';
const filename = new URL('./LobeHubIcon.jsx', import.meta.url);
const { code } = await transformWithEsbuild(
await readFile(filename, 'utf8'),
filename.pathname,
{ loader: 'jsx', format: 'cjs', jsx: 'transform', target: 'node22' },
);
const Mono = () => null;
const Color = () => null;
const Avatar = () => null;
const module = { exports: {} };
runInNewContext(code, {
module,
exports: module.exports,
require(name) {
if (name === 'react') return React;
if (name === '@douyinfe/semi-ui') return { Avatar: 'avatar' };
if (name === 'lucide-react') return { Bot: Mono, Image: Mono };
assert.ok(name.startsWith('@lobehub/icons/es/'), name);
return Object.assign(Mono, { Color, Avatar });
},
});
const render = module.exports.default;
test('brand icon keeps the selected variant and configured props', () => {
const icon = render({
iconName: "OpenAI.Avatar.type={'platform'}.shape='square'.size=24",
size: 14,
});
assert.equal(icon.type, Avatar);
assert.equal(icon.props.type, 'platform');
assert.equal(icon.props.shape, 'square');
assert.equal(icon.props.size, 24);
});
test('brand icon preserves color and default size', () => {
const icon = render({ iconName: 'Claude.Color' });
assert.equal(icon.type, Color);
assert.equal(icon.props.size, 14);
});
test('empty and unknown brand names preserve the fallback avatar', () => {
for (const [iconName, letter] of [
[null, '?'],
['', '?'],
['UnknownBrand', 'U'],
]) {
const icon = render({ iconName });
assert.equal(icon.type, 'avatar');
assert.equal(icon.props.children, letter);
}
});
test('base icons and lucide compatibility brands remain supported', () => {
for (const iconName of ['OpenAI', 'FastGPT', 'Jimeng']) {
const icon = render({ iconName, size: 20 });
assert.equal(icon.type, Mono);
assert.equal(icon.props.size, 20);
}
});