1package dag
 2
 3import (
 4	"encoding/json"
 5	"testing"
 6	"time"
 7
 8	"github.com/stretchr/testify/require"
 9
10	"github.com/git-bug/git-bug/entities/identity"
11	"github.com/git-bug/git-bug/entity"
12	"github.com/git-bug/git-bug/repository"
13)
14
15// SerializeRoundTripTest realize a marshall/unmarshall round-trip in the same condition as with OperationPack,
16// and check if the recovered operation is identical.
17func SerializeRoundTripTest[OpT Operation](
18	t *testing.T,
19	unmarshaler OperationUnmarshaler,
20	maker func(author identity.Interface, unixTime int64) (OpT, entity.Resolvers),
21) {
22	repo := repository.NewMockRepo()
23
24	rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
25	require.NoError(t, err)
26
27	op, resolvers := maker(rene, time.Now().Unix())
28	// enforce having an id
29	op.Id()
30
31	data, err := json.Marshal(op)
32	require.NoError(t, err)
33
34	after, err := unmarshaler(data, resolvers)
35	require.NoError(t, err)
36
37	// Set the id from the serialized data
38	after.setId(entity.DeriveId(data))
39	// Set the author, as OperationPack would do
40	after.setAuthor(rene)
41
42	require.Equal(t, op, after)
43}