Skip to content

Commit

Permalink
[Main]: Make file size checks flexible for nightly/dev builds (#2421)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSNev authored Sep 27, 2024
1 parent 2aaf54a commit ed8632a
Show file tree
Hide file tree
Showing 8 changed files with 722 additions and 361 deletions.
89 changes: 71 additions & 18 deletions AISKU/Tests/Unit/src/AISKUSize.Tests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,57 @@
import { AITestClass, Assert } from "@microsoft/ai-test-framework";
import { dumpObj } from '@nevware21/ts-utils';
import { createPromise, doAwait, IPromise } from '@nevware21/ts-async';
import { strUndefined } from "@microsoft/applicationinsights-core-js";
import { utlRemoveSessionStorage } from "@microsoft/applicationinsights-common";
import * as pako from "pako";
import { Snippet } from "../../../src/Snippet";
import { utlRemoveSessionStorage } from "@microsoft/applicationinsights-common";

const PACKAGE_JSON = "../package.json";

function removeTrailingComma(text) {
return text.replace(/,(\s*[}\],])/g, "$1");
}

function _loadPackageJson(cb:(isNightly: boolean, packageJson: any) => IPromise<void>): IPromise<void> {
return createPromise<void>((testCompleted, testFailed) => {
function _handleCallback(packageJson: any) {
let version = packageJson.version || "unknown";
let isNightly = version.includes("nightly") || version.includes("dev");
doAwait(cb(isNightly, packageJson), () => {
testCompleted();
}, (error) => {
Assert.ok(false, `checkIsNightlyBuild error: ${error}`);
testFailed(error);
});
}

fetch(PACKAGE_JSON).then((response) => {
if (!response.ok) {
Assert.ok(false, `fetch package.json error: ${dumpObj(response)}`);
_handleCallback(false);
} else {
return response.text().then((content) => {
let json = JSON.parse(removeTrailingComma(content));
_handleCallback(json);
}, (error) => {
Assert.ok(false, `fetch package.json error: ${error}`);
_handleCallback({});
});
}
}, (error) => {
Assert.ok(false, `fetch package.json error: ${error}`);
_handleCallback({});
});
});
}

function _checkSize(checkType: string, maxSize: number, size: number, isNightly: boolean): void {
if (isNightly) {
maxSize += .5;
}

Assert.ok(size <= maxSize, `exceed ${maxSize} KB, current ${checkType} size is: ${size} KB`);
}

export class AISKUSizeCheck extends AITestClass {
private readonly MAX_RAW_SIZE = 144;
Expand Down Expand Up @@ -67,23 +116,27 @@ export class AISKUSizeCheck extends AITestClass {
name: `Test AISKU${postfix} deflate size`,
test: () => {
Assert.ok(true, `test file: ${fileName}`);
let request = new Request(_filePath, {method:"GET"});
return fetch(request).then((response) => {
if (!response.ok) {
Assert.ok(false, `fetch AISKU${postfix} error: ${response.statusText}`);
return;
} else {
return response.text().then(text => {
let size = Math.ceil((text.length/1024) * 100) / 100.0;
Assert.ok(size <= _maxFullSize, `max ${_maxFullSize} KB, current deflate size is: ${size} KB`);
let deflateSize = Math.ceil((pako.deflate(text).length/1024) * 100) / 100.0;
Assert.ok(deflateSize <= _maxDeflateSize ,`max ${_maxDeflateSize} KB, current deflate size is: ${deflateSize} KB`);
}).catch((error: Error) => {
Assert.ok(false, `AISKU${postfix} response error: ${error}`);
});
}
}).catch((error: Error) => {
Assert.ok(false, `AISKU${postfix} deflate size error: ${error}`);
return _loadPackageJson((isNightly, packageJson) => {
Assert.ok(true, ` checking : ${packageJson.name || "??"} v${packageJson.version || "unknown"}`);
let request = new Request(_filePath, {method:"GET"});
return fetch(request).then((response) => {
if (!response.ok) {
Assert.ok(false, `fetch AISKU${postfix} error: ${response.statusText}`);
return;
} else {
return response.text().then(text => {
let size = Math.ceil((text.length/1024) * 100) / 100.0;
_checkSize("bundle", _maxFullSize, size, isNightly);

let deflateSize = Math.ceil((pako.deflate(text).length/1024) * 100) / 100.0;
_checkSize("deflate", _maxDeflateSize, deflateSize, isNightly);
}).catch((error: Error) => {
Assert.ok(false, `AISKU${postfix} response error: ${error}`);
});
}
}).catch((error: Error) => {
Assert.ok(false, `AISKU${postfix} deflate size error: ${error}`);
});
});
}
});
Expand Down
87 changes: 70 additions & 17 deletions AISKULight/Tests/Unit/src/AISKULightSize.Tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
import { AITestClass, Assert } from "@microsoft/ai-test-framework";
import { dumpObj } from '@nevware21/ts-utils';
import { createPromise, doAwait, IPromise } from '@nevware21/ts-async';
import * as pako from "pako";

const PACKAGE_JSON = "../package.json";

function removeTrailingComma(text) {
return text.replace(/,(\s*[}\],])/g, "$1");
}

