-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: ensure compressed bundle size is less than 1MB
closes #36
- Loading branch information
1 parent
9aae653
commit 0e925c2
Showing
4 changed files
with
30 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
/* eslint-disable import/order -- https://github.com/endojs/endo/issues/1235 */ | ||
import { test } from './prepare-test-env-ava.js'; | ||
import { execa } from 'execa'; | ||
import { promises as fs } from 'fs'; | ||
import { execSync } from 'node:child_process'; | ||
import { promises as fs } from 'node:fs'; | ||
import { makeCompressFile } from './utils.js'; | ||
|
||
test('proposal builder generates bundles less than 5MB', async t => { | ||
const { stdout } = await execa('agoric', [ | ||
'run', | ||
'scripts/build-contract-deployer.js', | ||
]); | ||
test.before(t => (t.context.compressFile = makeCompressFile(fs.readFile))); | ||
|
||
test.only('proposal builder generates compressed bundles less than 1MB', async t => { | ||
const stdout = execSync('agoric run scripts/build-contract-deployer.js',{ encoding: 'utf8' }); | ||
t.log('agoric run stdout:', stdout); | ||
t.truthy(stdout, 'Proposal successfully bundled.'); | ||
|
||
const regex = /agd tx swingset install-bundle @(.*?)\.json/g; | ||
const bundles = Array.from(stdout.matchAll(regex), m => `${m[1]}.json`); | ||
t.assert(bundles.length, 'Found bundles in stdout'); | ||
|
||
for (const bundle of bundles) { | ||
const { size } = await fs.stat(bundle); | ||
t.assert(size); | ||
const sizeInMb = size / (1024 * 1024); | ||
t.assert(sizeInMb < 5, `Bundle ${bundle} is less than 5MB`); | ||
// eslint-disable-next-line @jessie.js/safe-await-separator | ||
const buffer = await t.context.compressFile(bundle); | ||
t.assert(buffer); | ||
const sizeInMb = buffer.length / (1024 * 1024); | ||
t.assert(sizeInMb < 1, 'Compressed bundle is less than 1MB'); | ||
t.log({ | ||
bundleId: bundle.split('cache/')[1].split('.json')[0], | ||
fileSize: `${sizeInMb} MB`, | ||
compressedSize: `${sizeInMb} MB`, | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { gzip } from 'zlib'; | ||
import { promisify } from 'util'; | ||
import { Buffer } from 'buffer'; | ||
|
||
/** | ||
* @typedef {import('fs').promises['readFile']} PromisifiedFSReadFile | ||
*/ | ||
|
||
/** @param {PromisifiedFSReadFile} readFile */ | ||
export const makeCompressFile = readFile => async filePath => { | ||
const fileContents = await readFile(filePath, 'utf8'); | ||
const buffer = Buffer.from(fileContents, 'utf-8'); | ||
const compressed = await promisify(gzip)(buffer); | ||
return compressed; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters