Skip to content
Repos

WO_DevPlaceIOS

Swift 11.1 KB248 lines Opens in site

Directory Raw Download

import Foundation
import Testing
@testable import DevPlaceSwiftSDK

struct NotificationsDecodingTests {
    @Test func decodesGroupedResponse() throws {
        let notifications = try decode(Fixture.grouped)

        #expect(notifications.grouping)
        #expect(notifications.nextCursor == nil)
        #expect(notifications.groups.count == 1)

        let group = try #require(notifications.groups.first)
        #expect(group.label == "Today")
        #expect(group.entries.isEmpty)
        #expect(group.eventEntries.count == 2)

        let votes = try #require(group.eventEntries.first)
        #expect(votes.key == "vote\u{1F}/posts/grouped-seed")
        #expect(votes.count == 4)
        #expect(votes.unread)
        #expect(votes.actors.map(\.username) == ["actoruser"])
        #expect(votes.latest.data.type == .vote)
        #expect(votes.latest.data.message == "voter-0 upvoted your post")
        #expect(votes.latest.actor?.username == "actoruser")
    }

    @Test func decodesUngroupedResponse() throws {
        let notifications = try decode(Fixture.ungrouped)

        #expect(!notifications.grouping)

        let group = try #require(notifications.groups.first)
        #expect(group.entries.count == 2)
        #expect(group.eventEntries.isEmpty)
        #expect(group.entries.first?.data.type == .vote)
        #expect(group.entries.first?.data.read == false)
    }

    @Test func keepsNextCursorExactlyAsSent() throws {
        let json = #"{"notification_groups":[],"next_cursor":"2026-11-14T12:00:00.123456+00:00","grouping":false}"#

        let notifications = try decode(json)

        #expect(notifications.nextCursor == "2026-11-14T12:00:00.123456+00:00")
    }

