Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Add Missing Test Vectors #128

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class TbdexTestVectorsProtocolTest {
)
}

@Test
fun parse_orderinstructions() {
testVector(
"parse-orderinstructions.json",
OrderInstructions.Companion::fromJsonString,
{ it.toJsonString() },
{ it.verify() }
)
}

@Test
fun parse_rfq() {
testVector("parse-rfq.json", { input ->
Expand Down Expand Up @@ -70,16 +80,15 @@ class TbdexTestVectorsProtocolTest {
)
}

// TODO: uncomment when quote test vector is updated
// @Test
// fun parse_quote() {
// testVector(
// "parse-quote.json",
// Quote.Companion::fromJsonString,
// { it.toJsonString() },
// { it.verify() }
// )
// }
@Test
fun parse_quote() {
testVector(
"parse-quote.json",
Quote.Companion::fromJsonString,
{ it.toJsonString() },
{ it.verify() }
)
}

@Test
fun parse_close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ object TestVectors {
"parse-cancel.json",
"parse-offering.json",
"parse-order.json",
"parse-orderinstructions.json",
"parse-orderstatus.json",
"parse-quote.json",
"parse-rfq.json",
Expand Down
54 changes: 42 additions & 12 deletions bound/typescript/tests/test-vectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BalanceVector from "../../../tbdex/hosted/test-vectors/protocol/vectors/p
import RfqVector from "../../../tbdex/hosted/test-vectors/protocol/vectors/parse-rfq.json" assert { type: "json" };
import RfqOmitPrivateDataVector from "../../../tbdex/hosted/test-vectors/protocol/vectors/parse-rfq-omit-private-data.json" assert { type: "json" };
import QuoteVector from "../../../tbdex/hosted/test-vectors/protocol/vectors/parse-quote.json" assert { type: "json" };
import OrderInstructionsVector from "../../../tbdex/hosted/test-vectors/protocol/vectors/parse-orderinstructions.json" assert { type: "json" };
import OrderVector from "../../../tbdex/hosted/test-vectors/protocol/vectors/parse-order.json" assert { type: "json" };
import CancelVector from "../../../tbdex/hosted/test-vectors/protocol/vectors/parse-cancel.json" assert { type: "json" };
import OrderStatusVector from "../../../tbdex/hosted/test-vectors/protocol/vectors/parse-orderstatus.json" assert { type: "json" };
Expand All @@ -19,6 +20,7 @@ import { Order } from "../src/messages/order";
import { Cancel } from "../src/messages/cancel";
import { OrderStatus } from "../src/messages/order-status";
import { Close } from "../src/messages/close";
import { OrderInstructions } from "../src/messages/order-instructions";

describe("test vectors", () => {
let bearerDID: BearerDid;
Expand Down Expand Up @@ -145,18 +147,17 @@ describe("test vectors", () => {

describe("quote", () => {
it("should parse", async () => {
// TODO test vector needs updating, needs the `paymentInstruction`'s on the payin & payout removed
// const input = QuoteVector.input;
// const quote = Quote.fromJSONString(input);
// expect(quote.metadata).to.deep.equal(QuoteVector.output.metadata);
// expect(quote.data).to.deep.equal(QuoteVector.output.data);
// expect(quote.signature).to.equal(QuoteVector.output.signature);
//
// const quoteJSONString = quote.toJSONString();
// const quoteJSON = JSON.parse(quoteJSONString);
// expect(quoteJSON).to.deep.equal(QuoteVector.output);
//
// await quote.verify();
const input = QuoteVector.input;
const quote = Quote.fromJSONString(input);
expect(quote.metadata).to.deep.equal(QuoteVector.output.metadata);
expect(quote.data).to.deep.equal(QuoteVector.output.data);
expect(quote.signature).to.equal(QuoteVector.output.signature);

const quoteJSONString = quote.toJSONString();
const quoteJSON = JSON.parse(quoteJSONString);
expect(quoteJSON).to.deep.equal(QuoteVector.output);

quote.verify();
});

it("should create, sign, and verify", async () => {
Expand All @@ -173,6 +174,35 @@ describe("test vectors", () => {
});
});

describe("order instructions", () => {
it("should parse", async () => {
const input = OrderInstructionsVector.input;
const orderInstructions = OrderInstructions.fromJSONString(input);
expect(orderInstructions.metadata).to.deep.equal(OrderInstructionsVector.output.metadata);
expect(orderInstructions.data).to.deep.equal(OrderInstructionsVector.output.data);
expect(orderInstructions.signature).to.equal(OrderInstructionsVector.output.signature);

const orderInstructionsJSONString = orderInstructions.toJSONString();
const orderInstructionsJSON = JSON.parse(orderInstructionsJSONString);
expect(orderInstructionsJSON).to.deep.equal(OrderInstructionsVector.output);

await orderInstructions.verify();
});

it("should create, sign, and verify", async () => {
const orderInstructions = OrderInstructions.create(
OrderInstructionsVector.output.metadata.to,
OrderInstructionsVector.output.metadata.from,
OrderInstructionsVector.output.metadata.exchangeId,
OrderInstructionsVector.output.data,
OrderInstructionsVector.output.metadata.protocol
);

orderInstructions.sign(bearerDID);
await orderInstructions.verify();
});
});

describe("order", () => {
it("should parse", async () => {
const input = OrderVector.input;
Expand Down
23 changes: 23 additions & 0 deletions crates/tbdex/src/messages/order_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,26 @@ pub struct PaymentInstruction {
#[serde(skip_serializing_if = "Option::is_none")]
pub instruction: Option<String>,
}
#[cfg(test)]
mod tbdex_test_vectors_protocol {
use super::*;
use std::fs;

#[derive(Debug, serde::Deserialize)]
pub struct OrderInstructionsTestVector {
pub input: String,
pub output: OrderInstructions,
}

#[test]
fn parse_order_instructions() {
print!("~~RUNNING");
let path = "../../tbdex/hosted/test-vectors/protocol/vectors/parse-orderinstructions.json";
let test_vector_json: String = fs::read_to_string(path).unwrap();

let test_vector: OrderInstructionsTestVector = serde_json::from_str(&test_vector_json).unwrap();
let parsed_order_instructions: OrderInstructions = OrderInstructions::from_json_string(&test_vector.input).unwrap();

assert_eq!(test_vector.output, parsed_order_instructions);
}
}
47 changes: 23 additions & 24 deletions crates/tbdex/src/messages/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,27 +155,26 @@ pub struct QuoteDetails {
pub fee: Option<String>,
}

// TODO: Uncomment when we have parse_quote.json vector updated with no payment instructions
// #[cfg(test)]
// mod tbdex_test_vectors_protocol {
// use super::*;
// use std::fs;
//
// #[derive(Debug, serde::Deserialize)]
// pub struct TestVector {
// pub input: String,
// pub output: Quote,
// }
//
//
// #[test]
// fn parse_quote() {
// let path = "../../tbdex/hosted/test-vectors/protocol/vectors/parse-quote.json";
// let test_vector_json: String = fs::read_to_string(path).unwrap();
//
// let test_vector: TestVector = serde_json::from_str(&test_vector_json).unwrap();
// let parsed_quote: Quote = Quote::from_json_string(&test_vector.input).unwrap();
//
// assert_eq!(test_vector.output, parsed_quote);
// }
// }
#[cfg(test)]
mod tbdex_test_vectors_protocol {
use super::*;
use std::fs;

#[derive(Debug, serde::Deserialize)]
pub struct TestVector {
pub input: String,
pub output: Quote,
}


#[test]
fn parse_quote() {
let path = "../../tbdex/hosted/test-vectors/protocol/vectors/parse-quote.json";
let test_vector_json: String = fs::read_to_string(path).unwrap();

let test_vector: TestVector = serde_json::from_str(&test_vector_json).unwrap();
let parsed_quote: Quote = Quote::from_json_string(&test_vector.input).unwrap();

assert_eq!(test_vector.output, parsed_quote);
}
}
Loading