From 611a74bfdef37719bfba223f245642ba59f8e4bc Mon Sep 17 00:00:00 2001
From: Matt Ellis <matell@microsoft.com>
Date: Mon, 29 Mar 2021 17:47:12 -0700
Subject: [PATCH] [EventGrid] Split tests to enable min/max testing (#14404)

This change splits our tests into two folders:

- `public` which only uses surface area exported by the package.
- `internal` which tests internal methods which we don't export from
   the top level package.

This allows us to test the public surface area independently from the
private one, which is helpful for classes of testing.

Fixes #13813
---
 sdk/eventgrid/eventgrid/rollup.base.config.js |   4 +-
 ...ntDistributedTracingEnricherPolicy.spec.ts |   2 +-
 .../test/internal/convertToWireModel.spec.ts  | 173 +++++++++++++++++
 .../test/{ => internal}/util.spec.ts          |   2 +-
 .../test/{ => public}/consumer.spec.ts        |   2 +-
 .../test/{ => public}/eventGridClient.spec.ts | 174 +-----------------
 .../generateSharedAccessSignature.spec.ts     |   2 +-
 .../test/{ => public}/utils/recordedClient.ts |   2 +-
 .../test/{ => public}/utils/testData.ts       |   0
 .../test/{ => public}/utils/testUtils.ts      |   0
 sdk/eventgrid/eventgrid/tests.yml             |   2 -
 11 files changed, 181 insertions(+), 182 deletions(-)
 rename sdk/eventgrid/eventgrid/test/{ => internal}/cloudEventDistributedTracingEnricherPolicy.spec.ts (97%)
 create mode 100644 sdk/eventgrid/eventgrid/test/internal/convertToWireModel.spec.ts
 rename sdk/eventgrid/eventgrid/test/{ => internal}/util.spec.ts (92%)
 rename sdk/eventgrid/eventgrid/test/{ => public}/consumer.spec.ts (99%)
 rename sdk/eventgrid/eventgrid/test/{ => public}/eventGridClient.spec.ts (67%)
 rename sdk/eventgrid/eventgrid/test/{ => public}/generateSharedAccessSignature.spec.ts (98%)
 rename sdk/eventgrid/eventgrid/test/{ => public}/utils/recordedClient.ts (96%)
 rename sdk/eventgrid/eventgrid/test/{ => public}/utils/testData.ts (100%)
 rename sdk/eventgrid/eventgrid/test/{ => public}/utils/testUtils.ts (100%)

diff --git a/sdk/eventgrid/eventgrid/rollup.base.config.js b/sdk/eventgrid/eventgrid/rollup.base.config.js
index 8e7df4767693..827d9a3c6070 100644
--- a/sdk/eventgrid/eventgrid/rollup.base.config.js
+++ b/sdk/eventgrid/eventgrid/rollup.base.config.js
@@ -39,7 +39,7 @@ export function nodeConfig(test = false) {
 
   if (test) {
     // Entry points - test files under the `test` folder(common for both browser and node), node specific test files
-    baseConfig.input = ["dist-esm/test/*.spec.js", "dist-esm/test/node/*.spec.js"];
+    baseConfig.input = ["dist-esm/test/internal/**/*.spec.js", "dist-esm/test/public/**/*.spec.js"];
     baseConfig.plugins.unshift(multiEntry({ exports: false }));
 
     // different output file
@@ -105,7 +105,7 @@ export function browserConfig(test = false, production = false) {
 
   if (test) {
     // Entry points - test files under the `test` folder(common for both browser and node), browser specific test files
-    baseConfig.input = ["dist-esm/test/*.spec.js", "dist-esm/test/browser/*.spec.js"];
+    baseConfig.input = ["dist-esm/test/internal/**/*.spec.js", "dist-esm/test/public/**/*.spec.js"];
     baseConfig.plugins.unshift(multiEntry({ exports: false }));
     baseConfig.output.file = "dist-test/index.browser.js";
 
diff --git a/sdk/eventgrid/eventgrid/test/cloudEventDistributedTracingEnricherPolicy.spec.ts b/sdk/eventgrid/eventgrid/test/internal/cloudEventDistributedTracingEnricherPolicy.spec.ts
similarity index 97%
rename from sdk/eventgrid/eventgrid/test/cloudEventDistributedTracingEnricherPolicy.spec.ts
rename to sdk/eventgrid/eventgrid/test/internal/cloudEventDistributedTracingEnricherPolicy.spec.ts
index b95ed22bdc81..386449c49a76 100644
--- a/sdk/eventgrid/eventgrid/test/cloudEventDistributedTracingEnricherPolicy.spec.ts
+++ b/sdk/eventgrid/eventgrid/test/internal/cloudEventDistributedTracingEnricherPolicy.spec.ts
@@ -2,7 +2,7 @@
 // Licensed under the MIT license.
 
 import { assert } from "chai";
-import { cloudEventDistributedTracingEnricherPolicy } from "../src/cloudEventDistrubtedTracingEnricherPolicy";
+import { cloudEventDistributedTracingEnricherPolicy } from "../../src/cloudEventDistrubtedTracingEnricherPolicy";
 import {
   PipelineRequest,
   PipelineResponse,
diff --git a/sdk/eventgrid/eventgrid/test/internal/convertToWireModel.spec.ts b/sdk/eventgrid/eventgrid/test/internal/convertToWireModel.spec.ts
new file mode 100644
index 000000000000..b1278d358345
--- /dev/null
+++ b/sdk/eventgrid/eventgrid/test/internal/convertToWireModel.spec.ts
@@ -0,0 +1,173 @@
+// Copyright (c) Microsoft Corporation.
+// Licensed under the MIT license.
+import { assert } from "chai";
+
+import {
+  convertEventGridEventToModelType,
+  convertCloudEventToModelType
+} from "../../src/eventGridClient";
+
+describe("convertEventGridEventToModelType", function() {
+  it("sets a default ID if one is not provided", () => {
+    const convertedEvent = convertEventGridEventToModelType({
+      dataVersion: "1.0",
+      eventType: "Azure.Sdk.TestEvent",
+      subject: "Test Event",
+      data: { hello: "world " }
+    });
+
+    assert.isDefined(convertedEvent.id);
+  });
+
+  it("sets a default event time if one is not provided", () => {
+    const convertedEvent = convertEventGridEventToModelType({
+      dataVersion: "1.0",
+      eventType: "Azure.Sdk.TestEvent",
+      subject: "Test Event",
+      data: { hello: "world " }
+    });
+
+    assert.isDefined(convertedEvent.eventTime);
+  });
+
+  it("does not change set values", () => {
+    const time = new Date();
+    const id = "272871ba-2496-4750-9a90-bedd1ea10191";
+
+    const convertedEvent = convertEventGridEventToModelType({
+      id: id,
+      eventTime: time,
+      dataVersion: "1.0",
+      eventType: "Azure.Sdk.TestEvent",
+      subject: "Test Event",
+      data: { hello: "world " }
+    });
+
+    assert.strictEqual(convertedEvent.id, id);
+    assert.strictEqual(convertedEvent.eventTime, time);
+  });
+});
+
+describe("convertCloudEventToModelType", function() {
+  it("sets a default ID if one is not provided", () => {
+    const convertedEvent = convertCloudEventToModelType({
+      source: "/azure/sdk/tests",
+      type: "Azure.Sdk.TestEvent"
+    });
+
+    assert.isDefined(convertedEvent.id);
+  });
+
+  it("sets a default event time if one is not provided", () => {
+    const convertedEvent = convertCloudEventToModelType({
+      source: "/azure/sdk/tests",
+      type: "Azure.Sdk.TestEvent"
+    });
+
+    assert.isDefined(convertedEvent.time);
+  });
+
+  it("does not change set values", () => {
+    const time = new Date();
+    const id = "272871ba-2496-4750-9a90-bedd1ea10191";
+
+    const convertedEvent = convertCloudEventToModelType({
+      id: id,
+      time: time,
+      source: "/azure/sdk/tests",
+      type: "Azure.Sdk.TestEvent"
+    });
+
+    assert.strictEqual(convertedEvent.id, id);
+    assert.strictEqual(convertedEvent.time, time);
+  });
+
+  it("promotes extension attributes", () => {
+    const traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
+    const tracestate =
+      "rojo=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01,congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4";
+
+    const convertedEvent = convertCloudEventToModelType({
+      source: "/azure/sdk/tests",
+      type: "Azure.Sdk.TestEvent",
+      extensionAttributes: {
+        traceparent,
+        tracestate
+      }
+    });
+
+    // When converted to a model type to send over the wire, the extension attributes are promoted to be
+    // properties on the envelope itself.
+    assert.equal(convertedEvent["traceparent"], traceparent);
+    assert.equal(convertedEvent["tracestate"], tracestate);
+  });
+
+  it("base64 encodes binary data", () => {
+    const binaryData = new Uint8Array(10);
+    for (let i = 0; i < binaryData.length; i++) {
+      binaryData[i] = i;
+    }
+
+    const convertedEvent = convertCloudEventToModelType({
+      source: "/azure/sdk/tests",
+      type: "Azure.Sdk.TestEvent",
+      data: binaryData,
+      datacontenttype: "application/binary"
+    });
+
+    assert.isUndefined(convertedEvent.data);
+    assert.strictEqual(convertedEvent.dataBase64, binaryData);
+  });
+
+  it("fails if data content type is missing for binary data", () => {
+    const binaryData = new Uint8Array(10);
+    for (let i = 0; i < binaryData.length; i++) {
+      binaryData[i] = i;
+    }
+
+    assert.throws(() => {
+      convertCloudEventToModelType({
+        source: "/azure/sdk/tests",
+        type: "Azure.Sdk.TestEvent",
+        data: binaryData
+      });
+    }, /data content type/);
+  });
+
+  it("fails if extenion attributes are invalid", () => {
+    const binaryData = new Uint8Array(10);
+    for (let i = 0; i < binaryData.length; i++) {
+      binaryData[i] = i;
+    }
+
+    assert.throws(() => {
+      convertCloudEventToModelType({
+        source: "/azure/sdk/tests",
+        type: "Azure.Sdk.TestEvent",
+        extensionAttributes: {
+          source: "this-is-not-allowed"
+        }
+      });
+    }, /invalid extension attribute name: source/);
+
+    assert.throws(() => {
+      convertCloudEventToModelType({
+        source: "/azure/sdk/tests",
+        type: "Azure.Sdk.TestEvent",
+        extensionAttributes: {
+          MiXedCasE: "this-is-not-allowed"
+        }
+      });
+    }, /invalid extension attribute name: MiXedCasE/);
+
+    assert.throws(() => {
+      convertCloudEventToModelType({
+        source: "/azure/sdk/tests",
+        type: "Azure.Sdk.TestEvent",
+        extensionAttributes: {
+          data_base64: "this-is-not-allowed"
+        }
+      });
+    }, /invalid extension attribute name: data_base64/);
+  });
+});
diff --git a/sdk/eventgrid/eventgrid/test/util.spec.ts b/sdk/eventgrid/eventgrid/test/internal/util.spec.ts
similarity index 92%
rename from sdk/eventgrid/eventgrid/test/util.spec.ts
rename to sdk/eventgrid/eventgrid/test/internal/util.spec.ts
index fe0605149c86..bd9306378a7f 100644
--- a/sdk/eventgrid/eventgrid/test/util.spec.ts
+++ b/sdk/eventgrid/eventgrid/test/internal/util.spec.ts
@@ -3,7 +3,7 @@
 
 import { assert } from "chai";
 
-import { dateToServiceTimeString } from "../src/util";
+import { dateToServiceTimeString } from "../../src/util";
 
 describe("util", function() {
   describe("dateToServiceTimeString", () => {
diff --git a/sdk/eventgrid/eventgrid/test/consumer.spec.ts b/sdk/eventgrid/eventgrid/test/public/consumer.spec.ts
similarity index 99%
rename from sdk/eventgrid/eventgrid/test/consumer.spec.ts
rename to sdk/eventgrid/eventgrid/test/public/consumer.spec.ts
index ddc4bf3e528b..968aa2731a7c 100644
--- a/sdk/eventgrid/eventgrid/test/consumer.spec.ts
+++ b/sdk/eventgrid/eventgrid/test/public/consumer.spec.ts
@@ -4,7 +4,7 @@
 import { assert, use as chaiUse } from "chai";
 import chaiPromises from "chai-as-promised";
 
-import { EventGridDeserializer } from "../src";
+import { EventGridDeserializer } from "../../src";
 import * as testData from "./utils/testData";
 
 chaiUse(chaiPromises);
diff --git a/sdk/eventgrid/eventgrid/test/eventGridClient.spec.ts b/sdk/eventgrid/eventgrid/test/public/eventGridClient.spec.ts
similarity index 67%
rename from sdk/eventgrid/eventgrid/test/eventGridClient.spec.ts
rename to sdk/eventgrid/eventgrid/test/public/eventGridClient.spec.ts
index 9d592b02627e..89da72841e80 100644
--- a/sdk/eventgrid/eventgrid/test/eventGridClient.spec.ts
+++ b/sdk/eventgrid/eventgrid/test/public/eventGridClient.spec.ts
@@ -10,12 +10,8 @@ import { createRecordedClient, testEnv } from "./utils/recordedClient";
 
 import { TestTracer, setTracer } from "@azure/core-tracing";
 
-import { AzureKeyCredential, EventGridPublisherClient } from "../src/index";
+import { AzureKeyCredential, EventGridPublisherClient } from "../../src";
 
-import {
-  convertEventGridEventToModelType,
-  convertCloudEventToModelType
-} from "../src/eventGridClient";
 import { FullOperationResponse } from "@azure/core-client";
 import { RestError } from "@azure/core-rest-pipeline";
 import { setSpan, context } from "@azure/core-tracing";
@@ -132,7 +128,6 @@ describe("EventGridPublisherClient", function(this: Suite) {
 
         rejected = false;
       } catch (error) {
-        assert.isTrue(error instanceof RestError);
         assert.equal((error as RestError).statusCode, 404);
       }
 
@@ -280,7 +275,6 @@ describe("EventGridPublisherClient", function(this: Suite) {
         ]);
         rejected = false;
       } catch (error) {
-        assert.isTrue(error instanceof RestError);
         assert.equal((error as RestError).statusCode, 404);
       }
 
@@ -382,7 +376,6 @@ describe("EventGridPublisherClient", function(this: Suite) {
 
         rejected = false;
       } catch (error) {
-        assert.isTrue(error instanceof RestError);
         assert.equal((error as RestError).statusCode, 404);
       }
 
@@ -391,171 +384,6 @@ describe("EventGridPublisherClient", function(this: Suite) {
   });
 });
 
-describe("convertEventGridEventToModelType", function() {
-  it("sets a default ID if one is not provided", () => {
-    const convertedEvent = convertEventGridEventToModelType({
-      dataVersion: "1.0",
-      eventType: "Azure.Sdk.TestEvent",
-      subject: "Test Event",
-      data: { hello: "world " }
-    });
-
-    assert.isDefined(convertedEvent.id);
-  });
-
-  it("sets a default event time if one is not provided", () => {
-    const convertedEvent = convertEventGridEventToModelType({
-      dataVersion: "1.0",
-      eventType: "Azure.Sdk.TestEvent",
-      subject: "Test Event",
-      data: { hello: "world " }
-    });
-
-    assert.isDefined(convertedEvent.eventTime);
-  });
-
-  it("does not change set values", () => {
-    const time = new Date();
-    const id = "272871ba-2496-4750-9a90-bedd1ea10191";
-
-    const convertedEvent = convertEventGridEventToModelType({
-      id: id,
-      eventTime: time,
-      dataVersion: "1.0",
-      eventType: "Azure.Sdk.TestEvent",
-      subject: "Test Event",
-      data: { hello: "world " }
-    });
-
-    assert.strictEqual(convertedEvent.id, id);
-    assert.strictEqual(convertedEvent.eventTime, time);
-  });
-});
-
-describe("convertCloudEventToModelType", function() {
-  it("sets a default ID if one is not provided", () => {
-    const convertedEvent = convertCloudEventToModelType({
-      source: "/azure/sdk/tests",
-      type: "Azure.Sdk.TestEvent"
-    });
-
-    assert.isDefined(convertedEvent.id);
-  });
-
-  it("sets a default event time if one is not provided", () => {
-    const convertedEvent = convertCloudEventToModelType({
-      source: "/azure/sdk/tests",
-      type: "Azure.Sdk.TestEvent"
-    });
-
-    assert.isDefined(convertedEvent.time);
-  });
-
-  it("does not change set values", () => {
-    const time = new Date();
-    const id = "272871ba-2496-4750-9a90-bedd1ea10191";
-
-    const convertedEvent = convertCloudEventToModelType({
-      id: id,
-      time: time,
-      source: "/azure/sdk/tests",
-      type: "Azure.Sdk.TestEvent"
-    });
-
-    assert.strictEqual(convertedEvent.id, id);
-    assert.strictEqual(convertedEvent.time, time);
-  });
-
-  it("promotes extension attributes", () => {
-    const traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
-    const tracestate =
-      "rojo=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01,congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4";
-
-    const convertedEvent = convertCloudEventToModelType({
-      source: "/azure/sdk/tests",
-      type: "Azure.Sdk.TestEvent",
-      extensionAttributes: {
-        traceparent,
-        tracestate
-      }
-    });
-
-    // When converted to a model type to send over the wire, the extension attributes are promoted to be
-    // properties on the envelope itself.
-    assert.equal(convertedEvent["traceparent"], traceparent);
-    assert.equal(convertedEvent["tracestate"], tracestate);
-  });
-
-  it("base64 encodes binary data", () => {
-    const binaryData = new Uint8Array(10);
-    for (let i = 0; i < binaryData.length; i++) {
-      binaryData[i] = i;
-    }
-
-    const convertedEvent = convertCloudEventToModelType({
-      source: "/azure/sdk/tests",
-      type: "Azure.Sdk.TestEvent",
-      data: binaryData,
-      datacontenttype: "application/binary"
-    });
-
-    assert.isUndefined(convertedEvent.data);
-    assert.strictEqual(convertedEvent.dataBase64, binaryData);
-  });
-
-  it("fails if data content type is missing for binary data", () => {
-    const binaryData = new Uint8Array(10);
-    for (let i = 0; i < binaryData.length; i++) {
-      binaryData[i] = i;
-    }
-
-    assert.throws(() => {
-      convertCloudEventToModelType({
-        source: "/azure/sdk/tests",
-        type: "Azure.Sdk.TestEvent",
-        data: binaryData
-      });
-    }, /data content type/);
-  });
-
-  it("fails if extenion attributes are invalid", () => {
-    const binaryData = new Uint8Array(10);
-    for (let i = 0; i < binaryData.length; i++) {
-      binaryData[i] = i;
-    }
-
-    assert.throws(() => {
-      convertCloudEventToModelType({
-        source: "/azure/sdk/tests",
-        type: "Azure.Sdk.TestEvent",
-        extensionAttributes: {
-          source: "this-is-not-allowed"
-        }
-      });
-    }, /invalid extension attribute name: source/);
-
-    assert.throws(() => {
-      convertCloudEventToModelType({
-        source: "/azure/sdk/tests",
-        type: "Azure.Sdk.TestEvent",
-        extensionAttributes: {
-          MiXedCasE: "this-is-not-allowed"
-        }
-      });
-    }, /invalid extension attribute name: MiXedCasE/);
-
-    assert.throws(() => {
-      convertCloudEventToModelType({
-        source: "/azure/sdk/tests",
-        type: "Azure.Sdk.TestEvent",
-        extensionAttributes: {
-          data_base64: "this-is-not-allowed"
-        }
-      });
-    }, /invalid extension attribute name: data_base64/);
-  });
-});
-
 function removeApiEventsSuffix(endpoint: string): string {
   const suffix = "/api/events";
 
diff --git a/sdk/eventgrid/eventgrid/test/generateSharedAccessSignature.spec.ts b/sdk/eventgrid/eventgrid/test/public/generateSharedAccessSignature.spec.ts
similarity index 98%
rename from sdk/eventgrid/eventgrid/test/generateSharedAccessSignature.spec.ts
rename to sdk/eventgrid/eventgrid/test/public/generateSharedAccessSignature.spec.ts
index 85821849fb6a..8013a7ac9380 100644
--- a/sdk/eventgrid/eventgrid/test/generateSharedAccessSignature.spec.ts
+++ b/sdk/eventgrid/eventgrid/test/public/generateSharedAccessSignature.spec.ts
@@ -2,7 +2,7 @@
 // Licensed under the MIT license.
 
 import { assert } from "chai";
-import { AzureKeyCredential, generateSharedAccessSignature } from "../src/index";
+import { AzureKeyCredential, generateSharedAccessSignature } from "../../src";
 
 describe("generateSharedAccessSignature", function() {
   it("generates the correct signiture", async () => {
diff --git a/sdk/eventgrid/eventgrid/test/utils/recordedClient.ts b/sdk/eventgrid/eventgrid/test/public/utils/recordedClient.ts
similarity index 96%
rename from sdk/eventgrid/eventgrid/test/utils/recordedClient.ts
rename to sdk/eventgrid/eventgrid/test/public/utils/recordedClient.ts
index 609ef4fb05d5..4b34dddb1abe 100644
--- a/sdk/eventgrid/eventgrid/test/utils/recordedClient.ts
+++ b/sdk/eventgrid/eventgrid/test/public/utils/recordedClient.ts
@@ -7,7 +7,7 @@ import * as dotenv from "dotenv";
 import { env, Recorder, record, RecorderEnvironmentSetup } from "@azure/test-utils-recorder";
 import { isNode } from "./testUtils";
 
-import { EventGridPublisherClient, InputSchema } from "../../src/index";
+import { EventGridPublisherClient, InputSchema } from "../../../src";
 import { KeyCredential } from "@azure/core-auth";
 
 if (isNode) {
diff --git a/sdk/eventgrid/eventgrid/test/utils/testData.ts b/sdk/eventgrid/eventgrid/test/public/utils/testData.ts
similarity index 100%
rename from sdk/eventgrid/eventgrid/test/utils/testData.ts
rename to sdk/eventgrid/eventgrid/test/public/utils/testData.ts
diff --git a/sdk/eventgrid/eventgrid/test/utils/testUtils.ts b/sdk/eventgrid/eventgrid/test/public/utils/testUtils.ts
similarity index 100%
rename from sdk/eventgrid/eventgrid/test/utils/testUtils.ts
rename to sdk/eventgrid/eventgrid/test/public/utils/testUtils.ts
diff --git a/sdk/eventgrid/eventgrid/tests.yml b/sdk/eventgrid/eventgrid/tests.yml
index eef5a6b48495..5c802ace4c1c 100644
--- a/sdk/eventgrid/eventgrid/tests.yml
+++ b/sdk/eventgrid/eventgrid/tests.yml
@@ -5,5 +5,3 @@ stages:
     parameters:
       PackageName: "@azure/eventgrid"
       ServiceDirectory: eventgrid
-      MatrixFilters:
-        - DependencyVersion=^$