chore: more test cases

This commit is contained in:
Nathnael
2026-08-03 12:57:32 +00:00
parent 72532f36cd
commit 3b45990023
4 changed files with 627 additions and 0 deletions

View File

@@ -1492,6 +1492,38 @@ export async function expectContainersPlaced(scheduleId: string, containers: num
}
}
/**
* GROSS tonnage riding a schedule — cargo PLUS the tare of every wagon it
* occupies, because a locomotive hauls the wagon as well as what is in it.
*
* `allocated_weight_tons` holds the CARGO alone, so the tare of each allocated
* wagon type is added here. Reading the column raw understates a loaded consist
* by 22.4 T a wagon and makes a weight-bound train look half empty.
*/
export async function allocatedGrossTons(scheduleId: string): Promise<number> {
const [row] = await db<{ tons: string | null }>(
`SELECT COALESCE(sum(wba.allocated_weight_tons + wt.tare_weight_tons), 0)::text AS tons
FROM freight.wagon_booking_allocations wba
JOIN freight.train_set_wagons tsw ON tsw.id = wba.train_set_wagon_id
JOIN freight.wagon_types wt ON wt.id = tsw.wagon_type_id
JOIN freight.train_schedule_bookings tsb
ON tsb.booking_id = wba.booking_id AND tsb.train_schedule_id = $1
WHERE wba.deleted_at IS NULL AND tsb.deleted_at IS NULL`,
[scheduleId],
);
return Number(row.tons ?? 0);
}
/** Wagons a single booking currently holds. */
export async function wagonAllocationCount(bookingId: string): Promise<number> {
const [row] = await db<{ n: string }>(
`SELECT count(*)::text AS n FROM freight.wagon_booking_allocations
WHERE booking_id = $1 AND deleted_at IS NULL`,
[bookingId],
);
return Number(row.n);
}
/** Bookings still linked to a schedule — the seats actually held. */
export async function linkedBookings(scheduleId: string): Promise<number> {
const [row] = await db<{ n: string }>(