ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

131 lines
3.5 KiB

  1. require 'rails_helper'
  2. RSpec.describe Youtube::ApiClient do
  3. let(:api_key) { 'test-api-key' }
  4. let(:client) { described_class.new(api_key:) }
  5. describe '#search_videos' do
  6. it 'calls YouTube search API with expected params' do
  7. published_after = Time.zone.parse('2026-05-01 00:00:00')
  8. published_before = Time.zone.parse('2026-05-02 00:00:00')
  9. expect(client).to receive(:get_json).with(
  10. '/search',
  11. {
  12. part: 'snippet',
  13. type: 'video',
  14. q: 'ぼざろクリーチャー',
  15. order: 'date',
  16. maxResults: 50,
  17. regionCode: 'JP',
  18. relevanceLanguage: 'ja',
  19. publishedAfter: published_after.iso8601,
  20. publishedBefore: published_before.iso8601,
  21. pageToken: 'NEXT'
  22. }
  23. ).and_return({ 'items' => [] })
  24. client.search_videos(
  25. q: 'ぼざろクリーチャー',
  26. published_after:,
  27. published_before:,
  28. page_token: 'NEXT'
  29. )
  30. end
  31. it 'omits nil optional params' do
  32. expect(client).to receive(:get_json).with(
  33. '/search',
  34. hash_excluding(:publishedAfter, :publishedBefore, :pageToken)
  35. ).and_return({ 'items' => [] })
  36. client.search_videos(q: 'ぼざろクリーチャー')
  37. end
  38. end
  39. describe '#videos' do
  40. it 'returns empty items when ids are empty' do
  41. expect(client).not_to receive(:get_json)
  42. expect(client.videos([])).to eq({ 'items' => [] })
  43. end
  44. it 'calls videos API with comma separated ids' do
  45. expect(client).to receive(:get_json).with(
  46. '/videos',
  47. {
  48. part: 'snippet,status,contentDetails',
  49. id: 'video-1,video-2'
  50. }
  51. ).and_return({ 'items' => [] })
  52. client.videos(['video-1', 'video-2'])
  53. end
  54. end
  55. describe '#playlist_items' do
  56. it 'calls playlistItems API with page token' do
  57. expect(client).to receive(:get_json).with(
  58. '/playlistItems',
  59. {
  60. part: 'snippet,contentDetails,status',
  61. playlistId: 'PL123',
  62. maxResults: 50,
  63. pageToken: 'NEXT'
  64. }
  65. ).and_return({ 'items' => [] })
  66. client.playlist_items(playlist_id: 'PL123', page_token: 'NEXT')
  67. end
  68. it 'omits page token when nil' do
  69. expect(client).to receive(:get_json).with(
  70. '/playlistItems',
  71. {
  72. part: 'snippet,contentDetails,status',
  73. playlistId: 'PL123',
  74. maxResults: 50
  75. }
  76. ).and_return({ 'items' => [] })
  77. client.playlist_items(playlist_id: 'PL123')
  78. end
  79. end
  80. describe '#channel' do
  81. it 'calls channels API by id' do
  82. expect(client).to receive(:get_json).with(
  83. '/channels',
  84. {
  85. part: 'snippet,contentDetails',
  86. id: 'UC123'
  87. }
  88. ).and_return({ 'items' => [] })
  89. client.channel(id: 'UC123')
  90. end
  91. it 'calls channels API by handle' do
  92. expect(client).to receive(:get_json).with(
  93. '/channels',
  94. {
  95. part: 'snippet,contentDetails',
  96. forHandle: '@some_handle'
  97. }
  98. ).and_return({ 'items' => [] })
  99. client.channel(handle: '@some_handle')
  100. end
  101. it 'raises when neither id nor handle is given' do
  102. expect { client.channel }.to raise_error(ArgumentError, 'id or handle is required')
  103. end
  104. it 'raises when both id and handle are given' do
  105. expect do
  106. client.channel(id: 'UC123', handle: '@some_handle')
  107. end.to raise_error(ArgumentError, 'id or handle is required')
  108. end
  109. end
  110. end