Files
PORTAL_COD2/docs/.vitepress/theme/components/TestRunner.vue
T
2025-02-11 14:16:58 +03:00

182 lines
4.9 KiB
Vue

<template>
<div>
<h2>Результаты тестов:</h2>
<!-- Общее состояние тестов -->
<div v-if="results.success" class="success">✅ Все тесты успешно пройдены!</div>
<div v-else-if="results.numFailedTestSuites > 0" class="error">
❌ Ошибки в тестах: {{ results.numFailedTestSuites }} из {{ results.numTotalTestSuites }}
</div>
<!-- Сводная информация -->
<div v-if="results.testResults" class="summary">
<h3>Сводка по тестам:</h3>
<ul>
<li><strong>Общее количество тестов:</strong> {{ results.numTotalTests }}</li>
<li>
<strong>Пройдено:</strong> {{ results.numPassedTests }} ({{
getPercentage(results.numPassedTests, results.numTotalTests)
}}%)
</li>
<li>
<strong>Неудачных:</strong> {{ results.numFailedTests }} ({{
getPercentage(results.numFailedTests, results.numTotalTests)
}}%)
</li>
</ul>
</div>
<!-- Список успешных тестов -->
<div v-if="passedTests.length">
<h3>Успешные тесты:</h3>
<ul>
<li v-for="test in passedTests" :key="test.fullName">
<strong>{{ test.fullName }}</strong> ✅ ({{ test.duration }} мс)
</li>
</ul>
</div>
<!-- Список неудачных тестов -->
<div v-if="failedTests.length">
<h3>Неудачные тесты:</h3>
<ul>
<li v-for="(test, index) in failedTests" :key="test.fullName">
<strong>{{ test.fullName }}</strong> ❌ ({{ test.duration }} мс)
<pre v-if="test.failureMessages.length" class="error-log">
<button @click="toggleError(index)" class="error-toggle-btn">
{{ isErrorVisible(index) ? 'Скрыть ошибки' : 'Показать ошибки' }}
</button>
<div v-if="isErrorVisible(index)">
<strong>Ожидалось:</strong> {{ extractTestValue(test.failureMessages, 'Expected') }}
<strong>Получено:</strong> {{ extractTestValue(test.failureMessages, 'Received') }}
</div>
</pre>
</li>
</ul>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, computed } from 'vue';
const results = ref({});
const errorMessage = ref('');
const errorVisibility = ref({});
const toggleError = (index) => {
errorVisibility.value[index] = !errorVisibility.value[index];
};
const isErrorVisible = (index) => {
return !!errorVisibility.value[index];
};
const getPercentage = (num, total) => {
return ((num / total) * 100).toFixed(2);
};
onMounted(async () => {
try {
const response = await fetch('/test-results.json');
const data = await response.json();
results.value = data;
if (!data.success) {
errorMessage.value = data.testResults
.map((t) => (t.failureMessages ? t.failureMessages.join('\n') : ''))
.join('\n\n');
}
} catch (error) {
errorMessage.value = error;
}
});
const passedTests = computed(() => {
return (
results.value?.testResults?.flatMap((testSuite) =>
testSuite.assertionResults
?.filter((test) => test.status === 'passed')
.map((test) => ({
...test,
failureMessages: test.failureMessages || [],
})),
) || []
);
});
const failedTests = computed(() => {
return (
results.value?.testResults?.flatMap((testSuite) =>
testSuite.assertionResults
?.filter((test) => test.status === 'failed')
.map((test) => ({
...test,
failureMessages: test.failureMessages || [],
})),
) || []
);
});
// Очистка от лишних данных и ANSI кодов
/* eslint-disable no-control-regex */
const cleanTestMessage = (message) => {
return message.replace(/[\x1b][[0-?9;]*[mG]/g, '').trim();
};
/* eslint-enable no-control-regex */
// Универсальная функция для извлечения данных из сообщений
const extractTestValue = (messages, type) => {
const failureMessage = messages.find((msg) => msg.includes(type + ':'));
if (failureMessage) {
const match = failureMessage.match(new RegExp(`${type}: (.*)`));
if (match) {
return cleanTestMessage(match[1]);
}
}
return 'Не указано';
};
</script>
<style scoped>
.success {
color: green;
font-weight: bold;
}
.error {
color: red;
font-weight: bold;
}
.warning {
color: orange;
font-weight: bold;
}
.error-log {
background: rgba(255, 221, 221, 0.1);
border-left: 4px solid red;
padding: 5px;
white-space: pre-wrap;
font-family: monospace;
}
.summary {
margin-bottom: 20px;
}
.error-toggle-btn {
background-color: #ff6347;
color: white;
border: none;
padding: 5px;
cursor: pointer;
margin-bottom: 5px;
}
.error-toggle-btn:hover {
background-color: #ff4500;
}
</style>