|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +require "json" |
| 6 | +require "tempfile" |
| 7 | +require_relative "../lib/deprecation_tracker/shard_merger" |
| 8 | + |
| 9 | +RSpec.describe DeprecationTracker::ShardMerger do |
| 10 | + let(:base_path) do |
| 11 | + dir = Dir.tmpdir |
| 12 | + File.join(dir, "shitlist-#{Process.pid}-#{rand(1000)}.json") |
| 13 | + end |
| 14 | + |
| 15 | + after do |
| 16 | + FileUtils.rm_f(base_path) |
| 17 | + Dir.glob("#{base_path.chomp('.json')}.node-*.json").each { |f| FileUtils.rm_f(f) } |
| 18 | + end |
| 19 | + |
| 20 | + def write_shard(index, data) |
| 21 | + path = "#{base_path.chomp('.json')}.node-#{index}.json" |
| 22 | + File.write(path, JSON.pretty_generate(data)) |
| 23 | + path |
| 24 | + end |
| 25 | + |
| 26 | + subject { described_class.new(base_path) } |
| 27 | + |
| 28 | + it "merges multiple shard files into the canonical file" do |
| 29 | + write_shard(0, { "bucket 1" => ["a"], "bucket 2" => ["b"] }) |
| 30 | + write_shard(1, { "bucket 3" => ["c"] }) |
| 31 | + |
| 32 | + output = subject.merge |
| 33 | + result = output[:result] |
| 34 | + |
| 35 | + expect(result).to eq( |
| 36 | + "bucket 1" => ["a"], |
| 37 | + "bucket 2" => ["b"], |
| 38 | + "bucket 3" => ["c"] |
| 39 | + ) |
| 40 | + expect(output[:shards]).to eq(2) |
| 41 | + expect(JSON.parse(File.read(base_path))).to eq(result) |
| 42 | + end |
| 43 | + |
| 44 | + it "deep-merges overlapping buckets by concatenating and sorting messages" do |
| 45 | + write_shard(0, { "bucket 1" => ["b", "a"] }) |
| 46 | + write_shard(1, { "bucket 1" => ["c", "a"] }) |
| 47 | + |
| 48 | + result = subject.merge[:result] |
| 49 | + |
| 50 | + expect(result).to eq("bucket 1" => ["a", "a", "b", "c"]) |
| 51 | + end |
| 52 | + |
| 53 | + it "warns and returns empty result when no shards exist" do |
| 54 | + expect { output = subject.merge }.to output(/No shards found/).to_stderr |
| 55 | + |
| 56 | + output = subject.merge |
| 57 | + |
| 58 | + expect(output[:result]).to eq({}) |
| 59 | + expect(output[:shards]).to eq(0) |
| 60 | + expect(File.exist?(base_path)).to be false |
| 61 | + end |
| 62 | + |
| 63 | + it "warns and returns empty result when directory does not exist" do |
| 64 | + merger = described_class.new("/nonexistent/path/shitlist.json") |
| 65 | + |
| 66 | + expect { output = merger.merge }.to output(/Directory does not exist/).to_stderr |
| 67 | + |
| 68 | + output = merger.merge |
| 69 | + |
| 70 | + expect(output[:result]).to eq({}) |
| 71 | + expect(output[:shards]).to eq(0) |
| 72 | + end |
| 73 | + |
| 74 | + it "handles a single shard file" do |
| 75 | + write_shard(0, { "bucket 1" => ["a"] }) |
| 76 | + |
| 77 | + output = subject.merge |
| 78 | + |
| 79 | + expect(output[:result]).to eq("bucket 1" => ["a"]) |
| 80 | + expect(output[:shards]).to eq(1) |
| 81 | + end |
| 82 | + |
| 83 | + it "deletes shard files when delete_shards is true" do |
| 84 | + shard0 = write_shard(0, { "bucket 1" => ["a"] }) |
| 85 | + shard1 = write_shard(1, { "bucket 2" => ["b"] }) |
| 86 | + |
| 87 | + merger = described_class.new(base_path, delete_shards: true) |
| 88 | + merger.merge |
| 89 | + |
| 90 | + expect(File.exist?(shard0)).to be false |
| 91 | + expect(File.exist?(shard1)).to be false |
| 92 | + expect(File.exist?(base_path)).to be true |
| 93 | + end |
| 94 | + |
| 95 | + it "preserves shard files by default" do |
| 96 | + shard0 = write_shard(0, { "bucket 1" => ["a"] }) |
| 97 | + |
| 98 | + subject.merge |
| 99 | + |
| 100 | + expect(File.exist?(shard0)).to be true |
| 101 | + end |
| 102 | + |
| 103 | + it "sorts buckets alphabetically" do |
| 104 | + write_shard(0, { "z_bucket" => ["a"] }) |
| 105 | + write_shard(1, { "a_bucket" => ["b"] }) |
| 106 | + |
| 107 | + result = subject.merge[:result] |
| 108 | + |
| 109 | + expect(result.keys).to eq(["a_bucket", "z_bucket"]) |
| 110 | + end |
| 111 | + |
| 112 | + it "raises an error for invalid JSON in a shard file" do |
| 113 | + shard_path = "#{base_path.chomp('.json')}.node-0.json" |
| 114 | + File.write(shard_path, "not valid json") |
| 115 | + |
| 116 | + expect { subject.merge }.to raise_error(/Invalid JSON in shard file/) |
| 117 | + end |
| 118 | +end |
0 commit comments