1package bug
2
3import (
4 "encoding/json"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/require"
9
10 "github.com/MichaelMure/git-bug/identity"
11 "github.com/MichaelMure/git-bug/repository"
12 "github.com/MichaelMure/git-bug/util/timestamp"
13)
14
15func TestCreate(t *testing.T) {
16 snapshot := Snapshot{}
17
18 repo := repository.NewMockRepoClock()
19
20 rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
21 require.NoError(t, err)
22
23 unix := time.Now().Unix()
24
25 create := NewCreateOp(rene, unix, "title", "message", nil)
26
27 create.Apply(&snapshot)
28
29 id := create.Id()
30 require.NoError(t, id.Validate())
31
32 commentId := DeriveCommentId(create.Id(), create.Id())
33
34 comment := Comment{
35 id: commentId,
36 Author: rene,
37 Message: "message",
38 UnixTime: timestamp.Timestamp(create.UnixTime),
39 }
40
41 expected := Snapshot{
42 id: create.Id(),
43 Title: "title",
44 Comments: []Comment{
45 comment,
46 },
47 Author: rene,
48 Participants: []identity.Interface{rene},
49 Actors: []identity.Interface{rene},
50 CreateTime: create.Time(),
51 Timeline: []TimelineItem{
52 &CreateTimelineItem{
53 CommentTimelineItem: NewCommentTimelineItem(commentId, comment),
54 },
55 },
56 }
57
58 require.Equal(t, expected, snapshot)
59}
60
61func TestCreateSerialize(t *testing.T) {
62 repo := repository.NewMockRepo()
63
64 rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
65 require.NoError(t, err)
66
67 unix := time.Now().Unix()
68 before := NewCreateOp(rene, unix, "title", "message", nil)
69
70 data, err := json.Marshal(before)
71 require.NoError(t, err)
72
73 var after CreateOperation
74 err = json.Unmarshal(data, &after)
75 require.NoError(t, err)
76
77 // enforce creating the ID
78 before.Id()
79
80 // Replace the identity stub with the real thing
81 require.Equal(t, rene.Id(), after.base().Author.Id())
82 after.Author = rene
83
84 require.Equal(t, before, &after)
85}