Files
wyndham-Condon/backend/scripts/api-smoke.mjs
2026-08-02 13:33:13 +08:00

97 lines
2.8 KiB
JavaScript

import process from "node:process";
import { createInterface } from "node:readline";
import pg from "pg";
import { buildApp } from "../dist/src/app.js";
import { PostgresCondoRepository } from "../dist/src/postgres-repository.js";
const { Pool } = pg;
async function readStandardInput() {
const lines = createInterface({
input: process.stdin,
terminal: false
});
for await (const line of lines) {
lines.close();
return line.trim();
}
throw Object.assign(new Error("Missing connection input"), {
code: "MISSING_CONNECTION_INPUT"
});
}
function safeError(error) {
return {
ok: false,
errorCode: typeof error?.code === "string" ? error.code : "API_SMOKE_FAILED"
};
}
let app;
try {
const input = JSON.parse(await readStandardInput());
const pool = new Pool({
host: input.host,
port: input.port,
user: input.user,
password: input.password,
database: input.database,
ssl: input.ssl ? { rejectUnauthorized: false } : undefined,
max: 2,
application_name: "condon_api_readonly_smoke",
connectionTimeoutMillis: 10_000,
options: "-c default_transaction_read_only=on -c statement_timeout=15000 -c lock_timeout=2000"
});
const repository = new PostgresCondoRepository(pool);
app = await buildApp({ repository, logger: false });
const requests = await Promise.all([
app.inject({ method: "GET", url: "/health" }),
app.inject({ method: "GET", url: "/room-types" }),
app.inject({ method: "GET", url: "/owner-accounts?year=2026" }),
app.inject({ method: "GET", url: "/usage-records" }),
app.inject({ method: "GET", url: "/dashboard?year=2026" })
]);
const [health, roomTypes, owners, usage, dashboard] = requests;
const checks = {
health:
health.statusCode === 200
&& health.json().database === "booking_test"
&& health.json().schema === "condon",
roomTypes:
roomTypes.statusCode === 200
&& roomTypes.json().length === 11,
ownersImported:
owners.statusCode === 200
&& owners.json().total === 388
&& owners.json().items.length > 0,
usageImported:
usage.statusCode === 200
&& usage.json().total === 157
&& usage.json().items.length > 0,
dashboardImported:
dashboard.statusCode === 200
&& dashboard.json().ownerRooms === 388
&& dashboard.json().remainingPrivileges === 5394
&& dashboard.json().used === 438,
openApiPaths: Object.keys(app.swagger().paths ?? {}).length === 6
};
process.stdout.write(`${JSON.stringify({
ok: Object.values(checks).every(Boolean),
checks
}, null, 2)}\n`);
if (Object.values(checks).some(result => !result)) process.exitCode = 1;
} catch (error) {
process.stdout.write(`${JSON.stringify(safeError(error), null, 2)}\n`);
process.exitCode = 1;
} finally {
if (app) {
await app.close().catch(() => {
process.exitCode = 1;
});
}
}