    @Test func decodesNotificationCounts() throws {
        let data = Data(#"{"notifications":3,"messages":0}"#.utf8)
        let counts = try JSONDecoder.devPlace.decode(NotificationCounts.CodingData.self, from: data).decoded

        #expect(counts == NotificationCounts(notifications: 3, messages: 0))
    }

    private func decode(_ json: String) throws -> Notifications {
        try JSONDecoder.devPlace.decode(Notifications.CodingData.self, from: Data(json.utf8)).decoded
    }
}

private enum Fixture {
    static let grouped = #"""
        {
            "notification_groups": [
                {
                    "label": "Today",
                    "entries": [],
                    "event_entries": [
                        {
                            "key": "vote\u001f/posts/grouped-seed",
                            "count": 4,
                            "unread": true,
                            "actors": [
                                {
                                    "uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                    "username": "actoruser",
                                    "avatar_seed": null,
                                    "bio": "",
                                    "location": "",
                                    "git_link": "",
                                    "website": "",
                                    "level": 1,
                                    "xp": 0,
                                    "xp_progress_pct": null,
                                    "xp_next_level": null,
                                    "stars": 0,
                                    "created_at": "2026-11-15T01:04:35.490389+00:00",
                                    "last_seen": null
                                }
                            ],
                            "latest": {
                                "notification": {
                                    "uid": "01a1dcbc-4a09-7de1-aa67-d82553f5a3b9",
                                    "type": "vote",
                                    "message": "voter-0 upvoted your post",
                                    "read": false,
                                    "related_uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                    "target_url": "/posts/grouped-seed",
                                    "created_at": "2026-11-15T01:03:49.664796+00:00"
                                },
                                "actor": {
                                    "uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                    "username": "actoruser",
                                    "avatar_seed": null,
                                    "bio": "",
                                    "location": "",
                                    "git_link": "",
                                    "website": "",
                                    "level": 1,
                                    "xp": 0,
                                    "xp_progress_pct": null,
                                    "xp_next_level": null,
                                    "stars": 0,
                                    "created_at": "2026-11-15T01:04:35.490389+00:00",
                                    "last_seen": null
                                },
                                "time_ago": "1m ago"
                            }
                        },
                        {
                            "key": "comment\u001f01a1dcbc-12a0-7bf1-b2e6-64567a59cc51\u001f/posts/other-post",
                            "count": 2,
                            "unread": true,
                            "actors": [
                                {
                                    "uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                    "username": "actoruser",
                                    "avatar_seed": null,
                                    "bio": "",
                                    "location": "",
                                    "git_link": "",
                                    "website": "",
                                    "level": 1,
                                    "xp": 0,
                                    "xp_progress_pct": null,
                                    "xp_next_level": null,
                                    "stars": 0,
                                    "created_at": "2026-11-15T01:04:35.490389+00:00",
                                    "last_seen": null
                                }
                            ],
                            "latest": {
                                "notification": {
                                    "uid": "01a1dcbc-4a6c-7e71-8e85-31a4d14d04c7",
                                    "type": "comment",
                                    "message": "actoruser commented on your post",
                                    "read": false,
                                    "related_uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                    "target_url": "/posts/other-post",
                                    "created_at": "2026-11-15T00:54:49.664796+00:00"
                                },
                                "actor": {
                                    "uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                    "username": "actoruser",
                                    "avatar_seed": null,
                                    "bio": "",
                                    "location": "",
                                    "git_link": "",
                                    "website": "",
                                    "level": 1,
                                    "xp": 0,
                                    "xp_progress_pct": null,
                                    "xp_next_level": null,
                                    "stars": 0,
                                    "created_at": "2026-11-15T01:04:35.490389+00:00",
                                    "last_seen": null
                                },
                                "time_ago": "10m ago"
                            }
                        }
                    ]
                }
            ],
            "next_cursor": null,
            "grouping": true
        }
    """#

    static let ungrouped = #"""
        {
            "notification_groups": [
                {
                    "label": "Today",
                    "entries": [
                        {
                            "notification": {
                                "uid": "01a1dcbc-4a09-7de1-aa67-d82553f5a3b9",
                                "type": "vote",
                                "message": "voter-0 upvoted your post",
                                "read": false,
                                "related_uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                "target_url": "/posts/grouped-seed",
                                "created_at": "2026-11-15T01:03:49.664796+00:00"
                            },
                            "actor": {
                                "uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                "username": "actoruser",
                                "avatar_seed": null,
                                "bio": "",
                                "location": "",
                                "git_link": "",
                                "website": "",
                                "level": 1,
                                "xp": 0,
                                "xp_progress_pct": null,
                                "xp_next_level": null,
                                "stars": 0,
                                "created_at": "2026-11-15T01:04:35.490389+00:00",
                                "last_seen": null
                            },
                            "time_ago": "1m ago"
                        },
                        {
                            "notification": {
                                "uid": "01a1dcbc-4a6b-7cb1-8602-4c797f6911bb",
                                "type": "vote",
                                "message": "voter-1 upvoted your post",
                                "read": false,
                                "related_uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                "target_url": "/posts/grouped-seed",
                                "created_at": "2026-11-15T01:02:49.664796+00:00"
                            },
                            "actor": {
                                "uid": "01a1dcbc-12a0-7bf1-b2e6-64567a59cc51",
                                "username": "actoruser",
                                "avatar_seed": null,
                                "bio": "",
                                "location": "",
                                "git_link": "",
                                "website": "",
                                "level": 1,
                                "xp": 0,
                                "xp_progress_pct": null,
                                "xp_next_level": null,
                                "stars": 0,
                                "created_at": "2026-11-15T01:04:35.490389+00:00",
                                "last_seen": null
                            },
                            "time_ago": "2m ago"
                        }
                    ],
                    "event_entries": []
                }
            ],
            "next_cursor": null,
            "grouping": false
        }
    """#
}
24.5ms