From 4090627724081a0895b679760f73646941a1ea47 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 3 Jan 2023 14:05:09 +0100 Subject: [PATCH] fix: config.replace tests --- test/interface-tests.spec.js | 10 +----- test/interface-tests/src/config/replace.js | 39 ++++++++++++++++++---- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/test/interface-tests.spec.js b/test/interface-tests.spec.js index 693a42092..a1cab8dee 100644 --- a/test/interface-tests.spec.js +++ b/test/interface-tests.spec.js @@ -45,15 +45,7 @@ function executeTests (commonFactory) { tests.bootstrap(commonFactory) - tests.config(commonFactory, { - skip: [ - // config.replace - { - name: 'replace', - reason: 'FIXME https://github.com/ipfs/js-kubo-rpc-client/issues/97' - } - ] - }) + tests.config(commonFactory) tests.dag(commonFactory) diff --git a/test/interface-tests/src/config/replace.js b/test/interface-tests/src/config/replace.js index 3850c456b..26a8724e7 100644 --- a/test/interface-tests/src/config/replace.js +++ b/test/interface-tests/src/config/replace.js @@ -26,24 +26,49 @@ export function testReplace (factory, options) { after(async function () { return await factory.clean() }) - const config = { - Addresses: { - API: '' + it('should replace the whole config', async () => { + const config = { + Addresses: { + API: '/ip4/1.2.3.4/tcp/54321/' + } } - } - it('should replace the whole config', async () => { await ipfs.config.replace(config) const _config = await ipfs.config.getAll() - expect(_config).to.deep.equal(config) + expect(cleanConfig(_config)).to.deep.equal(config) }) it('should replace to empty config', async () => { await ipfs.config.replace({}) const _config = await ipfs.config.getAll() - expect(_config).to.deep.equal({}) + expect(cleanConfig(_config)).to.deep.equal({}) }) }) } + +// cleanConfig removes all null properties of the configuration. When replacing +// a configuration, Kubo always fills missing properties with null values. +function cleanConfig (config) { + if (Array.isArray(config)) { + return config.filter(e => Boolean(e)) + } + + return Object.keys(config).reduce((acc, key) => { + if (!config[key]) { + return acc + } + + if (typeof config[key] === 'object') { + const o = cleanConfig(config[key]) + if (o && Object.keys(o).length) { + acc[key] = o + } + } else if (config[key]) { + acc[key] = config[key] + } + + return acc + }, {}) +}