122 lines
4.2 KiB
TypeScript
122 lines
4.2 KiB
TypeScript
import { describe, it, beforeEach, mock, after } from "node:test";
|
|
import assert from "node:assert";
|
|
import { SimController } from "./Sim.controller.js";
|
|
import { EventBus } from "sim-shared/domain/EventBus.port.js";
|
|
import { SimUseCases } from "./Sim.usecases.js";
|
|
import { ConsumeMessage } from "amqplib";
|
|
import { postgrClient, pgPool } from "#config/postgreConfig.js";
|
|
import { httpInstance } from "#config/httpClient.config.js";
|
|
import { OrderRepository } from "sim-shared/infrastructure/OrderRepository.js";
|
|
import { PauseCancelTaskRepository } from "#adapters/PauseCancelTaskRepository.js";
|
|
import { ObjeniousOperationsRepository } from "sim-shared/infrastructure/ObjeniousOperationRepository.js";
|
|
import { ActionData } from "#domain/DTOs/objeniousapi.js";
|
|
import { ObjeniousLinesRepository } from "sim-shared/infrastructure/ObjeniousLinesRepository.js";
|
|
|
|
describe("SimController Integration Tests (Real UseCases)", () => {
|
|
let eventBusMock: any;
|
|
let controller: SimController;
|
|
let useCases: SimUseCases;
|
|
|
|
beforeEach(() => {
|
|
// Mock ONLY the event bus as requested
|
|
eventBusMock = {
|
|
publish: mock.fn(),
|
|
addSubscribers: mock.fn(),
|
|
consume: mock.fn(),
|
|
ack: mock.fn(async () => { }),
|
|
nack: mock.fn(async () => { }),
|
|
};
|
|
|
|
const operationRepository = new ObjeniousOperationsRepository(
|
|
httpInstance,
|
|
postgrClient,
|
|
);
|
|
const orderRepository = new OrderRepository(postgrClient);
|
|
const pauseRepository = new PauseCancelTaskRepository(postgrClient);
|
|
const linesRepository = new ObjeniousLinesRepository(postgrClient) // tiene que apuntar a "intranet"
|
|
useCases = new SimUseCases({
|
|
httpClient: httpInstance,
|
|
operationRepository: operationRepository,
|
|
orderRepository: orderRepository,
|
|
pauseRepository: pauseRepository,
|
|
objeniousLinesRepository: linesRepository
|
|
});
|
|
// @ts-expect-error
|
|
useCases.findActivationDate = async (data: ActionData) => new Date()
|
|
|
|
controller = new SimController(eventBusMock as unknown as EventBus, useCases);
|
|
});
|
|
|
|
const createMockMsg = (payload: any): ConsumeMessage => {
|
|
return {
|
|
content: Buffer.from(JSON.stringify(payload)),
|
|
fields: {},
|
|
properties: {
|
|
headers: {
|
|
message_id: "test-correlation-id"
|
|
}
|
|
},
|
|
} as unknown as ConsumeMessage;
|
|
};
|
|
|
|
after(async () => {
|
|
await pgPool.end();
|
|
});
|
|
|
|
describe("suspend", () => {
|
|
it("should call stage_suspend and interact with DB and EventBus", async () => {
|
|
const iccid = "test-iccid-suspend-" + Date.now();
|
|
const msg = createMockMsg({
|
|
key: "sim.test.pause",
|
|
payload: {
|
|
iccid: iccid
|
|
},
|
|
headers: {
|
|
message_id: "correlation-suspend-" + iccid
|
|
}
|
|
});
|
|
|
|
const handler = controller.suspend();
|
|
await handler(msg);
|
|
|
|
// Verify that it reached the stage_suspend logic (which adds to pauseRepository)
|
|
// We can query the DB or check if ACK was called
|
|
assert.strictEqual(eventBusMock.ack.mock.callCount(), 1, "Message should be ACKed on success");
|
|
assert.strictEqual(eventBusMock.nack.mock.callCount(), 0, "Message should not be NACKed");
|
|
});
|
|
});
|
|
|
|
describe("terminate", () => {
|
|
it("should call stage_terminate and interact with DB and EventBus", async () => {
|
|
const iccid = "test-iccid-terminate-" + Date.now();
|
|
const msg = createMockMsg({
|
|
key: "sim.test.pause",
|
|
payload: {
|
|
iccid: iccid
|
|
},
|
|
headers: {
|
|
message_id: "correlation-terminate-" + iccid
|
|
}
|
|
});
|
|
|
|
const handler = controller.terminate();
|
|
await handler(msg);
|
|
|
|
assert.strictEqual(eventBusMock.ack.mock.callCount(), 1, "Message should be ACKed on success");
|
|
assert.strictEqual(eventBusMock.nack.mock.callCount(), 0, "Message should not be NACKed");
|
|
});
|
|
});
|
|
|
|
describe("Error Handling", () => {
|
|
it("should nack if message is invalid", async () => {
|
|
const msg = {
|
|
content: Buffer.from("invalid json"),
|
|
fields: {},
|
|
properties: {},
|
|
} as unknown as ConsumeMessage;
|
|
const handler = controller.suspend();
|
|
await assert.rejects(handler(msg), "Error de suspension consumiendo el mensaje no es valido");
|
|
});
|
|
});
|
|
});
|