// SPDX-License-Identifier: Apache-2.0
import { expect } from 'chai';
import sinon from 'sinon';
import { RemoteConfigSource } from '../../../../../src/data/configuration/impl/remote-config-source.js';
import { RemoteConfigSchemaDefinition } from '../../../../../src/data/schema/migration/impl/remote/remote-config-schema-definition.js';
import { SimpleObjectStorageBackend } from '../../../fixtures/simple-object-storage-backend.fixture.js';
describe('RemoteConfigSource', () => {
    let schema;
    let mapper;
    let backend;
    let source;
    const map = new Map([
        [
            'local-config',
            {
                schemaVersion: 1,
                deployments: [{ name: 'true', namespace: 'false', clusters: ['true', { key: 'value' }, '{"key": "value"}'] }],
            },
        ],
    ]);
    beforeEach(() => {
        mapper = {};
        schema = new RemoteConfigSchemaDefinition(mapper);
        backend = new SimpleObjectStorageBackend(map);
        sinon.stub(backend, 'writeObject').resolves();
        source = new RemoteConfigSource(schema, mapper, backend);
        mapper.applyPropertyValue = sinon.stub();
    });
    it('should call backend.writeObject on persist', async () => {
        await source.persist();
        expect(backend.writeObject.calledOnce).to.be.true;
    });
    it('should instantiate without error', () => {
        expect(() => new RemoteConfigSource(schema, mapper, backend)).not.to.throw();
    });
    it('should have name "RemoteConfigSource"', () => {
        const source = new RemoteConfigSource(schema, mapper, backend);
        expect(source.name).to.equal('RemoteConfigSource');
    });
    it('should have ordinal 300', () => {
        const source = new RemoteConfigSource(schema, mapper, backend);
        expect(source.ordinal).to.equal(300);
    });
    it('should call load() when refresh() is called', async () => {
        const source = new RemoteConfigSource(schema, mapper, backend);
        const loadStub = sinon.stub(source, 'load').resolves();
        await source.refresh();
        expect(loadStub.calledOnce).to.be.true;
        loadStub.restore();
    });
    it('should throw if putObject called with missing key', () => {
        expect(() => source.putObject(undefined, { foo: 1 })).to.throw('key must not be null or undefined');
    });
    it('should throw if putObjectArray called with missing key', () => {
        expect(() => source.putObjectArray(undefined, [{ foo: 1 }])).to.throw('key must not be null or undefined');
    });
    it('should throw if putScalar called with missing key', () => {
        expect(() => source.putScalar(undefined, 'val')).to.throw('key must not be null or undefined');
    });
    it('should throw if putScalarArray called with missing key', () => {
        expect(() => source.putScalarArray(undefined, ['a'])).to.throw('key must not be null or undefined');
    });
});
//# sourceMappingURL=remote-config-source.test.js.map