Blame


1 1d5ddf5d 2024-02-20 o package apub
2 1d5ddf5d 2024-02-20 o
3 1d5ddf5d 2024-02-20 o import (
4 1d5ddf5d 2024-02-20 o "os"
5 8403ab16 2024-03-21 o "path"
6 8403ab16 2024-03-21 o "reflect"
7 8403ab16 2024-03-21 o "sort"
8 1d5ddf5d 2024-02-20 o "testing"
9 1d5ddf5d 2024-02-20 o )
10 1d5ddf5d 2024-02-20 o
11 1d5ddf5d 2024-02-20 o func TestDecode(t *testing.T) {
12 77918b00 2024-03-07 o f, err := os.Open("testdata/announce1.json")
13 77918b00 2024-03-07 o if err != nil {
14 77918b00 2024-03-07 o t.Fatal(err)
15 1d5ddf5d 2024-02-20 o }
16 77918b00 2024-03-07 o defer f.Close()
17 77918b00 2024-03-07 o a, err := Decode(f)
18 77918b00 2024-03-07 o if err != nil {
19 77918b00 2024-03-07 o t.Fatal("decode activity", err)
20 77918b00 2024-03-07 o }
21 77918b00 2024-03-07 o want := "https://lemmy.sdf.org/activities/like/b5bd1577-9677-4130-8312-cd2e2fd4ea44"
22 77918b00 2024-03-07 o inner, err := a.Unwrap(nil)
23 77918b00 2024-03-07 o if err != nil {
24 77918b00 2024-03-07 o t.Fatal("unwrap activity:", err)
25 77918b00 2024-03-07 o }
26 77918b00 2024-03-07 o if inner.ID != want {
27 77918b00 2024-03-07 o t.Errorf("wanted wrapped activity id %s, got %s", want, inner.ID)
28 77918b00 2024-03-07 o }
29 1d5ddf5d 2024-02-20 o }
30 1738cb32 2024-03-12 o
31 1738cb32 2024-03-12 o func TestTag(t *testing.T) {
32 1738cb32 2024-03-12 o tests := []struct {
33 1738cb32 2024-03-12 o Name string
34 1738cb32 2024-03-12 o Mention string
35 1738cb32 2024-03-12 o }{
36 1738cb32 2024-03-12 o {"testdata/note/akkoma.json", "@otl@apubtest2.srcbeat.com"},
37 1738cb32 2024-03-12 o {"testdata/note/lemmy.json", "@Feathercrown@lemmy.world"},
38 1738cb32 2024-03-12 o {"testdata/note/mastodon.json", "@selfhosted@lemmy.world"},
39 1738cb32 2024-03-12 o }
40 1738cb32 2024-03-12 o for _, tt := range tests {
41 1738cb32 2024-03-12 o f, err := os.Open(tt.Name)
42 1738cb32 2024-03-12 o if err != nil {
43 1738cb32 2024-03-12 o t.Error(err)
44 1738cb32 2024-03-12 o continue
45 1738cb32 2024-03-12 o }
46 1738cb32 2024-03-12 o defer f.Close()
47 1738cb32 2024-03-12 o a, err := Decode(f)
48 1738cb32 2024-03-12 o if err != nil {
49 1738cb32 2024-03-12 o t.Errorf("%s: decode: %v", tt.Name, err)
50 1738cb32 2024-03-12 o continue
51 1738cb32 2024-03-12 o }
52 1738cb32 2024-03-12 o if len(a.Tag) == 0 {
53 1738cb32 2024-03-12 o t.Errorf("%s: no tags", tt.Name)
54 1738cb32 2024-03-12 o continue
55 1738cb32 2024-03-12 o }
56 1738cb32 2024-03-12 o var found bool
57 1738cb32 2024-03-12 o for _, tag := range a.Tag {
58 1738cb32 2024-03-12 o if tag.Name == tt.Mention {
59 1738cb32 2024-03-12 o found = true
60 1738cb32 2024-03-12 o }
61 1738cb32 2024-03-12 o }
62 1738cb32 2024-03-12 o if !found {
63 1738cb32 2024-03-12 o t.Errorf("%s: did not find mention %s", tt.Name, tt.Mention)
64 1738cb32 2024-03-12 o }
65 1738cb32 2024-03-12 o }
66 1738cb32 2024-03-12 o }
67 8403ab16 2024-03-21 o
68 8403ab16 2024-03-21 o func TestInboxes(t *testing.T) {
69 8403ab16 2024-03-21 o want := []string{
70 8403ab16 2024-03-21 o "https://apubtest2.srcbeat.com/otl/inbox",
71 8403ab16 2024-03-21 o "https://hachyderm.io/inbox",
72 8403ab16 2024-03-21 o "https://lemmy.world/inbox",
73 8403ab16 2024-03-21 o "https://social.harpia.red/inbox",
74 8403ab16 2024-03-21 o }
75 8403ab16 2024-03-21 o
76 8403ab16 2024-03-21 o root := "testdata/actor"
77 8403ab16 2024-03-21 o dirent, err := os.ReadDir("testdata/actor")
78 8403ab16 2024-03-21 o if err != nil {
79 8403ab16 2024-03-21 o t.Fatal(err)
80 8403ab16 2024-03-21 o }
81 8403ab16 2024-03-21 o actors := make([]Actor, len(dirent))
82 8403ab16 2024-03-21 o for i, ent := range dirent {
83 8403ab16 2024-03-21 o f, err := os.Open(path.Join(root, ent.Name()))
84 8403ab16 2024-03-21 o if err != nil {
85 8403ab16 2024-03-21 o t.Fatal(err)
86 8403ab16 2024-03-21 o }
87 8403ab16 2024-03-21 o defer f.Close()
88 8403ab16 2024-03-21 o a, err := DecodeActor(f)
89 8403ab16 2024-03-21 o if err != nil {
90 8403ab16 2024-03-21 o t.Fatal(err)
91 8403ab16 2024-03-21 o }
92 8403ab16 2024-03-21 o actors[i] = *a
93 8403ab16 2024-03-21 o }
94 8403ab16 2024-03-21 o got := Inboxes(actors)
95 8403ab16 2024-03-21 o sort.Strings(got)
96 8403ab16 2024-03-21 o if !reflect.DeepEqual(want, got) {
97 8403ab16 2024-03-21 o t.Errorf("unexpected inbox slice of multiple actors, want %s got %s", want, got)
98 8403ab16 2024-03-21 o }
99 8403ab16 2024-03-21 o }