function _loadPackageJson(cb:(isNightly: boolean, packageJson: any) => IPromise<void>): IPromise<void> {
return createPromise<void>((testCompleted, testFailed) => {
function _handleCallback(packageJson: any) {
let version = packageJson.version || "unknown";
let isNightly = version.includes("nightly") || version.includes("dev");
doAwait(cb(isNightly, packageJson), () => {
testCompleted();
}, (error) => {
Assert.ok(false, `checkIsNightlyBuild error: ${error}`);
testFailed(error);
});
}

fetch(PACKAGE_JSON).then((response) => {
if (!response.ok) {
Assert.ok(false, `fetch package.json error: ${dumpObj(response)}`);
_handleCallback(false);
} else {
return response.text().then((content) => {
let json = JSON.parse(removeTrailingComma(content));
_handleCallback(json);
}, (error) => {
Assert.ok(false, `fetch package.json error: ${error}`);
_handleCallback({});
});
}
}, (error) => {
Assert.ok(false, `fetch package.json error: ${error}`);
_handleCallback({});
});
});
}

function _checkSize(checkType: string, maxSize: number, size: number, isNightly: boolean): void {
if (isNightly) {
maxSize += .5;
}

Assert.ok(size <= maxSize, `exceed ${maxSize} KB, current ${checkType} size is: ${size} KB`);
}

export class AISKULightSizeCheck extends AITestClass {
private readonly MAX_RAW_SIZE = 90;
private readonly MAX_BUNDLE_SIZE = 90;
Expand Down Expand Up @@ -43,23 +92,27 @@ export class AISKULightSizeCheck extends AITestClass {
name: `Test AISKULight${postfix} deflate size`,
test: () => {
Assert.ok(true, `test file: ${fileName}`);
let request = new Request(_filePath, {method:"GET"});
return fetch(request).then((response) => {
if (!response.ok) {
Assert.ok(false, `fetch AISKULight${postfix} error: ${response.statusText}`);
return;
} else {
return response.text().then(text => {
let size = Math.ceil((text.length/1024) * 100) / 100.0;
Assert.ok(size <= _maxFullSize, `max ${_maxFullSize} KB, current deflate size is: ${size} KB`);
let deflateSize = Math.ceil((pako.deflate(text).length/1024) * 100) / 100.0;
Assert.ok(deflateSize <= _maxDeflateSize ,`max ${_maxDeflateSize} KB, current deflate size is: ${deflateSize} KB`);
}).catch((error: Error) => {
Assert.ok(false, `AISKULight${postfix} response error: ${error}`);
});
}
}).catch((error: Error) => {
Assert.ok(false, `AISKULight${postfix} deflate size error: ${error}`);
return _loadPackageJson((isNightly, packageJson) => {
Assert.ok(true, ` checking : ${packageJson.name || "??"} v${packageJson.version || "unknown"}`);
let request = new Request(_filePath, {method:"GET"});
return fetch(request).then((response) => {
if (!response.ok) {
Assert.ok(false, `fetch AISKULight${postfix} error: ${response.statusText}`);
return;
} else {
return response.text().then(text => {
let size = Math.ceil((text.length/1024) * 100) / 100.0;
_checkSize("bundle", _maxFullSize, size, isNightly);

let deflateSize = Math.ceil((pako.deflate(text).length/1024) * 100) / 100.0;
_checkSize("deflate", _maxDeflateSize, deflateSize, isNightly);
}).catch((error: Error) => {
Assert.ok(false, `AISKULight${postfix} response error: ${error}`);
});
}
}).catch((error: Error) => {
Assert.ok(false, `AISKULight${postfix} deflate size error: ${error}`);
});
});
}
});
Expand Down
94 changes: 73 additions & 21 deletions channels/1ds-post-js/test/Unit/src/FileSizeCheckTest.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,88 @@
import { AITestClass } from "@microsoft/ai-test-framework";
import { createPromise } from '@nevware21/ts-async';
import { dumpObj } from '@nevware21/ts-utils';
import { createPromise, doAwait, IPromise } from '@nevware21/ts-async';
import * as pako from 'pako';

const MAX_DEFLATE_SIZE = 26.5;

const PACKAGE_JSON = "../package.json";

function removeTrailingComma(text) {
return text.replace(/,(\s*[}\],])/g, "$1");
}

