Skip to content

Commit

Permalink
test: add e2e test for script with deps
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Oct 23, 2023
1 parent f0e431d commit 68a256e
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 60 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/test-noir_wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ jobs:
name: nargo
path: ./nargo

- name: Compile test program with Nargo CLI
working-directory: ./compiler/wasm/noir-script
- name: Compile fixtures with Nargo CLI
working-directory: ./compiler/wasm/fixtures
run: |
nargo_binary=${{ github.workspace }}/nargo/nargo
chmod +x $nargo_binary
$nargo_binary compile
for dir in $(ls -d */); do
pushd $dir/noir-script
$nargo_binary compile
popd
done
- name: Install Yarn dependencies
uses: ./.github/actions/setup
Expand Down
8 changes: 8 additions & 0 deletions compiler/wasm/fixtures/deps/lib-a/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name="lib_a"
type="lib"
authors = [""]
compiler_version = "0.1"

[dependencies]
lib_b = { path = "../lib-b" }
7 changes: 7 additions & 0 deletions compiler/wasm/fixtures/deps/lib-a/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

use dep::lib_b::assert_non_zero;

pub fn divide(a: u64, b: u64) -> u64 {
assert_non_zero(b);
a / b
}
7 changes: 7 additions & 0 deletions compiler/wasm/fixtures/deps/lib-b/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name="lib_b"
type="lib"
authors = [""]
compiler_version = "0.1"

[dependencies]
4 changes: 4 additions & 0 deletions compiler/wasm/fixtures/deps/lib-b/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

pub fn assert_non_zero(x: u64) {
assert(x != 0);
}
8 changes: 8 additions & 0 deletions compiler/wasm/fixtures/deps/noir-script/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name="noir_wasm_testing"
type="bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
lib_a = { path="../lib-a" }
4 changes: 4 additions & 0 deletions compiler/wasm/fixtures/deps/noir-script/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use dep::lib_a::divide;
fn main(x : u64, y : pub u64) {
divide(x, y);
}
File renamed without changes.
File renamed without changes.
110 changes: 70 additions & 40 deletions compiler/wasm/test/browser/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,98 @@
import { expect } from '@esm-bundle/chai';
import initNoirWasm, { compile } from '@noir-lang/noir_wasm';
import { initializeResolver } from '@noir-lang/source-resolver';
import { nargoArtifactPath, noirSourcePath } from '../shared';
import {
depsScriptExpectedArtifact,
depsScriptSourcePath,
libASourcePath,
libBSourcePath,
simpleScriptExpectedArtifact,
simpleScriptSourcePath,
} from '../shared';

beforeEach(async () => {
await initNoirWasm();
});

async function getFileContent(path: string): Promise<string> {
const mainnrSourceURL = new URL(path, import.meta.url);
const response = await fetch(mainnrSourceURL);
const url = new URL(path, import.meta.url);
const response = await fetch(url);
return await response.text();
}

