import { expect } from 'chai'; import sinon from 'sinon'; import * as create from '../create'; import * as clone from '../clone'; import * as apiFunctions from '../../helpers/root-api'; import * as checkPackageAndNodeVersionHelper from '../../helpers/version-check'; import { SANDBOX_ROOT_API_DEFAULT_HOST } from '../../helpers/constants'; const newProduct = { product_module_definition_id: 'efeeae9b-64f7-4b40-8a2a-ae1fa629a4a6', product_module_id: '9ee8c7de-7c29-4274-a180-6fb585432474', version_minor: 1, version_major: 1, terms_file_id: 'fe5e2e04-fad1-11ed-b537-ef5e7f12bfe7', schedule_id: '87130c9c-e10a-4ee4-8e82-2b29d36a2633', welcome_letter_id: '9ad9d3d0-7345-4f66-8bf0-d500258b6fb7', member_certificate_id: '903bd518-c61c-4715-b14f-0928164dd7c8', claim_blocks_schema_id: '9f20b4fe-997e-4a27-8b59-2e0b4bb4e1b7', claim_schema_id: 'a11e5186-d9d8-4e7c-96c4-0f37430a919d', quote_schema_id: '021fa5bc-bbd9-479d-a5fd-0e1a5fba9a00', application_schema_id: '1657718e-b831-4719-a3db-60e83cdc9898', application_schema: null, code_id: 'e8de6e64-2e0c-4675-b8fc-7ce1a9208841', published_at: '2023-05-25T07:58:54.107Z', created_at: '2023-05-25T07:58:54.107Z', created_by: { type: '', id: '', owner_id: '', }, unit_testing_code_id: '', quote_summary_id: '', documentation_id: '', policy_anniversary_file_id: '', readme_markdown_id: '', changelog_markdown_id: '', settings: {}, }; const productModuleTemplatesArray = [ { product_module_id: '1286d760-6041-4cd9-9a2e-f21bc08b9079', organization_id: 'ba066044-c6d6-4f1f-b27f-578f04da0aa9', sandbox: true, live: false, draft_id: '50847029-9f99-4e6d-91e4-7a9deb611155', created_at: '2023-05-17T15:37:12.558Z', restricted: false, key: 'template_blank_starter', name: 'Blank Starter ', created_by: { type: 'user', id: '04f4c7aa-7bd9-439a-a76d-eb4166f31285', }, owned_by_organization_id: 'ba066044-c6d6-4f1f-b27f-578f04da0aa9', live_id: '6bcb75da-9dde-4db8-86d9-46ed7cd332c1', }, ]; describe('create', function () { const sandbox = sinon.createSandbox(); beforeEach(function () { sandbox.stub(checkPackageAndNodeVersionHelper, 'checkPackageAndNodeVersion').resolves({ latest: '0.123', current: '0.123', }); }); afterEach(function () { sandbox.verifyAndRestore(); }); it('should create a product module and clone it successfully', async function () { const consoleLogSpy = sandbox.spy(console, 'log'); // Arrange const apiKey = 'test-api-key'; const productModuleName = 'Test Product Module'; const productModuleKey = 'test-product-module'; const hostPath = SANDBOX_ROOT_API_DEFAULT_HOST; const expectedBody = { clone_from_product_module_id: '1286d760-6041-4cd9-9a2e-f21bc08b9079', name: productModuleName, key: productModuleKey, }; // Stub all requests to platform const rootApi = new apiFunctions.RootAPIHelper({ host: 'test', apiKey: 'sandbox_123' }); const sendStub = sandbox.stub(rootApi, 'send'); // POST sendStub .withArgs({ method: apiFunctions.HttpMethod.Post, path: `/insurance/product-modules`, body: expectedBody }) .resolves(newProduct); // GET sendStub.withArgs({ path: `/template-product-modules` }).resolves(productModuleTemplatesArray); sandbox.stub(apiFunctions, 'getNewApiHelper').returns(rootApi); const cloneStub = sandbox.stub(clone, 'clone'); cloneStub.withArgs({ apiKey, moduleKey: productModuleKey, options: { host: hostPath } }); // Act await create.create({ apiKey, productModuleName, productModuleKey, options: { host: hostPath } }); // Assert const expectedMessage = "Product module 'test-product-module' successfully created on the Root Platform"; const actualMessage = String(consoleLogSpy.getCall(0).args[0]); const expectedRegex = new RegExp(expectedMessage.replaceAll("'", String.raw`\'`).replaceAll('.', String.raw`\.`)); expect(actualMessage).to.match(expectedRegex); }); });