import { before, after, describe, it } from "node:test"; import { ObjeniousOperationsRepository } from "./ObjeniousOperationRepository.js"; import { httpObjClient, postgresClient } from "../config/config.test.js"; import { ObjeniousOperation } from "../domain/operationsRepository.port.js"; const correctOperation: ObjeniousOperation = { iccids: "test", operation: "activate", status: "finished" } const errorOperation: ObjeniousOperation = { iccids: "test", operation: "terminate", status: "error", error: "mensaje de error" } describe("[Integration] Test API requests", () => { const repository = new ObjeniousOperationsRepository( httpObjClient, postgresClient ) const suspend_iccid = "test_suspended_time_iccid"; before(async () => { await repository.createOperation(correctOperation) await repository.createOperation(errorOperation) }) it("Read last operation by line", () => { /** * Objetivo: * - Cuando se va a hacer una operacion de sim hay que cancelarla directamente si: * - Ya hay una en curso del mismo tipo. * - Ya ha terminado una del mismo tipo. * - Se ignoran las erroneas */ }) it("Calculates suspended time accurately", async () => { // Se crean registros con un iccid concocido await postgresClient.query(` INSERT INTO objenious_operation (operation, iccids, status, error, start_date, end_date) VALUES ('suspend', $1, 'finished', NULL, NOW() - INTERVAL '3 days', NOW() - INTERVAL '3 days'), ('reactivate', $1, 'finished', NULL, NOW() - INTERVAL '2 days', NOW() - INTERVAL '2 days'), ('suspend', $1, 'finished', NULL, NOW() - INTERVAL '1 day', NOW() - INTERVAL '1 day'); `, [suspend_iccid]); const result = await repository.getSuspendedTime(suspend_iccid); if (result.error) { throw new Error("Query returned an error: " + result.error); } // Se esperan mas o menos 2 dias para cada periodo, total 4 (Puede cambiar un poco por zona horaria) // 2 dias en ms const expectedApproxMs = 2 * 24 * 60 * 60 * 1000; const msDiff = Math.abs(result.data!.total_milliseconds - expectedApproxMs); // Margen de 5s if (msDiff > 5000) { throw new Error(`Expected approx ${expectedApproxMs} ms, got ${result.data!.total_milliseconds}`); } // como se incluye el dia de sespension los dias van a variar de 2 a 3 if (result.data!.total_days < 2) { throw new Error("total_days should be at least 2"); } }); after(async () => { // Eliminacion de los iccid de periodo de suspensiones await postgresClient.query(`DELETE FROM objenious_operation WHERE iccids = '${suspend_iccid}'`); }); })