Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed Sep 12, 2024
1 parent cf0581f commit dc9bbc8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
15 changes: 8 additions & 7 deletions circuits/test/http/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ interface Response {
}


function executeCodegen(inputFilename: string, outputFilename: string) {
function executeCodegen(circuitName: string, inputFileName: string, lockfileName: string, outputFilename: string) {
return new Promise((resolve, reject) => {
const inputPath = join(__dirname, "..", "..", "..", "examples", "http", "lockfile", inputFilename);
const inputFilePath = join(__dirname, "..", "..", "..", "examples", "http", inputFileName);
const lockfilePath = join(__dirname, "..", "..", "..", "examples", "http", "lockfile", lockfileName);

const codegen = spawn("cargo", ["run", "http", "--lockfile", inputPath, "--output-filename", outputFilename]);
const codegen = spawn("cargo", ["run", "http", "--circuit-name", circuitName, "--input-file", inputFilePath, "--lockfile", lockfilePath, "--output-filename", outputFilename]);

codegen.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
Expand Down Expand Up @@ -73,7 +74,7 @@ describe("HTTP :: Codegen :: Request", async () => {
let inputfile = "get_request.http";

// generate extractor circuit using codegen
await executeCodegen(`${lockfile}.json`, lockfile);
await executeCodegen("get_request_test", inputfile, `${lockfile}.json`, lockfile);

const lockData = readLockFile<Request>(`${lockfile}.json`);
console.log("lockData: ", JSON.stringify(lockData));
Expand Down Expand Up @@ -115,7 +116,7 @@ describe("HTTP :: Codegen :: Request", async () => {
let inputfile = "get_request.http";

// generate extractor circuit using codegen
await executeCodegen(`${lockfile}.json`, lockfile);
await executeCodegen("get_request_test", inputfile, `${lockfile}.json`, lockfile);

const lockData = readLockFile<Request>(`${lockfile}.json`);

Expand Down Expand Up @@ -161,7 +162,7 @@ describe("HTTP :: Codegen :: Response", async () => {
let inputfile = "get_response.http";

// generate extractor circuit using codegen
await executeCodegen(`${lockfile}.json`, lockfile);
await executeCodegen("get_response_test", inputfile, `${lockfile}.json`, lockfile);

const lockData = readLockFile<Response>(`${lockfile}.json`);
console.log("lockData: ", JSON.stringify(lockData));
Expand Down Expand Up @@ -207,7 +208,7 @@ describe("HTTP :: Codegen :: Response", async () => {
let inputfile = "get_response.http";

// generate extractor circuit using codegen
await executeCodegen(`${lockfile}.json`, lockfile);
await executeCodegen("get_response_test", inputfile, `${lockfile}.json`, lockfile);

const lockData = readLockFile<Response>(`${lockfile}.json`);

Expand Down
30 changes: 17 additions & 13 deletions circuits/test/json/extractor/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { join } from "path";
import { spawn } from "child_process";


function executeCodegen(inputFilename: string, outputFilename: string) {
function executeCodegen(circuitName: string, inputFileName: string, lockfileName: string, outputFilename: string) {
return new Promise((resolve, reject) => {
const inputPath = join(__dirname, "..", "..", "..", "..", "examples", "json", "lockfile", inputFilename);
const inputFilePath = join(__dirname, "..", "..", "..", "..", "examples", "json", "test", inputFileName);
const lockfilePath = join(__dirname, "..", "..", "..", "..", "examples", "json", "lockfile", lockfileName);

const codegen = spawn("cargo", ["run", "json", "--template", inputPath, "--output-filename", outputFilename]);
const codegen = spawn("cargo", ["run", "json", "--circuit-name", circuitName, "--input-file", inputFilePath, "--lockfile", lockfilePath, "--output-filename", outputFilename]);

codegen.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
Expand All @@ -34,7 +35,7 @@ describe("ExtractValue", async () => {
let filename = "value_string";

// generate extractor circuit using codegen
await executeCodegen(`${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);

// read JSON input file into bytes
let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["k"]);
Expand All @@ -56,7 +57,7 @@ describe("ExtractValue", async () => {

it("two_keys: {\"key1\": \"abc\", \"key2\": \"def\" }", async () => {
let filename = "two_keys"
await executeCodegen(`${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["key2"]);

circuit = await circomkit.WitnessTester(`Extract`, {
Expand All @@ -71,7 +72,7 @@ describe("ExtractValue", async () => {

it("value_number: {\"k\": 69 }", async () => {
let filename = "value_number";
await executeCodegen(`${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["k"]);

circuit = await circomkit.WitnessTester(`Extract`, {
Expand All @@ -88,10 +89,11 @@ describe("ExtractValue", async () => {

it("value_array_string: { \"k\" : [ 420 , 69 , 4200 , 600 ], \"b\": [ \"ab\" , \"ba\", \"ccc\", \"d\" ] }", async () => {
let filename = "value_array_string";
await executeCodegen(`${filename}.json`, filename);
let inputFileName = "value_array.json";
await executeCodegen(`${filename}_test`, `${inputFileName}`, `${filename}.json`, filename);

for (let i = 0; i < 4; i++) {
let [input, keyUnicode, output] = readJSONInputFile("value_array.json", ["b", i]);
let [input, keyUnicode, output] = readJSONInputFile(`${inputFileName}`, ["b", i]);

circuit = await circomkit.WitnessTester(`Extract`, {
file: `main/${filename}`,
Expand All @@ -106,10 +108,12 @@ describe("ExtractValue", async () => {

it("value_array_number: { \"k\" : [ 420 , 69 , 4200 , 600 ], \"b\": [ \"ab\" , \"ba\", \"ccc\", \"d\" ] }", async () => {
let filename = "value_array_number";
await executeCodegen(`${filename}.json`, filename);
let inputFileName = "value_array.json";

await executeCodegen(`${filename}_test`, `${inputFileName}`, `${filename}.json`, filename);

for (let i = 0; i < 4; i++) {
let [input, keyUnicode, output] = readJSONInputFile("value_array.json", ["k", i]);
let [input, keyUnicode, output] = readJSONInputFile(`${inputFileName}`, ["k", i]);

circuit = await circomkit.WitnessTester(`Extract`, {
file: `main/${filename}`,
Expand All @@ -125,7 +129,7 @@ describe("ExtractValue", async () => {

it("value_array_nested: { \"a\": [[1,0],[0,1,3]] }", async () => {
let filename = "value_array_nested";
await executeCodegen(`${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
let index_0 = 1;
let index_1 = 0;
let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["a", index_0, index_1]);
Expand All @@ -150,7 +154,7 @@ describe("ExtractValueMultiDepth", () => {
it("value_object: { \"a\": { \"d\" : \"e\", \"e\": \"c\" }, \"e\": { \"f\": \"a\", \"e\": \"2\" } }", async () => {
let filename = "value_object";

await executeCodegen(`${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);

let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["e", "e"]);

Expand All @@ -176,7 +180,7 @@ describe("ExtractValueArrayObject", () => {
it("value_array_object: {\"a\":[{\"b\":[1,4]},{\"c\":\"b\"}]}", async () => {
let filename = "value_array_object";

await executeCodegen(`${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);

let index_0 = 0;
let index_1 = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ pub fn write_circuit_config(
}

std::fs::write(
circomkit_circuits_config,
circomkit_circuits_config.clone(),
serde_json::to_string_pretty(&circomkit_circuits)?,
)?;

println!("circomkit circuits config updated for {}", name);
println!("config updated: {}", circomkit_circuits_config.display());

Ok(())
}
1 change: 0 additions & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ fn build_circuit_config(
params.push(response.status.len());
params.push(response.message.len());
for (key, value) in response.headers.iter() {
println!("{}, {}", key, value);
params.push(key.len());
params.push(value.len());
}
Expand Down
4 changes: 2 additions & 2 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,9 @@ fn build_circuit_config(
let mut curr_stack_height = 1;
let mut inside_string: bool = false;

for (i, char) in input[1..].iter().enumerate() {
for (i, char) in input.iter().skip(1).enumerate() {
match char {
b'"' if input[i - 1] != b'\\' => inside_string = !inside_string,
b'"' if input[i] != b'\\' => inside_string = !inside_string,
b'{' | b'[' if !inside_string => {
curr_stack_height += 1;
max_stack_height = max_by(max_stack_height, curr_stack_height, |x, y| x.cmp(y));
Expand Down

0 comments on commit dc9bbc8

Please sign in to comment.