Skip to content

Commit ffdbddc

Browse files
committed
fix: add tests
1 parent 2a71726 commit ffdbddc

6 files changed

Lines changed: 127 additions & 5 deletions

File tree

server/retrieval/cache_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package retrieval
2+
3+
import (
4+
"testing"
5+
6+
"github.com/storacha/go-ucanto/core/delegation"
7+
"github.com/storacha/go-ucanto/testing/fixtures"
8+
"github.com/storacha/go-ucanto/ucan"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestMemoryDelegationCache(t *testing.T) {
13+
dlg, err := delegation.Delegate(
14+
fixtures.Alice,
15+
fixtures.Alice,
16+
[]ucan.Capability[ucan.NoCaveats]{
17+
ucan.NewCapability(
18+
"test/cache",
19+
fixtures.Alice.DID().String(),
20+
ucan.NoCaveats{},
21+
),
22+
},
23+
)
24+
require.NoError(t, err)
25+
26+
t.Run("put", func(t *testing.T) {
27+
cache, err := NewMemoryDelegationCache(5)
28+
require.NoError(t, err)
29+
err = cache.Put(t.Context(), dlg)
30+
require.NoError(t, err)
31+
})
32+
33+
t.Run("get", func(t *testing.T) {
34+
cache, err := NewMemoryDelegationCache(5)
35+
require.NoError(t, err)
36+
37+
err = cache.Put(t.Context(), dlg)
38+
require.NoError(t, err)
39+
40+
cached, ok, err := cache.Get(t.Context(), dlg.Link())
41+
require.NoError(t, err)
42+
require.True(t, ok)
43+
require.Equal(t, dlg.Link().String(), cached.Link().String())
44+
})
45+
46+
t.Run("miss", func(t *testing.T) {
47+
cache, err := NewMemoryDelegationCache(5)
48+
require.NoError(t, err)
49+
50+
cached, ok, err := cache.Get(t.Context(), dlg.Link())
51+
require.NoError(t, err)
52+
require.False(t, ok)
53+
require.Nil(t, cached)
54+
})
55+
}

server/retrieval/datamodel/errors.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ type MissingProofsModel struct {
3737
Message string
3838
Proofs []ipld.Link
3939
}
40+
41+
func AgentMessageInvocationErrorType() schema.Type {
42+
return errorTypeSystem.TypeByName("AgentMessageInvocationError")
43+
}
44+
45+
type AgentMessageInvocationErrorModel struct {
46+
Name string
47+
Message string
48+
}

server/retrieval/datamodel/errors.ipldsch

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ type MissingProofs struct {
33
message String
44
proofs [Link]
55
}
6+
7+
type AgentMessageInvocationError struct {
8+
name String
9+
message String
10+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package datamodel
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ipld/go-ipld-prime"
7+
"github.com/storacha/go-ucanto/core/ipld/codec/json"
8+
"github.com/storacha/go-ucanto/testing/helpers"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestAgentMessageInvocationError(t *testing.T) {
13+
t.Run("encode decode", func(t *testing.T) {
14+
mdl := AgentMessageInvocationErrorModel{
15+
Name: "AgentMessageInvocationError",
16+
Message: "boom",
17+
}
18+
data, err := json.Encode(&mdl, AgentMessageInvocationErrorType())
19+
require.NoError(t, err)
20+
var decoded AgentMessageInvocationErrorModel
21+
err = json.Decode(data, &decoded, AgentMessageInvocationErrorType())
22+
require.NoError(t, err)
23+
require.Equal(t, mdl.Name, decoded.Name)
24+
require.Equal(t, mdl.Message, decoded.Message)
25+
})
26+
}
27+
28+
func TestMissingProofs(t *testing.T) {
29+
t.Run("encode decode", func(t *testing.T) {
30+
prf := helpers.RandomCID()
31+
mdl := MissingProofsModel{
32+
Name: "MissingProofs",
33+
Message: "boom",
34+
Proofs: []ipld.Link{prf},
35+
}
36+
data, err := json.Encode(&mdl, MissingProofsType())
37+
require.NoError(t, err)
38+
var decoded MissingProofsModel
39+
err = json.Decode(data, &decoded, MissingProofsType())
40+
require.NoError(t, err)
41+
require.Equal(t, mdl.Name, decoded.Name)
42+
require.Equal(t, mdl.Message, decoded.Message)
43+
require.Len(t, decoded.Proofs, 1)
44+
require.Equal(t, prf.String(), decoded.Proofs[0].String())
45+
})
46+
}

server/retrieval/error.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66

77
"github.com/storacha/go-ucanto/core/ipld"
8-
"github.com/storacha/go-ucanto/core/result/failure"
98
rdm "github.com/storacha/go-ucanto/server/retrieval/datamodel"
109
)
1110

@@ -20,7 +19,11 @@ func (amie AgentMessageInvocationError) Name() string {
2019
}
2120

2221
func (amie AgentMessageInvocationError) ToIPLD() (ipld.Node, error) {
23-
return failure.FromError(amie).ToIPLD()
22+
mdl := rdm.AgentMessageInvocationErrorModel{
23+
Name: amie.Name(),
24+
Message: amie.Error(),
25+
}
26+
return ipld.WrapWithRecovery(&mdl, rdm.AgentMessageInvocationErrorType())
2427
}
2528

2629
func NewAgentMessageInvocationError() AgentMessageInvocationError {

server/retrieval/server.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import (
1010
"strings"
1111
"time"
1212

13+
ipldprime "github.com/ipld/go-ipld-prime"
14+
"github.com/ipld/go-ipld-prime/codec/dagjson"
1315
"github.com/storacha/go-ucanto/core/dag/blockstore"
1416
"github.com/storacha/go-ucanto/core/delegation"
1517
"github.com/storacha/go-ucanto/core/invocation"
1618
"github.com/storacha/go-ucanto/core/invocation/ran"
1719
"github.com/storacha/go-ucanto/core/ipld"
18-
"github.com/storacha/go-ucanto/core/ipld/codec/json"
1920
"github.com/storacha/go-ucanto/core/message"
2021
"github.com/storacha/go-ucanto/core/receipt"
2122
"github.com/storacha/go-ucanto/core/result"
2223
"github.com/storacha/go-ucanto/core/result/failure"
2324
"github.com/storacha/go-ucanto/principal"
2425
"github.com/storacha/go-ucanto/server"
25-
rdm "github.com/storacha/go-ucanto/server/retrieval/datamodel"
2626
"github.com/storacha/go-ucanto/server/transaction"
2727
"github.com/storacha/go-ucanto/transport"
2828
"github.com/storacha/go-ucanto/transport/headercar"
@@ -292,7 +292,11 @@ func Execute(ctx context.Context, srv CachingServer, msg message.AgentMessage, r
292292
if err != nil {
293293
mpe := MissingProofs{}
294294
if errors.As(err, &mpe) {
295-
body, err := json.Encode(&mpe, rdm.MissingProofsType())
295+
n, err := mpe.ToIPLD()
296+
if err != nil {
297+
return nil, Response{}, fmt.Errorf("building missing proofs IPLD view: %w", err)
298+
}
299+
body, err := ipldprime.Encode(n, dagjson.Encode)
296300
if err != nil {
297301
return nil, Response{}, fmt.Errorf("encoding missing proofs repsonse: %w", err)
298302
}

0 commit comments

Comments
 (0)