Skip to content

integration with new api and update default handling #102

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

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
24 changes: 13 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
4 changes: 2 additions & 2 deletions packages/synthetics-sdk-broken-links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
"chai": "^4.3.7",
"chai-exclude": "^2.1.0",
"express": "^4.18.2",
"sinon": "^15.2.0",
"sinon": "^16.1.1",
"supertest": "^6.3.3",
"synthetics-sdk-broken-links": "file:./"
},
"engines": {
"node": ">=18"
},
"dependencies": {
"@google-cloud/synthetics-sdk-api": "^0.5.1",
"@google-cloud/synthetics-sdk-api": "google-cloud-synthetics-sdk-api-0.5.1.tgz",
"puppeteer": "21.3.6"
}
}
27 changes: 13 additions & 14 deletions packages/synthetics-sdk-broken-links/src/broken_links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
retrieveLinksFromPage,
openNewPage,
} from './navigation_func';
import { setDefaultOptions, validateInputOptions } from './options_func';
import { processOptions } from './options_func';

export interface BrokenLinkCheckerOptions {
origin_uri: string;
Expand All @@ -48,6 +48,7 @@ export interface BrokenLinkCheckerOptions {
wait_for_selector?: string;
per_link_options?: { [key: string]: PerLinkOption };
total_synthetic_timeout_millis?: number;
screenshot_options?: ScreenshotOptions;
}

export interface PerLinkOption {
Expand All @@ -70,6 +71,17 @@ export enum StatusClass {
STATUS_CLASS_ANY = 'STATUS_CLASS_ANY',
}

export interface ScreenshotOptions {
storage_location?: string;
screenshot_condition?: ScreenshotCondition;
}

export enum ScreenshotCondition {
NONE = 'NONE',
FAILING = 'FAILING',
ALL = 'ALL',
}

let synthetics_sdk_broken_links_package;
try {
synthetics_sdk_broken_links_package = require('../package.json');
Expand Down Expand Up @@ -263,16 +275,3 @@ async function scrapeLinks(
options.link_order
);
}

/**
* Validates input options and sets defaults in `options`.
*
* @param inputOptions - The input options for the broken link checker.
* @returns The processed broken link checker options.
*/
function processOptions(
inputOptions: BrokenLinkCheckerOptions
): BrokenLinksResultV1_BrokenLinkCheckerOptions {
const validOptions = validateInputOptions(inputOptions);
return setDefaultOptions(validOptions);
}
2 changes: 2 additions & 0 deletions packages/synthetics-sdk-broken-links/src/link_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ function parseFollowedLinks(
options: {} as BrokenLinksResultV1_BrokenLinkCheckerOptions,
origin_link_result: {} as BrokenLinksResultV1_SyntheticLinkResult,
followed_link_results: [],
execution_data_storage_path: '', // TODO: make sure that when this is set it begins with gs://
errors: [],
};

for (const link of followed_links) {
Expand Down
5 changes: 5 additions & 0 deletions packages/synthetics-sdk-broken-links/src/navigation_func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Browser, HTTPResponse, Page } from 'puppeteer';
import {
BrokenLinksResultV1_BrokenLinkCheckerOptions,
BrokenLinksResultV1_SyntheticLinkResult,
BaseError,
ResponseStatusCode,
ResponseStatusCode_StatusClass,
} from '@google-cloud/synthetics-sdk-api';
Expand Down Expand Up @@ -220,6 +221,10 @@ export async function checkLink(
link_start_time: linkStartTime,
link_end_time: linkEndTime,
is_origin: isOrigin,
screenshot_output: {
screenshot_file: '',
screenshot_error: {} as BaseError,
}, // TODO: this is temporary in an effort to make PRs more manageable
};
}

Expand Down
82 changes: 77 additions & 5 deletions packages/synthetics-sdk-broken-links/src/options_func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,31 @@ import {
BrokenLinksResultV1_BrokenLinkCheckerOptions,
BrokenLinksResultV1_BrokenLinkCheckerOptions_LinkOrder,
BrokenLinksResultV1_BrokenLinkCheckerOptions_PerLinkOption,
BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions,
BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions_ScreenshotCondition,
ResponseStatusCode,
ResponseStatusCode_StatusClass,
} from '@google-cloud/synthetics-sdk-api';
import {
BrokenLinkCheckerOptions,
LinkOrder,
StatusClass,
ScreenshotCondition,
} from './broken_links';

/**
* Validates input options and sets defaults in `options`.
*
* @param inputOptions - The input options for the broken link checker.
* @returns The processed broken link checker options.
*/
export function processOptions(
inputOptions: BrokenLinkCheckerOptions
): BrokenLinksResultV1_BrokenLinkCheckerOptions {
const validOptions = validateInputOptions(inputOptions);
return setDefaultOptions(validOptions);
}

/**
* Validates the input options for the Broken Link Checker.
*
Expand Down Expand Up @@ -130,6 +146,26 @@ export function validateInputOptions(
);
}

// Check storage_location
if (
inputOptions.screenshot_options?.storage_location !== undefined &&
typeof inputOptions.screenshot_options?.storage_location !== 'string'
) {
throw new Error('Invalid storage_location value, must be a string');
}

// check storage_condition
if (
inputOptions.screenshot_options?.screenshot_condition !== undefined &&
!Object.values(ScreenshotCondition).includes(
inputOptions.screenshot_options?.screenshot_condition
)
) {
throw new Error(
'Invalid screenshot_condition value, must be `ALL`, `FAILING`, OR `NONE`'
);
}

// per_link_options
for (const [key, value] of Object.entries(
inputOptions.per_link_options || {}
Expand Down Expand Up @@ -180,6 +216,11 @@ export function validateInputOptions(
wait_for_selector: inputOptions.wait_for_selector,
per_link_options: inputOptions.per_link_options,
total_synthetic_timeout_millis: inputOptions.total_synthetic_timeout_millis,
screenshot_options: {
screenshot_condition:
inputOptions.screenshot_options?.screenshot_condition,
storage_location: inputOptions.screenshot_options?.storage_location,
},
};
}

Expand All @@ -192,7 +233,7 @@ export function validateInputOptions(
export function setDefaultOptions(
inputOptions: BrokenLinkCheckerOptions
): BrokenLinksResultV1_BrokenLinkCheckerOptions {
const defaulOptions: BrokenLinksResultV1_BrokenLinkCheckerOptions = {
const defaultOptions: BrokenLinksResultV1_BrokenLinkCheckerOptions = {
origin_uri: '',
link_limit: 10,
query_selector_all: 'a',
Expand All @@ -203,17 +244,26 @@ export function setDefaultOptions(
wait_for_selector: '',
per_link_options: {},
total_synthetic_timeout_millis: 60000,
screenshot_options: {
screenshot_condition:
BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions_ScreenshotCondition.FAILING,
storage_location: '',
},
};

const outputOptions: BrokenLinksResultV1_BrokenLinkCheckerOptions =
{} as BrokenLinksResultV1_BrokenLinkCheckerOptions;

const optionsKeys = Object.keys(defaulOptions) as Array<
const optionsKeys = Object.keys(defaultOptions) as Array<
keyof BrokenLinksResultV1_BrokenLinkCheckerOptions
>;
for (const optionName of optionsKeys) {
// per_link_options and linkorder are handled below
if (optionName === 'per_link_options' || optionName === 'link_order')
if (
optionName === 'per_link_options' ||
optionName === 'link_order' ||
optionName === 'screenshot_options'
)
continue;

if (
Expand All @@ -222,13 +272,35 @@ export function setDefaultOptions(
(inputOptions as any)[optionName] === undefined
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(outputOptions as any)[optionName] = defaulOptions[optionName];
(outputOptions as any)[optionName] = defaultOptions[optionName];
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(outputOptions as any)[optionName] = inputOptions[optionName];
}
}

// converting inputOptions.screenshot_options to
// BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions
outputOptions.screenshot_options =
{} as BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions;
if (inputOptions.screenshot_options?.screenshot_condition) {
outputOptions.screenshot_options!.screenshot_condition =
BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions_ScreenshotCondition[
inputOptions.screenshot_options.screenshot_condition
];
} else {
outputOptions.screenshot_options!.screenshot_condition =
defaultOptions.screenshot_options!.screenshot_condition;
}

if (outputOptions.screenshot_options?.storage_location) {
outputOptions.screenshot_options.storage_location =
inputOptions.screenshot_options!.storage_location!;
} else {
outputOptions.screenshot_options.storage_location =
defaultOptions.screenshot_options!.storage_location!;
}

// converting inputOptions.link_order, type: LinkOrder to
// outputOptions.link_order, type BrokenLinksResultV1_BrokenLinkCheckerOptions_LinkOrder
if (inputOptions.link_order) {
Expand All @@ -237,7 +309,7 @@ export function setDefaultOptions(
inputOptions.link_order
];
} else {
outputOptions.link_order = defaulOptions.link_order;
outputOptions.link_order = defaultOptions.link_order;
}

// Convert `inputOptions.per_link_options`, type: {[key: string]: PerLinkOption}
Expand Down
Loading