Files
CLProject/scripts/test-environment.js
2026-04-19 21:14:16 +02:00

165 lines
4.1 KiB
JavaScript

import 'dotenv/config';
import assert from 'node:assert/strict';
import http from 'node:http';
import https from 'node:https';
import * as mariadb from 'mariadb';
function unique(values) {
return [...new Set(values.filter(Boolean))];
}
function requestUrl(targetUrl) {
const url = new URL(targetUrl);
const transport = url.protocol === 'https:' ? https : http;
return new Promise((resolve, reject) => {
const request = transport.get(url, (response) => {
let body = '';
response.on('data', (chunk) => {
body += chunk;
});
response.on('end', () => {
resolve({
body,
statusCode: response.statusCode
});
});
});
request.on('error', reject);
});
}
async function getJson(targetUrl) {
const response = await requestUrl(targetUrl);
return {
...response,
json: JSON.parse(response.body)
};
}
async function testApi() {
const appPort = Number(process.env.PORT || 3000);
const candidates = unique([
process.env.APP_URL,
`http://localhost:${appPort}`,
`http://app:${appPort}`
]);
let lastError;
for (const baseUrl of candidates) {
try {
const health = await getJson(`${baseUrl}/api/v1/health`);
assert.equal(health.statusCode, 200, `Unexpected health status from ${baseUrl}`);
assert.equal(health.json.status, 'ok', 'API health endpoint is not healthy');
assert.equal(health.json.service, 'check-list-poc-api', 'Unexpected API service name');
assert.equal(health.json.database, 'connected', 'API cannot reach MariaDB');
const templates = await getJson(`${baseUrl}/api/v1/templates`);
assert.equal(templates.statusCode, 200, `Unexpected templates status from ${baseUrl}`);
assert.ok(Array.isArray(templates.json.items), 'Templates response must contain an items array');
assert.ok(templates.json.items.length >= 1, 'Seeded templates are missing');
return {
baseUrl,
health: health.json,
templates: templates.json.items
};
} catch (error) {
lastError = error;
}
}
throw lastError;
}
async function testDatabase() {
const candidates = unique([
process.env.DB_HOST,
'127.0.0.1',
'localhost',
'db'
]);
let lastError;
for (const host of candidates) {
const pool = mariadb.createPool({
host,
port: Number(process.env.DB_PORT || 3306),
user: process.env.DB_USER || 'check_list_user',
password: process.env.DB_PASSWORD || 'check_list_password',
database: process.env.DB_NAME || 'check_list',
connectionLimit: 1
});
try {
const connection = await pool.getConnection();
const rows = await connection.query(
'SELECT code FROM templates ORDER BY id'
);
connection.release();
await pool.end();
assert.ok(rows.some((row) => row.code === 'incoming-inspection'), 'Expected seed template is missing');
return { host, rows };
} catch (error) {
lastError = error;
await pool.end();
}
}
throw lastError;
}
async function testPhpMyAdmin() {
const candidates = unique([
'http://phpmyadmin/',
`http://localhost:${process.env.PHPMYADMIN_PORT || 8080}/`
]);
let lastError;
for (const url of candidates) {
try {
const response = await requestUrl(url);
assert.equal(response.statusCode, 200, `Unexpected phpMyAdmin status from ${url}`);
assert.match(response.body, /phpMyAdmin/i, 'phpMyAdmin landing page did not load');
return { url };
} catch (error) {
lastError = error;
}
}
throw lastError;
}
async function main() {
const apiResult = await testApi();
const databaseResult = await testDatabase();
const phpMyAdminResult = await testPhpMyAdmin();
console.log('Environment test passed');
console.log(`API endpoint: ${apiResult.baseUrl}`);
console.log(`Seed templates: ${apiResult.templates.length}`);
console.log(`Database host: ${databaseResult.host}`);
console.log(`phpMyAdmin: ${phpMyAdminResult.url}`);
}
main().catch((error) => {
console.error('Environment test failed');
console.error(error.message);
process.exit(1);
});