Skip to content

Commit

Permalink
fix: spotify test
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed Sep 26, 2024
1 parent 856ad18 commit db655bb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 74 deletions.
47 changes: 47 additions & 0 deletions circuits/test/http/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,51 @@ describe("HTTP :: Codegen :: Response", async () => {
circuitInput.value1 = toByte("/aip");
await circuit.expectFail(circuitInput);
});
});

describe("spotify_top_artists_http", async () => {
let http_circuit: WitnessTester<["data", "version", "status", "message", "header1", "value1"], ["body"]>;

it("POST response body", async () => {
let httpLockfile = "spotify.lock"
let httpInputFile = "spotify_top_artists_response.http";
let httpCircuitName = "spotify_top_artists";

await executeCodegen(`${httpCircuitName}_test`, httpInputFile, `${httpLockfile}.json`);

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

const http = readHTTPInputFile(`${httpInputFile}`);
const inputHttp = http.input;

const headers = getHeaders(lockData);

const params = [inputHttp.length, http.bodyBytes.length, lockData.version.length, lockData.status.length, lockData.message.length];
headers.forEach(header => {
params.push(header[0].length);
params.push(header[1].length);
});

http_circuit = await circomkit.WitnessTester(`Extract`, {
file: `main/http_${httpCircuitName}_test`,
template: "LockHTTPResponse",
params: params,
});
console.log("#constraints:", await http_circuit.getConstraintCount());

// match circuit output to original JSON value
const circuitInput: any = {
data: inputHttp,
version: toByte(lockData.version),
status: toByte(lockData.status),
message: toByte(lockData.message),
};

headers.forEach((header, index) => {
circuitInput[`header${index + 1}`] = toByte(header[0]);
circuitInput[`value${index + 1}`] = toByte(header[1]);
});

await http_circuit.expectPass(circuitInput, { body: http.bodyBytes });
});
});
32 changes: 32 additions & 0 deletions circuits/test/json/extractor/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,36 @@ describe("ExtractValueArrayObject", () => {

await circuit.expectPass({ data: input, key1: keyUnicode[0], key3: keyUnicode[2] }, { value: num });
});
});

describe("spotify_top_artists_json", async () => {
let json_circuit: WitnessTester<["data", "key1", "key2", "key4", "key5"], ["value"]>;

it("response matcher", async () => {
let jsonFilename = "spotify";

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

let index_0 = 0;

let [inputJson, key, output] = readJSONInputFile(
`${jsonFilename}.json`,
[
"data",
"items",
index_0,
"profile",
"name"
]
);

json_circuit = await circomkit.WitnessTester(`Extract`, {
file: `main/json_${jsonFilename}_test`,
template: "ExtractStringValue",
params: [inputJson.length, 5, 4, 0, 5, 1, index_0, 2, 7, 3, 4, 4, 12],
});
console.log("#constraints:", await json_circuit.getConstraintCount());

await json_circuit.expectPass({ data: inputJson, key1: key[0], key2: key[1], key4: key[3], key5: key[4] }, { value: output });
});
});
74 changes: 0 additions & 74 deletions circuits/test/spotify_top_artists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,80 +32,6 @@ async function extendedLockfileCodegen(circuitName: string, inputFileName: strin
})
}

// describe("spotify top artists separate", async () => {
// let http_circuit: WitnessTester<["data", "version", "status", "message", "header1", "value1"], ["body"]>;
// let json_circuit: WitnessTester<["data", "key1", "key2", "key4", "key5"], ["value"]>;

// it("POST response body extraction", async () => {
// let httpLockfile = "spotify.lock"
// let httpInputFile = "spotify_top_artists_response.http";
// let httpCircuitName = "spotify_top_artists";

// await httpLockfileCodegen(httpCircuitName, httpInputFile, `${httpLockfile}.json`);

// let jsonFilename = "spotify";

// await jsonLockfileCodegen(`${jsonFilename}_test`, `${jsonFilename}.json`, `${jsonFilename}.json`);

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

// const http = readHTTPInputFile(`${httpInputFile}`);
// const inputHttp = http.input;

// const headers = getHttpHeaders(lockData);

// const params = [inputHttp.length, http.bodyBytes.length, lockData.version.length, lockData.status.length, lockData.message.length];
// headers.forEach(header => {
// params.push(header[0].length);
// params.push(header[1].length);
// });

// http_circuit = await circomkit.WitnessTester(`Extract`, {
// file: `main/http_${httpCircuitName}`,
// template: "LockHTTPResponse",
// params: params,
// });
// console.log("#constraints:", await http_circuit.getConstraintCount());

// // match circuit output to original JSON value
// const circuitInput: any = {
// data: inputHttp,
// version: toByte(lockData.version),
// status: toByte(lockData.status),
// message: toByte(lockData.message),
// };

// headers.forEach((header, index) => {
// circuitInput[`header${index + 1}`] = toByte(header[0]);
// circuitInput[`value${index + 1}`] = toByte(header[1]);
// });

// await http_circuit.expectPass(circuitInput, { body: http.bodyBytes });

// let index_0 = 0;

// let [inputJson, key, output] = readJSONInputFile(
// `${jsonFilename}.json`,
// [
// "data",
// "items",
// index_0,
// "profile",
// "name"
// ]
// );

// json_circuit = await circomkit.WitnessTester(`Extract`, {
// file: `main/json_${jsonFilename}_test`,
// template: "ExtractStringValue",
// params: [inputJson.length, 5, 4, 0, 5, 1, index_0, 2, 7, 3, 4, 4, 12],
// });
// console.log("#constraints:", await json_circuit.getConstraintCount());

// await json_circuit.expectPass({ data: inputJson, key1: key[0], key2: key[1], key4: key[3], key5: key[4] }, { value: output });
// });
// });

interface JsonLockfile {
keys: any[],
valueType: string,
Expand Down

0 comments on commit db655bb

Please sign in to comment.