require 'rails_helper' RSpec.describe PostMetadataFetcher do Response = Struct.new(:body) def fetch_with_published_time(value, url: 'https://example.com/video') html = <<~HTML HTML uri = URI.parse(url) allow(Preview::UrlSafety).to receive(:validate).with(url).and_return([uri, ['8.8.8.8']]) allow(Preview::HttpFetcher).to receive(:fetch).and_return(Response.new(html)) allow(Preview::HtmlMetadataExtractor).to receive(:extract) .and_return(title: 'title', image_url: nil) described_class.fetch(url) end it 'builds ranges explicitly for year, month, and day precision' do year = fetch_with_published_time('2024') month = fetch_with_published_time('2024-02') day = fetch_with_published_time('2024-02-03') expect(year).to include( original_created_from: Time.zone.local(2024, 1, 1).iso8601, original_created_before: Time.zone.local(2025, 1, 1).iso8601 ) expect(month).to include( original_created_from: Time.zone.local(2024, 2, 1).iso8601, original_created_before: Time.zone.local(2024, 3, 1).iso8601 ) expect(day).to include( original_created_from: Time.zone.local(2024, 2, 3).iso8601, original_created_before: Time.zone.local(2024, 2, 4).iso8601 ) end it 'preserves an input offset and fractional-second precision' do result = fetch_with_published_time('2024-02-03T12:34:56.123+02:30') expect(Time.iso8601(result.fetch(:original_created_from))) .to eq(Time.iso8601('2024-02-03T12:34:56.123+02:30')) expect( Time.iso8601(result.fetch(:original_created_before)) - Time.iso8601(result.fetch(:original_created_from)) ).to eq(0.001) expect(result.fetch(:original_created_from)).to include('.123000000') end it 'adds platform tags for known video URLs' do result = fetch_with_published_time('2024', url: 'https://youtu.be/abc123') expect(result.fetch(:tags)).to eq('動画 YouTube') end it 'returns nil dates for an invalid timestamp' do result = fetch_with_published_time('not-a-time') expect(result.fetch(:original_created_from)).to be_nil expect(result.fetch(:original_created_before)).to be_nil end end