e2e_rb 0.1.0.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module E2eRb
4
+ # One e2e test attempt. Hands out the fixtures and runs expectations.
5
+ class Session
6
+ attr_reader :name, :attempt_id, :artifacts_dir, :trace_path, :fixture_names, :failure_class
7
+
8
+ def initialize(runtime, name:, info:, failure_class: TestFailure)
9
+ @runtime = runtime
10
+ @name = name
11
+ @failure_class = failure_class
12
+ @attempt_id = info.fetch("attemptId")
13
+ @artifacts_dir = info.fetch("artifactsDir")
14
+ @trace_path = info.fetch("tracePath")
15
+ @fixture_names = info.fetch("fixtures")
16
+ @closed = false
17
+ end
18
+
19
+ def app = fixture("app")
20
+ def screen = fixture("screen")
21
+ def agent = fixture("agent")
22
+ def platform = fixture("platform")
23
+
24
+ # Any fixture the target's engine contributes.
25
+ def fixture(name)
26
+ name = name.to_s
27
+ unless @fixture_names.include?(name)
28
+ raise ConfigurationError,
29
+ "this target has no fixture #{name.inspect} (has: #{@fixture_names.join(", ")})"
30
+ end
31
+
32
+ Proxy.new(self, name)
33
+ end
34
+
35
+ def expect(subject, soft: false) = Expectation.new(self, subject, soft: soft)
36
+
37
+ def invoke(root, chain)
38
+ call("invoke", "root" => root, "chain" => chain)
39
+ end
40
+
41
+ # args: the matcher's arguments, encoded (Codec.args).
42
+ def run_expectation(subject, matcher, args, negate:, soft:)
43
+ call("expect", "subject" => subject.__e2e_subject__, "matcher" => matcher, "args" => args,
44
+ "negate" => negate, "soft" => soft)
45
+ nil
46
+ end
47
+
48
+ # Ends the attempt and writes trace.md. Returns e2e's soft/cleanup failures.
49
+ def close
50
+ return @failures if @closed
51
+
52
+ @closed = true
53
+ result = @runtime.close_session(self)
54
+ @failures = result.fetch("failures")
55
+ end
56
+
57
+ def closed? = @closed
58
+
59
+ private
60
+
61
+ def call(method, params)
62
+ raise Error, "session #{@name.inspect} is closed" if @closed
63
+
64
+ @runtime.bridge.request(method, params) do |error|
65
+ E2eRb.error_from_wire(error, trace_path: @trace_path, failure_class: @failure_class)
66
+ end
67
+ end
68
+ end
69
+
70
+ # session.expect(locator).to_have_text("x") / .not.to_be_visible
71
+ class Expectation
72
+ def initialize(session, subject, soft:, negate: false)
73
+ unless subject.respond_to?(:__e2e_subject__)
74
+ raise ArgumentError, "e2e expectations take a locator or screen, got #{subject.inspect}; " \
75
+ "use your framework's own assertions for plain values"
76
+ end
77
+
78
+ @session = session
79
+ @subject = subject
80
+ @soft = soft
81
+ @negate = negate
82
+ end
83
+
84
+ def not = Expectation.new(@session, @subject, soft: @soft, negate: !@negate)
85
+
86
+ # Runs e2e's matcher by its e2e name: matcher("toHaveText", "x").
87
+ def matcher(e2e_name, *positional, **options)
88
+ @session.run_expectation(@subject, e2e_name, Codec.args(positional, options), negate: @negate, soft: @soft)
89
+ end
90
+
91
+ def method_missing(name, *positional, **)
92
+ return super unless name.start_with?("to_")
93
+
94
+ matcher(Codec.js_name(name), *positional, **)
95
+ end
96
+
97
+ def respond_to_missing?(name, include_private = false) = name.start_with?("to_") || super
98
+ end
99
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module E2eRb
4
+ VERSION = "0.1.0.beta"
5
+ # Bridge <-> gem wire protocol. Bumped on any incompatible change.
6
+ PROTOCOL_VERSION = 1
7
+ # The e2e npm versions this gem is tested against. The project installs e2e;
8
+ # the bridge refuses to start on any other version.
9
+ E2E_REQUIREMENT = Gem::Requirement.new("~> 0.23.1")
10
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module E2eRb
6
+ # JSON-RPC 2.0 messages with LSP framing (Content-Length headers).
7
+ #
8
+ # A frame is written and read whole: an asynchronous exception (Timeout, Interrupt)
9
+ # is held until the frame is done, so it can never leave half a frame in the pipe.
10
+ # Waiting for the first byte of a frame stays interruptible.
11
+ class Wire
12
+ def initialize(reader, writer)
13
+ @reader = reader
14
+ @writer = writer
15
+ end
16
+
17
+ def write(message)
18
+ body = JSON.generate(message.merge("jsonrpc" => "2.0"))
19
+ Thread.handle_interrupt(Object => :never) do
20
+ @writer.write("Content-Length: #{body.bytesize}\r\n\r\n#{body}")
21
+ @writer.flush
22
+ end
23
+ end
24
+
25
+ # Returns the next message, or nil at end of stream.
26
+ def read
27
+ Thread.handle_interrupt(Object => :never) do
28
+ first = Thread.handle_interrupt(Object => :immediate) { @reader.gets("\r\n") }
29
+ read_rest(first)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ # nil at end of stream, else the frame's parsed body.
36
+ def read_rest(first)
37
+ length = content_length(first)
38
+ return nil if length == :eof
39
+
40
+ read_body(length)
41
+ end
42
+
43
+ # Reads the header lines up to the blank one; :eof if the stream ends first.
44
+ def content_length(line)
45
+ length = nil
46
+ until line.nil? || (line = line.chomp("\r\n")).empty?
47
+ name, value = line.split(":", 2)
48
+ length = Integer(value.strip) if name.strip.casecmp?("Content-Length")
49
+ line = @reader.gets("\r\n")
50
+ end
51
+ return :eof if line.nil?
52
+
53
+ length || raise(BridgeError, "bridge sent a frame without Content-Length")
54
+ end
55
+
56
+ def read_body(length)
57
+ body = @reader.read(length)
58
+ raise BridgeError, "bridge closed mid-frame" if body.nil? || body.bytesize < length
59
+
60
+ JSON.parse(body.force_encoding(Encoding::UTF_8))
61
+ end
62
+ end
63
+ end
data/lib/e2e_rb.rb ADDED
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "e2e_rb/version"
5
+ require_relative "e2e_rb/errors"
6
+ require_relative "e2e_rb/wire"
7
+ require_relative "e2e_rb/configuration"
8
+ require_relative "e2e_rb/matchers"
9
+ require_relative "e2e_rb/js_regexp"
10
+ require_relative "e2e_rb/codec"
11
+ require_relative "e2e_rb/proxy"
12
+ require_relative "e2e_rb/bridge"
13
+ require_relative "e2e_rb/session"
14
+ require_relative "e2e_rb/runtime"
15
+
16
+ # Ruby tests on top of e2e (https://github.com/e2e-inc/e2e): agentic steps plus locators and
17
+ # expect, driven through a Node bridge that hosts e2e sessions.
18
+ module E2eRb
19
+ class << self
20
+ def configuration = @configuration ||= Configuration.new
21
+
22
+ def configure
23
+ raise Error, "configure E2eRb before the first session opens" if @runtime
24
+
25
+ yield configuration
26
+ end
27
+
28
+ # The process's runtime. At exit it only reaps the bridge; reporting a host-close
29
+ # failure is the adapters' job (E2eRb.close! at the end of the suite).
30
+ def runtime = @runtime ||= Runtime.new(configuration).tap { |runtime| at_exit { runtime.reap } }
31
+
32
+ # Opens one test attempt. Call #close on it when the test ends.
33
+ # failure_class: the class a failed expectation raises (the adapters pass their framework's).
34
+ def open_session(name:, **) = runtime.open_session(name: name, **)
35
+
36
+ # Closes the host and the bridge; raises InfrastructureError if e2e reports a failure.
37
+ # A no-op when no session was ever opened in this process.
38
+ def close!
39
+ @runtime&.close!
40
+ end
41
+
42
+ # A secret from e2e's config (E2E_SECRET_<NAME>); its value never enters Ruby.
43
+ def secret(name) = SecretRef.new("$secret" => name.to_s)
44
+
45
+ # A credential from e2e's config (E2E_USER_<NAME>_*): field :username or :password.
46
+ def credential(name, field) = SecretRef.new("$credential" => name.to_s, "field" => field.to_s)
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: e2e_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.beta
5
+ platform: ruby
6
+ authors:
7
+ - e2e_rb contributors
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: 'Drives e2e (npm package `e2e`) from Ruby: agent.act / agent.assert plus
13
+ auto-waiting locators and expect, through a Node bridge that hosts e2e sessions.'
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - CHANGELOG.md
19
+ - LICENSE
20
+ - README.md
21
+ - bridge/node-range.mjs
22
+ - bridge/plain.mjs
23
+ - bridge/server.mjs
24
+ - lib/e2e_rb.rb
25
+ - lib/e2e_rb/bridge.rb
26
+ - lib/e2e_rb/codec.rb
27
+ - lib/e2e_rb/configuration.rb
28
+ - lib/e2e_rb/dsl.rb
29
+ - lib/e2e_rb/errors.rb
30
+ - lib/e2e_rb/js_regexp.rb
31
+ - lib/e2e_rb/matchers.rb
32
+ - lib/e2e_rb/minitest.rb
33
+ - lib/e2e_rb/minitest/plugin.rb
34
+ - lib/e2e_rb/proxy.rb
35
+ - lib/e2e_rb/rspec.rb
36
+ - lib/e2e_rb/runtime.rb
37
+ - lib/e2e_rb/session.rb
38
+ - lib/e2e_rb/version.rb
39
+ - lib/e2e_rb/wire.rb
40
+ homepage: https://github.com/robertziel/e2e_rb
41
+ licenses:
42
+ - Apache-2.0
43
+ metadata:
44
+ source_code_uri: https://github.com/robertziel/e2e_rb
45
+ changelog_uri: https://github.com/robertziel/e2e_rb/blob/main/CHANGELOG.md
46
+ bug_tracker_uri: https://github.com/robertziel/e2e_rb/issues
47
+ rubygems_mfa_required: 'true'
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '3.2'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.6.9
63
+ specification_version: 4
64
+ summary: RSpec and Minitest tests on top of e2e, the agentic end-to-end testing framework
65
+ test_files: []