function _loadPackageJson(cb: (isNightly: boolean, packageJson: any) => IPromise<void>): IPromise<void> {
return createPromise<void>((testCompleted, testFailed) => {
function _handleCallback(packageJson: any) {
let version = packageJson.version || "unknown";
let isNightly = version.includes("nightly") || version.includes("dev");
doAwait(cb(isNightly, packageJson), () => {
testCompleted();
}, (error) => {
QUnit.assert.ok(false, `checkIsNightlyBuild error: ${error}`);
testFailed(error);
});
}

fetch(PACKAGE_JSON).then((response) => {
if (!response.ok) {
QUnit.assert.ok(false, `fetch package.json error: ${dumpObj(response)}`);
_handleCallback(false);
} else {
return response.text().then((content) => {
let json = JSON.parse(removeTrailingComma(content));
_handleCallback(json);
}, (error) => {
QUnit.assert.ok(false, `fetch package.json error: ${error}`);
_handleCallback({});
});
}
}, (error) => {
QUnit.assert.ok(false, `fetch package.json error: ${error}`);
_handleCallback({});
});
});
}

function _checkSize(checkType: string, maxSize: number, size: number, isNightly: boolean): void {
if (isNightly) {
maxSize += .5;
}

QUnit.assert.ok(size <= maxSize, `exceed ${maxSize} KB, current ${checkType} size is: ${size} KB`);
}


export class FileSizeCheckTest extends AITestClass {

public testInitialize() {
}

public registerTests() {
this.testCase({
name: "Test ms.post gzip size",
useFakeServer: false,
test: () => {
return createPromise<void>((testCompleted, testFailed) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', '../bundle/es5/ms.post.min.js',true);
xhr.onload = () => {
let size = Math.ceil(pako.deflate(xhr.responseText).length/1024);
QUnit.assert.ok(size <= MAX_DEFLATE_SIZE ,`exceed ${MAX_DEFLATE_SIZE} KB, current deflate size is: ${size} KB`)
testCompleted();
};
xhr.send()
xhr.onerror = (err) =>{
QUnit.assert.ok(false,"error in getting deflate size: " + err)
testFailed(err);
this.testCase({
name: "Test ms.post gzip size",
useFakeServer: false,
test: () => {
return _loadPackageJson((isNightly, packageJson) => {
QUnit.assert.ok(true, ` checking : ${packageJson.name || "??"} v${packageJson.version || "unknown"}`);
return createPromise<void>((testCompleted, testFailed) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', '../bundle/es5/ms.post.min.js', true);
xhr.onload = () => {
let size = Math.ceil(pako.deflate(xhr.responseText).length / 1024);
_checkSize("deflate", MAX_DEFLATE_SIZE, size, isNightly);
testCompleted();
};
xhr.send()
xhr.onerror = (err) => {
QUnit.assert.ok(false, "error in getting deflate size: " + err)
testFailed(err);
}
});
});
}
})
}
});
}
});
}
}


Loading

0 comments on commit ed8632a

Please sign in to comment.