"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const tsyringe_1 = require("tsyringe");
const SecurityService_1 = __importDefault(require("./SecurityService"));
const Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const GetSecurityQuery_1 = require("../../usecase/query/security/get/GetSecurityQuery");
const Security_1 = require("../../../domain/context/security/Security");
const SecurityNotFound_1 = require("./error/SecurityNotFound");
const ts_jest_1 = require("@golevelup/ts-jest");
const SecurityFixture_1 = require("../../../../__tests__/fixtures/shared/SecurityFixture");
describe("SecurityService", () => {
    let service;
    const queryBusMock = (0, ts_jest_1.createMock)();
    const security = new Security_1.Security(SecurityFixture_1.SecurityPropsFixture.create());
    const securityId = security.diamondAddress.toString();
    beforeEach(() => {
        service = new SecurityService_1.default();
        jest.spyOn(Injectable_1.default, "resolve").mockReturnValue(queryBusMock);
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("get", () => {
        it("should return a Security object for a valid securityId", async () => {
            queryBusMock.execute.mockResolvedValue({ security });
            const result = await service.get(securityId);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetSecurityQuery_1.GetSecurityQuery(securityId));
            expect(result.diamondAddress).toMatchObject(security.diamondAddress);
        });
        it.each([
            [
                "missing name",
                {
                    decimals: security.decimals,
                    symbol: security.symbol,
                    isin: security.isin,
                    evmDiamondAddress: security.evmDiamondAddress,
                },
            ],
            [
                "missing decimals",
                {
                    name: security.name,
                    symbol: security.symbol,
                    isin: security.isin,
                    evmDiamondAddress: security.evmDiamondAddress,
                },
            ],
            [
                "missing symbol",
                {
                    name: security.name,
                    decimals: security.decimals,
                    isin: security.isin,
                    evmDiamondAddress: security.evmDiamondAddress,
                },
            ],
            [
                "missing isin",
                {
                    name: security.name,
                    decimals: security.decimals,
                    symbol: security.symbol,
                    evmDiamondAddress: security.evmDiamondAddress,
                },
            ],
            [
                "missing evmDiamondAddress",
                {
                    name: security.name,
                    decimals: security.decimals,
                    symbol: security.symbol,
                    isin: security.isin,
                },
            ],
        ])("should throw SecurityNotFound when %s", async (_description, incompleteViewModel) => {
            queryBusMock.execute.mockResolvedValue({
                security: incompleteViewModel,
            });
            await expect(service.get(securityId)).rejects.toThrow(SecurityNotFound_1.SecurityNotFound);
            await expect(service.get(securityId)).rejects.toThrow(`${securityId} was not found`);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetSecurityQuery_1.GetSecurityQuery(securityId));
        });
        it("should be a singleton", () => {
            const service1 = tsyringe_1.container.resolve(SecurityService_1.default);
            const service2 = tsyringe_1.container.resolve(SecurityService_1.default);
            expect(service1).toBe(service2);
        });
    });
});
