Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- **Generate runnable agent tests** (`activeagent`). TestUnit agent tests
inherit from `ActiveSupport::TestCase` instead of the nonexistent
`ActiveAgent::TestCase`. Plain and namespaced agents, including agents
without actions, can load in a Rails host without calling a provider.

### Changed

- **One badge for every status and message role in the dashboard**
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/test_unit/templates/functional_test.rb.tt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "test_helper"

<% module_namespacing do -%>
class <%= class_name %>AgentTest < ActiveAgent::TestCase
class <%= class_name %>AgentTest < ActiveSupport::TestCase
<% actions.each_with_index do |action, index| -%>
<% if index != 0 -%>

Expand Down
52 changes: 52 additions & 0 deletions test/generators/active_agent/generated_agent_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "test_helper"
require "generators/active_agent/agent/agent_generator"
require "open3"

class ActiveAgent::Generators::GeneratedAgentTest < Rails::Generators::TestCase
tests ActiveAgent::Generators::AgentGenerator
destination Rails.root.join("tmp/generated_agent_tests")
setup :prepare_destination

test "generated agent tests run in a Rails host" do
assert_generated_tests_run "generated_example", 2
end

test "generated namespaced agent tests run in a Rails host" do
assert_generated_tests_run "admin/generated_example", 2
end

test "generated agent tests without actions load in a Rails host" do
assert_generated_tests_run "generated_example", 0, actions: []
end

private

def assert_generated_tests_run(name, count, actions: %w[create update])
run_generator [ name, *actions, "--test-framework=test_unit" ]
FileUtils.mkdir_p(File.join(destination_root, "config"))
File.write(File.join(destination_root, "config/routes.rb"), "Rails.application.routes.draw {}\n")
File.write(File.join(destination_root, "test/test_helper.rb"), <<~RUBY)
ENV["RAILS_ENV"] = "test"
require "rails"
require "active_model/railtie"
require "active_job/railtie"
require "action_controller/railtie"
require "active_agent"
class GeneratedHost < Rails::Application
config.root = File.expand_path("..", __dir__)
config.eager_load = false
config.secret_key_base = "synthetic-test-secret"
config.logger = Logger.new(File::NULL)
end
GeneratedHost.initialize!
require "rails/test_help"
RUBY

output, status = Open3.capture2e(
RbConfig.ruby, "-Itest", "test/agents/#{name}_agent_test.rb",
chdir: destination_root
)
assert status.success?, output
assert_match(/#{count} runs, #{count} assertions, 0 failures, 0 errors/, output)
end
end
8 changes: 4 additions & 4 deletions test/generators/test_unit/agent_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestUnit::Generators::AgentGeneratorTest < Rails::Generators::TestCase
run_generator [ "user", "create", "update" ]

assert_file "test/agents/user_agent_test.rb" do |content|
assert_match(/class UserAgentTest < ActiveAgent::TestCase/, content)
assert_match(/class UserAgentTest < ActiveSupport::TestCase/, content)
assert_match(/test "create"/, content)
assert_match(/test "update"/, content)
end
Expand All @@ -30,7 +30,7 @@ class TestUnit::Generators::AgentGeneratorTest < Rails::Generators::TestCase
run_generator [ "admin/user", "create" ]

assert_file "test/agents/admin/user_agent_test.rb" do |content|
assert_match(/class Admin::UserAgentTest < ActiveAgent::TestCase/, content)
assert_match(/class Admin::UserAgentTest < ActiveSupport::TestCase/, content)
end

assert_file "test/docs/previews/admin/user_agent_preview.rb" do |content|
Expand All @@ -47,15 +47,15 @@ class TestUnit::Generators::AgentGeneratorTest < Rails::Generators::TestCase
run_generator [ "user_agent", "create" ]

assert_file "test/agents/user_agent_test.rb" do |content|
assert_match(/class UserAgentTest < ActiveAgent::TestCase/, content)
assert_match(/class UserAgentTest < ActiveSupport::TestCase/, content)
end
end

test "generates test without actions" do
run_generator [ "user" ]

assert_file "test/agents/user_agent_test.rb" do |content|
assert_match(/class UserAgentTest < ActiveAgent::TestCase/, content)
assert_match(/class UserAgentTest < ActiveSupport::TestCase/, content)
assert_no_match(/^ test "/, content) # No actual test methods (only commented ones)
end

Expand Down
Loading