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 1d5ddf5d 2024-02-20 o "testing"
6 1d5ddf5d 2024-02-20 o )
7 1d5ddf5d 2024-02-20 o
8 1d5ddf5d 2024-02-20 o func TestDecode(t *testing.T) {
9 77918b00 2024-03-07 o f, err := os.Open("testdata/announce1.json")
10 77918b00 2024-03-07 o if err != nil {
11 77918b00 2024-03-07 o t.Fatal(err)
12 1d5ddf5d 2024-02-20 o }
13 77918b00 2024-03-07 o defer f.Close()
14 77918b00 2024-03-07 o a, err := Decode(f)
15 77918b00 2024-03-07 o if err != nil {
16 77918b00 2024-03-07 o t.Fatal("decode activity", err)
17 77918b00 2024-03-07 o }
18 77918b00 2024-03-07 o want := "https://lemmy.sdf.org/activities/like/b5bd1577-9677-4130-8312-cd2e2fd4ea44"
19 77918b00 2024-03-07 o inner, err := a.Unwrap(nil)
20 77918b00 2024-03-07 o if err != nil {
21 77918b00 2024-03-07 o t.Fatal("unwrap activity:", err)
22 77918b00 2024-03-07 o }
23 77918b00 2024-03-07 o if inner.ID != want {
24 77918b00 2024-03-07 o t.Errorf("wanted wrapped activity id %s, got %s", want, inner.ID)
25 77918b00 2024-03-07 o }
26 1d5ddf5d 2024-02-20 o }
27 1738cb32 2024-03-12 o
28 1738cb32 2024-03-12 o func TestTag(t *testing.T) {
29 1738cb32 2024-03-12 o tests := []struct {
30 1738cb32 2024-03-12 o Name string
31 1738cb32 2024-03-12 o Mention string
32 1738cb32 2024-03-12 o }{
33 1738cb32 2024-03-12 o {"testdata/note/akkoma.json", "@otl@apubtest2.srcbeat.com"},
34 1738cb32 2024-03-12 o {"testdata/note/lemmy.json", "@Feathercrown@lemmy.world"},
35 1738cb32 2024-03-12 o {"testdata/note/mastodon.json", "@selfhosted@lemmy.world"},
36 1738cb32 2024-03-12 o }
37 1738cb32 2024-03-12 o for _, tt := range tests {
38 1738cb32 2024-03-12 o f, err := os.Open(tt.Name)
39 1738cb32 2024-03-12 o if err != nil {
40 1738cb32 2024-03-12 o t.Error(err)
41 1738cb32 2024-03-12 o continue
42 1738cb32 2024-03-12 o }
43 1738cb32 2024-03-12 o defer f.Close()
44 1738cb32 2024-03-12 o a, err := Decode(f)
45 1738cb32 2024-03-12 o if err != nil {
46 1738cb32 2024-03-12 o t.Errorf("%s: decode: %v", tt.Name, err)
47 1738cb32 2024-03-12 o continue
48 1738cb32 2024-03-12 o }
49 1738cb32 2024-03-12 o if len(a.Tag) == 0 {
50 1738cb32 2024-03-12 o t.Errorf("%s: no tags", tt.Name)
51 1738cb32 2024-03-12 o continue
52 1738cb32 2024-03-12 o }
53 1738cb32 2024-03-12 o var found bool
54 1738cb32 2024-03-12 o for _, tag := range a.Tag {
55 1738cb32 2024-03-12 o if tag.Name == tt.Mention {
56 1738cb32 2024-03-12 o found = true
57 1738cb32 2024-03-12 o }
58 1738cb32 2024-03-12 o }
59 1738cb32 2024-03-12 o if !found {
60 1738cb32 2024-03-12 o t.Errorf("%s: did not find mention %s", tt.Name, tt.Mention)
61 1738cb32 2024-03-12 o }
62 1738cb32 2024-03-12 o }
63 1738cb32 2024-03-12 o }