types_test.go (3213B)
1 package textlog 2 3 import ( 4 "encoding/json" 5 "reflect" 6 "testing" 7 "time" 8 ) 9 10 func TestWireTypesDecodeReferenceShapes(t *testing.T) { 11 raw := `{ 12 "data": [{ 13 "id": "activity-1", 14 "type": "mention", 15 "created_at": "2026-01-02T03:04:05Z", 16 "unread": true, 17 "payload": {"id": 7, "body": "hello", "created_at": "2026-01-01T00:00:00Z", "author": {"handle": "david"}} 18 }], 19 "pagination": {"next_cursor": "next"}, 20 "has_unread": true 21 }` 22 var collection ActivityCollection 23 if err := json.Unmarshal([]byte(raw), &collection); err != nil { 24 t.Fatal(err) 25 } 26 if len(collection.Data) != 1 || collection.Data[0].Type != ActivityMention || !collection.Data[0].Unread { 27 t.Fatalf("activity collection = %#v", collection) 28 } 29 if got := collection.Data[0].CreatedAt; !got.Equal(time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)) { 30 t.Fatalf("created_at = %v", got) 31 } 32 post, ok := collection.Data[0].Post() 33 if !ok || post.ID != 7 || post.Author.Handle != "david" { 34 t.Fatalf("activity post = %#v, %v", post, ok) 35 } 36 if got := collection.Pagination.NextCursor; got == nil || *got != "next" || !collection.HasUnread { 37 t.Fatalf("pagination = %#v, has_unread = %v", collection.Pagination, collection.HasUnread) 38 } 39 40 replyRaw := `{"id":8,"body":"reply","created_at":"2026-01-01T00:00:00Z","author":{"handle":"amy"},"depth":3}` 41 var reply Reply 42 if err := json.Unmarshal([]byte(replyRaw), &reply); err != nil { 43 t.Fatal(err) 44 } 45 if reply.ID != 8 || reply.Depth != 3 { 46 t.Fatalf("reply = %#v", reply) 47 } 48 49 postRaw, err := json.Marshal(Post{ID: 9, ParentID: intPointer(1), CreatedAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)}) 50 if err != nil { 51 t.Fatal(err) 52 } 53 var fields map[string]any 54 if err := json.Unmarshal(postRaw, &fields); err != nil { 55 t.Fatal(err) 56 } 57 if !reflect.DeepEqual(fields["parent_id"], float64(1)) || fields["created_at"] != "2026-01-01T00:00:00Z" { 58 t.Fatalf("marshaled post = %s", postRaw) 59 } 60 } 61 62 func TestReferenceEnumValues(t *testing.T) { 63 if got := []ThemeName{ThemeAuto, ThemeLight, ThemeDark}; !reflect.DeepEqual(got, []ThemeName{"auto", "light", "dark"}) { 64 t.Fatalf("themes = %v", got) 65 } 66 if got := []FeedKind{FeedForYou, FeedToMe, FeedHot, FeedLatest}; !reflect.DeepEqual(got, []FeedKind{"for-you", "to-me", "hot", "latest"}) { 67 t.Fatalf("feeds = %v", got) 68 } 69 if got := []ReportReason{ReportHarassment, ReportSpam, ReportImpersonation, ReportOther}; !reflect.DeepEqual(got, []ReportReason{"harassment", "spam", "impersonation", "other"}) { 70 t.Fatalf("report reasons = %v", got) 71 } 72 if got := []ActivityType{ActivityPost, ActivityReply, ActivityMention, ActivityUserFollow, ActivityTagFollow, ActivitySignup}; !reflect.DeepEqual(got, []ActivityType{"post", "reply", "mention", "user_follow", "tag_follow", "signup"}) { 73 t.Fatalf("activity types = %v", got) 74 } 75 if got := []ScreenKind{ScreenFeed, ScreenSearch, ScreenPost, ScreenProfile, ScreenTag, ScreenLive, ScreenCompose, ScreenLogin, ScreenAccount, ScreenSettings, ScreenHelp}; len(got) != 11 { 76 t.Fatalf("screen kinds = %v", got) 77 } 78 if got := []AppActionType{ActionPush, ActionReplace, ActionBack, ActionStatus}; len(got) != 4 { 79 t.Fatalf("action types = %v", got) 80 } 81 } 82 83 func intPointer(value int) *int { return &value }