2026-04-29 17:08:30 +02:00
|
|
|
import { before, after, describe, it } from "node:test";
|
2026-04-07 13:20:31 +02:00
|
|
|
import { ObjeniousOperationsRepository } from "./ObjeniousOperationRepository.js";
|
|
|
|
|
import { httpObjClient, postgresClient } from "../config/config.test.js";
|
2026-04-15 12:47:26 +02:00
|
|
|
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"
|
|
|
|
|
}
|
2026-04-07 13:20:31 +02:00
|
|
|
|
|
|
|
|
describe("[Integration] Test API requests", () => {
|
|
|
|
|
const repository = new ObjeniousOperationsRepository(
|
|
|
|
|
httpObjClient,
|
|
|
|
|
postgresClient
|
|
|
|
|
)
|
2026-04-29 17:08:30 +02:00
|
|
|
const suspend_iccid = "test_suspended_time_iccid";
|
2026-04-07 13:20:31 +02:00
|
|
|
|
2026-04-15 12:47:26 +02:00
|
|
|
before(async () => {
|
|
|
|
|
await repository.createOperation(correctOperation)
|
|
|
|
|
await repository.createOperation(errorOperation)
|
|
|
|
|
})
|
2026-04-07 13:20:31 +02:00
|
|
|
|
2026-04-15 12:47:26 +02:00
|
|
|
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.
|
2026-04-15 13:50:20 +02:00
|
|
|
* - Se ignoran las erroneas
|
2026-04-15 12:47:26 +02:00
|
|
|
*/
|
2026-04-07 13:20:31 +02:00
|
|
|
})
|
2026-04-29 17:08:30 +02:00
|
|
|
|
|
|
|
|
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}'`);
|
|
|
|
|
});
|
2026-04-07 13:20:31 +02:00
|
|
|
})
|