|
1 |
| -require "rails_helper" |
| 1 | +RSpec.describe BatchPublisher do |
| 2 | + subject(:batch_publisher) do |
| 3 | + described_class.new( |
| 4 | + posts: [first_post, second_post], |
| 5 | + freefeed_client: freefeed_client, |
| 6 | + publisher_class: publisher_class |
| 7 | + ) |
| 8 | + end |
| 9 | + |
| 10 | + let(:freefeed_client) do |
| 11 | + Freefeed::Client.new( |
| 12 | + token: "TEST_TOKEN", |
| 13 | + base_url: "https://freefeed.test" |
| 14 | + ) |
| 15 | + end |
| 16 | + |
| 17 | + let(:publisher_class) { class_double(PostPublisher) } |
| 18 | + let(:publisher_instance) { instance_double(PostPublisher) } |
| 19 | + |
| 20 | + let(:first_post) { create(:post, state: "enqueued", feed: feed) } |
| 21 | + let(:second_post) { create(:post, state: "enqueued", feed: feed) } |
| 22 | + let(:feed) { create(:feed) } |
| 23 | + |
| 24 | + describe "#publish" do |
| 25 | + before do |
| 26 | + allow(publisher_class).to receive(:new) |
| 27 | + .with(post: anything, freefeed_client: freefeed_client) |
| 28 | + .and_return(publisher_instance) |
| 29 | + |
| 30 | + allow(publisher_instance).to receive(:publish) |
| 31 | + end |
| 32 | + |
| 33 | + context "when posts are enqueued" do |
| 34 | + it "attempts to publish all enqueued posts" do |
| 35 | + allow(publisher_class).to receive(:new) |
| 36 | + .with(post: first_post, freefeed_client: freefeed_client) |
| 37 | + .and_return(publisher_instance) |
| 38 | + |
| 39 | + allow(publisher_class).to receive(:new) |
| 40 | + .with(post: second_post, freefeed_client: freefeed_client) |
| 41 | + .and_return(publisher_instance) |
| 42 | + |
| 43 | + expect(publisher_instance).to receive(:publish).twice |
| 44 | + |
| 45 | + batch_publisher.publish |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context "when some posts are not enqueued" do |
| 50 | + let(:second_post) { create(:post, state: "draft", feed: feed) } |
| 51 | + |
| 52 | + it "only publishes enqueued posts" do |
| 53 | + expect(publisher_class).to receive(:new) |
| 54 | + .with(post: first_post, freefeed_client: freefeed_client) |
| 55 | + .once |
| 56 | + .and_return(publisher_instance) |
| 57 | + |
| 58 | + expect(publisher_instance).to receive(:publish).once |
| 59 | + |
| 60 | + expect(publisher_class).not_to receive(:new) |
| 61 | + .with(post: second_post, freefeed_client: freefeed_client) |
| 62 | + |
| 63 | + batch_publisher.publish |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context "when post state changes during processing" do |
| 68 | + it "skips posts that are no longer enqueued" do |
| 69 | + allow(first_post).to receive(:reload).and_return( |
| 70 | + create(:post, state: "published", feed: feed) |
| 71 | + ) |
| 72 | + |
| 73 | + expect(publisher_class).not_to receive(:new) |
| 74 | + .with(post: first_post, freefeed_client: freefeed_client) |
| 75 | + |
| 76 | + expect(publisher_class).to receive(:new) |
| 77 | + .with(post: second_post, freefeed_client: freefeed_client) |
| 78 | + .once |
| 79 | + .and_return(publisher_instance) |
| 80 | + |
| 81 | + expect(publisher_instance).to receive(:publish).once |
| 82 | + |
| 83 | + batch_publisher.publish |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments