import sinon from 'sinon'; import { expect } from '../../../test-utils'; import { fetchSkillFiles, SkillFilesResponse } from '../fetch-skill-files'; import * as rootApiModule from '../root-api'; describe('fetchSkillFiles', function () { const sandbox = sinon.createSandbox(); afterEach(function () { sandbox.verifyAndRestore(); }); const validResponse: SkillFilesResponse = { overview: '# Overview\nTask map here', skills: [ { domain: 'schema-form', filename: 'schema-form-overview.mdc', content: '---\ndescription: overview\n---' }, { domain: 'claim-blocks', filename: 'claim-blocks-inputs.mdc', content: '---\ndescription: inputs\n---' }, ], }; it('should return skill files when endpoint responds successfully', async function () { const sendStub = sandbox.stub().resolves(validResponse); sandbox.stub(rootApiModule, 'RootAPIHelper').returns({ send: sendStub } as any); const result = await fetchSkillFiles({ apiKey: 'test_key', host: 'https://api.example.com' }); expect(result).to.deep.equal(validResponse); expect(sendStub.calledOnce).to.equal(true); expect(sendStub.firstCall.args[0].path).to.equal('/insurance/docs/ai-context/skill-files'); }); it('should return null when the API throws (e.g. 404 with throwResponseErrors)', async function () { const sendStub = sandbox.stub().rejects(new Error('Not found')); sandbox.stub(rootApiModule, 'RootAPIHelper').returns({ send: sendStub } as any); const result = await fetchSkillFiles({ apiKey: 'test_key', host: 'https://api.example.com' }); expect(result).to.equal(null); }); it('should return null on network errors', async function () { const sendStub = sandbox.stub().rejects(new Error('ECONNREFUSED')); sandbox.stub(rootApiModule, 'RootAPIHelper').returns({ send: sendStub } as any); const result = await fetchSkillFiles({ apiKey: 'test_key', host: 'https://unreachable.example.com' }); expect(result).to.equal(null); }); it('should construct RootAPIHelper with throwResponseErrors: true', async function () { const sendStub = sandbox.stub().resolves(validResponse); const constructorStub = sandbox.stub(rootApiModule, 'RootAPIHelper').returns({ send: sendStub } as any); await fetchSkillFiles({ apiKey: 'test_key', host: 'https://api.example.com' }); expect(constructorStub.calledOnce).to.equal(true); expect(constructorStub.firstCall.args[0]).to.deep.equal({ host: 'https://api.example.com', apiKey: 'test_key', throwResponseErrors: true, }); }); it('should return response with empty skills array without error', async function () { const emptyResponse: SkillFilesResponse = { overview: '# Empty', skills: [] }; const sendStub = sandbox.stub().resolves(emptyResponse); sandbox.stub(rootApiModule, 'RootAPIHelper').returns({ send: sendStub } as any); const result = await fetchSkillFiles({ apiKey: 'test_key', host: 'https://api.example.com' }); expect(result).to.deep.equal(emptyResponse); expect(result!.skills).to.have.length(0); }); });