import { expect, describe, it, beforeAll } from 'vitest';
import db, { multipleMigrations } from '../db/database.js';
import connectionService from './connection.service.js';
import { createConfigSeeds } from '../db/seeders/config.seeder.js';
import { createConnectionSeeds } from '../db/seeders/connection.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('Metadata simple operations', async () => {
        it('Should replace existing metadata, overwriting anything existing', async () => {
            const connections = await createConnectionSeeds(env);
            const initialMetadata = {
                name: 'test',
                host: 'test'
            };
            const newMetadata = {
                additionalName: 'test23'
            };
            const [connectionId] = connections;
            const connection = { id: connectionId };
            await db.knex.transaction(async (trx) => {
                await connectionService.replaceMetadata([connection.id], initialMetadata, trx);
                await connectionService.replaceMetadata([connection.id], newMetadata, trx);
            });
            const dbConnection = await connectionService.getConnectionById(connectionId);
            const updatedMetadata = dbConnection?.metadata;
            expect(updatedMetadata).toEqual(newMetadata);
        });
        it('Should update metadata and not overwrite', async () => {
            const connections = await createConnectionSeeds(env);
            const initialMetadata = {
                name: 'test',
                host: 'test'
            };
            const newMetadata = {
                additionalName: 'test23'
            };
            const connectionId = connections[1];
            const dbConnection = (await connectionService.getConnectionById(connectionId));
            await db.knex.transaction(async (trx) => {
                await connectionService.replaceMetadata([dbConnection.id], initialMetadata, trx);
            });
            const updatedMetadataConnection = (await connectionService.getConnectionById(connectionId));
            await connectionService.updateMetadata([updatedMetadataConnection], newMetadata);
            const updatedDbConnection = await connectionService.getConnectionById(connectionId);
            const updatedMetadata = updatedDbConnection?.metadata;
            expect(updatedMetadata).toEqual({ ...initialMetadata, ...newMetadata });
        });
    });
});
//# sourceMappingURL=connection.service.integration.test.js.map