# frozen_string_literal: true require 'test_helper' require 'fic_tracker/backends/ao3/backend' class TestAo3 < Minitest::Test def setup stub_request(:get, 'https://archiveofourown.org/works/1234567890?view_adult=true').to_return( status: 302, headers: { 'Location' => 'https://archiveofourown.org/works/1234567890/chapters/1234567890?view_adult=true' }, ) stub_request(:get, 'https://archiveofourown.org/works/1234567890/navigate').to_return( status: 200, body: File.read('test/fixtures/ao3/story_nav.html') ) stub_request(:get, 'https://archiveofourown.org/works/1234567890/chapters/1234567890?view_adult=true').to_return( status: 200, body: File.read('test/fixtures/ao3/story_chapter.html') ) stub_request(:get, 'https://archiveofourown.org/works/1234567890?view_full_work=true&view_adult=true').to_return( status: 200, body: File.read('test/fixtures/ao3/story_fullstory.html') ) stub_request(:get, 'https://archiveofourown.org/users/Example/pseuds/Example').to_return( status: 200, body: File.read('test/fixtures/ao3/user.html') ) stub_request(:get, 'https://archiveofourown.org/users/Example/pseuds/Example2').to_return( status: 200, body: File.read('test/fixtures/ao3/user.html') ) FicTracker.test! @backend = FicTracker::Backends::Ao3::Backend.new end def test_url_handling klass = @backend.class assert klass.supports_url? 'https://archiveofourown.org/works/1234567890' assert klass.supports_url? 'https://archiveofourown.org/works/1234567890/chapters/1234567890' assert klass.supports_url? 'https://archiveofourown.org/users/Example' assert klass.supports_url? 'https://archiveofourown.org/users/Example/pseuds/Example' refute klass.supports_url? 'https://example.com' end def test_slugging assert_equal '1234567890', @backend.parse_slug('https://archiveofourown.org/works/1234567890') assert_equal '1234567890', @backend.parse_slug('https://archiveofourown.org/works/1234567890/chapters/1234567890') assert_equal :story, @backend.parse_slug_type('https://archiveofourown.org/works/1234567890') assert_equal :story, @backend.parse_slug_type('https://archiveofourown.org/works/1234567890/chapters/1234567890') assert_equal 'Example', @backend.parse_slug('https://archiveofourown.org/users/Example') assert_equal 'Example', @backend.parse_slug('https://archiveofourown.org/users/Example/pseuds/Example') assert_equal :author, @backend.parse_slug_type('https://archiveofourown.org/users/Example') assert_equal :author, @backend.parse_slug_type('https://archiveofourown.org/users/Example/pseuds/Example') end def test_load_author aut = @backend.load_author 'Example' assert_nil aut.name assert_equal 'https://s3.amazonaws.com/otw-ao3-icons/icons/1363284/standard.png?1668353161', aut.image assert_equal 'https://archiveofourown.org/users/Example/pseuds/Example', aut.url end def test_load_chapter story = FicTracker::Models::Story.new(slug: '1234567890', backend: @backend) story.id = 0 chapter = @backend.load_chapter '1234567890', story assert_equal 1, chapter.index assert_equal "Gotham's Top Ten Memes", chapter.name assert_equal 'https://archiveofourown.org/works/1234567890/chapters/1234567890', chapter.url assert_equal 6213, chapter.content.size assert_equal 'text/html', chapter.content_type assert_equal 'f247c6ce215f2cf9398d6af29fafbb564220631a', chapter.etag end def test_load_story story = @backend.load_story '1234567890' assert_equal "Gotham's Top Ten Memes", story.name assert_equal 1, story.authors.size assert_equal 'Example', story.authors.first.slug assert_equal 364, story.synopsis.size assert_equal "https://archiveofourown.org/works/1234567890", story.url assert_equal 'en', story.language assert_equal 4, story.chapter_count assert_equal 1333, story.word_count refute story.completed? end def test_find_chapters story = @backend.find_chapters '1234567890' assert_equal 4, story.chapters.size assert_equal [22, 48, 35, 10], story.chapters.map(&:name).map(&:size) assert_equal '2019-06-19', story.chapters.first.published_at.strftime('%Y-%m-%d') end def test_load_full_story story = @backend.load_full_story '1234567890' assert_equal "Gotham's Top Ten Memes", story.name assert_equal 1, story.authors.size assert_equal 'Example', story.authors.first.slug assert_equal 364, story.synopsis.size assert_equal "https://archiveofourown.org/works/1234567890", story.url assert_equal 'en', story.language assert_equal 4, story.chapter_count assert_equal 1333, story.word_count refute story.completed? assert_equal 4, story.chapters.size assert_equal [22, 48, 35, 10], story.chapters.map(&:name).map(&:size) end end