// SPDX-License-Identifier: Apache-2.0
import { expect } from 'chai';
import { LocalConfigRuntimeState } from '../../../../src/business/runtime-state/config/local/local-config-runtime-state.js';
import { DeploymentNotFoundError } from '../../../../src/business/errors/deployment-not-found-error.js';
import { getTemporaryDirectory } from '../../../test-utility.js';
import fs from 'node:fs';
import { PathEx } from '../../../../src/business/utils/path-ex.js';
describe('LocalConfigRuntimeState', () => {
    let runtimeState;
    let basePath;
    const testFileName = 'local-config.yaml';
    async function createDeployment() {
        if (!runtimeState.isLoaded) {
            await runtimeState.load();
        }
        const deployment = runtimeState.configuration.deployments.addNew();
        deployment.name = 'deployment-1';
        deployment.namespace = 'namespace-1';
        deployment.realm = 1;
        deployment.shard = 2;
        await runtimeState.persist();
        await runtimeState.load();
    }
    beforeEach(() => {
        basePath = getTemporaryDirectory();
        runtimeState = new LocalConfigRuntimeState(basePath, testFileName);
    });
    it('should create a new configuration file', async () => {
        await runtimeState.persist();
        expect(fs.existsSync(PathEx.join(basePath, testFileName))).to.be.true;
        expect(fs.readFileSync(PathEx.join(basePath, testFileName), 'utf8')).to.not.be.empty;
    });
    it('should load configuration successfully', async () => {
        await runtimeState.persist();
        await runtimeState.load();
        expect(fs.existsSync(PathEx.join(basePath, testFileName))).to.be.true;
        expect(fs.readFileSync(PathEx.join(basePath, testFileName), 'utf8')).to.not.be.empty;
    });
    it('should return deployments', async () => {
        await createDeployment();
        const deployments = runtimeState.configuration.deployments;
        expect(deployments.find((d) => d.name === 'deployment-1')).to.not.be.undefined;
    });
    it('should throw DeploymentNotFoundError if deployment is not found', async () => {
        await runtimeState.load();
        expect(() => runtimeState.configuration.deploymentByName('non-existent-deployment')).to.throw(DeploymentNotFoundError);
    });
    it('should return the realm of a deployment', async () => {
        await createDeployment();
        const realm = runtimeState.configuration.realmForDeployment('deployment-1');
        expect(realm).to.equal(1);
    });
    it('should return the shard of a deployment', async () => {
        await createDeployment();
        const shard = runtimeState.configuration.shardForDeployment('deployment-1');
        expect(shard).to.equal(2);
    });
});
//# sourceMappingURL=local-config-runtime-state.test.js.map