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
package test

import (
	"encoding/json"
	"rss-kakapo/dto"
	"slices"
	"testing"
)

// checkNote parses the note in fileName and checks its addressing and its one
// mention of the birb.
func checkNote(t *testing.T, fileName string, wantTo, wantCc []string) {
	t.Helper()
	bytes, err := fs.ReadFile(fileName)
	if err != nil {
		t.Fatal(err)
	}
	var note dto.Note
	if err := json.Unmarshal(bytes, &note); err != nil {
		t.Fatalf("%s: %v", fileName, err)
	}
	if !slices.Equal(note.To, wantTo) {
		t.Errorf("%s: to = %v, want %v", fileName, note.To, wantTo)
	}
	if !slices.Equal(note.Cc, wantCc) {
		t.Errorf("%s: cc = %v, want %v", fileName, note.Cc, wantCc)
	}
	if note.Tag == nil || len(*note.Tag) != 1 {
		t.Fatalf("%s: tag = %+v, want exactly one", fileName, note.Tag)
	}
	tag := (*note.Tag)[0]
	if tag.Href != "https://rss-parrot.zydeo.net/u/birb" || tag.Name != "@birb@rss-parrot.zydeo.net" || tag.Type != "Mention" {
		t.Errorf("%s: tag = %+v, want a Mention of @birb@rss-parrot.zydeo.net", fileName, tag)
	}
}

func Test_Deserialize_Note(t *testing.T) {
	// Note from GoToSocial: 'tag' is an object; 'cc' missing; 'to' is a string
	checkNote(t, "data/note-01.json",
		[]string{"https://rss-parrot.zydeo.net/u/birb"},
		nil)

	// Note from Mastodon: 'tag' is array; 'cc' and 'to' are arrays
	checkNote(t, "data/note-02.json",
		[]string{"https://www.w3.org/ns/activitystreams#Public"},
		[]string{"https://toot.community/users/gaborparrot/followers", "https://rss-parrot.zydeo.net/u/birb"})
}