Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: efficient journey bundling and hashing #809

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions __tests__/push/__snapshots__/transform-journey.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TransformJourneyPlugin alias import specifier 1`] = `
"import { journey as x } from '@elastic/synthetics';

x('j1', () => {});"
`;

exports[`TransformJourneyPlugin alias import specifier 2`] = `
"import { journey as x } from '@elastic/synthetics';


x('j2', () => {});"
`;

exports[`TransformJourneyPlugin alias import specifier 3`] = `
"import { journey as x } from '@elastic/synthetics';

x('j1', () => {});
x('j2', () => {});"
`;

exports[`TransformJourneyPlugin dynamic journeys 1`] = `
"import { journey, step, monitor } from '@elastic/synthetics';

journey('j1', () => {});

const createJourney = (name) => {
journey(name, () => {
monitor.use({ id: 'duplicate id' });
step("step1", () => {});
});
};

createJourney('j2');"
`;

exports[`TransformJourneyPlugin dynamic journeys 2`] = `
"import { journey, step, monitor } from '@elastic/synthetics';



const createJourney = (name) => {
journey(name, () => {
monitor.use({ id: 'duplicate id' });
step("step1", () => {});
});
};

createJourney('j2');"
`;

exports[`TransformJourneyPlugin dynamic journeys 3`] = `
"import { journey, step, monitor } from '@elastic/synthetics';

journey('j1', () => {});

const createJourney = (name) => {
journey(name, () => {
monitor.use({ id: 'duplicate id' });
step("step1", () => {});
});
};

createJourney('j2');"
`;

exports[`TransformJourneyPlugin static journeys 1`] = `
"import { journey, step, monitor } from '@elastic/synthetics';
journey('j1', () => {
monitor.use({ id: 'duplicate id' });
});

function util() {}"
`;

exports[`TransformJourneyPlugin static journeys 2`] = `
"import { journey, step, monitor } from '@elastic/synthetics';




function util() {}

journey('j2', () => {});"
`;

exports[`TransformJourneyPlugin static journeys 3`] = `
"import { journey, step, monitor } from '@elastic/synthetics';
journey('j1', () => {
monitor.use({ id: 'duplicate id' });
});

function util() {}

journey('j2', () => {});"
`;
10 changes: 5 additions & 5 deletions __tests__/push/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,21 @@ describe('Bundler', () => {
});

it('build journey', async () => {
const content = await bundler.build(journeyFile, generateTempPath());
const content = await bundler.build(journeyFile, '', generateTempPath());
await validateZip(content);
});

it('bundle should be idempotent', async () => {
const content1 = await bundler.build(journeyFile, generateTempPath());
const content2 = await bundler.build(journeyFile, generateTempPath());
const content1 = await bundler.build(journeyFile, '', generateTempPath());
const content2 = await bundler.build(journeyFile, '', generateTempPath());
expect(content1).toEqual(content2);
});

it('throw errors on incorrect path', async () => {
try {
await bundler.build(join(PROJECT_DIR, 'blah.ts'), generateTempPath());
await bundler.build(join(PROJECT_DIR, 'blah.ts'), '', generateTempPath());
} catch (e) {
expect(e.message).toContain('Build failed');
expect(e.message).toContain('no such file or directory');
}
});
});
105 changes: 105 additions & 0 deletions __tests__/push/transform-journey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* MIT License
*
* Copyright (c) 2020-present, Elastic NV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

import { join } from 'path';
import { transform } from '../../src/push/transform-journey';
import { mkdir, rm, writeFile } from 'fs/promises';

describe('TransformJourneyPlugin', () => {
const PROJECT_DIR = join(__dirname, 'test-transform');
const journeyFile = join(PROJECT_DIR, 'transform.journey.ts');

beforeAll(async () => {
await mkdir(PROJECT_DIR, { recursive: true });
});

afterAll(async () => {
await rm(PROJECT_DIR, { recursive: true });
});
it('static journeys', async () => {
await writeFile(
journeyFile,
`import {journey, step, monitor} from '@elastic/synthetics';
journey('j1', () => {
monitor.use({ id: 'duplicate id' });
});

function util(){}

journey('j2', () => {});`
);

const res1 = await transform(journeyFile, 'j1');
expect(res1?.code).toMatchSnapshot();
const res2 = await transform(journeyFile, 'j2');
expect(res2?.code).toMatchSnapshot();
const res3 = await transform(journeyFile, '');
expect(res3?.code).toMatchSnapshot();
});

it('dynamic journeys', async () => {
await writeFile(
journeyFile,
`import {journey, step, monitor} from '@elastic/synthetics';

journey('j1', () => {});

const createJourney = (name) => {
journey(name, () => {
monitor.use({ id: 'duplicate id' });
step("step1", () => {})
});
};

createJourney('j2');
`
);

const res1 = await transform(journeyFile, 'j1');
expect(res1?.code).toMatchSnapshot();
const res2 = await transform(journeyFile, 'j2');
expect(res2?.code).toMatchSnapshot();
const res3 = await transform(journeyFile, '');
expect(res3?.code).toMatchSnapshot();
});

it('alias import specifier', async () => {
await writeFile(
journeyFile,
`import {journey as x} from '@elastic/synthetics';

x('j1', () => {});
x('j2', ()=>{});
`
);

const res1 = await transform(journeyFile, 'j1');
expect(res1?.code).toMatchSnapshot();
const res2 = await transform(journeyFile, 'j2');
expect(res2?.code).toMatchSnapshot();
const res3 = await transform(journeyFile, '');
expect(res3?.code).toMatchSnapshot();
});
});
Loading