Stage after merging with project files
This commit is contained in:
+84
-41
@@ -1,44 +1,50 @@
|
||||
require("dotenv").config();
|
||||
import 'dotenv/config';
|
||||
|
||||
const assert = require("node:assert/strict");
|
||||
const http = require("node:http");
|
||||
const https = require("node:https");
|
||||
const mariadb = require("mariadb");
|
||||
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 getJson(targetUrl) {
|
||||
function requestUrl(targetUrl) {
|
||||
const url = new URL(targetUrl);
|
||||
const transport = url.protocol === "https:" ? https : http;
|
||||
const transport = url.protocol === 'https:' ? https : http;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = transport.get(url, (response) => {
|
||||
let body = "";
|
||||
let body = '';
|
||||
|
||||
response.on("data", (chunk) => {
|
||||
response.on('data', (chunk) => {
|
||||
body += chunk;
|
||||
});
|
||||
|
||||
response.on("end", () => {
|
||||
try {
|
||||
resolve({
|
||||
body: JSON.parse(body),
|
||||
statusCode: response.statusCode
|
||||
});
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
response.on('end', () => {
|
||||
resolve({
|
||||
body,
|
||||
statusCode: response.statusCode
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
request.on("error", reject);
|
||||
request.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
async function testApp() {
|
||||
const appPort = Number(process.env.APP_PORT || 3000);
|
||||
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}`,
|
||||
@@ -49,13 +55,24 @@ async function testApp() {
|
||||
|
||||
for (const baseUrl of candidates) {
|
||||
try {
|
||||
const response = await getJson(`${baseUrl}/health`);
|
||||
const health = await getJson(`${baseUrl}/api/health`);
|
||||
|
||||
assert.equal(response.statusCode, 200, `Unexpected status from ${baseUrl}`);
|
||||
assert.equal(response.body.status, "ok", "Application health endpoint is not healthy");
|
||||
assert.equal(response.body.database, "reachable", "Application cannot reach MariaDB");
|
||||
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');
|
||||
|
||||
return { baseUrl, payload: response.body };
|
||||
const templates = await getJson(`${baseUrl}/api/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;
|
||||
}
|
||||
@@ -67,9 +84,9 @@ async function testApp() {
|
||||
async function testDatabase() {
|
||||
const candidates = unique([
|
||||
process.env.DB_HOST,
|
||||
"127.0.0.1",
|
||||
"localhost",
|
||||
"db"
|
||||
'127.0.0.1',
|
||||
'localhost',
|
||||
'db'
|
||||
]);
|
||||
|
||||
let lastError;
|
||||
@@ -78,22 +95,22 @@ async function testDatabase() {
|
||||
const pool = mariadb.createPool({
|
||||
host,
|
||||
port: Number(process.env.DB_PORT || 3306),
|
||||
user: process.env.DB_USER || "app_user",
|
||||
password: process.env.DB_PASSWORD || "app_password",
|
||||
database: process.env.DB_NAME || "app_db",
|
||||
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 name FROM environment_checks ORDER BY id"
|
||||
'SELECT code FROM templates ORDER BY id'
|
||||
);
|
||||
|
||||
connection.release();
|
||||
await pool.end();
|
||||
|
||||
assert.ok(rows.length >= 1, "Seed table is empty");
|
||||
assert.ok(rows.some((row) => row.code === 'incoming-inspection'), 'Expected seed template is missing');
|
||||
|
||||
return { host, rows };
|
||||
} catch (error) {
|
||||
@@ -105,18 +122,44 @@ async function testDatabase() {
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const appResult = await testApp();
|
||||
const databaseResult = await testDatabase();
|
||||
async function testPhpMyAdmin() {
|
||||
const candidates = unique([
|
||||
'http://phpmyadmin/',
|
||||
`http://localhost:${process.env.PHPMYADMIN_PORT || 8080}/`
|
||||
]);
|
||||
|
||||
console.log("Environment test passed");
|
||||
console.log(`App endpoint: ${appResult.baseUrl}`);
|
||||
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(`Seed rows: ${databaseResult.rows.length}`);
|
||||
console.log(`phpMyAdmin: ${phpMyAdminResult.url}`);
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error("Environment test failed");
|
||||
console.error('Environment test failed');
|
||||
console.error(error.message);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user