"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("assert");
const domain_event_publisher_1 = require("./services/domain-event.publisher");
const event_subscriber_registry_1 = require("./services/event-subscriber.registry");
const inbox_service_1 = require("./services/inbox.service");
const integration_link_service_1 = require("./services/integration-link.service");
const types_1 = require("./types");
async function validatePublisher() {
    const createdEvent = {
        id: 'evt-1',
        eventName: 'operations.project.billing_ready',
        sourceModule: 'operations',
        aggregateType: 'project',
        aggregateId: 'prj-1',
        payload: { projectId: 'prj-1' },
        status: 'pending',
        attemptCount: 0,
        lastError: null,
        availableAt: new Date(),
        processedAt: null,
        createdAt: new Date(),
        updatedAt: new Date(),
    };
    const outboxService = {
        createEvent: async () => createdEvent,
    };
    let notified = 0;
    const notifier = {
        notifyEventWritten: () => {
            notified += 1;
        },
    };
    const publisher = new domain_event_publisher_1.DomainEventPublisher(outboxService, notifier);
    const result = await publisher.publishEvent({
        eventName: 'operations.project.billing_ready',
        sourceModule: 'operations',
        aggregateType: 'project',
        aggregateId: 'prj-1',
        payload: { projectId: 'prj-1' },
    });
    assert_1.strict.equal(result.id, 'evt-1');
    assert_1.strict.equal(notified, 1);
}
async function validateRegistry() {
    const registry = new event_subscriber_registry_1.EventSubscriberRegistry();
    registry.subscribe('operations.contract.payable_generated', 'consumer-low', async () => undefined, 1);
    registry.subscribe('operations.contract.payable_generated', 'consumer-high', async () => undefined, 10);
    const handlers = registry.getHandlers('operations.contract.payable_generated');
    assert_1.strict.equal(handlers.length, 2);
    const firstHandler = handlers[0];
    assert_1.strict.ok(firstHandler);
    assert_1.strict.equal(firstHandler.consumerName, 'consumer-high');
}
async function validateLinks() {
    const link = {
        id: 'link-1',
        sourceModule: 'operations',
        sourceEntityType: 'project',
        sourceEntityId: 'prj-1',
        targetModule: 'finance',
        targetEntityType: 'payable',
        targetEntityId: 'pay-1',
        linkType: types_1.LinkType.REFERENCE,
        metadata: null,
        createdAt: new Date(),
        updatedAt: new Date(),
    };
    const prisma = {
        integrationLink: {
            create: async () => link,
            findMany: async () => [link],
        },
    };
    const service = new integration_link_service_1.IntegrationLinkService(prisma);
    const created = await service.createLink({
        sourceModule: 'operations',
        sourceEntityType: 'project',
        sourceEntityId: 'prj-1',
        targetModule: 'finance',
        targetEntityType: 'payable',
        targetEntityId: 'pay-1',
        linkType: types_1.LinkType.REFERENCE,
    });
    const outbound = await service.findOutbound('operations', 'project', 'prj-1');
    assert_1.strict.equal(created.id, 'link-1');
    assert_1.strict.equal(outbound.length, 1);
}
async function validateInboxIdempotency() {
    const existing = {
        id: 1,
        outbox_event_id: 1,
        consumer_name: 'finance-module',
        status: 'processed',
        attempt_count: 1,
        last_error: null,
        processed_at: new Date(),
        created_at: new Date(),
        updated_at: new Date(),
    };
    const prisma = {
        inbox_event: {
            findFirst: async () => existing,
            create: async () => {
                throw new Error('create should not be called for existing inbox item');
            },
        },
    };
    const inbox = new inbox_service_1.InboxService(prisma);
    const result = await inbox.getOrCreate('1', 'finance-module');
    assert_1.strict.equal(result.id, '1');
}
async function main() {
    await validatePublisher();
    await validateRegistry();
    await validateLinks();
    await validateInboxIdempotency();
    // eslint-disable-next-line no-console
    console.log('Integration API validation passed');
}
main().catch((error) => {
    // eslint-disable-next-line no-console
    console.error('Integration API validation failed', error);
    process.exit(1);
});
//# sourceMappingURL=integration-api.validation.js.map