From c1a8182e7f1820ad6f4c7f08cdd37026687bf69c Mon Sep 17 00:00:00 2001 From: kurok <22548029+kurok@users.noreply.github.com> Date: Tue, 9 Jun 2026 09:30:46 +0100 Subject: [PATCH] test: assert boot initialization and clear cleanup in unit.test.mjs - Verify a booted client is actually initialized from the supplied options: the API url is wired into the API client and the auth provider is a VaultTokenAuth carrying the configured token. - Verify clear() releases instance resources by spying on the instance's close() (which cancels the auth-refresh timer) and asserting it is invoked once before the instance is removed. Signed-off-by: kurok <22548029+kurok@users.noreply.github.com> --- test/unit.test.mjs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/unit.test.mjs b/test/unit.test.mjs index fe311ec..f8c36e9 100644 --- a/test/unit.test.mjs +++ b/test/unit.test.mjs @@ -1,6 +1,11 @@ import deepFreeze from 'deep-freeze'; -import { expect } from 'chai'; +import sinon from 'sinon'; +import { expect, use } from 'chai'; +import sinonChai from 'sinon-chai'; import VaultClient from '../src/VaultClient.js'; +import VaultTokenAuth from '../src/auth/VaultTokenAuth.js'; + +use(sinonChai); describe('Unit tests', function () { @@ -19,11 +24,17 @@ describe('Unit tests', function () { afterEach(function () { VaultClient.clear(); + sinon.restore(); }); - it('should boot a new VaultClient instance', () => { + it('should boot a new VaultClient instance initialized from the given options', () => { const client = VaultClient.boot('primaryClient', bootOpts); expect(client).to.be.instanceOf(VaultClient); + + // The configuration is wired into the API client and the auth provider. + expect(client.__api.__config.url).to.equal(bootOpts.api.url); + expect(client.__auth).to.be.instanceOf(VaultTokenAuth); + expect(client.__auth.__token).to.equal(bootOpts.auth.config.token); }); it('should return the same instance when booting with the same name', () => { @@ -41,9 +52,14 @@ describe('Unit tests', function () { expect(() => VaultClient.get('neverBootedClient')).to.throw(Error, 'Invalid instance name'); }); - it('should clear a client and allow re-creation', () => { + it('should release the instance resources and allow re-creation when cleared', () => { const client = VaultClient.boot('primaryClient', bootOpts); + const closeSpy = sinon.spy(client, 'close'); + VaultClient.clear('primaryClient'); + + // clear() disposes the instance (close() cancels the auth-refresh timer) before removing it. + expect(closeSpy).to.have.been.calledOnce; expect(() => VaultClient.get('primaryClient')).to.throw(Error, 'Invalid instance name'); const recreatedClient = VaultClient.boot('primaryClient', bootOpts);