base/test/base.test.js

79 lines
3.0 KiB
JavaScript

import { HTTP, UI, State } from '@web/base';
export async function runTests() {
console.log('Starting comprehensive Base.js tests...');
// 1. HTTP Test
console.log('Testing HTTP (local check)...');
if (typeof HTTP.request !== 'function') throw new Error('HTTP.request missing');
// 2. State Test
console.log('Testing State...');
State.exitBlocks = 1;
if (State.exitBlocks !== 1) throw new Error('State update failed');
State.exitBlocks = 0;
// 3. UI Namespace Test
console.log('Testing UI...');
if (typeof UI.alert !== 'function') throw new Error('UI.alert missing');
if (typeof UI.toast !== 'function') throw new Error('UI.toast missing');
// 4. API Component Test
console.log('Testing API Component...');
const api = document.createElement('API');
document.body.appendChild(api);
await new Promise(r => setTimeout(r, 50));
api.request.url = 'https://jsonplaceholder.typicode.com/todos/1';
const apiResp = await api.do();
if (!apiResp.ok) throw new Error('API component request failed');
api.remove();
// 5. AutoForm & TagsInput Test
console.log('Testing AutoForm...');
const form = document.createElement('AutoForm');
document.body.appendChild(form);
await new Promise(r => setTimeout(r, 200));
form.state.schema = [
{ name: 'tags', type: 'TagsInput', label: '标签' },
{ name: 'name', type: 'text', label: '姓名' }
];
await new Promise(r => setTimeout(r, 200));
if (!form.querySelector('TagsInput')) throw new Error('TagsInput not rendered in AutoForm');
form.remove();
// 6. List Components Basic Verification
console.log('Verifying List Components...');
const { Component } = await import('@web/state');
console.log('FastList exists:', Component.exists('FastList'));
const listIds = ['ll', 'gl', 'tt', 'ct'];
for (const id of listIds) {
const el = document.getElementById(id);
if (!el) throw new Error(`Component #${id} not found`);
console.log(`Component #${id} tagName:`, el.tagName, 'has refresh:', !!el.refresh, 'setupFunc exists:', !!Component.getSetupFunction(el.tagName));
if (!el.state.list || el.state.list.length === 0) throw new Error(`Component #${id} list data not bound`);
}
// 7. Benchmarks
console.log('Recording list benchmarks...');
window.benchResults = {};
const measure = async (id, name) => {
const el = document.getElementById(id);
const start = performance.now();
el.scrollTop = 5000;
el.refresh?.();
await new Promise(r => setTimeout(r, 50));
const time = performance.now() - start;
window.benchResults[name] = time;
console.log(`BENCHMARK: ${name} scroll & refresh: ${time.toFixed(2)}ms`);
};
await measure('ll', 'FastList');
await measure('gl', 'FastGroupedList');
await measure('tt', 'FastTree');
await measure('ct', 'CollapseTree');
console.log('All Base.js unit tests completed.');
}