Add test fixtures for the revive WASM version (#160)

This commit is contained in:
Sebastian Miasojed
2025-01-16 16:01:34 +01:00
committed by GitHub
parent 8ffe072fee
commit b78b2b2af9
8 changed files with 252 additions and 112 deletions
+63 -39
View File
@@ -1,63 +1,87 @@
const { test, expect } = require('@playwright/test');
const fs = require('fs');
const path = require('path');
const validCompilerInput = {
language: 'Solidity',
sources: {
'MyContract.sol': {
content: `
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract MyContract {
function greet() public pure returns (string memory) {
return "Hello";
}
}
`,
},
},
settings: {
optimizer: {
enabled: true,
runs: 200,
},
outputSelection: {
'*': {
'*': ['abi', 'evm.bytecode'],
},
},
},
};
function loadFixture(fixture) {
const fixturePath = path.resolve(__dirname, `../fixtures/${fixture}`);
return JSON.parse(fs.readFileSync(fixturePath, 'utf-8'));
}
async function runWorker(page, input) {
return await page.evaluate((input) => {
return new Promise((resolve, reject) => {
const worker = new Worker('worker.js'); // Path to your worker.js file
const worker = new Worker('worker.js');
worker.postMessage(JSON.stringify(input));
worker.onmessage = (event) => {
resolve(event.data.output);
worker.terminate(); // Clean up the worker
worker.terminate();
};
worker.onerror = (error) => {
reject(error.message || error); // Provide error message for clarity
worker.terminate(); // Clean up the worker
reject(error.message || error);
worker.terminate();
};
});
}, input); // Pass the input as an argument to the function
}, input);
}
test('Test browser', async ({ page }) => {
test('should successfully compile valid Solidity code in browser', async ({ page }) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
const result = await runWorker(page, validCompilerInput);
const standardInput = loadFixture('storage.json')
const result = await runWorker(page, standardInput);
expect(typeof result).toBe('string');
let output = JSON.parse(result);
expect(output).toHaveProperty('contracts');
expect(output.contracts['MyContract.sol']).toHaveProperty('MyContract');
expect(output.contracts['MyContract.sol'].MyContract).toHaveProperty('abi');
expect(output.contracts['MyContract.sol'].MyContract).toHaveProperty('evm');
expect(output.contracts['MyContract.sol'].MyContract.evm).toHaveProperty('bytecode');
expect(output.contracts['fixtures/storage.sol']).toHaveProperty('Storage');
expect(output.contracts['fixtures/storage.sol'].Storage).toHaveProperty('abi');
expect(output.contracts['fixtures/storage.sol'].Storage).toHaveProperty('evm');
expect(output.contracts['fixtures/storage.sol'].Storage.evm).toHaveProperty('bytecode');
});
test('should successfully compile large valid Solidity code in browser', async ({ page }) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
const standardInput = loadFixture('token.json')
const result = await runWorker(page, standardInput);
expect(typeof result).toBe('string');
let output = JSON.parse(result);
expect(output).toHaveProperty('contracts');
expect(output.contracts['fixtures/token.sol']).toHaveProperty('MyToken');
expect(output.contracts['fixtures/token.sol'].MyToken).toHaveProperty('abi');
expect(output.contracts['fixtures/token.sol'].MyToken).toHaveProperty('evm');
expect(output.contracts['fixtures/token.sol'].MyToken.evm).toHaveProperty('bytecode');
});
test('should throw an error for invalid Solidity code in browser', async ({ page }) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
const standardInput = loadFixture('invalid_contract_content.json')
const result = await runWorker(page, standardInput);
expect(typeof result).toBe('string');
let output = JSON.parse(result);
expect(output).toHaveProperty('errors');
expect(Array.isArray(output.errors)).toBeTruthy(); // Check if it's an array
expect(output.errors.length).toBeGreaterThan(0);
expect(output.errors[0]).toHaveProperty('type');
expect(output.errors[0].type).toContain('ParserError');
});
test('should return not found error for missing imports in browser', async ({page}) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
const standardInput = loadFixture('missing_import.json')
const result = await runWorker(page, standardInput);
expect(typeof result).toBe('string');
let output = JSON.parse(result);
expect(output).toHaveProperty('errors');
expect(Array.isArray(output.errors)).toBeTruthy(); // Check if it's an array
expect(output.errors.length).toBeGreaterThan(0);
expect(output.errors[0]).toHaveProperty('message');
expect(output.errors[0].message).toContain('Source "nonexistent/console.sol" not found');
});
+22
View File
@@ -0,0 +1,22 @@
{
"language": "Solidity",
"sources": {
"fixtures/storage.sol": {
"content": "// SPDX-License-Identifier: UNLICENSED\npragma solidity ^0.8.0;\nimport \"nonexistent/console.sol\";\ncontract MyContract { function greet() public pure returns (string memory) { return \"Hello\" // Missing semicolon }}"
}
},
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi"
]
}
}
}
}
+22
View File
@@ -0,0 +1,22 @@
{
"language": "Solidity",
"sources": {
"fixtures/storage.sol": {
"content": "// SPDX-License-Identifier: UNLICENSED\npragma solidity ^0.8.0;\nimport \"nonexistent/console.sol\";\ncontract MyContract { function f() public { } }"
}
},
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi"
]
}
}
}
}
+22
View File
@@ -0,0 +1,22 @@
{
"language": "Solidity",
"sources": {
"fixtures/storage.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity >=0.8.2 <0.9.0;\ncontract Storage {\n uint256 number;\n function store(uint256 num) public { number = num; }\n function retrieve() public view returns (uint256){ return number; }\n}"
}
},
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi"
]
}
}
}
}
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -8,8 +8,8 @@
"fetch:soljson": "wget https://binaries.soliditylang.org/wasm/soljson-v0.8.28+commit.7893614a.js -O ./examples/web/soljson.js",
"example:web": "npm run fetch:soljson && http-server ./examples/web/",
"example:node": "node ./examples/node/run_revive.js",
"test:node": "mocha --timeout 20000 ./tests",
"test:bun": "bun test --timeout 20000 node.test",
"test:node": "mocha --timeout 60000 ./tests",
"test:bun": "bun test --timeout 60000 node.test",
"test:all": "npm run test:node && npm run test:bun"
},
"devDependencies": {
+1 -1
View File
@@ -23,7 +23,7 @@ module.exports = defineConfig({
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
timeout: 120000,
/* Configure projects for major browsers */
projects: [
{
+38 -70
View File
@@ -1,68 +1,52 @@
import { expect } from 'chai';
import { compile } from '../examples/node/revive.js';
import { fileURLToPath } from 'url';
import path from 'path';
import fs from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const validCompilerInput = {
language: 'Solidity',
sources: {
'MyContract.sol': {
content: `
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract MyContract {
function greet() public pure returns (string memory) {
return "Hello";
}
}
`,
},
},
settings: {
optimizer: {
enabled: true,
runs: 200,
},
outputSelection: {
'*': {
'*': ['abi', 'evm.bytecode'],
},
},
},
};
function loadFixture(fixture) {
const fixturePath = path.resolve(__dirname, `../fixtures/${fixture}`);
return JSON.parse(fs.readFileSync(fixturePath, 'utf-8'));
}
describe('Compile Function Tests', function () {
it('should successfully compile valid Solidity code', async function () {
const result = await compile(validCompilerInput);
const standardInput = loadFixture('storage.json')
// Ensure result contains compiled contract
const result = await compile(standardInput);
expect(result).to.be.a('string');
const output = JSON.parse(result);
expect(output).to.have.property('contracts');
expect(output.contracts['MyContract.sol']).to.have.property('MyContract');
expect(output.contracts['MyContract.sol'].MyContract).to.have.property('abi');
expect(output.contracts['MyContract.sol'].MyContract).to.have.property('evm');
expect(output.contracts['MyContract.sol'].MyContract.evm).to.have.property('bytecode');
expect(output.contracts['fixtures/storage.sol']).to.have.property('Storage');
expect(output.contracts['fixtures/storage.sol'].Storage).to.have.property('abi');
expect(output.contracts['fixtures/storage.sol'].Storage).to.have.property('evm');
expect(output.contracts['fixtures/storage.sol'].Storage.evm).to.have.property('bytecode');
});
it('should throw an error for invalid Solidity code', async function () {
const invalidCompilerInput = {
...validCompilerInput,
sources: {
'MyContract.sol': {
content: `
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "nonexistent/console.sol";
contract MyContract {
function greet() public pure returns (string memory) {
return "Hello" // Missing semicolon
}
}
`,
},
},
};
if (typeof globalThis.Bun == 'undefined') {
// Running this test with Bun on a Linux host causes:
// RuntimeError: Out of bounds memory access (evaluating 'getWasmTableEntry(index)(a1, a2, a3, a4, a5)')
// Once this issue is resolved, the test will be re-enabled.
it('should successfully compile large Solidity code', async function () {
const standardInput = loadFixture('token.json')
const result = await compile(invalidCompilerInput);
const result = await compile(standardInput);
expect(result).to.be.a('string');
const output = JSON.parse(result);
expect(output).to.have.property('contracts');
expect(output.contracts['fixtures/token.sol']).to.have.property('MyToken');
expect(output.contracts['fixtures/token.sol'].MyToken).to.have.property('abi');
expect(output.contracts['fixtures/token.sol'].MyToken).to.have.property('evm');
expect(output.contracts['fixtures/token.sol'].MyToken.evm).to.have.property('bytecode');
});
}
it('should throw an error for invalid Solidity code', async function () {
const standardInput = loadFixture('invalid_contract_content.json')
const result = await compile(standardInput);
expect(result).to.be.a('string');
const output = JSON.parse(result);
expect(output).to.have.property('errors');
@@ -73,25 +57,9 @@ describe('Compile Function Tests', function () {
});
it('should return not found error for missing imports', async function () {
const compilerInputWithImport = {
...validCompilerInput,
sources: {
'MyContract.sol': {
content: `
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "nonexistent/console.sol";
contract MyContract {
function greet() public pure returns (string memory) {
return "Hello";
}
}
`,
},
},
};
const standardInput = loadFixture('missing_import.json')
let result = await compile(compilerInputWithImport);
const result = await compile(standardInput);
const output = JSON.parse(result);
expect(output).to.have.property('errors');
expect(output.errors).to.be.an('array');