|
| 1 | +require "../spec_helper" |
| 2 | + |
| 3 | +private def subject_from(type : String, url = "", latest_comment_url = "") |
| 4 | + Github::Subject.from_json({ |
| 5 | + type: type, |
| 6 | + title: "title", |
| 7 | + url: url, |
| 8 | + latest_comment_url: latest_comment_url, |
| 9 | + }.to_json) |
| 10 | +end |
| 11 | + |
| 12 | +describe Github::Subject do |
| 13 | + describe "#update?" do |
| 14 | + it "is true for tracked subject types" do |
| 15 | + [ |
| 16 | + Github::Subject::Type::PULL_REQUEST, |
| 17 | + Github::Subject::Type::ISSUE, |
| 18 | + Github::Subject::Type::COMMIT, |
| 19 | + Github::Subject::Type::DISCUSSION, |
| 20 | + ].each do |type| |
| 21 | + subject_from(type).update?.should be_true |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + it "is false for unknown subject types" do |
| 26 | + subject_from("Release").update?.should be_false |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe "#color" do |
| 31 | + it "returns a distinct color per known type" do |
| 32 | + subject_from(Github::Subject::Type::PULL_REQUEST).color.should eq "#F6CEE3" |
| 33 | + subject_from(Github::Subject::Type::ISSUE).color.should eq "#A9D0F5" |
| 34 | + subject_from(Github::Subject::Type::COMMIT).color.should eq "#f5d7a9" |
| 35 | + subject_from(Github::Subject::Type::DISCUSSION).color.should eq "#7fffd4" |
| 36 | + end |
| 37 | + |
| 38 | + it "falls back to a default color for unknown types" do |
| 39 | + subject_from("Release").color.should eq "#D8D8D8" |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + describe "#comment_url" do |
| 44 | + it "prefers latest_comment_url when present" do |
| 45 | + subject = subject_from("Issue", url: "u", latest_comment_url: "c") |
| 46 | + subject.comment_url.should eq "c" |
| 47 | + end |
| 48 | + |
| 49 | + it "falls back to url when latest_comment_url is blank" do |
| 50 | + subject = subject_from("Issue", url: "u", latest_comment_url: "") |
| 51 | + subject.comment_url.should eq "u" |
| 52 | + end |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +describe Github::Notification do |
| 57 | + describe "#mention?" do |
| 58 | + it "is true for reasons that mention the user" do |
| 59 | + Github::Notification::MENTION_REASONS.each do |reason| |
| 60 | + notification_from(reason).mention?.should be_true |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + it "is false for non-mention reasons" do |
| 65 | + notification_from("subscribed").mention?.should be_false |
| 66 | + notification_from("ci_activity").mention?.should be_false |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + it "parses a GitHub notifications API payload" do |
| 71 | + notifications = Array(Github::Notification).from_json(NOTIFICATIONS_FIXTURE) |
| 72 | + notifications.size.should eq 1 |
| 73 | + |
| 74 | + notification = notifications.first |
| 75 | + notification.reason.should eq "mention" |
| 76 | + notification.subject.type.should eq "Issue" |
| 77 | + notification.subject.title.should eq "Spurious failure" |
| 78 | + notification.repository.full_name.should eq "octocat/Hello-World" |
| 79 | + notification.mention?.should be_true |
| 80 | + end |
| 81 | +end |
| 82 | + |
| 83 | +private def notification_from(reason : String) |
| 84 | + Github::Notification.from_json({ |
| 85 | + reason: reason, |
| 86 | + subject: {type: "Issue", title: "title"}, |
| 87 | + repository: {owner: {login: "octocat"}}, |
| 88 | + }.to_json) |
| 89 | +end |
| 90 | + |
| 91 | +NOTIFICATIONS_FIXTURE = <<-JSON |
| 92 | +[ |
| 93 | + { |
| 94 | + "reason": "mention", |
| 95 | + "subject": { |
| 96 | + "title": "Spurious failure", |
| 97 | + "url": "https://api.github.com/repos/octocat/Hello-World/issues/1", |
| 98 | + "latest_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments/1", |
| 99 | + "type": "Issue" |
| 100 | + }, |
| 101 | + "repository": { |
| 102 | + "full_name": "octocat/Hello-World", |
| 103 | + "html_url": "https://github.com/octocat/Hello-World", |
| 104 | + "owner": { |
| 105 | + "login": "octocat", |
| 106 | + "avatar_url": "https://github.com/images/error/octocat.gif", |
| 107 | + "html_url": "https://github.com/octocat" |
| 108 | + } |
| 109 | + }, |
| 110 | + "subscription_url": "https://api.github.com/notifications/threads/1/subscription" |
| 111 | + } |
| 112 | +] |
| 113 | +JSON |
0 commit comments