Skip to content

Commit 5e38e7a

Browse files
committed
blc-api-integration-def (#102)
1 parent 7e59d4a commit 5e38e7a

12 files changed

+231
-34
lines changed

package-lock.json

+13-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

packages/synthetics-sdk-broken-links/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
"chai": "^4.3.7",
3333
"chai-exclude": "^2.1.0",
3434
"express": "^4.18.2",
35-
"sinon": "^15.2.0",
35+
"sinon": "^16.1.1",
3636
"supertest": "^6.3.3",
3737
"synthetics-sdk-broken-links": "file:./"
3838
},
3939
"engines": {
4040
"node": ">=18"
4141
},
4242
"dependencies": {
43-
"@google-cloud/synthetics-sdk-api": "^0.5.1",
43+
"@google-cloud/synthetics-sdk-api": "google-cloud-synthetics-sdk-api-0.5.1.tgz",
4444
"puppeteer": "21.3.6"
4545
}
4646
}

packages/synthetics-sdk-broken-links/src/broken_links.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
retrieveLinksFromPage,
3636
openNewPage,
3737
} from './navigation_func';
38-
import { setDefaultOptions, validateInputOptions } from './options_func';
38+
import { processOptions } from './options_func';
3939

4040
export interface BrokenLinkCheckerOptions {
4141
origin_uri: string;
@@ -48,6 +48,7 @@ export interface BrokenLinkCheckerOptions {
4848
wait_for_selector?: string;
4949
per_link_options?: { [key: string]: PerLinkOption };
5050
total_synthetic_timeout_millis?: number;
51+
screenshot_options?: ScreenshotOptions;
5152
}
5253

5354
export interface PerLinkOption {
@@ -70,6 +71,17 @@ export enum StatusClass {
7071
STATUS_CLASS_ANY = 'STATUS_CLASS_ANY',
7172
}
7273

74+
export interface ScreenshotOptions {
75+
storage_location?: string;
76+
screenshot_condition?: ScreenshotCondition;
77+
}
78+
79+
export enum ScreenshotCondition {
80+
NONE = 'NONE',
81+
FAILING = 'FAILING',
82+
ALL = 'ALL',
83+
}
84+
7385
let synthetics_sdk_broken_links_package;
7486
try {
7587
synthetics_sdk_broken_links_package = require('../package.json');
@@ -263,16 +275,3 @@ async function scrapeLinks(
263275
options.link_order
264276
);
265277
}
266-
267-
/**
268-
* Validates input options and sets defaults in `options`.
269-
*
270-
* @param inputOptions - The input options for the broken link checker.
271-
* @returns The processed broken link checker options.
272-
*/
273-
function processOptions(
274-
inputOptions: BrokenLinkCheckerOptions
275-
): BrokenLinksResultV1_BrokenLinkCheckerOptions {
276-
const validOptions = validateInputOptions(inputOptions);
277-
return setDefaultOptions(validOptions);
278-
}

packages/synthetics-sdk-broken-links/src/link_utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ function parseFollowedLinks(
153153
options: {} as BrokenLinksResultV1_BrokenLinkCheckerOptions,
154154
origin_link_result: {} as BrokenLinksResultV1_SyntheticLinkResult,
155155
followed_link_results: [],
156+
execution_data_storage_path: '', // TODO: make sure that when this is set it begins with gs://
157+
errors: [],
156158
};
157159

158160
for (const link of followed_links) {

packages/synthetics-sdk-broken-links/src/navigation_func.ts

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Browser, HTTPResponse, Page } from 'puppeteer';
1616
import {
1717
BrokenLinksResultV1_BrokenLinkCheckerOptions,
1818
BrokenLinksResultV1_SyntheticLinkResult,
19+
BaseError,
1920
ResponseStatusCode,
2021
ResponseStatusCode_StatusClass,
2122
} from '@google-cloud/synthetics-sdk-api';
@@ -220,6 +221,10 @@ export async function checkLink(
220221
link_start_time: linkStartTime,
221222
link_end_time: linkEndTime,
222223
is_origin: isOrigin,
224+
screenshot_output: {
225+
screenshot_file: '',
226+
screenshot_error: {} as BaseError,
227+
}, // TODO: this is temporary in an effort to make PRs more manageable
223228
};
224229
}
225230

packages/synthetics-sdk-broken-links/src/options_func.ts

+77-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,31 @@ import {
1616
BrokenLinksResultV1_BrokenLinkCheckerOptions,
1717
BrokenLinksResultV1_BrokenLinkCheckerOptions_LinkOrder,
1818
BrokenLinksResultV1_BrokenLinkCheckerOptions_PerLinkOption,
19+
BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions,
20+
BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions_ScreenshotCondition,
1921
ResponseStatusCode,
2022
ResponseStatusCode_StatusClass,
2123
} from '@google-cloud/synthetics-sdk-api';
2224
import {
2325
BrokenLinkCheckerOptions,
2426
LinkOrder,
2527
StatusClass,
28+
ScreenshotCondition,
2629
} from './broken_links';
2730

31+
/**
32+
* Validates input options and sets defaults in `options`.
33+
*
34+
* @param inputOptions - The input options for the broken link checker.
35+
* @returns The processed broken link checker options.
36+
*/
37+
export function processOptions(
38+
inputOptions: BrokenLinkCheckerOptions
39+
): BrokenLinksResultV1_BrokenLinkCheckerOptions {
40+
const validOptions = validateInputOptions(inputOptions);
41+
return setDefaultOptions(validOptions);
42+
}
43+
2844
/**
2945
* Validates the input options for the Broken Link Checker.
3046
*
@@ -130,6 +146,26 @@ export function validateInputOptions(
130146
);
131147
}
132148

149+
// Check storage_location
150+
if (
151+
inputOptions.screenshot_options?.storage_location !== undefined &&
152+
typeof inputOptions.screenshot_options?.storage_location !== 'string'
153+
) {
154+
throw new Error('Invalid storage_location value, must be a string');
155+
}
156+
157+
// check storage_condition
158+
if (
159+
inputOptions.screenshot_options?.screenshot_condition !== undefined &&
160+
!Object.values(ScreenshotCondition).includes(
161+
inputOptions.screenshot_options?.screenshot_condition
162+
)
163+
) {
164+
throw new Error(
165+
'Invalid screenshot_condition value, must be `ALL`, `FAILING`, OR `NONE`'
166+
);
167+
}
168+
133169
// per_link_options
134170
for (const [key, value] of Object.entries(
135171
inputOptions.per_link_options || {}
@@ -180,6 +216,11 @@ export function validateInputOptions(
180216
wait_for_selector: inputOptions.wait_for_selector,
181217
per_link_options: inputOptions.per_link_options,
182218
total_synthetic_timeout_millis: inputOptions.total_synthetic_timeout_millis,
219+
screenshot_options: {
220+
screenshot_condition:
221+
inputOptions.screenshot_options?.screenshot_condition,
222+
storage_location: inputOptions.screenshot_options?.storage_location,
223+
},
183224
};
184225
}
185226

@@ -192,7 +233,7 @@ export function validateInputOptions(
192233
export function setDefaultOptions(
193234
inputOptions: BrokenLinkCheckerOptions
194235
): BrokenLinksResultV1_BrokenLinkCheckerOptions {
195-
const defaulOptions: BrokenLinksResultV1_BrokenLinkCheckerOptions = {
236+
const defaultOptions: BrokenLinksResultV1_BrokenLinkCheckerOptions = {
196237
origin_uri: '',
197238
link_limit: 10,
198239
query_selector_all: 'a',
@@ -203,17 +244,26 @@ export function setDefaultOptions(
203244
wait_for_selector: '',
204245
per_link_options: {},
205246
total_synthetic_timeout_millis: 60000,
247+
screenshot_options: {
248+
screenshot_condition:
249+
BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions_ScreenshotCondition.FAILING,
250+
storage_location: '',
251+
},
206252
};
207253

208254
const outputOptions: BrokenLinksResultV1_BrokenLinkCheckerOptions =
209255
{} as BrokenLinksResultV1_BrokenLinkCheckerOptions;
210256

211-
const optionsKeys = Object.keys(defaulOptions) as Array<
257+
const optionsKeys = Object.keys(defaultOptions) as Array<
212258
keyof BrokenLinksResultV1_BrokenLinkCheckerOptions
213259
>;
214260
for (const optionName of optionsKeys) {
215261
// per_link_options and linkorder are handled below
216-
if (optionName === 'per_link_options' || optionName === 'link_order')
262+
if (
263+
optionName === 'per_link_options' ||
264+
optionName === 'link_order' ||
265+
optionName === 'screenshot_options'
266+
)
217267
continue;
218268

219269
if (
@@ -222,13 +272,35 @@ export function setDefaultOptions(
222272
(inputOptions as any)[optionName] === undefined
223273
) {
224274
// eslint-disable-next-line @typescript-eslint/no-explicit-any
225-
(outputOptions as any)[optionName] = defaulOptions[optionName];
275+
(outputOptions as any)[optionName] = defaultOptions[optionName];
226276
} else {
227277
// eslint-disable-next-line @typescript-eslint/no-explicit-any
228278
(outputOptions as any)[optionName] = inputOptions[optionName];
229279
}
230280
}
231281

282+
// converting inputOptions.screenshot_options to
283+
// BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions
284+
outputOptions.screenshot_options =
285+
{} as BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions;
286+
if (inputOptions.screenshot_options?.screenshot_condition) {
287+
outputOptions.screenshot_options!.screenshot_condition =
288+
BrokenLinksResultV1_BrokenLinkCheckerOptions_ScreenshotOptions_ScreenshotCondition[
289+
inputOptions.screenshot_options.screenshot_condition
290+
];
291+
} else {
292+
outputOptions.screenshot_options!.screenshot_condition =
293+
defaultOptions.screenshot_options!.screenshot_condition;
294+
}
295+
296+
if (outputOptions.screenshot_options?.storage_location) {
297+
outputOptions.screenshot_options.storage_location =
298+
inputOptions.screenshot_options!.storage_location!;
299+
} else {
300+
outputOptions.screenshot_options.storage_location =
301+
defaultOptions.screenshot_options!.storage_location!;
302+
}
303+
232304
// converting inputOptions.link_order, type: LinkOrder to
233305
// outputOptions.link_order, type BrokenLinksResultV1_BrokenLinkCheckerOptions_LinkOrder
234306
if (inputOptions.link_order) {
@@ -237,7 +309,7 @@ export function setDefaultOptions(
237309
inputOptions.link_order
238310
];
239311
} else {
240-
outputOptions.link_order = defaulOptions.link_order;
312+
outputOptions.link_order = defaultOptions.link_order;
241313
}
242314

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

0 commit comments

Comments
 (0)