1 // OpenTween - Client of Twitter
2 // Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
26 using System.Runtime.Serialization.Json;
28 using System.Text.RegularExpressions;
29 using System.Threading.Tasks;
31 using System.Xml.Linq;
32 using OpenTween.Models;
35 namespace OpenTween.Api.GraphQL
37 public class TimelineTweetTest
39 private XElement LoadResponseDocument(string filename)
41 using var stream = File.OpenRead($"Resources/Responses/{filename}");
42 using var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
43 return XElement.Load(jsonReader);
46 private TabInformations CreateTabInfo()
48 var tabinfo = new TabInformations();
49 tabinfo.AddDefaultTabs();
54 public void ExtractTimelineTweets_Single_Test()
56 var rootElm = this.LoadResponseDocument("ListLatestTweetsTimeline_SimpleTweet.json");
57 var timelineTweets = TimelineTweet.ExtractTimelineTweets(rootElm);
58 Assert.Single(timelineTweets);
62 public void ExtractTimelineTweets_Conversation_Test()
64 var rootElm = this.LoadResponseDocument("ListLatestTweetsTimeline_Conversation.json");
65 var timelineTweets = TimelineTweet.ExtractTimelineTweets(rootElm);
66 Assert.Equal(3, timelineTweets.Length);
70 public void ToStatus_WithTwitterPostFactory_SimpleTweet_Test()
72 var rootElm = this.LoadResponseDocument("TimelineTweet_SimpleTweet.json");
73 var timelineTweet = new TimelineTweet(rootElm);
74 var status = timelineTweet.ToTwitterStatus();
75 var postFactory = new TwitterPostFactory(this.CreateTabInfo());
76 var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
78 Assert.Equal("1613784711020826626", post.StatusId.Id);
79 Assert.Equal(40480664L, post.UserId);
80 Assert.False(post.IsPromoted);
84 public void ToStatus_WithTwitterPostFactory_TweetWithMedia_Test()
86 var rootElm = this.LoadResponseDocument("TimelineTweet_TweetWithMedia.json");
87 var timelineTweet = new TimelineTweet(rootElm);
88 var status = timelineTweet.ToTwitterStatus();
89 var postFactory = new TwitterPostFactory(this.CreateTabInfo());
90 var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
92 Assert.Equal("1614587968567783424", post.StatusId.Id);
93 Assert.Equal(40480664L, post.UserId);
94 Assert.Equal(2, post.Media.Count);
95 Assert.Equal("https://pbs.twimg.com/media/FmgrJiEaAAEU42G.png", post.Media[0].Url);
96 Assert.Equal("OpenTweenで @opentween のツイート一覧を表示しているスクショ", post.Media[0].AltText);
97 Assert.Equal("https://pbs.twimg.com/media/FmgrJiXaMAEu873.jpg", post.Media[1].Url);
98 Assert.Equal("OpenTweenの新しい画像投稿画面を動かしている様子のスクショ", post.Media[1].AltText);
102 public void ToStatus_WithTwitterPostFactory_RetweetedTweet_Test()
104 var rootElm = this.LoadResponseDocument("TimelineTweet_RetweetedTweet.json");
105 var timelineTweet = new TimelineTweet(rootElm);
106 var status = timelineTweet.ToTwitterStatus();
107 var postFactory = new TwitterPostFactory(this.CreateTabInfo());
108 var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
110 Assert.Equal("1617128268548964354", post.StatusId.Id);
111 Assert.Equal(40480664L, post.RetweetedByUserId);
112 Assert.Equal("1617126084138659840", post.RetweetedId!.Id);
113 Assert.Equal(514241801L, post.UserId);
117 public void ToStatus_WithTwitterPostFactory_TweetWithVisibility_Test()
119 var rootElm = this.LoadResponseDocument("TimelineTweet_TweetWithVisibility.json");
120 var timelineTweet = new TimelineTweet(rootElm);
121 var status = timelineTweet.ToTwitterStatus();
122 var postFactory = new TwitterPostFactory(this.CreateTabInfo());
123 var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
125 Assert.Equal("1602775353088524288", post.StatusId.Id);
126 Assert.Equal(357750891L, post.UserId);
130 public void ToStatus_WithTwitterPostFactory_SelfThread_Test()
132 var rootElm = this.LoadResponseDocument("TimelineTweet_SelfThread.json");
133 var timelineTweet = new TimelineTweet(rootElm);
134 var status = timelineTweet.ToTwitterStatus();
135 var postFactory = new TwitterPostFactory(this.CreateTabInfo());
136 var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
138 Assert.Equal("1511751702684499968", post.StatusId.Id);
139 Assert.Equal(40480664L, post.UserId);
143 public void ToStatus_WithTwitterPostFactory_PromotedTweet_Test()
145 var rootElm = this.LoadResponseDocument("TimelineTweet_PromotedTweet.json");
146 var timelineTweet = new TimelineTweet(rootElm);
147 var status = timelineTweet.ToTwitterStatus();
148 var postFactory = new TwitterPostFactory(this.CreateTabInfo());
149 var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
151 Assert.Equal("1674737917363888129", post.StatusId.Id);
152 Assert.Equal(2941313791L, post.UserId);
153 Assert.True(post.IsPromoted);
154 Assert.Matches(new Regex(@"^\[Promoted\]\n"), post.TextFromApi);
158 public void ToStatus_TweetTombstone_Test()
160 var rootElm = this.LoadResponseDocument("TimelineTweet_TweetTombstone.json");
161 var timelineTweet = new TimelineTweet(rootElm);
163 Assert.True(timelineTweet.IsTombstone);
164 var ex = Assert.Throws<WebApiException>(
165 () => timelineTweet.ToTwitterStatus()
167 Assert.Equal("This Post is from a suspended account. Learn more", ex.Message);