twitter_concern_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require 'rails_helper'
  2. describe TwitterConcern do
  3. class TestTwitterAgent < Agent
  4. include TwitterConcern
  5. end
  6. before do
  7. allow(TestTwitterAgent).to receive(:valid_type?).with("TestTwitterAgent") { true }
  8. @agent = TestTwitterAgent.create(name: "some agent") { |agent|
  9. agent.user = users(:bob)
  10. }
  11. end
  12. describe 'format_tweet' do
  13. let(:tweet_hash) {
  14. {
  15. created_at: "Wed Mar 01 01:52:07 +0000 2023",
  16. id: 9_000_000_000_000_000_000,
  17. id_str: "9000000000000000000",
  18. full_text: "Test &gt; Test &amp; https://t.co/XXXXXXXXXX &amp; Test &lt; https://t.co/YYYYYYYYYY",
  19. truncated: false,
  20. display_text_range: [
  21. 0,
  22. 84
  23. ],
  24. entities: {
  25. hashtags: [],
  26. symbols: [],
  27. user_mentions: [],
  28. urls: [
  29. {
  30. url: "https://t.co/XXXXXXXXXX",
  31. expanded_url: "https://example.org/foo/bar/baz.html",
  32. display_url: "example.org/foo/bar/baz…",
  33. indices: [
  34. 21,
  35. 44
  36. ]
  37. },
  38. {
  39. url: "https://t.co/YYYYYYYYYY",
  40. expanded_url: "https://example.com/quux/",
  41. display_url: "example.org/quux/",
  42. indices: [
  43. 61,
  44. 84
  45. ]
  46. }
  47. ]
  48. },
  49. }
  50. }
  51. let(:expected) {
  52. {
  53. created_at: "Wed Mar 01 01:52:07 +0000 2023",
  54. id: 9_000_000_000_000_000_000,
  55. id_str: "9000000000000000000",
  56. full_text: "Test > Test & https://t.co/XXXXXXXXXX & Test < https://t.co/YYYYYYYYYY",
  57. expanded_text: "Test > Test & https://example.org/foo/bar/baz.html & Test < https://example.com/quux/",
  58. truncated: false,
  59. display_text_range: [
  60. 0,
  61. 84
  62. ],
  63. entities: {
  64. hashtags: [],
  65. symbols: [],
  66. user_mentions: [],
  67. urls: [
  68. {
  69. url: "https://t.co/XXXXXXXXXX",
  70. expanded_url: "https://example.org/foo/bar/baz.html",
  71. display_url: "example.org/foo/bar/baz…",
  72. indices: [
  73. 21,
  74. 44
  75. ]
  76. },
  77. {
  78. url: "https://t.co/YYYYYYYYYY",
  79. expanded_url: "https://example.com/quux/",
  80. display_url: "example.org/quux/",
  81. indices: [
  82. 61,
  83. 84
  84. ]
  85. }
  86. ]
  87. },
  88. }
  89. }
  90. let(:input) { tweet_hash }
  91. subject { @agent.send(:format_tweet, input) }
  92. it "formats a tweet" do
  93. expect(subject).to eq(expected)
  94. end
  95. context "when a string key hash is given" do
  96. let(:input) { tweet_hash.deep_stringify_keys }
  97. it "formats a tweet" do
  98. expect(subject).to eq(expected)
  99. end
  100. end
  101. context "when a Twitter::Tweet object is given" do
  102. let(:input) { Twitter::Tweet.new(tweet_hash) }
  103. let(:expected) { super().then { |attrs| attrs.update(text: attrs[:full_text]) } }
  104. it "formats a tweet" do
  105. expect(subject).to eq(expected)
  106. end
  107. end
  108. context "when nil is given" do
  109. let(:input) { nil }
  110. it "raises an exception" do
  111. expect { subject }.to raise_exception(TypeError)
  112. end
  113. end
  114. end
  115. end