Pigeon-Ruby/spec/pigeon/bundle_spec.rb

60 lines
1.7 KiB
Ruby
Raw Normal View History

require "spec_helper"
RSpec.describe Pigeon::Message do
before(:each) do
2020-04-01 13:07:39 +00:00
p = Pigeon::DEFAULT_BUNDLE_PATH
File.delete(p) if File.file?(p)
end
2020-04-17 13:55:18 +00:00
let(:db) do
db = Pigeon::Database.new
db.reset_database
2020-04-17 13:55:18 +00:00
db
end
2020-04-01 13:07:39 +00:00
def create_fake_messages
blobs = [db.add_message(db.add_blob("one"), { "a" => "b" }),
db.add_message("a", { db.add_blob("two") => "b" }),
db.add_message("a", { "b" => db.add_blob("three") })]
normal = (1..10)
.to_a
2020-04-25 15:11:25 +00:00
.map { |_n| { "foo" => ["bar", "123", SecureRandom.uuid].sample } }
.map { |d| db.add_message(SecureRandom.uuid, d) }
blobs + normal
2020-04-01 13:07:39 +00:00
end
it "creates a bundle" do
expected_bundle = create_fake_messages.map(&:render).join("\n\n") + "\n"
2020-04-24 11:20:02 +00:00
db.export_bundle
actual_bundle = File.read(File.join(Pigeon::DEFAULT_BUNDLE_PATH, "gossip.pgn"))
expect(expected_bundle).to eq(actual_bundle)
end
2020-04-01 13:07:39 +00:00
it "does not crash when ingesting old messages" do
2020-04-01 13:07:39 +00:00
create_fake_messages
2020-04-24 11:20:02 +00:00
db.export_bundle
db.import_bundle
2020-04-01 13:07:39 +00:00
end
2020-04-20 02:32:37 +00:00
it "does not ingest messages from blocked peers" do
db.reset_database
antagonist = "@PPJQ3Q36W258VQ1NKYY2G7VW24J8NMAACHXCD83GCQ3K8F4C9X2G.ed25519"
db.block_peer(antagonist)
2020-04-24 11:20:02 +00:00
db.import_bundle("./spec/fixtures/x")
expect(db.all_messages.count).to eq(0)
end
it "ingests a bundle's blobs" do
db.reset_database
2020-04-25 14:47:58 +00:00
db.add_message(db.add_blob(File.read("a.gif")), {
db.add_blob(File.read("b.gif")) => db.add_blob(File.read("c.gif")),
})
db.export_bundle("./spec/fixtures/has_blobs")
2020-04-25 15:11:25 +00:00
warn("The directory structure is not correct.")
exit(1)
2020-04-24 11:20:02 +00:00
db.import_bundle("./spec/fixtures/has_blobs")
expect(db.all_messages.count).to eq(0)
2020-04-20 02:32:37 +00:00
end
end