import { expect, describe, it, beforeAll } from 'vitest';
import { multipleMigrations } from '../db/database.js';
import { NangoAction } from './sync.js';
import connectionService from '../services/connection.service.js';
import environmentService from '../services/environment.service.js';
import { createConnectionSeeds } from '../db/seeders/connection.seeder.js';
import { createConfigSeeds } from '../db/seeders/config.seeder.js';
import { createEnvironmentSeed } from '../db/seeders/environment.seeder.js';
describe('Connection service integration tests', async () => {
    let env;
    beforeAll(async () => {
        await multipleMigrations();
        env = await createEnvironmentSeed();
        await createConfigSeeds(env);
    });
    describe('Nango object tests', () => {
        it('Should retrieve connections correctly if different connection credentials are passed in', async () => {
            const connections = await createConnectionSeeds(env);
            const [nangoConnectionId, secondNangoConnectionId] = connections;
            const establishedConnection = await connectionService.getConnectionById(nangoConnectionId);
            if (!establishedConnection) {
                throw new Error('Connection not established');
            }
            const environment = await environmentService.getById(establishedConnection.environment_id);
            if (!environment) {
                throw new Error('Environment not found');
            }
            const nangoProps = {
                host: 'http://localhost:3003',
                accountId: environment.account_id,
                connectionId: String(establishedConnection.connection_id),
                environmentId: environment.id,
                providerConfigKey: String(establishedConnection?.provider_config_key),
                provider: 'hubspot',
                activityLogId: 1,
                secretKey: '****',
                nangoConnectionId: nangoConnectionId,
                syncId: 'aaa-bbb-ccc',
                syncJobId: 2,
                lastSyncDate: new Date()
            };
            const nango = new NangoAction(nangoProps);
            // @ts-expect-error we are overriding a private method here
            nango.nango.getConnection = async (providerConfigKey, connectionId) => {
                const { response } = await connectionService.getConnection(connectionId, providerConfigKey, environment.id);
                return response;
            };
            const connection = await nango.getConnection();
            expect(connection).toBeDefined();
            expect(connection.connection_id).toBe(establishedConnection.connection_id);
            expect(connection.provider_config_key).toBe(establishedConnection.provider_config_key);
            const secondEstablishedConnection = await connectionService.getConnectionById(secondNangoConnectionId);
            if (!secondEstablishedConnection) {
                throw new Error('Connection not established');
            }
            const secondConnection = await nango.getConnection(secondEstablishedConnection.provider_config_key, String(secondEstablishedConnection.connection_id));
            expect(secondConnection).toBeDefined();
            expect(secondConnection.connection_id).toBe(secondEstablishedConnection.connection_id);
            expect(secondConnection.provider_config_key).toBe(secondEstablishedConnection.provider_config_key);
        });
    });
});
//# sourceMappingURL=sync.integration.test.js.map