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: extra validations for browser checks #942

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const checkType = CheckType.Browser;

const browserImport = `import { browser } from 'k6/browser';`;
const exportOptions = `export const options = { };`;
const exportCorrectOptions = `export const options = {scenarios: {
const exportCorrectOptions = `export const options = {
scenarios: {
ui: {
options: {
browser: {
Expand Down Expand Up @@ -62,5 +63,77 @@ describe(`BrowserCheck - 1 (Script) UI`, () => {
const err = await screen.findByText('Script must set the type to chromium in the browser options.');
expect(err).toBeInTheDocument();
});

it(`will display an error when it defines a duration prop`, async () => {
const { user } = await renderNewForm(checkType);
const scriptTextAreaPreSubmit = screen.getByTestId(`code-editor`);
await user.clear(scriptTextAreaPreSubmit);

const invalidDurationOptions = `export const options = {
scenarios: {
ui: {
duration: '1m',
options: {
browser: {
type: 'chromium',
},
},
},
},
};`;
await user.type(scriptTextAreaPreSubmit, browserImport + invalidDurationOptions);

await submitForm(user);
const err = await screen.findByText("Script can't define a duration value for this check");
expect(err).toBeInTheDocument();
});

it(`will display an error when it defines vus > 1`, async () => {
const { user } = await renderNewForm(checkType);
const scriptTextAreaPreSubmit = screen.getByTestId(`code-editor`);
await user.clear(scriptTextAreaPreSubmit);

const invalidVusOptions = `export const options = {
scenarios: {
ui: {
vus: 2,
options: {
browser: {
type: 'chromium',
},
},
},
},
};`;
await user.type(scriptTextAreaPreSubmit, browserImport + invalidVusOptions);

await submitForm(user);
const err = await screen.findByText("Script can't define vus > 1 for this check");
expect(err).toBeInTheDocument();
});

it(`will display an error when it defines iterations > 1`, async () => {
const { user } = await renderNewForm(checkType);
const scriptTextAreaPreSubmit = screen.getByTestId(`code-editor`);
await user.clear(scriptTextAreaPreSubmit);

const invalidIterationsOptions = `export const options = {
scenarios: {
ui: {
iterations: 2,
options: {
browser: {
type: 'chromium',
},
},
},
},
};`;
await user.type(scriptTextAreaPreSubmit, browserImport + invalidIterationsOptions);

await submitForm(user);
const err = await screen.findByText("Script can't define iterations > 1 for this check");
expect(err).toBeInTheDocument();
});
});
});
16 changes: 11 additions & 5 deletions src/schemas/forms/script/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ const optionExportMatcher: SimpleVisitors<{ options: ObjectExpression | null }>
},
};

export function checkForChromium(objectExpression: ObjectExpression): boolean {
// Path to the property we're interested in
const path = ['scenarios', 'ui', 'options', 'browser', 'type'];
const value = getPropertyValueByPath(objectExpression, path);
export function getProperty(objectExpression: ObjectExpression, propPath: string[]) {
const scenarios = getPropertyValueByPath(objectExpression, ['scenarios']);
if (!scenarios || scenarios.type !== 'ObjectExpression') {
return false;
}

return value === 'chromium';
for (const scenario of scenarios.properties) {
if (scenario.key.type === 'Identifier') {
const propValue = getPropertyValueByPath(scenario.value, propPath);
return propValue;
}
}
}

interface ImportBrowserState {
Expand Down
31 changes: 29 additions & 2 deletions src/schemas/forms/script/validation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RefinementCtx, ZodIssueCode } from 'zod';

import { checkForChromium, extractImportStatement, extractOptionsExport, parseScript } from './parser';
import { extractImportStatement, extractOptionsExport, getProperty, parseScript } from './parser';

const MAX_SCRIPT_IN_KB = 128;

Expand Down Expand Up @@ -36,13 +36,40 @@ export function validateBrowserScript(script: string, context: RefinementCtx) {
});
}

if (!checkForChromium(options)) {
const hasChromium = getProperty(options, ['options', 'browser', 'type']) === 'chromium';
if (!hasChromium) {
return context.addIssue({
code: ZodIssueCode.custom,
message: 'Script must set the type to chromium in the browser options.',
});
}

const hasInvalidDuration = getProperty(options, ['duration']) !== undefined;
if (hasInvalidDuration) {
return context.addIssue({
code: ZodIssueCode.custom,
message: "Script can't define a duration value for this check",
});
}

const vus = getProperty(options, ['vus']);
const hasInvaludVus = vus !== undefined && vus > 1;
if (hasInvaludVus) {
return context.addIssue({
code: ZodIssueCode.custom,
message: "Script can't define vus > 1 for this check",
});
}

const iterations = getProperty(options, ['iterations']);
const hasInvalidIterations = iterations !== undefined && iterations > 1;
if (hasInvalidIterations) {
return context.addIssue({
code: ZodIssueCode.custom,
message: "Script can't define iterations > 1 for this check",
});
}

const browserImport = extractImportStatement(program);

if (browserImport === null) {
Expand Down
Loading