Skip to content

Commit

Permalink
add tests and todos
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed Jul 29, 2023
1 parent 0f8c180 commit e770892
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 7 deletions.
77 changes: 77 additions & 0 deletions __tests__/push/__snapshots__/transform-journey.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TransformJourneyPlugin dynamic journeys 1`] = `
"import { journey, step, monitor } from '../../';
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 '../../';
const createJourney = (name) => {
journey(name, () => {
monitor.use({ id: 'duplicate id' });
step("step1", () => {});
});
};
createJourney('j2');"
`;

exports[`TransformJourneyPlugin dynamic journeys 3`] = `
"import { journey, step, monitor } from '../../';
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 '../../';
journey('j1', () => {
monitor.use({ id: 'duplicate id' });
});
function util() {}"
`;

exports[`TransformJourneyPlugin static journeys 2`] = `
"import { journey, step, monitor } from '../../';
function util() {}
journey('j2', () => {});"
`;

exports[`TransformJourneyPlugin static journeys 3`] = `
"import { journey, step, monitor } from '../../';
journey('j1', () => {
monitor.use({ id: 'duplicate id' });
});
function util() {}
journey('j2', () => {});"
`;
87 changes: 87 additions & 0 deletions __tests__/push/transform-journey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* 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 '../../';
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 '../../';
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();
});
});
8 changes: 4 additions & 4 deletions src/push/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export class Bundler {
moduleMap = new Map<string, string>();
constructor() {}

async prepare(absPath: string, id: string) {
async prepare(absPath: string, name: string) {
const original = await readFile(absPath, 'utf-8');
const { code } = await transform(absPath, id);
const { code } = await transform(absPath, name);
await writeFile(absPath, code);

const options: esbuild.BuildOptions = {
Expand Down Expand Up @@ -89,8 +89,8 @@ export class Bundler {
});
}

async build(entry: string, output: string, id: string) {
await this.prepare(entry, id);
async build(entry: string, name: string, output: string) {
await this.prepare(entry, name);
await this.zip(output);
const data = await this.encode(output);
await this.checkSize(output);
Expand Down
3 changes: 2 additions & 1 deletion src/push/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ export async function buildMonitorSchema(monitors: Monitor[], isV2: boolean) {
...config,
locations: translateLocation(config.locations),
};

if (type === 'browser') {
const outPath = join(bundlePath, config.name + '.zip');
const content = await bundler.build(source.file, outPath, config.name);
const content = await bundler.build(source.file, config.name, outPath);
monitor.setContent(content);
Object.assign(schema, { content, filter });
}
Expand Down
19 changes: 17 additions & 2 deletions src/push/transform-journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,25 @@ export function JourneyTransformPlugin(
{ types: t }: { types: typeof types },
opts: JourneyPluginOptions
): PluginObj {
// TODO: Perform a Program level visit to check if import/require declarations are tampered
// If so, dont perform any transformation
// Ex: import * as synthetics from '@elastic/synthetics'
// synthetics.journey('name', () => {})
return {
name: 'transform-journeys',
visitor: {
CallExpression(path) {
if (!path.parentPath.isExpressionStatement()) {
return;
}
const { callee } = path.node;
if (t.isIdentifier(callee) && callee.name === 'journey') {
const args = path.node.arguments;
if (!t.isStringLiteral(args[0])) {
return;
}
if (args[0].value === opts.name) {
// TODO: Compare based on function body, solid than relying on name
if (opts.name == '' || args[0].value === opts.name) {
path.skip();
} else {
path.parentPath.remove();
Expand All @@ -61,6 +69,13 @@ export async function transform(absPath: string, journeyName: string) {
retainLines: true,
babelrc: false,
configFile: false,
plugins: [[JourneyTransformPlugin, { name: journeyName }]],
plugins: [
[
JourneyTransformPlugin,
{
name: journeyName,
},
],
],
});
}

0 comments on commit e770892

Please sign in to comment.