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

[BUG] CDN Packaging is not exposing the internal tools (CoreUtils / Telemetry / etc) #1857 #1858

Merged
merged 1 commit into from
Jun 29, 2022
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
273 changes: 273 additions & 0 deletions AISKU/Tests/Unit/src/CdnPackaging.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
import { AITestClass, Assert } from "@microsoft/ai-test-framework";
import { AnalyticsPluginIdentifier, BreezeChannelIdentifier, DEFAULT_BREEZE_ENDPOINT, DisabledPropertyName, DistributedTracingModes, PropertiesPluginIdentifier, RequestHeaders, SeverityLevel } from "@microsoft/applicationinsights-common";
import { addEventHandler, eventOff, eventOn, generateW3CId, LoggingSeverity, mergeEvtNamespace, objForEachKey, removeEventHandler, strUndefined } from "@microsoft/applicationinsights-core-js";
import { Snippet } from "../../../src/Initialization";

declare var define;

const enum CdnFormat {
Umd = 0,
Gbl = 1,
CommonJs = 2
}

export class CdnPackagingChecks extends AITestClass {

public testInitialize() {
}

public testFinishedCleanup(): void {
if (typeof window !== strUndefined) {
var _window = window;
let aiName = _window["appInsightsSDK"] || "appInsights";
if (_window[aiName] !== undefined) {
const snippet: Snippet = _window[aiName] as any;
if (snippet["unload"]) {
snippet["unload"](false);
} else {
if (snippet["appInsightsNew"]) {
snippet["appInsightsNew"].unload();
}
}
}
}
}

public testCleanup() {
}

public registerTests() {
this.checkFullPackaging();
this.addMinifiedPackaging();
}

constructor() {
super("CdnPackagingChecks");
}

private checkFullPackaging(): void {
this._checkPackaging("../browser/ai.2.js", CdnFormat.Umd);
this._checkPackaging("../browser/ai.2.gbl.js", CdnFormat.Gbl);
this._checkPackaging("../browser/ai.2.cjs.js", CdnFormat.CommonJs);
}

private addMinifiedPackaging(): void {
this._checkPackaging("../browser/ai.2.min.js", CdnFormat.Umd);
this._checkPackaging("../browser/ai.2.gbl.min.js", CdnFormat.Gbl);
this._checkPackaging("../browser/ai.2.cjs.min.js", CdnFormat.CommonJs);
}

private _validateExpectedExports(theExports: any) {
Assert.ok(theExports.ApplicationInsights, "ApplicationInsights exists");
Assert.ok(theExports.Telemetry, "Telemetry exists");
Assert.ok(theExports.Telemetry.DistributedTracingModes, "Telemetry.DistributedTracingModes exists");
Assert.ok(theExports.Telemetry.Util, "Telemetry exists");

Assert.ok(theExports.LoggingSeverity, "LoggingSeverity exists");
objForEachKey(LoggingSeverity, (name, value) => {
Assert.equal(name, theExports.LoggingSeverity[value], `Checking LoggingSeverity.${name} === ${value}`);
Assert.equal(value, theExports.LoggingSeverity[name], `Checking LoggingSeverity.${value} === ${name}`);
});

Assert.ok(theExports.PerfEvent, "PerfEvent exists");
Assert.ok(theExports.PerfManager, "PerfManager exists");
Assert.ok(theExports.doPerf, "doPerf exists");
Assert.ok(theExports.CoreUtils, "CoreUtils exists");
Assert.ok(theExports.newId, "newId exists");
Assert.ok(theExports.newGuid, "newGuid exists");
Assert.ok(theExports.random32, "random32 exists");
Assert.ok(theExports.randomValue, "randomValue exists");
Assert.ok(theExports.generateW3CId, "generateW3CId exists");
Assert.ok(theExports.findW3cTraceParent, "findW3cTraceParent exists");
Assert.ok(theExports.findMetaTag, "findMetaTag exists");
Assert.ok(theExports.mergeEvtNamespace, "mergeEvtNamespace exists");
Assert.ok(theExports.eventOn, "eventOn exists");
Assert.ok(theExports.eventOff, "eventOff exists");
Assert.ok(theExports.addEventHandler, "addEventHandler exists");
Assert.ok(theExports.removeEventHandler, "removeEventHandler exists");

Assert.ok(theExports.isBeaconsSupported, "isBeaconsSupported exists");

Assert.ok(theExports.Util, "Util exists");
Assert.equal(theExports.Util, theExports.Telemetry.Util, "Telemetry.Util matches Util");
Assert.ok(theExports.RequestHeaders, "RequestHeaders exists");
objForEachKey(RequestHeaders, (name, value) => {
Assert.equal(value, theExports.RequestHeaders[name], `Checking RequestHeaders.${value} === ${name}`);
Assert.notEqual(undefined, theExports.RequestHeaders[name], `Checking RequestHeaders.${name} exists`);
});

Assert.equal(theExports.DisabledPropertyName, DisabledPropertyName, "DisabledPropertyName value");
Assert.equal(theExports.DEFAULT_BREEZE_ENDPOINT, DEFAULT_BREEZE_ENDPOINT, "DEFAULT_BREEZE_ENDPOINT value");

Assert.ok(theExports.SeverityLevel, "SeverityLevel exists");
objForEachKey(SeverityLevel, (name, value) => {
Assert.equal(name, theExports.SeverityLevel[value], `Checking SeverityLevel.${name} === ${value}`);
Assert.equal(value, theExports.SeverityLevel[name], `Checking SeverityLevel.${value} === ${name}`);
});

Assert.ok(theExports.DistributedTracingModes, "DistributedTracingModes exists");
Assert.equal(theExports.DistributedTracingModes, theExports.Telemetry.DistributedTracingModes, "Telemetry.DistributedTracingModes equals DistributedTracingModes");
objForEachKey(DistributedTracingModes, (name, value) => {
Assert.equal(name, theExports.DistributedTracingModes[value], `Checking DistributedTracingMode.${name} === ${value}`);
Assert.equal(value, theExports.DistributedTracingModes[name], `Checking DistributedTracingMode.${value} === ${name}`);
});

Assert.equal(theExports.PropertiesPluginIdentifier, PropertiesPluginIdentifier, "PropertiesPluginIdentifier value");
Assert.equal(theExports.BreezeChannelIdentifier, BreezeChannelIdentifier, "BreezeChannelIdentifier value");
Assert.equal(theExports.AnalyticsPluginIdentifier, AnalyticsPluginIdentifier, "AnalyticsPluginIdentifier value");
}

private _validateExports(text: string, format: CdnFormat) {
let orgExports = exports;
let orgDefine = define;
try {

// remove any previously registered bundle
delete window["Microsoft"];

// Hide define()
define = undefined;

// Remove any "exports"
exports = {};

// Used to simulate loading a cjs module
let cjsModule = this["_cjsModule"] = {};

// "process" the script
eval(text);
if (format == CdnFormat.Umd) {
// Because "exports" exists then no namespace is expected
let microsoft: any = window["Microsoft"];
Assert.equal(undefined, microsoft, "global Microsoft namespace does not exists");
Assert.equal(undefined, cjsModule["Microsoft"], "global not added to cjs exports");

this._validateExpectedExports(exports);
} else if (format === CdnFormat.Gbl) {
let microsoft: any = window["Microsoft"];
Assert.equal(undefined, microsoft, "global Microsoft namespace does not exists");
Assert.equal(undefined, exports["Microsoft"], "global not added to exports");
Assert.equal(undefined, cjsModule["Microsoft"], "global not added to cjs exports");

microsoft = this["Microsoft"];
Assert.ok(microsoft, "Microsoft namespace exists on this");
Assert.ok(microsoft.ApplicationInsights, "Microsoft namespace exists");

this._validateExpectedExports(microsoft.ApplicationInsights);
} else if (format === CdnFormat.CommonJs) {
// There is no namespace for common js
let microsoft: any = window["Microsoft"];
Assert.equal(undefined, microsoft, "global Microsoft namespace does not exists");
Assert.equal(undefined, exports["Microsoft"], "global Microsoft namespace does not exist on exports");

this._validateExpectedExports(cjsModule);
}

} finally {
if (orgExports) {
exports = orgExports;
} else {
exports = undefined;
}

if (orgDefine) {
define = orgDefine;
} else {
define = undefined;
}
}
}

private _validateGlobalExports(text: string, format: CdnFormat) {
let orgExports = window.exports;
let orgDefine = define;
try {
// remove any previously registered bundle
delete window["Microsoft"];

// Hide define()
define = undefined;

// Remove any "exports"
exports = undefined;

// Used to simulate loading a cjs module
let cjsModule = this["_cjsModule"] = {};

// "process" the script
eval(text);
if (format == CdnFormat.Umd) {
// Because we are simulating no "exports" then there should be a global namespace defined
Assert.equal(undefined, exports, "No global exports should have been defined");
Assert.equal(undefined, cjsModule["Microsoft"], "global not added to cjs exports");

let microsoft: any = window["Microsoft"];
Assert.ok(microsoft, "Microsoft namespace exists");
Assert.ok(microsoft.ApplicationInsights, "Microsoft namespace exists");

this._validateExpectedExports(microsoft.ApplicationInsights);
} else if (format === CdnFormat.Gbl) {
let microsoft: any = window["Microsoft"];
Assert.equal(undefined, microsoft, "global Microsoft namespace does not exists");
Assert.equal(undefined, exports, "global not added to exports");
Assert.equal(undefined, cjsModule["Microsoft"], "global not added to cjs exports");

microsoft = this["Microsoft"];
Assert.ok(microsoft, "Microsoft namespace exists on this");
Assert.ok(microsoft.ApplicationInsights, "Microsoft namespace exists");

this._validateExpectedExports(microsoft.ApplicationInsights);
} else if (format === CdnFormat.CommonJs) {
// There is no namespace for common js
let microsoft: any = window["Microsoft"];
Assert.equal(undefined, microsoft, "global Microsoft namespace does not exists");
Assert.equal(undefined, exports, "global not added to exports");

this._validateExpectedExports(cjsModule);
}
} finally {
if (orgExports) {
exports = orgExports;
} else {
exports = undefined;
}

if (orgDefine) {
define = orgDefine;
} else {
define = undefined;
}
}
}

private _checkPackaging(cdnPackage: string, format: CdnFormat): void {
let fileName = cdnPackage.split("..")[1];
this.testCase({
name: `Test AISKU Cdn packaging - ${fileName}`,
test: () => {
Assert.ok(true, `test file: ${fileName}`);
let request = new Request(cdnPackage, {method:"GET"});
return fetch(request).then((response) => {
if (!response.ok) {
Assert.ok(false, `fetch bundle AISKU ${fileName} error: ${response.statusText}`);
return;
} else {
return response.text().then(text => {
if (format === CdnFormat.CommonJs) {
// Wrap commonJs in a closure so the global space is not polluted
text = "(function(exports) {" + text + "})(this._cjsModule);";
}

this._validateExports(text, format);
this._validateGlobalExports(text, format);
}).catch((error: Error) => {
Assert.ok(false, `AISKU bundle ${fileName} response error: ${error}`);
});
}
}).catch((error: Error) => {
Assert.ok(false, `AISKU bundle ${fileName} deflate size error: ${error}`);
});
}
});
}
}
2 changes: 2 additions & 0 deletions AISKU/Tests/Unit/src/aiskuunittests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AISKUSizeCheck } from "./AISKUSize.Tests";
import { ApplicationInsightsTests } from './applicationinsights.e2e.tests';
import { ApplicationInsightsFetchTests } from './applicationinsights.e2e.fetch.tests';
import { CdnPackagingChecks } from './CdnPackaging.tests';
import { SanitizerE2ETests } from './sanitizer.e2e.tests';
import { ValidateE2ETests } from './validate.e2e.tests';
import { SenderE2ETests } from './sender.e2e.tests';
Expand All @@ -13,6 +14,7 @@ export function runTests() {
new ApplicationInsightsTests().registerTests();
new ApplicationInsightsFetchTests().registerTests();
new ApplicationInsightsDeprecatedTests().registerTests();
new CdnPackagingChecks().registerTests();
new SanitizerE2ETests().registerTests();
new ValidateE2ETests().registerTests();
new SenderE2ETests().registerTests();
Expand Down
45 changes: 44 additions & 1 deletion AISKU/src/Init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,50 @@ import { Snippet } from "./Initialization";
import { ApplicationInsightsContainer } from "./ApplicationInsightsContainer";
import { strUndefined } from "@microsoft/applicationinsights-core-js";

export { Initialization as ApplicationInsights, Snippet } from "./Initialization";
// ----------------------------------------------------------------------------------------------------
// Exports available from the Cdn bundles
// ----------------------------------------------------------------------------------------------------
export {
Initialization as ApplicationInsights, Snippet,
Telemetry
} from "./Initialization";
export {
LoggingSeverity,
PerfEvent,
PerfManager,
doPerf,
CoreUtils,
newId,
newGuid,
random32,
randomValue,
generateW3CId,
findW3cTraceParent,
findMetaTag,
mergeEvtNamespace,
eventOn,
eventOff,
addEventHandler,
removeEventHandler,
isBeaconsSupported
} from "@microsoft/applicationinsights-core-js";

export {
Util,
RequestHeaders,
DisabledPropertyName,
DEFAULT_BREEZE_ENDPOINT,
SeverityLevel,
DistributedTracingModes,
PropertiesPluginIdentifier,
BreezeChannelIdentifier,
AnalyticsPluginIdentifier
} from "@microsoft/applicationinsights-common";

// ----------------------------------------------------------------------------------------------------
// End of Exports available from the Cdn bundles
// ----------------------------------------------------------------------------------------------------


function _logWarn(aiName:string, message:string) {
// TODO: Find better place to warn to console when SDK initialization fails
Expand Down