Repository: elastic/synthetics (ts)

Issue:
Disable throttling
I'm trying to disable throttling at all in the config, but it seems that it's not possible.
I've tried to set
```
    monitor: {
      throttling: undefined
    }
```
and other variations, but when running `push` I get:
```
> preparing all monitors
> creating all monitors
x Error
   > Failed to save or update monitor. Configuration is not valid: monitor(prod-uk login)
       Invalid value "undefined" supplied to "throttling,download" | Invalid value "undefined" supplied to "throttling,upload" | Invalid value "undefined" supplied to "throttling,latency"

   > Failed to save or update monitor. Configuration is not valid: monitor(prod-uk matching page)
       Invalid value "undefined" supplied to "throttling,download" | Invalid value "undefined" supplied to "throttling,upload" | Invalid value "undefined" supplied to "throttling,latency"
```



Pull request:
fix: allow disabling throttling in project monitors
+ fix #689 
+ PR fixes the types and allows throttling to be disabled via
  - CLI - `--no-throttling`
  - Synthetics Config File - `monitor.throttling: false`
  - Monitor API - `monitor.use({ throttling: false })`
+ Needs Kibana fix to work End to End - https://github.com/elastic/kibana/issues/148344

Patch:
diff --git a/src/common_types.ts b/src/common_types.ts
index 3a1d292..ce3ddd2 100644
--- a/src/common_types.ts
+++ b/src/common_types.ts
@@ -209,7 +209,7 @@ type BaseArgs = {
   ignoreHttpsErrors?: boolean;
   playwrightOptions?: PlaywrightOptions;
   quietExitCode?: boolean;
-  throttling?: ThrottlingOptions;
+  throttling?: MonitorConfig['throttling'];
   schedule?: MonitorConfig['schedule'];
   locations?: MonitorConfig['locations'];
   privateLocations?: MonitorConfig['privateLocations'];
@@ -226,7 +226,6 @@ export type CliArgs = BaseArgs & {
   richEvents?: boolean;
   capability?: Array<string>;
   ignoreHttpsErrors?: boolean;
-  throttling?: boolean | ThrottlingOptions;
 };
 
 export type RunOptions = BaseArgs & {
diff --git a/src/dsl/monitor.ts b/src/dsl/monitor.ts
index 95015b9..785c392 100644
--- a/src/dsl/monitor.ts
+++ b/src/dsl/monitor.ts
@@ -51,7 +51,7 @@ export type MonitorConfig = {
   enabled?: boolean;
   locations?: SyntheticsLocationsType[];
   privateLocations?: string[];
-  throttling?: ThrottlingOptions;
+  throttling?: boolean | ThrottlingOptions;
   screenshot?: ScreenshotOptions;
   params?: Params;
   playwrightOptions?: PlaywrightOptions;
diff --git a/src/options.ts b/src/options.ts
index 0ba2cef..f954943 100644
--- a/src/options.ts
+++ b/src/options.ts
@@ -123,16 +123,24 @@ export function normalizeOptions(cliArgs: CliArgs): RunOptions {
   if (cliArgs.throttling) {
     const throttleConfig = merge.all([
       DEFAULT_THROTTLING_OPTIONS,
-      monitor?.throttling || {},
-      cliArgs.throttling as ThrottlingOptions,
+      toObject(monitor?.throttling),
+      toObject(cliArgs.throttling),
     ]);
-    options.throttling = throttleConfig;
-    options.networkConditions = getNetworkConditions(throttleConfig);
+    if (monitor?.throttling !== false) {
+      options.throttling = throttleConfig;
+      options.networkConditions = getNetworkConditions(throttleConfig);
+    } else {
+      /**
+       * If the throttling is disabled via Project monitor config, use it a source
+       * of truth and disable it for pushing those monitors.
+       */
+      options.throttling = false;
+    }
   } else {
     /**
      * Do not apply throttling when `--no-throttling` flag is passed
      */
-    options.throttling = {};
+    options.throttling = false;
   }
 
   options.schedule = cliArgs.schedule ?? monitor?.schedule;
@@ -143,6 +151,14 @@ export function normalizeOptions(cliArgs: CliArgs): RunOptions {
   return options;
 }
 
+function toObject(value: boolean | Record<string, any>): Record<string, any> {
+  const defaulVal = {};
+  if (typeof value === 'boolean') {
+    return defaulVal;
+  }
+  return value || defaulVal;
+}
+
 /**
  * Parses the throttling CLI settings and also
  * adapts to the format to keep the backwards compatability


Test patch:
diff --git a/__tests__/options.test.ts b/__tests__/options.test.ts
index 79a6bb7..bf14482 100644
--- a/__tests__/options.test.ts
+++ b/__tests__/options.test.ts
@@ -75,7 +75,7 @@ describe('options', () => {
 
   it('normalize monitor configs', () => {
     expect(normalizeOptions({ throttling: false })).toMatchObject({
-      throttling: {},
+      throttling: false,
     });
 
     expect(