Add plain text and coloured logging to the Client
Logs websocket events (connect, message, error and close events)
This commit is contained in:
62
spec/nostr/client/color_logger_spec.rb
Normal file
62
spec/nostr/client/color_logger_spec.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe Nostr::Client::ColorLogger do
|
||||
let(:client) { instance_spy(Nostr::Client) }
|
||||
let(:relay) { Nostr::Relay.new(url: 'ws://0.0.0.0:4180/', name: 'localhost') }
|
||||
let(:logger) { described_class.new }
|
||||
|
||||
describe '#attach_to' do
|
||||
it 'attaches event handlers to the client' do
|
||||
logger.attach_to(client)
|
||||
|
||||
aggregate_failures do
|
||||
expect(client).to have_received(:on).with(:connect)
|
||||
expect(client).to have_received(:on).with(:message)
|
||||
expect(client).to have_received(:on).with(:send)
|
||||
expect(client).to have_received(:on).with(:error)
|
||||
expect(client).to have_received(:on).with(:close)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_connect' do
|
||||
it 'logs connection to a relay' do
|
||||
expect do
|
||||
logger.on_connect(relay)
|
||||
end.to output("\e[32m\e[1mConnected to the relay\e[22m localhost (ws://0.0.0.0:4180/)\e[0m\n").to_stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_message' do
|
||||
it 'logs a message received from the relay' do
|
||||
message = 'Received message'
|
||||
expect { logger.on_message(message) }.to output("\e[32m\e[1m◄-\e[0m #{message}\n").to_stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_send' do
|
||||
it 'logs a message sent to the relay' do
|
||||
message = 'Sent message'
|
||||
expect { logger.on_send(message) }.to output("\e[32m\e[1m-►\e[0m #{message}\n").to_stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_error' do
|
||||
it 'logs an error message' do
|
||||
message = 'Error message'
|
||||
expect { logger.on_error(message) }.to output("\e[31m\e[1mError: \e[22m#{message}\e[0m\n").to_stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_close' do
|
||||
it 'logs a closure of connection with a relay' do
|
||||
code = '1000'
|
||||
reason = 'Connection closed'
|
||||
expect do
|
||||
logger.on_close(code, reason)
|
||||
end.to output("\e[31m\e[1mConnection closed: \e[22m#{reason} (##{code})\e[0m\n").to_stdout
|
||||
end
|
||||
end
|
||||
end
|
||||
58
spec/nostr/client/logger_spec.rb
Normal file
58
spec/nostr/client/logger_spec.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe Nostr::Client::Logger do
|
||||
let(:client) { instance_spy(Nostr::Client) }
|
||||
let(:relay) { Nostr::Relay.new(url: 'ws://0.0.0.0:4180/', name: 'localhost') }
|
||||
let(:logger) { described_class.new }
|
||||
|
||||
describe '#attach_to' do
|
||||
it 'attaches event handlers to the client' do
|
||||
logger.attach_to(client)
|
||||
|
||||
aggregate_failures do
|
||||
expect(client).to have_received(:on).with(:connect)
|
||||
expect(client).to have_received(:on).with(:message)
|
||||
expect(client).to have_received(:on).with(:send)
|
||||
expect(client).to have_received(:on).with(:error)
|
||||
expect(client).to have_received(:on).with(:close)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_connect' do
|
||||
it 'returns nil' do
|
||||
expect(logger.on_connect(relay)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_message' do
|
||||
it 'returns nil' do
|
||||
message = 'Received message'
|
||||
expect(logger.on_message(message)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_send' do
|
||||
it 'returns nil' do
|
||||
message = 'Sent message'
|
||||
expect(logger.on_send(message)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_error' do
|
||||
it 'returns nil' do
|
||||
message = 'Error message'
|
||||
expect(logger.on_error(message)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_close' do
|
||||
it 'returns nil' do
|
||||
code = 1000
|
||||
reason = 'Normal closure'
|
||||
expect(logger.on_close(code, reason)).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
63
spec/nostr/client/plain_logger_spec.rb
Normal file
63
spec/nostr/client/plain_logger_spec.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
require 'nostr/client/plain_logger'
|
||||
|
||||
RSpec.describe Nostr::Client::PlainLogger do
|
||||
let(:client) { instance_spy(Nostr::Client) }
|
||||
let(:relay) { Nostr::Relay.new(url: 'ws://0.0.0.0:4180/', name: 'localhost') }
|
||||
let(:logger) { described_class.new }
|
||||
|
||||
describe '#attach_to' do
|
||||
it 'attaches event handlers to the client' do
|
||||
logger.attach_to(client)
|
||||
|
||||
aggregate_failures do
|
||||
expect(client).to have_received(:on).with(:connect)
|
||||
expect(client).to have_received(:on).with(:message)
|
||||
expect(client).to have_received(:on).with(:send)
|
||||
expect(client).to have_received(:on).with(:error)
|
||||
expect(client).to have_received(:on).with(:close)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_connect' do
|
||||
it 'logs connection to a relay' do
|
||||
expect do
|
||||
logger.on_connect(relay)
|
||||
end.to output("Connected to the relay localhost (ws://0.0.0.0:4180/)\n").to_stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_message' do
|
||||
it 'logs a message received from the relay' do
|
||||
message = 'Received message'
|
||||
expect { logger.on_message(message) }.to output("◄- #{message}\n").to_stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_send' do
|
||||
it 'logs a message sent to the relay' do
|
||||
message = 'Sent message'
|
||||
expect { logger.on_send(message) }.to output("-► #{message}\n").to_stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_error' do
|
||||
it 'logs an error message' do
|
||||
message = 'Error message'
|
||||
expect { logger.on_error(message) }.to output("Error: #{message}\n").to_stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_close' do
|
||||
it 'logs a closure of connection with a relay' do
|
||||
code = '1000'
|
||||
reason = 'Connection closed'
|
||||
expect do
|
||||
logger.on_close(code, reason)
|
||||
end.to output("Connection closed: #{reason} (##{code})\n").to_stdout
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -17,7 +17,7 @@ RSpec.describe Nostr::Client do
|
||||
@echo_server.stop
|
||||
end
|
||||
|
||||
let(:client) { described_class.new }
|
||||
let(:client) { described_class.new(logger: nil) }
|
||||
let(:relay) { Nostr::Relay.new(url: plain_text_url, name: 'localhost') }
|
||||
|
||||
let(:port) { 4180 }
|
||||
@@ -27,8 +27,8 @@ RSpec.describe Nostr::Client do
|
||||
after { stop }
|
||||
|
||||
describe '.new' do
|
||||
client = described_class.new
|
||||
it 'creates an instance of a client' do
|
||||
client = described_class.new(logger: nil)
|
||||
|
||||
expect(client).to be_an_instance_of(described_class)
|
||||
end
|
||||
@@ -134,7 +134,7 @@ RSpec.describe Nostr::Client do
|
||||
it 'sends a REQ message to the relay, asking for all events and returns a subscription with the same id' do
|
||||
id = '16605b59b539f6e86762f28fb57db2fd'
|
||||
|
||||
client = described_class.new
|
||||
client = described_class.new(logger: nil)
|
||||
|
||||
sent_message = nil
|
||||
subscription = nil
|
||||
@@ -164,7 +164,7 @@ RSpec.describe Nostr::Client do
|
||||
allow(SecureRandom).to receive(:hex).and_return('16605b59b539f6e86762f28fb57db2fd')
|
||||
filter = Nostr::Filter.new(since: 1_230_981_305)
|
||||
|
||||
client = described_class.new
|
||||
client = described_class.new(logger: nil)
|
||||
|
||||
sent_message = nil
|
||||
subscription = nil
|
||||
@@ -194,7 +194,7 @@ RSpec.describe Nostr::Client do
|
||||
id = '16605b59b539f6e86762f28fb57db2fd'
|
||||
filter = Nostr::Filter.new(since: 1_230_981_305)
|
||||
|
||||
client = described_class.new
|
||||
client = described_class.new(logger: nil)
|
||||
|
||||
sent_message = nil
|
||||
subscription = nil
|
||||
@@ -223,7 +223,7 @@ RSpec.describe Nostr::Client do
|
||||
it 'sends a REQ message to the relay, asking for all events and returns a subscription with a random id' do
|
||||
allow(SecureRandom).to receive(:hex).and_return('16605b59b539f6e86762f28fb57db2fd')
|
||||
|
||||
client = described_class.new
|
||||
client = described_class.new(logger: nil)
|
||||
|
||||
sent_message = nil
|
||||
subscription = nil
|
||||
@@ -253,7 +253,7 @@ RSpec.describe Nostr::Client do
|
||||
it 'sends a CLOSE message to the relay, asking it to stop a subscription' do
|
||||
subscription_id = '16605b59b539f6e86762f28fb57db2fd'
|
||||
|
||||
client = described_class.new
|
||||
client = described_class.new(logger: nil)
|
||||
|
||||
sent_message = nil
|
||||
|
||||
@@ -277,7 +277,7 @@ RSpec.describe Nostr::Client do
|
||||
describe '#publish' do
|
||||
it 'sends a message to the relay' do
|
||||
relay = Nostr::Relay.new(url: plain_text_url, name: 'localhost')
|
||||
client = described_class.new
|
||||
client = described_class.new(logger: nil)
|
||||
event = Nostr::Event.new(
|
||||
id: '2a3184512d34077601e992ba3c3215354b21a8c76f85c2c7f66093481854e811',
|
||||
pubkey: '2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558',
|
||||
|
||||
Reference in New Issue
Block a user