140 lines
5.7 KiB
JavaScript
140 lines
5.7 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
||
const form = document.getElementById('test-config-form');
|
||
const statusArea = document.getElementById('status-area');
|
||
const resultsOutput = document.getElementById('results-output');
|
||
const reportLinkArea = document.getElementById('report-link-area');
|
||
|
||
form.addEventListener('submit', async (event) => {
|
||
event.preventDefault();
|
||
statusArea.textContent = '正在运行测试,请稍候...';
|
||
resultsOutput.textContent = '';
|
||
reportLinkArea.innerHTML = '';
|
||
|
||
const formData = new FormData(form);
|
||
const config = {};
|
||
formData.forEach((value, key) => {
|
||
// 处理复选框
|
||
if (key.startsWith('use_llm_for_')) {
|
||
config[key] = form.elements[key].checked;
|
||
} else if (value.trim() !== '') { // 只添加非空值
|
||
config[key] = value.trim();
|
||
}
|
||
});
|
||
|
||
// 如果复选框未被选中,FormData 不会包含它们,所以要确保它们是 false
|
||
['use_llm_for_request_body', 'use_llm_for_path_params', 'use_llm_for_query_params', 'use_llm_for_headers'].forEach(key => {
|
||
if (!(key in config)) {
|
||
config[key] = false;
|
||
}
|
||
});
|
||
|
||
// 从 YAPI 分类和 Swagger 标签中获取选中的项
|
||
const selectedYapiCategories = Array.from(document.querySelectorAll('#yapi-categories-container input[type="checkbox"]:checked'))
|
||
.map(cb => cb.value);
|
||
if (selectedYapiCategories.length > 0) {
|
||
config['categories'] = selectedYapiCategories.join(',');
|
||
}
|
||
|
||
const selectedSwaggerTags = Array.from(document.querySelectorAll('#swagger-tags-container input[type="checkbox"]:checked'))
|
||
.map(cb => cb.value);
|
||
if (selectedSwaggerTags.length > 0) {
|
||
config['tags'] = selectedSwaggerTags.join(',');
|
||
}
|
||
|
||
try {
|
||
const response = await fetch('/run-tests', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify(config)
|
||
});
|
||
|
||
const result = await response.json();
|
||
|
||
if (response.ok) {
|
||
statusArea.textContent = `测试完成: ${result.message || '成功'}`;
|
||
resultsOutput.textContent = JSON.stringify(result.summary, null, 2);
|
||
if (result.report_file) {
|
||
reportLinkArea.innerHTML = `<p>测试报告已保存到: <strong>${result.report_file}</strong></p>`;
|
||
}
|
||
} else {
|
||
statusArea.textContent = `测试失败: ${result.error || '未知错误'}`;
|
||
resultsOutput.textContent = JSON.stringify(result, null, 2);
|
||
}
|
||
} catch (error) {
|
||
statusArea.textContent = '运行测试时发生网络错误或服务器内部错误。';
|
||
resultsOutput.textContent = error.toString();
|
||
console.error('运行测试出错:', error);
|
||
}
|
||
});
|
||
});
|
||
|
||
async function fetchYapiCategories() {
|
||
const yapiFilePath = document.getElementById('yapi_file_path').value;
|
||
const container = document.getElementById('yapi-categories-container');
|
||
container.innerHTML = '正在加载分类...';
|
||
|
||
if (!yapiFilePath) {
|
||
container.innerHTML = '<p style="color: red;">请输入YAPI文件路径。</p>';
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch('/list-yapi-categories', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ yapi_file_path: yapiFilePath })
|
||
});
|
||
const categories = await response.json();
|
||
if (response.ok) {
|
||
renderCheckboxes(container, categories, 'yapi_category');
|
||
} else {
|
||
container.innerHTML = `<p style="color: red;">加载YAPI分类失败: ${categories.error || '未知错误'}</p>`;
|
||
}
|
||
} catch (error) {
|
||
container.innerHTML = `<p style="color: red;">请求YAPI分类时出错: ${error}</p>`;
|
||
}
|
||
}
|
||
|
||
async function fetchSwaggerTags() {
|
||
const swaggerFilePath = document.getElementById('swagger_file_path').value;
|
||
const container = document.getElementById('swagger-tags-container');
|
||
container.innerHTML = '正在加载标签...';
|
||
|
||
if (!swaggerFilePath) {
|
||
container.innerHTML = '<p style="color: red;">请输入Swagger文件路径。</p>';
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch('/list-swagger-tags', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ swagger_file_path: swaggerFilePath })
|
||
});
|
||
const tags = await response.json();
|
||
if (response.ok) {
|
||
renderCheckboxes(container, tags, 'swagger_tag');
|
||
} else {
|
||
container.innerHTML = `<p style="color: red;">加载Swagger标签失败: ${tags.error || '未知错误'}</p>`;
|
||
}
|
||
} catch (error) {
|
||
container.innerHTML = `<p style="color: red;">请求Swagger标签时出错: ${error}</p>`;
|
||
}
|
||
}
|
||
|
||
function renderCheckboxes(container, items, groupName) {
|
||
if (!items || items.length === 0) {
|
||
container.innerHTML = '<p>未找到任何项。</p>';
|
||
return;
|
||
}
|
||
let html = items.map((item, index) => {
|
||
const id = `${groupName}_${index}`;
|
||
return `<div>
|
||
<input type="checkbox" id="${id}" name="${groupName}[]" value="${item.name}">
|
||
<label for="${id}">${item.name} ${item.description ? '(' + item.description + ')' : ''}</label>
|
||
</div>`;
|
||
}).join('');
|
||
container.innerHTML = html;
|
||
}
|