Class: Plushie::Test::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/plushie/test/session.rb

Overview

Test session driving the Elm loop against a pooled renderer. Each test gets its own Session with isolated model state.

Interactions (click, type_text, toggle, etc.) are sent via the renderer's interact protocol; resulting events are fed through the app's update cycle so the model stays in sync.

See the Elixir SDK's mock renderer backend for reference.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_class, pool:, session_id:) ⇒ Session

Returns a new instance of Session.

Parameters:

  • app_class (Class)

    the app class (includes Plushie::App)

  • pool (SessionPool)
  • session_id (String)


22
23
24
25
26
27
28
29
30
# File 'lib/plushie/test/session.rb', line 22

def initialize(app_class, pool:, session_id:)
  @app = app_class.new
  @pool = pool
  @session_id = session_id
  @format = pool.format
  @model = nil
  @tree = nil
  init_app
end

Instance Attribute Details

#modelObject (readonly)

Returns current app model.

Returns:

  • (Object)

    current app model



17
18
19
# File 'lib/plushie/test/session.rb', line 17

def model
  @model
end

Instance Method Details

#click(selector) ⇒ Object

Click a button widget.

Parameters:

  • selector (String)

    "#id" or "text content"



36
37
38
# File 'lib/plushie/test/session.rb', line 36

def click(selector)
  interact("click", selector)
end

#element_text(element) ⇒ String?

Extract text content from an element hash. Checks content, label, value, placeholder in order.

Parameters:

  • element (Hash)

Returns:

  • (String, nil)


193
194
195
196
197
# File 'lib/plushie/test/session.rb', line 193

def element_text(element)
  return nil unless element
  props = element["props"] || element[:props] || {}
  props["content"] || props["label"] || props["value"] || props["placeholder"]
end

#find(selector) ⇒ Hash?

Find a widget by selector. Returns the node hash or nil.

Parameters:

  • selector (String)

    "#id" or "text content"

Returns:

  • (Hash, nil)


108
109
110
111
112
113
114
115
116
# File 'lib/plushie/test/session.rb', line 108

def find(selector)
  id = SecureRandom.hex(4)
  response = @pool.send_and_wait(
    {type: "query", id: id, target: "find", selector: build_selector(selector)},
    @session_id, :query_response
  )
  data = response[:data] || response["data"]
  (data.nil? || data.empty?) ? nil : data
end

#find!(selector) ⇒ Hash

Find a widget by selector. Raises with a helpful error if not found, including the current tree IDs and similar ID suggestions.

Parameters:

  • selector (String)

Returns:

  • (Hash)

Raises:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/plushie/test/session.rb', line 124

def find!(selector)
  result = find(selector)
  unless result
    all_ids = Tree.ids(@tree)
    target = selector.start_with?("#") ? selector[1..] : selector
    suggestions = find_similar_ids(target, all_ids)

    msg = "Widget not found: #{selector}\n"
    msg << "\n  Did you mean: #{suggestions.map { "##{_1}" }.join(", ")}\n" if suggestions.any?
    msg << "\n  Current tree IDs: #{all_ids.join(", ")}"
    raise Plushie::Error, msg
  end
  result
end

#move_to(x, y) ⇒ Object

Move cursor to coordinates.

Parameters:

  • x (Numeric)
  • y (Numeric)


99
100
101
# File 'lib/plushie/test/session.rb', line 99

def move_to(x, y)
  interact("move_to", nil, x: x, y: y)
end

#press(key) ⇒ Object

Press a key (key down).

Parameters:

  • key (String)

    key name, supports modifiers: "ctrl+s"



80
81
82
# File 'lib/plushie/test/session.rb', line 80

def press(key)
  interact("press", nil, key: key)
end

#release(key) ⇒ Object

Release a key (key up).

Parameters:

  • key (String)


86
87
88
# File 'lib/plushie/test/session.rb', line 86

def release(key)
  interact("release", nil, key: key)
end

#resetObject

Reset the session to initial state.



175
176
177
178
179
# File 'lib/plushie/test/session.rb', line 175

def reset
  @model = nil
  @tree = nil
  init_app
end

#screenshot(name, width: 1024, height: 768) ⇒ Hash

Capture a screenshot.

Parameters:

  • name (String)
  • width (Integer) (defaults to: 1024)
  • height (Integer) (defaults to: 768)

Returns:

  • (Hash)


166
167
168
169
170
171
172
# File 'lib/plushie/test/session.rb', line 166

def screenshot(name, width: 1024, height: 768)
  id = SecureRandom.hex(4)
  @pool.send_and_wait(
    {type: "screenshot", id: id, name: name, width: width, height: height},
    @session_id, :screenshot_response
  )
end

#select(selector, value) ⇒ Object

Select a value from pick_list, combo_box, or radio.

Parameters:

  • selector (String)
  • value (String)


67
68
69
# File 'lib/plushie/test/session.rb', line 67

def select(selector, value)
  interact("select", selector, value: value)
end

#slide(selector, value) ⇒ Object

Slide a slider to a value.

Parameters:

  • selector (String)
  • value (Numeric)


74
75
76
# File 'lib/plushie/test/session.rb', line 74

def slide(selector, value)
  interact("slide", selector, value: value)
end

#stopObject

Stop the session and release the renderer session.



182
183
184
185
186
187
# File 'lib/plushie/test/session.rb', line 182

def stop
  @pool.unregister(@session_id)
rescue => e
  # Swallow errors during cleanup
  warn "plushie test: error during session cleanup: #{e.message}" if $DEBUG
end

#submit(selector) ⇒ Object

Submit a text_input (press Enter).

Parameters:

  • selector (String)


49
50
51
52
53
54
# File 'lib/plushie/test/session.rb', line 49

def submit(selector)
  # Read current value from local tree
  node = find_in_local_tree(selector)
  value = node_prop(node, "value") || ""
  interact("submit", selector, value: value)
end

#toggle(selector) ⇒ Object

Toggle a checkbox or toggler.

Parameters:

  • selector (String)


58
59
60
61
62
# File 'lib/plushie/test/session.rb', line 58

def toggle(selector)
  node = find_in_local_tree(selector)
  current = node_prop(node, "checked") || node_prop(node, "active") || false
  interact("toggle", selector, value: !current)
end

#treeHash

Get the full tree from the renderer.

Returns:

  • (Hash)


141
142
143
144
145
146
147
148
# File 'lib/plushie/test/session.rb', line 141

def tree
  id = SecureRandom.hex(4)
  response = @pool.send_and_wait(
    {type: "query", id: id, target: "tree", selector: {}},
    @session_id, :query_response
  )
  response[:data] || response["data"]
end

#tree_hash(name) ⇒ Hash

Capture a structural tree hash.

Parameters:

  • name (String)

Returns:

  • (Hash)


153
154
155
156
157
158
159
# File 'lib/plushie/test/session.rb', line 153

def tree_hash(name)
  id = SecureRandom.hex(4)
  @pool.send_and_wait(
    {type: "tree_hash", id: id, name: name},
    @session_id, :tree_hash_response
  )
end

#type_key(key) ⇒ Object

Type a key (press + release).

Parameters:

  • key (String)


92
93
94
# File 'lib/plushie/test/session.rb', line 92

def type_key(key)
  interact("type_key", nil, key: key)
end

#type_text(selector, text) ⇒ Object

Type text into a text_input or text_editor.

Parameters:

  • selector (String)
  • text (String)


43
44
45
# File 'lib/plushie/test/session.rb', line 43

def type_text(selector, text)
  interact("type_text", selector, text: text)
end