OSDN Git Service

6747ad1583b1d323ba2128737c228953657a4a07
[opentween/open-tween.git] / OpenTween.Tests / Api / GraphQL / TimelineTweetTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 using System;
23 using System.Collections.Generic;
24 using System.IO;
25 using System.Linq;
26 using System.Runtime.Serialization.Json;
27 using System.Text;
28 using System.Text.RegularExpressions;
29 using System.Threading.Tasks;
30 using System.Xml;
31 using System.Xml.Linq;
32 using OpenTween.Models;
33 using Xunit;
34
35 namespace OpenTween.Api.GraphQL
36 {
37     public class TimelineTweetTest
38     {
39         private XElement LoadResponseDocument(string filename)
40         {
41             using var stream = File.OpenRead($"Resources/Responses/{filename}");
42             using var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
43             return XElement.Load(jsonReader);
44         }
45
46         private TabInformations CreateTabInfo()
47         {
48             var tabinfo = new TabInformations();
49             tabinfo.AddDefaultTabs();
50             return tabinfo;
51         }
52
53         [Fact]
54         public void ExtractTimelineTweets_Single_Test()
55         {
56             var rootElm = this.LoadResponseDocument("ListLatestTweetsTimeline_SimpleTweet.json");
57             var timelineTweets = TimelineTweet.ExtractTimelineTweets(rootElm);
58             Assert.Single(timelineTweets);
59         }
60
61         [Fact]
62         public void ExtractTimelineTweets_Conversation_Test()
63         {
64             var rootElm = this.LoadResponseDocument("ListLatestTweetsTimeline_Conversation.json");
65             var timelineTweets = TimelineTweet.ExtractTimelineTweets(rootElm);
66             Assert.Equal(3, timelineTweets.Length);
67         }
68
69         [Fact]
70         public void ToStatus_WithTwitterPostFactory_SimpleTweet_Test()
71         {
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>());
77
78             Assert.Equal("1613784711020826626", post.StatusId.Id);
79             Assert.Equal(40480664L, post.UserId);
80             Assert.False(post.IsPromoted);
81         }
82
83         [Fact]
84         public void ToStatus_WithTwitterPostFactory_TweetWithMedia_Test()
85         {
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>());
91
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);
99         }
100
101         [Fact]
102         public void ToStatus_WithTwitterPostFactory_RetweetedTweet_Test()
103         {
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>());
109
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);
114         }
115
116         [Fact]
117         public void ToStatus_WithTwitterPostFactory_TweetWithVisibility_Test()
118         {
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>());
124
125             Assert.Equal("1602775353088524288", post.StatusId.Id);
126             Assert.Equal(357750891L, post.UserId);
127         }
128
129         [Fact]
130         public void ToStatus_WithTwitterPostFactory_SelfThread_Test()
131         {
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>());
137
138             Assert.Equal("1511751702684499968", post.StatusId.Id);
139             Assert.Equal(40480664L, post.UserId);
140         }
141
142         [Fact]
143         public void ToStatus_WithTwitterPostFactory_PromotedTweet_Test()
144         {
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>());
150
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);
155         }
156
157         [Fact]
158         public void ToStatus_TweetTombstone_Test()
159         {
160             var rootElm = this.LoadResponseDocument("TimelineTweet_TweetTombstone.json");
161             var timelineTweet = new TimelineTweet(rootElm);
162
163             Assert.True(timelineTweet.IsTombstone);
164             var ex = Assert.Throws<WebApiException>(
165                 () => timelineTweet.ToTwitterStatus()
166             );
167             Assert.Equal("This Post is from a suspended account. Learn more", ex.Message);
168         }
169     }
170 }