async function getSource(): Promise<string> {
return getFileContent(noirSourcePath);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function getPrecompiledSource(): Promise<any> {
const compiledData = await getFileContent(nargoArtifactPath);
async function getPrecompiledSource(path: string): Promise<any> {
const compiledData = await getFileContent(path);
return JSON.parse(compiledData);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function compileNoirSource(noir_source: string): Promise<any> {
console.log('Compiling Noir source...');
describe('noir wasm', () => {
describe('can compile script without dependencies', () => {
beforeEach(async () => {
const source = await getFileContent(simpleScriptSourcePath);
initializeResolver((id: string) => {
console.log(`Resolving source ${id}`);

initializeResolver((id: string) => {
console.log(`Resolving source ${id}`);
if (typeof source === 'undefined') {
throw Error(`Could not resolve source for '${id}'`);
} else if (id !== '/main.nr') {
throw Error(`Unexpected id: '${id}'`);
} else {
return source;
}
});
});

const source = noir_source;
it('matching nargos compilation', async () => {
const wasmCircuit = await compile('/main.nr');
const cliCircuit = await getPrecompiledSource(simpleScriptExpectedArtifact);

if (typeof source === 'undefined') {
throw Error(`Could not resolve source for '${id}'`);
} else if (id !== '/main.nr') {
throw Error(`Unexpected id: '${id}'`);
} else {
return source;
}
// We don't expect the hashes to match due to how `noir_wasm` handles dependencies
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode);
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi);
expect(wasmCircuit.backend).to.eq(cliCircuit.backend);
}).timeout(20e3); // 20 seconds
});

try {
const compiled_noir = compile('main.nr');
describe('can compile script with dependencies', () => {
beforeEach(async () => {
const [scriptSource, libASource, libBSource] = await Promise.all([
getFileContent(depsScriptSourcePath),
getFileContent(libASourcePath),
getFileContent(libBSourcePath),
]);

console.log('Noir source compilation done.');
initializeResolver((file: string) => {
switch (file) {
case '/script/main.nr':
return scriptSource;

return compiled_noir;
} catch (e) {
console.log('Error while compiling:', e);
}
}
case '/lib_a/lib.nr':
return libASource;

case '/lib_b/lib.nr':
return libBSource;

describe('noir wasm compilation', () => {
it('matches nargos compilation', async () => {
const source = await getSource();
default:
return '';
}
});
});

const wasmCircuit = await compileNoirSource(source);
it('matching nargos compilation', async () => {
const wasmCircuit = await compile('/script/main.nr', false, {
root_dependencies: ['lib_a'],
library_dependencies: {
lib_a: ['lib_b'],
},
});

const cliCircuit = await getPrecompiledSource();
const cliCircuit = await getPrecompiledSource(depsScriptExpectedArtifact);

// We don't expect the hashes to match due to how `noir_wasm` handles dependencies
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode);
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi);
expect(wasmCircuit.backend).to.eq(cliCircuit.backend);
}).timeout(20e3); // 20 seconds
// We don't expect the hashes to match due to how `noir_wasm` handles dependencies
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode);
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi);
expect(wasmCircuit.backend).to.eq(cliCircuit.backend);
}).timeout(20e3); // 20 seconds
});
});
76 changes: 61 additions & 15 deletions compiler/wasm/test/node/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,72 @@
import { expect } from 'chai';
import { nargoArtifactPath, noirSourcePath } from '../shared';
import {
depsScriptSourcePath,
depsScriptExpectedArtifact,
libASourcePath,
libBSourcePath,
simpleScriptSourcePath,
simpleScriptExpectedArtifact,
} from '../shared';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { join, resolve } from 'node:path';
import { compile } from '@noir-lang/noir_wasm';
import { initializeResolver } from '@noir-lang/source-resolver';

const absoluteNoirSourcePath = join(__dirname, noirSourcePath);
const absoluteNargoArtifactPath = join(__dirname, nargoArtifactPath);
// const absoluteNoirSourcePath = ;
// const absoluteNargoArtifactPath = join(__dirname, nargoArtifactPath);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function getPrecompiledSource(): Promise<any> {
const compiledData = readFileSync(absoluteNargoArtifactPath).toString();
async function getPrecompiledSource(path: string): Promise<any> {
const compiledData = readFileSync(resolve(__dirname, path)).toString();
return JSON.parse(compiledData);
}

describe('noir wasm compilation', () => {
it('matches nargos compilation', async () => {
const wasmCircuit = await compile(absoluteNoirSourcePath);
const cliCircuit = await getPrecompiledSource();

// We don't expect the hashes to match due to how `noir_wasm` handles dependencies
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode);
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi);
expect(wasmCircuit.backend).to.eq(cliCircuit.backend);
}).timeout(10e3);
describe('can compile simple scripts', () => {
it('matching nargos compilation', async () => {
const wasmCircuit = await compile(join(__dirname, simpleScriptSourcePath));
const cliCircuit = await getPrecompiledSource(simpleScriptExpectedArtifact);

// We don't expect the hashes to match due to how `noir_wasm` handles dependencies
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode);
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi);
expect(wasmCircuit.backend).to.eq(cliCircuit.backend);
}).timeout(10e3);
});

describe('can compile scripts with dependencies', () => {
beforeEach(() => {
initializeResolver((file) => {
switch (file) {
case '/script/main.nr':
return readFileSync(join(__dirname, depsScriptSourcePath), 'utf-8');

case '/lib_a/lib.nr':
return readFileSync(join(__dirname, libASourcePath), 'utf-8');

case '/lib_b/lib.nr':
return readFileSync(join(__dirname, libBSourcePath), 'utf-8');

default:
return '';
}
});
});

it('matching nargos compilation', async () => {
const wasmCircuit = await compile('/script/main.nr', false, {
root_dependencies: ['lib_a'],
library_dependencies: {
lib_a: ['lib_b'],
},
});

const cliCircuit = await getPrecompiledSource(depsScriptExpectedArtifact);

// We don't expect the hashes to match due to how `noir_wasm` handles dependencies
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode);
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi);
expect(wasmCircuit.backend).to.eq(cliCircuit.backend);
}).timeout(10e3);
});
});
10 changes: 8 additions & 2 deletions compiler/wasm/test/shared.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export const noirSourcePath = '../../noir-script/src/main.nr';
export const nargoArtifactPath = '../../noir-script/target/noir_wasm_testing.json';
export const simpleScriptSourcePath = '../../fixtures/simple/noir-script/src/main.nr';
export const simpleScriptExpectedArtifact = '../../fixtures/simple/noir-script/target/noir_wasm_testing.json';

export const depsScriptSourcePath = '../../fixtures/deps/noir-script/src/main.nr';
export const depsScriptExpectedArtifact = '../../fixtures/deps/noir-script/target/noir_wasm_testing.json';

export const libASourcePath = '../../fixtures/deps/lib-a/src/lib.nr';
export const libBSourcePath = '../../fixtures/deps/lib-b/src/lib.nr';

0 comments on commit 68a256e

Please sign in to comment.