|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe ProcessSingleSiteJob do |
| 4 | + subject(:job) { described_class.new } |
| 5 | + |
| 6 | + let(:team) { create(:team) } |
| 7 | + let(:site_data) { { "url" => "https://example.com/", "name" => "Example Site", "tag_names" => ["tag1", "tag2"] } } |
| 8 | + let(:tag_ids) { [] } |
| 9 | + |
| 10 | + describe "#perform" do |
| 11 | + context "when site does not exist" do |
| 12 | + it "creates a new site with tags" do |
| 13 | + expect { |
| 14 | + job.perform(site_data, team.id, tag_ids) |
| 15 | + }.to change(Site, :count).by(1) |
| 16 | + |
| 17 | + site = Site.last |
| 18 | + expect(site.url).to eq("https://example.com/") |
| 19 | + expect(site.name).to eq("Example Site") |
| 20 | + expect(site.tags.pluck(:name)).to match_array(["tag1", "tag2"]) |
| 21 | + end |
| 22 | + |
| 23 | + it "creates tags if they don't exist" do |
| 24 | + expect { |
| 25 | + job.perform(site_data, team.id, tag_ids) |
| 26 | + }.to change(Tag, :count).by(2) |
| 27 | + end |
| 28 | + |
| 29 | + context "with form tag_ids" do |
| 30 | + let!(:form_tag) { create(:tag, name: "form_tag", team: team) } |
| 31 | + let(:tag_ids) { [form_tag.id] } |
| 32 | + |
| 33 | + it "combines CSV tags with form tags" do |
| 34 | + job.perform(site_data, team.id, tag_ids) |
| 35 | + |
| 36 | + site = Site.last |
| 37 | + expect(site.tags.pluck(:name)).to match_array(["tag1", "tag2", "form_tag"]) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context "without name in CSV" do |
| 42 | + let(:site_data) { { "url" => "https://example.com/", "name" => nil, "tag_names" => [] } } |
| 43 | + |
| 44 | + it "creates site without name" do |
| 45 | + job.perform(site_data, team.id, tag_ids) |
| 46 | + |
| 47 | + site = Site.last |
| 48 | + expect(site.name).to be_nil |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + context "when site already exists" do |
| 54 | + let!(:existing_site) { create(:site, url: "https://example.com/", name: nil, team: team) } |
| 55 | + let!(:existing_tag) { create(:tag, name: "existing_tag", team: team) } |
| 56 | + |
| 57 | + before do |
| 58 | + existing_site.tags << existing_tag |
| 59 | + end |
| 60 | + |
| 61 | + it "does not create a new site" do |
| 62 | + expect { |
| 63 | + job.perform(site_data, team.id, tag_ids) |
| 64 | + }.not_to change(Site, :count) |
| 65 | + end |
| 66 | + |
| 67 | + it "merges tags with existing tags" do |
| 68 | + job.perform(site_data, team.id, tag_ids) |
| 69 | + |
| 70 | + existing_site.reload |
| 71 | + expect(existing_site.tags.pluck(:name)).to match_array(["existing_tag", "tag1", "tag2"]) |
| 72 | + end |
| 73 | + |
| 74 | + it "sets name if site has no name" do |
| 75 | + job.perform(site_data, team.id, tag_ids) |
| 76 | + |
| 77 | + existing_site.reload |
| 78 | + expect(existing_site.name).to eq("Example Site") |
| 79 | + end |
| 80 | + |
| 81 | + it "does not overwrite existing name" do |
| 82 | + existing_site.update!(name: "Old Name") |
| 83 | + |
| 84 | + job.perform(site_data, team.id, tag_ids) |
| 85 | + |
| 86 | + existing_site.reload |
| 87 | + expect(existing_site.name).to eq("Old Name") |
| 88 | + end |
| 89 | + |
| 90 | + it "triggers audit on existing site" do |
| 91 | + expect(existing_site).to receive(:audit!) |
| 92 | + |
| 93 | + job.perform(site_data, team.id, tag_ids) |
| 94 | + end |
| 95 | + |
| 96 | + context "with form tag_ids" do |
| 97 | + let!(:form_tag) { create(:tag, name: "form_tag", team: team) } |
| 98 | + let(:tag_ids) { [form_tag.id] } |
| 99 | + |
| 100 | + it "combines all tags" do |
| 101 | + job.perform(site_data, team.id, tag_ids) |
| 102 | + |
| 103 | + existing_site.reload |
| 104 | + expect(existing_site.tags.pluck(:name)).to match_array(["existing_tag", "tag1", "tag2", "form_tag"]) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + context "with duplicate tags in CSV" do |
| 110 | + let(:site_data) { { "url" => "https://example.com/", "name" => "Example Site", "tag_names" => ["tag1", "tag1", "tag2"] } } |
| 111 | + |
| 112 | + it "deduplicates tags" do |
| 113 | + expect { |
| 114 | + job.perform(site_data, team.id, tag_ids) |
| 115 | + }.to change(Tag, :count).by(1) |
| 116 | + |
| 117 | + site = Site.last |
| 118 | + expect(site.tags.pluck(:name)).to match_array(["tag1", "tag2"]) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context "with existing tags" do |
| 123 | + let!(:existing_tag) { create(:tag, name: "tag1", team: team) } |
| 124 | + |
| 125 | + it "reuses existing tags instead of creating new ones" do |
| 126 | + expect { |
| 127 | + job.perform(site_data, team.id, tag_ids) |
| 128 | + }.to change(Tag, :count).by(1) |
| 129 | + |
| 130 | + site = Site.last |
| 131 | + expect(site.tags).to include(existing_tag) |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments