-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquote_test.go
More file actions
109 lines (99 loc) · 3.55 KB
/
quote_test.go
File metadata and controls
109 lines (99 loc) · 3.55 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tap
import (
"encoding/json"
"errors"
"testing"
)
func TestNewQuoteMessage(t *testing.T) {
body := &QuoteBody{
FromAsset: "eip155:1/slip44:60",
ToAsset: "USD",
FromAmount: "1.0",
ToAmount: "3000.00",
Provider: &Party{ID: "did:web:provider.example"},
Agents: []Agent{{ID: "did:web:provider.example"}},
ExpiresAt: "2025-03-22T15:00:00Z",
}
msg, err := NewQuoteMessage("did:web:provider.example", []string{"did:web:requester.example"}, "exchange-thread-1", body)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if msg.Type != TypeQuote {
t.Errorf("Type: got %q", msg.Type)
}
if msg.Thid != "exchange-thread-1" {
t.Errorf("Thid: got %q", msg.Thid)
}
}
func TestNewQuoteMessage_MissingFields(t *testing.T) {
tests := []struct {
name string
body *QuoteBody
}{
{"missing fromAsset", &QuoteBody{ToAsset: "USD", FromAmount: "1", ToAmount: "3000", Provider: &Party{ID: "did:eg:p"}, Agents: []Agent{{ID: "a"}}, ExpiresAt: "2025-01-01T00:00:00Z"}},
{"missing toAsset", &QuoteBody{FromAsset: "ETH", FromAmount: "1", ToAmount: "3000", Provider: &Party{ID: "did:eg:p"}, Agents: []Agent{{ID: "a"}}, ExpiresAt: "2025-01-01T00:00:00Z"}},
{"missing fromAmount", &QuoteBody{FromAsset: "ETH", ToAsset: "USD", ToAmount: "3000", Provider: &Party{ID: "did:eg:p"}, Agents: []Agent{{ID: "a"}}, ExpiresAt: "2025-01-01T00:00:00Z"}},
{"missing toAmount", &QuoteBody{FromAsset: "ETH", ToAsset: "USD", FromAmount: "1", Provider: &Party{ID: "did:eg:p"}, Agents: []Agent{{ID: "a"}}, ExpiresAt: "2025-01-01T00:00:00Z"}},
{"missing provider", &QuoteBody{FromAsset: "ETH", ToAsset: "USD", FromAmount: "1", ToAmount: "3000", Agents: []Agent{{ID: "a"}}, ExpiresAt: "2025-01-01T00:00:00Z"}},
{"missing agents", &QuoteBody{FromAsset: "ETH", ToAsset: "USD", FromAmount: "1", ToAmount: "3000", Provider: &Party{ID: "did:eg:p"}, ExpiresAt: "2025-01-01T00:00:00Z"}},
{"missing expiresAt", &QuoteBody{FromAsset: "ETH", ToAsset: "USD", FromAmount: "1", ToAmount: "3000", Provider: &Party{ID: "did:eg:p"}, Agents: []Agent{{ID: "a"}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewQuoteMessage("from", nil, "thid", tt.body)
if !errors.Is(err, ErrInvalidBody) {
t.Errorf("expected ErrInvalidBody, got %v", err)
}
})
}
}
func TestQuoteBody_JSONRoundTrip(t *testing.T) {
body := QuoteBody{
Context: TAPContext,
Type: TypeQuote,
FromAsset: "eip155:1/slip44:60",
ToAsset: "USD",
FromAmount: "1.0",
ToAmount: "3000.00",
Provider: &Party{ID: "did:web:provider.example"},
Agents: []Agent{{ID: "did:web:provider.example"}},
ExpiresAt: "2025-03-22T15:00:00Z",
}
data, err := json.Marshal(body)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var got QuoteBody
if err := json.Unmarshal(data, &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got.FromAsset != body.FromAsset || got.ToAmount != body.ToAmount {
t.Errorf("mismatch: %+v", got)
}
}
func TestQuoteBody_ParseBody(t *testing.T) {
body := &QuoteBody{
FromAsset: "ETH",
ToAsset: "USD",
FromAmount: "1",
ToAmount: "3000",
Provider: &Party{ID: "did:web:provider"},
Agents: []Agent{{ID: "did:web:provider"}},
ExpiresAt: "2025-01-01T00:00:00Z",
}
msg, err := NewQuoteMessage("from", nil, "thid", body)
if err != nil {
t.Fatalf("create: %v", err)
}
parsed, err := ParseBody(msg)
if err != nil {
t.Fatalf("parse: %v", err)
}
qb, ok := parsed.(*QuoteBody)
if !ok {
t.Fatalf("expected *QuoteBody, got %T", parsed)
}
if qb.FromAsset != "ETH" {
t.Errorf("FromAsset: got %q", qb.FromAsset)
}
}