using Octokit.Internal;
using Xunit;

namespace Octokit.Tests.Models
{
    public class CheckSuiteEventTests
    {
        [Fact]
        public void CanBeDeserialized()
        {
            const string json = "{\r\n  \"action\": \"rerequested\",\r\n  \"check_suite\": {\r\n    \"id\": 276096,\r\n    \"head_branch\": null,\r\n    \"head_sha\": \"73955d02043135d48809add98052c2170522158f\",\r\n    \"status\": \"queued\",\r\n    \"conclusion\": null,\r\n    \"url\": \"https://api.github.com/repos/tgstation/tgstation/check-suites/276096\",\r\n    \"before\": null,\r\n    \"after\": null,\r\n    \"pull_requests\": [\r\n\r\n    ],\r\n    \"app\": {\r\n      \"id\": 9880,\r\n      \"owner\": {\r\n        \"login\": \"Cyberboss\",\r\n        \"id\": 8171642,\r\n        \"avatar_url\": \"https://avatars3.githubusercontent.com/u/8171642?v=4\",\r\n        \"gravatar_id\": \"\",\r\n        \"url\": \"https://api.github.com/users/Cyberboss\",\r\n        \"html_url\": \"https://github.com/Cyberboss\",\r\n        \"followers_url\": \"https://api.github.com/users/Cyberboss/followers\",\r\n        \"following_url\": \"https://api.github.com/users/Cyberboss/following{/other_user}\",\r\n        \"gists_url\": \"https://api.github.com/users/Cyberboss/gists{/gist_id}\",\r\n        \"starred_url\": \"https://api.github.com/users/Cyberboss/starred{/owner}{/repo}\",\r\n        \"subscriptions_url\": \"https://api.github.com/users/Cyberboss/subscriptions\",\r\n        \"organizations_url\": \"https://api.github.com/users/Cyberboss/orgs\",\r\n        \"repos_url\": \"https://api.github.com/users/Cyberboss/repos\",\r\n        \"events_url\": \"https://api.github.com/users/Cyberboss/events{/privacy}\",\r\n        \"received_events_url\": \"https://api.github.com/users/Cyberboss/received_events\",\r\n        \"type\": \"User\",\r\n        \"site_admin\": false\r\n      },\r\n      \"name\": \"MapDiffBot\",\r\n      \"description\": \"Creates diff images of BYOND .dmm maps\",\r\n      \"external_url\": \"https://github.com/MapDiffBot/MapDiffBot\",\r\n      \"html_url\": \"https://github.com/apps/mapdiffbot\",\r\n      \"created_at\": 1520618356,\r\n      \"updated_at\": 1521225691\r\n    },\r\n    \"created_at\": \"2018-05-11T05:42:21Z\",\r\n    \"updated_at\": \"2018-05-11T13:45:34Z\",\r\n    \"unique_check_runs_count\": 1,\r\n    \"check_runs_url\": \"https://api.github.com/repos/tgstation/tgstation/check-suites/276096/check-runs\",\r\n    \"head_commit\": {\r\n      \"id\": \"73955d02043135d48809add98052c2170522158f\",\r\n      \"tree_id\": \"ef367b2a31c69d095c45e1b357afb9c78c4b6631\",\r\n      \"message\": \"Remove a duplicate pipe in MetaStation maint\",\r\n      \"timestamp\": \"2018-05-07T22:36:13-07:00\",\r\n      \"author\": {\r\n        \"name\": \"Tad Hardesty\",\r\n        \"email\": \"tad@platymuus.com\"\r\n      },\r\n      \"committer\": {\r\n        \"name\": \"Tad Hardesty\",\r\n        \"email\": \"tad@platymuus.com\"\r\n      }\r\n    }\r\n  },\r\n  \"repository\": {\r\n    \"id\": 3234987,\r\n    \"name\": \"tgstation\",\r\n    \"full_name\": \"tgstation/tgstation\",\r\n    \"owner\": {\r\n      \"login\": \"tgstation\",\r\n      \"id\": 1363778,\r\n      \"avatar_url\": \"https://avatars2.githubusercontent.com/u/1363778?v=4\",\r\n      \"gravatar_id\": \"\",\r\n      \"url\": \"https://api.github.com/users/tgstation\",\r\n      \"html_url\": \"https://github.com/tgstation\",\r\n      \"followers_url\": \"https://api.github.com/users/tgstation/followers\",\r\n      \"following_url\": \"https://api.github.com/users/tgstation/following{/other_user}\",\r\n      \"gists_url\": \"https://api.github.com/users/tgstation/gists{/gist_id}\",\r\n      \"starred_url\": \"https://api.github.com/users/tgstation/starred{/owner}{/repo}\",\r\n      \"subscriptions_url\": \"https://api.github.com/users/tgstation/subscriptions\",\r\n      \"organizations_url\": \"https://api.github.com/users/tgstation/orgs\",\r\n      \"repos_url\": \"https://api.github.com/users/tgstation/repos\",\r\n      \"events_url\": \"https://api.github.com/users/tgstation/events{/privacy}\",\r\n      \"received_events_url\": \"https://api.github.com/users/tgstation/received_events\",\r\n      \"type\": \"Organization\",\r\n      \"site_admin\": false\r\n    },\r\n    \"private\": false,\r\n    \"html_url\": \"https://github.com/tgstation/tgstation\",\r\n    \"description\": \"the /tg/station branch of SS13\",\r\n    \"fork\": false,\r\n    \"url\": \"https://api.github.com/repos/tgstation/tgstation\",\r\n    \"forks_url\": \"https://api.github.com/repos/tgstation/tgstation/forks\",\r\n    \"keys_url\": \"https://api.github.com/repos/tgstation/tgstation/keys{/key_id}\",\r\n    \"collaborators_url\": \"https://api.github.com/repos/tgstation/tgstation/collaborators{/collaborator}\",\r\n    \"teams_url\": \"https://api.github.com/repos/tgstation/tgstation/teams\",\r\n    \"hooks_url\": \"https://api.github.com/repos/tgstation/tgstation/hooks\",\r\n    \"issue_events_url\": \"https://api.github.com/repos/tgstation/tgstation/issues/events{/number}\",\r\n    \"events_url\": \"https://api.github.com/repos/tgstation/tgstation/events\",\r\n    \"assignees_url\": \"https://api.github.com/repos/tgstation/tgstation/assignees{/user}\",\r\n    \"branches_url\": \"https://api.github.com/repos/tgstation/tgstation/branches{/branch}\",\r\n    \"tags_url\": \"https://api.github.com/repos/tgstation/tgstation/tags\",\r\n    \"blobs_url\": \"https://api.github.com/repos/tgstation/tgstation/git/blobs{/sha}\",\r\n    \"git_tags_url\": \"https://api.github.com/repos/tgstation/tgstation/git/tags{/sha}\",\r\n    \"git_refs_url\": \"https://api.github.com/repos/tgstation/tgstation/git/refs{/sha}\",\r\n    \"trees_url\": \"https://api.github.com/repos/tgstation/tgstation/git/trees{/sha}\",\r\n    \"statuses_url\": \"https://api.github.com/repos/tgstation/tgstation/statuses/{sha}\",\r\n    \"languages_url\": \"https://api.github.com/repos/tgstation/tgstation/languages\",\r\n    \"stargazers_url\": \"https://api.github.com/repos/tgstation/tgstation/stargazers\",\r\n    \"contributors_url\": \"https://api.github.com/repos/tgstation/tgstation/contributors\",\r\n    \"subscribers_url\": \"https://api.github.com/repos/tgstation/tgstation/subscribers\",\r\n    \"subscription_url\": \"https://api.github.com/repos/tgstation/tgstation/subscription\",\r\n    \"commits_url\": \"https://api.github.com/repos/tgstation/tgstation/commits{/sha}\",\r\n    \"git_commits_url\": \"https://api.github.com/repos/tgstation/tgstation/git/commits{/sha}\",\r\n    \"comments_url\": \"https://api.github.com/repos/tgstation/tgstation/comments{/number}\",\r\n    \"issue_comment_url\": \"https://api.github.com/repos/tgstation/tgstation/issues/comments{/number}\",\r\n    \"contents_url\": \"https://api.github.com/repos/tgstation/tgstation/contents/{+path}\",\r\n    \"compare_url\": \"https://api.github.com/repos/tgstation/tgstation/compare/{base}...{head}\",\r\n    \"merges_url\": \"https://api.github.com/repos/tgstation/tgstation/merges\",\r\n    \"archive_url\": \"https://api.github.com/repos/tgstation/tgstation/{archive_format}{/ref}\",\r\n    \"downloads_url\": \"https://api.github.com/repos/tgstation/tgstation/downloads\",\r\n    \"issues_url\": \"https://api.github.com/repos/tgstation/tgstation/issues{/number}\",\r\n    \"pulls_url\": \"https://api.github.com/repos/tgstation/tgstation/pulls{/number}\",\r\n    \"milestones_url\": \"https://api.github.com/repos/tgstation/tgstation/milestones{/number}\",\r\n    \"notifications_url\": \"https://api.github.com/repos/tgstation/tgstation/notifications{?since,all,participating}\",\r\n    \"labels_url\": \"https://api.github.com/repos/tgstation/tgstation/labels{/name}\",\r\n    \"releases_url\": \"https://api.github.com/repos/tgstation/tgstation/releases{/id}\",\r\n    \"deployments_url\": \"https://api.github.com/repos/tgstation/tgstation/deployments\",\r\n    \"created_at\": \"2012-01-21T17:32:47Z\",\r\n    \"updated_at\": \"2018-05-11T06:24:11Z\",\r\n    \"pushed_at\": \"2018-05-11T12:11:58Z\",\r\n    \"git_url\": \"git://github.com/tgstation/tgstation.git\",\r\n    \"ssh_url\": \"git@github.com:tgstation/tgstation.git\",\r\n    \"clone_url\": \"https://github.com/tgstation/tgstation.git\",\r\n    \"svn_url\": \"https://github.com/tgstation/tgstation\",\r\n    \"homepage\": \"https://www.tgstation13.org/\",\r\n    \"size\": 1126165,\r\n    \"stargazers_count\": 515,\r\n    \"watchers_count\": 515,\r\n    \"language\": \"DM\",\r\n    \"has_issues\": true,\r\n    \"has_projects\": true,\r\n    \"has_downloads\": true,\r\n    \"has_wiki\": true,\r\n    \"has_pages\": false,\r\n    \"forks_count\": 1783,\r\n    \"mirror_url\": null,\r\n    \"archived\": false,\r\n    \"open_issues_count\": 1211,\r\n    \"license\": {\r\n      \"key\": \"agpl-3.0\",\r\n      \"name\": \"GNU Affero General Public License v3.0\",\r\n      \"spdx_id\": \"AGPL-3.0\",\r\n      \"url\": \"https://api.github.com/licenses/agpl-3.0\"\r\n    },\r\n    \"forks\": 1783,\r\n    \"open_issues\": 1211,\r\n    \"watchers\": 515,\r\n    \"default_branch\": \"main\"\r\n  },\r\n  \"organization\": {\r\n    \"login\": \"tgstation\",\r\n    \"id\": 1363778,\r\n    \"url\": \"https://api.github.com/orgs/tgstation\",\r\n    \"repos_url\": \"https://api.github.com/orgs/tgstation/repos\",\r\n    \"events_url\": \"https://api.github.com/orgs/tgstation/events\",\r\n    \"hooks_url\": \"https://api.github.com/orgs/tgstation/hooks\",\r\n    \"issues_url\": \"https://api.github.com/orgs/tgstation/issues\",\r\n    \"members_url\": \"https://api.github.com/orgs/tgstation/members{/member}\",\r\n    \"public_members_url\": \"https://api.github.com/orgs/tgstation/public_members{/member}\",\r\n    \"avatar_url\": \"https://avatars2.githubusercontent.com/u/1363778?v=4\",\r\n    \"description\": \"\"\r\n  },\r\n  \"sender\": {\r\n    \"login\": \"Cyberboss\",\r\n    \"id\": 8171642,\r\n    \"avatar_url\": \"https://avatars3.githubusercontent.com/u/8171642?v=4\",\r\n    \"gravatar_id\": \"\",\r\n    \"url\": \"https://api.github.com/users/Cyberboss\",\r\n    \"html_url\": \"https://github.com/Cyberboss\",\r\n    \"followers_url\": \"https://api.github.com/users/Cyberboss/followers\",\r\n    \"following_url\": \"https://api.github.com/users/Cyberboss/following{/other_user}\",\r\n    \"gists_url\": \"https://api.github.com/users/Cyberboss/gists{/gist_id}\",\r\n    \"starred_url\": \"https://api.github.com/users/Cyberboss/starred{/owner}{/repo}\",\r\n    \"subscriptions_url\": \"https://api.github.com/users/Cyberboss/subscriptions\",\r\n    \"organizations_url\": \"https://api.github.com/users/Cyberboss/orgs\",\r\n    \"repos_url\": \"https://api.github.com/users/Cyberboss/repos\",\r\n    \"events_url\": \"https://api.github.com/users/Cyberboss/events{/privacy}\",\r\n    \"received_events_url\": \"https://api.github.com/users/Cyberboss/received_events\",\r\n    \"type\": \"User\",\r\n    \"site_admin\": false\r\n  },\r\n  \"installation\": {\r\n    \"id\": 103621\r\n  }\r\n}";


            var serializer = new SimpleJsonSerializer();

            var payload = serializer.Deserialize<CheckSuiteEventPayload>(json);

            Assert.Equal("rerequested", payload.Action);
            Assert.Equal("73955d02043135d48809add98052c2170522158f", payload.CheckSuite.HeadSha);
            Assert.Equal(CheckStatus.Queued, payload.CheckSuite.Status.Value);
            Assert.NotNull(payload.Installation);
            Assert.NotNull(payload.Repository);
            Assert.NotNull(payload.Sender);
        }
    }
}
