Files
edr-platform/integration/src/global-setup.ts
2026-08-03 12:57:32 +00:00

73 lines
2.8 KiB
TypeScript

/**
* Runs once before any spec: wait for both APIs, then seed.
*
* Seeds are the Cypress suite's fixtures, reused verbatim (they are idempotent
* `insert … where not exists`), plus one of our own for the second tenant:
* seed-users.sql → seed-company.sql (order matters; company needs the users)
* seed-import-corridor.sql (yards, locos, wagons, rates, distances)
* seed-bulk-items.sql (PER_ITEM break-bulk cargo types)
* seed-g1-train.sql (the 53-wagon BUILT container train)
* seed-g2-weight.sql (the two 3 500 T weight-bound trains)
* seed-government.sql (the kind='government' company)
* seed-company-b.sql (user2@gmail.com's company — this suite)
* seed-customs-service-type.sql (a service type that bundles customs)
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { Client } from "pg";
const API = process.env.IT_API_URL ?? "http://localhost:3111";
const PAYMENT_API = process.env.IT_PAYMENT_URL ?? "http://localhost:3113";
const GATEWAY = process.env.IT_GATEWAY_URL ?? "http://localhost:4600";
const DB_URL =
process.env.IT_DB_URL ?? "postgres://edr_e2e:edr_e2e@localhost:5543/edr_freight_e2e";
const CYPRESS_FIXTURES = join(process.cwd(), "..", "e2e", "freight", "cypress", "fixtures");
const OWN_FIXTURES = join(process.cwd(), "sql");
const SEEDS: Array<[dir: string, file: string]> = [
[CYPRESS_FIXTURES, "seed-users.sql"],
[CYPRESS_FIXTURES, "seed-company.sql"],
[CYPRESS_FIXTURES, "seed-import-corridor.sql"],
[CYPRESS_FIXTURES, "seed-bulk-items.sql"],
[CYPRESS_FIXTURES, "seed-g1-train.sql"],
[CYPRESS_FIXTURES, "seed-g2-weight.sql"],
[CYPRESS_FIXTURES, "seed-government.sql"],
[OWN_FIXTURES, "seed-company-b.sql"],
[OWN_FIXTURES, "seed-customs-service-type.sql"],
];
async function waitFor(label: string, url: string, attempts = 60): Promise<void> {
for (let i = 0; i < attempts; i++) {
try {
const res = await fetch(url);
if (res.ok) return;
} catch {
/* not up yet */
}
await new Promise((r) => setTimeout(r, 2000));
}
throw new Error(`${label} never became healthy at ${url}`);
}
export async function setup(): Promise<void> {
await Promise.all([
waitFor("freight-api", `${API}/api/health`),
waitFor("payment-api", `${PAYMENT_API}/health`),
waitFor("gateway-mock", `${GATEWAY}/__control/health`),
]);
const client = new Client({ connectionString: DB_URL });
await client.connect();
try {
for (const [dir, file] of SEEDS) {
await client.query(readFileSync(join(dir, file), "utf8"));
console.log(`it: seeded ${file}`);
}
} finally {
await client.end();
}
await fetch(`${GATEWAY}/__control/reset`, { method: "POST" });
}