import { JobManifestV1 } from './JobManifestV1'; import { JobManifestV1Model } from '../../../domains/manifest/ManifestModel'; import path from 'path'; const fileRoot = process.cwd(); describe('JobManifestV1', () => { it('returns correct value for getSchemaVersionRegex()', async () => { expect(JobManifestV1.getJobSchemaVersionRegex()).toEqual(/\/schemas\/(?JobV\d+)\.json#?$/); }); const jobManifestModel = { $schema: 'http://localhost:3040/schemas/JobV1.json#', name: 'bar', version: '1.0.0', mainFunction: 'main', concurrency: 5, timeout: 10000, displayName: 'some display name', description: 'some description', functions: [ { name: 'main', input: { required: ['foo'], properties: { foo: { type: 'string', }, }, }, entry: 'main.js', }, { name: 'func1', input: { required: ['foo'], properties: { foo: { type: 'string', }, }, }, entry: 'func1.js', }, ], } as JobManifestV1Model; const jobManifestModelWithoutMainFunction = { $schema: 'http://localhost:3040/schemas/JobV1.json#', name: 'bar', version: '1.0.0', mainFunction: 'main', concurrency: 5, timeout: 10000, displayName: 'some display name', description: 'some description', functions: [ { name: 'notmain', input: { required: ['foo'], properties: { foo: { type: 'string', }, }, }, entry: 'notmain.js', }, ], } as JobManifestV1Model; const manifestPath = `${fileRoot}/manifest.json`; const manifest = new JobManifestV1(jobManifestModel, manifestPath); const manifestWithoutMainFunction = new JobManifestV1(jobManifestModelWithoutMainFunction, manifestPath); it('returns correct value for getName()', async () => { expect(manifest.getName()).toEqual('bar'); }); it('returns correct value for getSchemaVersion()', async () => { expect(manifest.getSchemaVersion()).toEqual('JobV1'); }); it('returns correct value for getModel()', async () => { expect(manifest.getModel()).toEqual(jobManifestModel); }); it('should return the job function associated with the function name', async () => { const loadedFunction = manifest.getJobFunctionByName('func1'); const expectedFunction = { name: 'func1', input: { required: ['foo'], properties: { foo: { type: 'string', }, }, }, entry: 'func1.js', }; expect(loadedFunction).toEqual(expectedFunction); }); it('should return the job main function definition when the function name is undefined', async () => { const loadedFunction = manifest.getJobFunctionByName(undefined); const expectedFunction = { name: 'main', input: { required: ['foo'], properties: { foo: { type: 'string', }, }, }, entry: 'main.js', }; expect(loadedFunction).toEqual(expectedFunction); }); it('should throw a meaningful error when the job main function does not exist and function name passed in is undefined', async () => { await expect(async () => manifestWithoutMainFunction.getJobFunctionByName(undefined), ).rejects.toThrowErrorMatchingInlineSnapshot(`""main" function for bar 1.0.0 could not be found"`); }); it('should throw a meaningful error if a function associated with the function name does not exist on the manifest', async () => { await expect(async () => manifest.getJobFunctionByName('fake-function-name'), ).rejects.toThrowErrorMatchingInlineSnapshot(`""fake-function-name" function for bar 1.0.0 could not be found"`); }); it('should return a functions absolute path to its entry file', async () => { const func1 = manifest.getJobFunctionByName('func1'); await expect(manifest.getJobFunctionEntryFilePath(func1)).resolves.toEqual(fileRoot + path.sep + 'func1.js'); }); it('should return the jobs directory', async () => { const jobDirectory = manifest.getJobDirectory(); expect(jobDirectory).toEqual(fileRoot); }); it('should return the jobs manifest path', async () => { const jobDirectory = manifest.getManifestPath(); expect(jobDirectory).toEqual(`${fileRoot}/manifest.json`); }); });