import path from 'node:path'; import fs from 'node:fs'; import sinon from 'sinon'; import chalk from 'chalk'; import * as invokeHelpers from '../invoke'; import { expect } from '../../../test-utils'; import * as apiFunctions from '../../helpers/root-api'; import * as readAuthAndConfigHelper from '../../helpers/read-auth-and-config'; import * as stubData from './stub-data'; import * as payloadsHelpers from '../../helpers/payloads-helpers'; import { ProductModulePayloadType } from '../../domain/invoke-payloads'; describe('invoke claims', function () { const sandbox = sinon.createSandbox(); beforeEach(function () { sandbox.stub(invokeHelpers, 'validateConfigAndEnvironment').resolves(true); sandbox.stub(readAuthAndConfigHelper, 'readAuthAndConfig').returns(stubData.productModuleAuthAndConfig); const payloadsHelpersMock = sandbox.mock(payloadsHelpers); payloadsHelpersMock .expects('readRequestPayloadFromFile') .withArgs({ type: ProductModulePayloadType.Quote }) .returns(stubData.quotePayload); payloadsHelpersMock .expects('readRequestPayloadFromFile') .withArgs({ type: ProductModulePayloadType.Application }) .returns(stubData.applicationPayload); // Create a mock instance of RootAPIHelper const rootApi = new apiFunctions.RootAPIHelper({ host: 'test', apiKey: 'sandbox_123' }); // Stub the send method of the mock instance const sendStub = sandbox.stub(rootApi, 'send'); sendStub.withArgs({ ...stubData.quoteAPIArgs, body: stubData.quotePayload }).resolves(stubData.quotePackage); sendStub .withArgs({ ...stubData.policyholderAPIArgs, body: stubData.policyholderPayload }) .resolves(stubData.policyholderResponse); sendStub .withArgs({ ...stubData.applicationAPIArgs, body: stubData.applicationPayload }) .resolves(stubData.applicationResponse); sendStub.withArgs(stubData.policyAPIArgs).resolves(stubData.policyResponse); sendStub.withArgs(stubData.claimAPIArgs).resolves(stubData.claimResponse); sandbox.stub(apiFunctions, 'getNewApiHelper').returns(rootApi); }); afterEach(function () { sandbox.verifyAndRestore(); }); it('should open a claim', async function () { const consoleLogSpy = sandbox.spy(console, 'log'); await invokeHelpers.invoke({ claim: true }); const linkClaimUrl = `https://app.rootplatform.com/orgs/123/insurance/claims/${stubData.claimResponse.claim_id}`; const linkCLaimText = 'Click here to view on dashboard.'; const expectedMessage = `\n${chalk.green('Claim opened successfully! ')}${chalk.blue( `\u001B]8;;${linkClaimUrl}\u001B\\${linkCLaimText}\u001B]8;;\u001B\\`, )}`; const actualMessage = consoleLogSpy.args[0][0]; expect(actualMessage).to.equal(expectedMessage); }); }); describe('invoke policyholder', function () { const sandbox = sinon.createSandbox(); const rootApi = new apiFunctions.RootAPIHelper({ host: 'test', apiKey: 'sandbox_123' }); beforeEach(function () { sandbox.stub(invokeHelpers, 'validateConfigAndEnvironment').resolves(true); sandbox.stub(readAuthAndConfigHelper, 'readAuthAndConfig').returns(stubData.productModuleAuthAndConfig); const payloadsHelpersMock = sandbox.mock(payloadsHelpers); payloadsHelpersMock .expects('readRequestPayloadFromFile') .withArgs({ type: ProductModulePayloadType.Quote }) .returns(stubData.quotePayload); const sendStub = sandbox.stub(rootApi, 'send'); sendStub.withArgs({ ...stubData.quoteAPIArgs, body: stubData.quotePayload }).resolves(stubData.quotePackage); sendStub .withArgs({ ...stubData.policyholderAPIArgs, body: stubData.policyholderPayload }) .resolves(stubData.policyholderResponse); }); afterEach(function () { sandbox.verifyAndRestore(); }); it('should issue a policy using policyholder payload if available', async function () { const fsMock = sandbox.mock(fs); fsMock.expects('existsSync').withArgs('payloads/policyholder.json').returns(true); fsMock .expects('existsSync') .withArgs(path.join('.', 'payloads', 'policyholder.json')) .returns(true); const policyholderPayloadFileContents = JSON.stringify(stubData.policyholderPayload); const readPayloadFileStub = fsMock .expects('readFileSync') .withArgs(path.join('.', 'payloads', 'policyholder.json'), { encoding: 'utf8' }) .returns(Buffer.from(policyholderPayloadFileContents)); sandbox.stub(apiFunctions, 'getNewApiHelper').returns(rootApi); await invokeHelpers.invoke({ quote: true }); expect(readPayloadFileStub.calledOnce).to.be.equal(true); expect(JSON.parse(readPayloadFileStub.returnValues[0])).to.deep.equal(stubData.policyholderPayload); }); });