import sinon from 'sinon'; import { expect } from '../../../test-utils'; import * as apiFunctions from '../../helpers/root-api'; import { logs } from '../logs'; import { rootConfigFileExample } from '../../../test-utils/test-root-funeral-pm'; import * as checkPackageAndNodeVersionHelper from '../../helpers/version-check'; import * as readAuthAndConfig from '../../helpers/read-auth-and-config'; import * as checkValidProductModuleDirectory from '../../helpers/check-valid-product-module-directory'; import { NetworkProductModuleCodeRun, ProductModuleCodeRunStatus } from '../../domain/product-module-code-run'; import { NetworkProductModuleCodeRunLogItem, ProductModuleCodeRunLogItemLevel, } from '../../domain/product-module-code-run-log-item'; import { Environment } from '../../domain/environment'; const productModuleRuns: NetworkProductModuleCodeRun[] = [ { product_module_code_run_id: '123', product_module_id: '123', product_module_definition_id: '123', triggered_by: { type: 'product_module', id: '123', owner_id: '123', }, created_at: '2023-04-11T13:31:22.822Z', completed_at: '2023-04-11T13:31:22.980Z', status: ProductModuleCodeRunStatus.Complete, environment: Environment.Sandbox, function_name: 'customScheduledFunction', policy_id: '123', }, ]; const productModuleRunLogs: NetworkProductModuleCodeRunLogItem[] = [ { product_module_code_run_id: '123', created_at: '2023-04-11T13:31:22.895Z', level: ProductModuleCodeRunLogItemLevel.Info, content: "I'm a scheduled function, and I'm running now!", }, ]; describe('logs', function () { const sandbox = sinon.createSandbox(); let readAuthAndConfigStub: sinon.SinonStub; beforeEach(function () { sandbox.stub(checkPackageAndNodeVersionHelper, 'checkPackageAndNodeVersion').resolves({ latest: '0.123', current: '0.123', }); sandbox.stub(checkValidProductModuleDirectory, 'checkValidProductModuleDirectory'); readAuthAndConfigStub = sandbox .stub(readAuthAndConfig, 'readAuthAndConfig') .returns({ apiKey: 'sandbox_123', config: rootConfigFileExample }); }); afterEach(function () { sandbox.verifyAndRestore(); }); it('should format and print execution logs', async function () { const consoleLogSpy = sandbox.spy(console, 'log'); // Stub all requests to platform const rootApi = new apiFunctions.RootAPIHelper({ host: 'test', apiKey: 'sandbox_123' }); const sendStub = sandbox.stub(rootApi, 'send'); sendStub .withArgs({ path: `/insurance/product-modules/${rootConfigFileExample.productModuleKey}` }) .resolves({ product_module_id: 123 }); sendStub .withArgs({ path: '/insurance/product-modules/123/runs', searchParams: { page: '1', page_size: '20', }, }) .resolves(productModuleRuns); sendStub.withArgs({ path: '/insurance/product-modules/123/runs/123/log-items' }).resolves(productModuleRunLogs); sandbox.stub(apiFunctions, 'getNewApiHelper').returns(rootApi); await logs({}); const firstLoggedLine = String(consoleLogSpy.getCall(0).args[0]) .replace('\u001B[32m', '') .replace('\u001B[39m', '') .replace('\u001B[1m', '') .replace('\u001B[22m', ''); expect(firstLoggedLine).to.equal('2023-04-11T13:31:22.822Z [complete] customScheduledFunction'); expect(consoleLogSpy.getCall(1).args[0]).to.equal( "2023-04-11T13:31:22.895Z [info] I'm a scheduled function, and I'm running now!", ); }); it('should validate that count is a number between 1 and 100', async function () { const errorMessage = 'Invalid value specified for count, must be a number between 1 and 100 (inclusive).'; await expect(logs({ count: '0' })).to.eventually.be.rejectedWith(errorMessage); await expect(logs({ count: '101' })).to.eventually.be.rejectedWith(errorMessage); await expect(logs({ count: 'test' })).to.eventually.be.rejectedWith(errorMessage); }); it('should not fetch production logs', async function () { readAuthAndConfigStub.reset(); readAuthAndConfigStub.returns({ apiKey: 'production_123', config: rootConfigFileExample }); const errorMessage = 'A sandbox API key is required to use this feature. Please update .root-auth accordingly.'; await expect(logs({})).to.eventually.be.rejectedWith(errorMessage); }); });