aboutsummaryrefslogtreecommitdiff
path: root/src/types-test.ts
blob: f8f25bc00dd212674427dd6e0597f67384a5b3b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {test, TestLib} from "testlib/talertest";
import {Amounts} from "./types";
import * as types from "./types";

let amt = (value: number, fraction: number, currency: string): types.AmountJson => ({value, fraction, currency});

test("amount addition (simple)", (t: TestLib) => {
  let a1 = amt(1,0,"EUR");
  let a2 = amt(1,0,"EUR");
  let a3 = amt(2,0,"EUR");
  t.assert(0 == types.Amounts.cmp(Amounts.add(a1, a2).amount, a3));
  t.pass();
});

test("amount addition (saturation)", (t: TestLib) => {
  let a1 = amt(1,0,"EUR");
  let res = Amounts.add(Amounts.getMaxAmount("EUR"), a1);
  t.assert(res.saturated);
  t.pass();
});

test("amount subtraction (simple)", (t: TestLib) => {
  let a1 = amt(2,5,"EUR");
  let a2 = amt(1,0,"EUR");
  let a3 = amt(1,5,"EUR");
  t.assert(0 == types.Amounts.cmp(Amounts.sub(a1, a2).amount, a3));
  t.pass();
});

test("amount subtraction (saturation)", (t: TestLib) => {
  let a1 = amt(0,0,"EUR");
  let a2 = amt(1,0,"EUR");
  let res = Amounts.sub(a1, a2);
  t.assert(res.saturated);
  res = Amounts.sub(a1, a1);
  t.assert(!res.saturated);
  t.pass();
});


test("contract validation", (t: TestLib) => {
  let c = {
    H_wire: "123",
    summary: "hello",
    amount: amt(1,2,"EUR"),
    auditors: [],
    pay_deadline: "Date(12346)",
    max_fee: amt(1,2,"EUR"),
    merchant_pub: "12345",
    exchanges: [{master_pub: "foo", url: "foo"}],
    products: [],
    refund_deadline: "Date(12345)",
    timestamp: "Date(12345)",
    transaction_id: 1234,
    fulfillment_url: "foo",
    repurchase_correlation_id: "blabla",
  };

  types.Contract.checked(c);

  let c1 = JSON.parse(JSON.stringify(c));
  c1.exchanges = []

  try {
    types.Contract.checked(c1);
  } catch (e) {
    t.pass();
    return;
  }

  t.fail();

});