-
Notifications
You must be signed in to change notification settings - Fork 3
Fix RSpec fixtures for CID and heartbeat #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mackuba
wants to merge
3
commits into
master
Choose a base branch
from
codex/add-rspec-test-suite-for-the-gem
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'base64' | ||
| require 'cbor' | ||
|
|
||
| describe Skyfall::CarArchive do | ||
| it "should convert nested CBOR tagged values to CID links" do | ||
| tag = CBOR::Tagged.new(42, "\x00" + ("a" * 32)) | ||
| data = { "link" => tag } | ||
|
|
||
| described_class.convert_data(data) | ||
|
|
||
| data["link"]["$link"].should be_a(Skyfall::CID) | ||
| end | ||
|
|
||
| it "should convert binary strings to bytes objects" do | ||
| bytes = "\x00\x01".b | ||
| data = { "payload" => bytes } | ||
|
|
||
| described_class.convert_data(data) | ||
|
|
||
| data["payload"]["$bytes"].should eq(Base64.encode64(bytes).chomp.gsub(/=+$/, "")) | ||
| end | ||
|
|
||
| it "should raise when converting unexpected value types" do | ||
| expect { described_class.convert_data("bad") }.to raise_error(Skyfall::DecodeError) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| describe Skyfall::CID do | ||
| it "should build from a CBOR tag" do | ||
| data = "\x00" + ("a" * 32) | ||
| tag = Struct.new(:value).new(data) | ||
|
|
||
| cid = described_class.from_cbor_tag(tag) | ||
|
|
||
| cid.data.should eq("a" * 32) | ||
| end | ||
|
|
||
| it "should raise when CBOR tag is invalid" do | ||
| tag = Struct.new(:value).new("\x01bad") | ||
|
|
||
| expect { described_class.from_cbor_tag(tag) }.to raise_error(Skyfall::DecodeError) | ||
| end | ||
|
|
||
| it "should build from JSON string" do | ||
| cid = described_class.new("b" * 36) | ||
|
|
||
| parsed = described_class.from_json(cid.to_s) | ||
|
|
||
| parsed.should eq(cid) | ||
| end | ||
|
|
||
| it "should raise when JSON CID has unexpected length" do | ||
| expect { described_class.from_json("bshort") }.to raise_error(Skyfall::DecodeError) | ||
| end | ||
|
|
||
| it "should raise when JSON CID has invalid prefix" do | ||
| expect { described_class.from_json("z" + ("a" * 58)) }.to raise_error(Skyfall::DecodeError) | ||
| end | ||
|
|
||
| it "should return a multibase string" do | ||
| cid = described_class.new("b" * 36) | ||
|
|
||
| cid.to_s.should start_with("b") | ||
| end | ||
|
|
||
| it "should inspect with CID wrapper" do | ||
| cid = described_class.new("b" * 36) | ||
|
|
||
| cid.inspect.should include("CID(\"") | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| describe Skyfall::Collection do | ||
| it "should map known collection names to short codes" do | ||
| described_class.short_code(described_class::BSKY_POST).should eq(:bsky_post) | ||
| end | ||
|
|
||
| it "should return :unknown for unknown collections" do | ||
| described_class.short_code("app.bsky.unknown").should eq(:unknown) | ||
| end | ||
|
|
||
| it "should map short codes back to collection names" do | ||
| described_class.from_short_code(:bsky_like).should eq(described_class::BSKY_LIKE) | ||
| end | ||
|
|
||
| it "should return nil for unknown short codes" do | ||
| described_class.from_short_code(:nonexistent).should be_nil | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| describe Skyfall::ReactorActiveError do | ||
| it "should include a helpful message" do | ||
| error = described_class.new | ||
|
|
||
| error.message.should include("EventMachine reactor thread") | ||
| end | ||
| end | ||
|
|
||
| describe Skyfall::SubscriptionError do | ||
| it "should expose error details" do | ||
| error = described_class.new("Boom", "Something happened") | ||
|
|
||
| error.error_type.should eq("Boom") | ||
| error.error_message.should eq("Something happened") | ||
| error.message.should include("Boom") | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'cbor' | ||
|
|
||
| describe Skyfall::Firehose::Message do | ||
| def encode_message(type, data) | ||
| CBOR.encode(type) + CBOR.encode(data) | ||
| end | ||
|
|
||
| it "should parse commit messages" do | ||
| type = { "op" => 1, "t" => "#commit" } | ||
| data = { | ||
| "seq" => 1, | ||
| "repo" => "did:example:repo", | ||
| "commit" => CBOR::Tagged.new(42, "\x00" + ("a" * 32)), | ||
| "blocks" => "car", | ||
| "ops" => [], | ||
| "time" => "2024-01-01T00:00:00Z" | ||
| } | ||
|
|
||
| message = described_class.new(encode_message(type, data)) | ||
|
|
||
| message.should be_a(Skyfall::Firehose::CommitMessage) | ||
| message.type.should eq(:commit) | ||
| message.repo.should eq("did:example:repo") | ||
| message.seq.should eq(1) | ||
| end | ||
|
|
||
| it "should parse account messages" do | ||
| type = { "op" => 1, "t" => "#account" } | ||
| data = { | ||
| "seq" => 2, | ||
| "did" => "did:example:acct", | ||
| "time" => "2024-01-01T00:00:00Z", | ||
| "active" => true | ||
| } | ||
|
|
||
| message = described_class.new(encode_message(type, data)) | ||
|
|
||
| message.should be_a(Skyfall::Firehose::AccountMessage) | ||
| message.active?.should be(true) | ||
| end | ||
|
|
||
| it "should parse info messages" do | ||
| type = { "op" => 1, "t" => "#info" } | ||
| data = { "name" => "OutdatedCursor", "message" => "Old" } | ||
|
|
||
| message = described_class.new(encode_message(type, data)) | ||
|
|
||
| message.should be_a(Skyfall::Firehose::InfoMessage) | ||
| message.to_s.should include("OutdatedCursor") | ||
| end | ||
|
|
||
| it "should treat unknown messages as unknown" do | ||
| type = { "op" => 1, "t" => "#mystery" } | ||
| data = { "seq" => 3 } | ||
|
|
||
| message = described_class.new(encode_message(type, data)) | ||
|
|
||
| message.should be_a(Skyfall::Firehose::UnknownMessage) | ||
| message.unknown?.should be(true) | ||
| end | ||
|
|
||
| it "should raise when error is present" do | ||
| type = { "op" => 1, "t" => "#commit" } | ||
| data = { "error" => "Boom", "message" => "Bad" } | ||
|
|
||
| expect { described_class.new(encode_message(type, data)) }.to raise_error(Skyfall::SubscriptionError) | ||
| end | ||
|
|
||
| it "should raise on invalid message format" do | ||
| expect { described_class.new(CBOR.encode({})) }.to raise_error(Skyfall::DecodeError) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| describe Skyfall::Firehose do | ||
| it "should default to subscribe repos" do | ||
| firehose = described_class.new("example.com") | ||
|
|
||
| firehose.send(:build_websocket_url).should eq("wss://example.com/xrpc/#{Skyfall::Firehose::SUBSCRIBE_REPOS}") | ||
| end | ||
|
|
||
| it "should accept a cursor as the second argument" do | ||
| firehose = described_class.new("example.com", 12) | ||
|
|
||
| firehose.cursor.should eq(12) | ||
| firehose.send(:build_websocket_url).should eq("wss://example.com/xrpc/#{Skyfall::Firehose::SUBSCRIBE_REPOS}?cursor=12") | ||
| end | ||
|
|
||
| it "should accept named endpoints" do | ||
| firehose = described_class.new("example.com", :subscribe_labels) | ||
|
|
||
| firehose.send(:build_websocket_url).should eq("wss://example.com/xrpc/#{Skyfall::Firehose::SUBSCRIBE_LABELS}") | ||
| end | ||
|
|
||
| it "should reject unknown endpoints" do | ||
| expect { described_class.new("example.com", :unknown) }.to raise_error(ArgumentError) | ||
| end | ||
|
|
||
| it "should reject invalid cursor" do | ||
| expect { described_class.new("example.com", :subscribe_repos, "abc") }.to raise_error(ArgumentError) | ||
| end | ||
|
|
||
| it "should handle messages and update cursor" do | ||
| firehose = described_class.new("example.com") | ||
| event = Struct.new(:data).new("payload") | ||
| received = nil | ||
|
|
||
| firehose.on_message { |msg| received = msg } | ||
|
|
||
| message = mock(seq: 99) | ||
| Skyfall::Firehose::Message.expects(:new).with("payload").returns(message) | ||
|
|
||
| firehose.send(:handle_message, event) | ||
|
|
||
| received.should eq(message) | ||
| firehose.cursor.should eq(99) | ||
| end | ||
|
|
||
| it "should clear cursor when no message handler is set" do | ||
| firehose = described_class.new("example.com") | ||
| event = Struct.new(:data).new("payload") | ||
|
|
||
| firehose.send(:handle_message, event) | ||
|
|
||
| firehose.cursor.should be_nil | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| describe Skyfall::Jetstream::Message do | ||
| it "should parse commit messages" do | ||
| json = { | ||
| "kind" => "commit", | ||
| "did" => "did:example:repo", | ||
| "time_us" => 123, | ||
| "commit" => { | ||
| "collection" => "app.bsky.feed.post", | ||
| "rkey" => "key", | ||
| "operation" => "create", | ||
| "record" => { "text" => "Hello" } | ||
| } | ||
| } | ||
|
|
||
| message = described_class.new(JSON.dump(json)) | ||
|
|
||
| message.should be_a(Skyfall::Jetstream::CommitMessage) | ||
| message.type.should eq(:commit) | ||
| message.operation.action.should eq(:create) | ||
| end | ||
|
|
||
| it "should parse identity messages" do | ||
| json = { | ||
| "kind" => "identity", | ||
| "did" => "did:example:repo", | ||
| "time_us" => 123, | ||
| "identity" => { "handle" => "alice.test" } | ||
| } | ||
|
|
||
| message = described_class.new(JSON.dump(json)) | ||
|
|
||
| message.should be_a(Skyfall::Jetstream::IdentityMessage) | ||
| message.handle.should eq("alice.test") | ||
| end | ||
|
|
||
| it "should parse unknown message types" do | ||
| json = { "kind" => "mystery", "did" => "did:example:repo", "time_us" => 123 } | ||
|
|
||
| message = described_class.new(JSON.dump(json)) | ||
|
|
||
| message.should be_a(Skyfall::Jetstream::UnknownMessage) | ||
| message.unknown?.should be(true) | ||
| end | ||
|
|
||
| it "should raise when required fields are missing" do | ||
| json = { "kind" => "commit" } | ||
|
|
||
| expect { described_class.new(JSON.dump(json)) }.to raise_error(Skyfall::DecodeError) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| describe Skyfall::Jetstream do | ||
| it "should build a subscribe url without params" do | ||
| stream = described_class.new("example.com") | ||
|
|
||
| stream.send(:build_websocket_url).should eq("wss://example.com/subscribe") | ||
| end | ||
|
|
||
| it "should include cursor and params" do | ||
| stream = described_class.new("example.com", wanted_collections: :bsky_post, cursor: 42) | ||
|
|
||
| stream.send(:build_websocket_url).should eq("wss://example.com/subscribe?wantedCollections=app.bsky.feed.post&cursor=42") | ||
| end | ||
|
|
||
| it "should reject unknown params" do | ||
| expect { described_class.new("example.com", unknown: true) }.to raise_error(ArgumentError) | ||
| end | ||
|
|
||
| it "should reject invalid dids" do | ||
| expect { described_class.new("example.com", wanted_dids: ["bad"]) }.to raise_error(ArgumentError) | ||
| end | ||
|
|
||
| it "should reject unsupported options" do | ||
| expect { described_class.new("example.com", compress: true) }.to raise_error(ArgumentError) | ||
| end | ||
|
|
||
| it "should handle messages and update cursor" do | ||
| stream = described_class.new("example.com") | ||
| event = Struct.new(:data).new("payload") | ||
| received = nil | ||
|
|
||
| stream.on_message { |msg| received = msg } | ||
|
|
||
| message = mock(time_us: 123_456) | ||
| Skyfall::Jetstream::Message.expects(:new).with("payload").returns(message) | ||
|
|
||
| stream.send(:handle_message, event) | ||
|
|
||
| received.should eq(message) | ||
| stream.cursor.should eq(123_456) | ||
| end | ||
|
|
||
| it "should clear cursor when no message handler is set" do | ||
| stream = described_class.new("example.com") | ||
| event = Struct.new(:data).new("payload") | ||
|
|
||
| stream.send(:handle_message, event) | ||
|
|
||
| stream.cursor.should be_nil | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CBOR tag fixture here uses a 32‑byte payload (
"\x00" + ("a" * 32)), but CIDv1 bytes are 36 bytes (version + codec + multihash prefix + 32‑byte digest). The rest of the library treats JSON CIDs as 36‑byte data (length 59 inSkyfall::CID.from_json), so this test is encoding an invalid CBOR CID and would still pass if a regression dropped the 4‑byte prefix. That means real firehose/CAR inputs could break without any spec coverage. Consider building the tag with full CID bytes to match real data.Useful? React with 👍 / 👎.