module RatatuiRuby::TestHelper::Snapshot

Snapshot testing assertions for terminal UIs.

Verifying every character of a TUI screen by hand is tedious. Snapshots let you capture the screen once and compare against it in future runs.

This mixin provides assert_plain_snapshot for plain text, assert_rich_snapshot for styled ANSI output, and assert_snapshots (plural) for both. All auto-create snapshot files on first run.

Use it to verify complex layouts, styles, and interactions without manual assertions.

Snapshot Files

Snapshots live in a snapshots/ subdirectory next to your test file:

test/examples/my_app/test_app.rb
test/examples/my_app/snapshots/initial_render.txt
test/examples/my_app/snapshots/initial_render.ansi

Creating and Updating Snapshots

Run tests with UPDATE_SNAPSHOTS=1 to create or refresh snapshots:

UPDATE_SNAPSHOTS=1 bundle exec rake test

Seeding Random Data

Random data (scatter plots, generated content) breaks snapshot stability. Use a seeded Random instance instead of Kernel.rand:

class MyApp
  def initialize(seed: nil)
    @rng = seed ? Random.new(seed) : Random.new
  end

  def generate_data
    (0..20).map { @rng.rand(0.0..10.0) }
  end
end

# In your test
def setup
  @app = MyApp.new(seed: 42)
end

For libraries like Faker, see their docs on deterministic random: github.com/faker-ruby/faker#deterministic-random

Normalization Blocks

Mask dynamic content (timestamps, IDs) with a normalization block:

assert_snapshots("dashboard") do |lines|
  lines.map { |l| l.gsub(/\d{4}-\d{2}-\d{2}/, "YYYY-MM-DD") }
end