Skip to content

Commit

Permalink
Update tests for the new decoding of compressed encoded base64 values
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilson WeeSheng Khoo committed Jan 7, 2025
1 parent 0fca159 commit 2d837cc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"typescript": "^5.5.2"
},
"dependencies": {
"@govtechsg/oobee": "github:GovTechSG/purple-a11y#master"
"@govtechsg/oobee": "github:GovTechSG/purple-a11y#feat/summary-report-partial",
"pako": "^2.1.0"
}
}
40 changes: 27 additions & 13 deletions src/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pako from 'pako';
import 'cypress-mochawesome-reporter/register';

Cypress.Commands.add("injectPurpleA11yScripts", () => {
Expand Down Expand Up @@ -43,20 +44,33 @@ export const getCliCommand = (cliOptionsJson, toRunInPurpleA11yDirectly = false)
}
};

const base64DecodeChunkedWithDecoder = (data, chunkSize = 1024 * 1024) => {
const encodedChunks = data.split('.');
const decoder = new TextDecoder();
const jsonParts = [];
const decompressJsonObject = (base64Gzipped, chunkSize = 65536) => {
const inflator = new pako.Inflate({ to: "string" });

encodedChunks.forEach(chunk => {
for (let i = 0; i < chunk.length; i += chunkSize) {
const chunkPart = chunk.slice(i, i + chunkSize);
const decodedBytes = Uint8Array.from(atob(chunkPart), c => c.charCodeAt(0));
jsonParts.push(decoder.decode(decodedBytes, { stream: true }));
let offset = 0;
const totalLength = base64Gzipped.length;

while (offset < totalLength) {
const chunk = base64Gzipped.slice(offset, offset + chunkSize);
offset += chunkSize;

const binaryString = atob(chunk);

const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
});

return JSON.parse(jsonParts.join(''));
const isLastChunk = offset >= totalLength;
inflator.push(bytes, isLastChunk);
}

if (inflator.err) {
throw new Error("Pako inflate error: " + inflator.msg);
}

const decompressedString = inflator.result;
return JSON.parse(decompressedString);
};

Cypress.Commands.add('runPurpleA11yProcess', (cliOptionsJson) => {
Expand Down Expand Up @@ -144,8 +158,8 @@ Cypress.Commands.add('checkResultFilesCreated', (cliOptionsJson, purpleA11yResul
Cypress.Commands.add('checkReportHtmlScanData', (cliOptionsJson, purpleA11yResultFolder, isIntegrationMode = false) => {
return cy.task('readFile', `${cliOptionsJson.e}/${purpleA11yResultFolder}/report.html`)
.then((reportHtmlData: string) => {
const scanDataEncoded = reportHtmlData.match(/scanData\s*=\s*base64DecodeChunkedWithDecoder\('([^']+)'\)/)[1];
const scanDataDecodedJson = base64DecodeChunkedWithDecoder(scanDataEncoded);
const scanDataEncoded = reportHtmlData.match(/scanData\s*=\s*decompressJsonObject\('([^']+)'\)/)[1];
const scanDataDecodedJson = decompressJsonObject(scanDataEncoded);

// TEST CASE: scanData.scanType should be according to the flag -c
let expectedScanType;
Expand Down

0 comments on commit 2d837cc

Please sign in to comment.