Class: NwcRuby::TestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/nwc_ruby/test_runner.rb

Overview

A diagnostic test runner for a live NWC connection string. Surfaces misbehavior with actionable error messages rather than cryptic failures.

Exposed both as a class and through the convenience method ‘NwcRuby.test(…)`. Call it from IRB, a Rails console, a custom rake task in your own app, a spec — anywhere.

Example:

NwcRuby.test(
  nwc_url: "nostr+walletconnect://...",
  pay_to_lightning_address: "you@getalby.com",
  pay_to_satoshis_amount: 10
)

Constant Summary collapse

PASS =
"\e[32m✓\e[0m"
FAIL =
"\e[31m✗\e[0m"
WARN =
"\e[33m!\e[0m"
SKIP =
"\e[90m—\e[0m"
BOLD =
"\e[1m"
DIM =
"\e[90m"
CLR =
"\e[0m"
DEFAULT_SATOSHIS =
100

Instance Method Summary collapse

Constructor Details

#initialize(nwc_url:, pay_to_lightning_address: nil, pay_to_satoshis_amount: DEFAULT_SATOSHIS, output: $stdout) ⇒ TestRunner

Returns a new instance of TestRunner.

Parameters:

  • nwc_url (String)

    the nostr+walletconnect:// connection string

  • pay_to_lightning_address (String, nil) (defaults to: nil)

    Lightning address to send the write test payment to. Only used if the NWC code is read+write.

  • pay_to_satoshis_amount (Integer) (defaults to: DEFAULT_SATOSHIS)

    amount in sats used for both the outbound write test (if applicable) and the inbound make_invoice generated for the notification test. Defaults to 100.

  • output (IO) (defaults to: $stdout)

    where to print diagnostic output.



39
40
41
42
43
44
45
46
47
48
# File 'lib/nwc_ruby/test_runner.rb', line 39

def initialize(nwc_url:,
               pay_to_lightning_address: nil,
               pay_to_satoshis_amount: DEFAULT_SATOSHIS,
               output: $stdout)
  @nwc_url                  = nwc_url
  @pay_to_lightning_address = pay_to_lightning_address
  @pay_to_satoshis_amount   = Integer(pay_to_satoshis_amount)
  @out                      = output
  @failures                 = []
end

Instance Method Details

#runObject

Runs info + read tests + write tests (if applicable). Does NOT test notifications — run run_notifications in a separate process.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nwc_ruby/test_runner.rb', line 52

def run
  return false unless preamble

  run_read_tests

  if @pay_to_lightning_address
    if @info.read_only?
      warn!('pay_to_lightning_address was provided but this is a READ-ONLY code — it does not support pay_invoice.')
      warn!('  → Generate a read+write NWC code if you need to test outbound payments.')
      @out.puts
    else
      run_write_tests
    end
  end

  summary
  @failures.empty?
end

#run_notificationsObject

Subscribes to notifications and blocks forever, printing each one. Ctrl-C / SIGTERM cause a clean exit. Run in a separate process.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/nwc_ruby/test_runner.rb', line 73

def run_notifications
  return false unless preamble

  @out.puts "#{BOLD}Listening for notifications...#{CLR}"
  @out.puts "#{DIM}Press Ctrl-C to stop.#{CLR}"
  @out.puts

  @client.subscribe_to_notifications do |n|
    @out.puts "  #{PASS} #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}#{n.type}"
    @out.puts "    #{DIM}payment_hash=#{n.payment_hash}#{CLR}"
    @out.puts "    #{DIM}amount=#{n.amount_msats} msats#{CLR}"
    @out.puts
  end
  # subscribe_to_notifications blocks until SIGTERM/SIGINT
  true
end