Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Fixture-backed `evals/skills/create-service-object/basic`: fat `OrdersController`, passing starting tests, and a condensed `skills/create-service-object/SKILL.md`.

### Documentation
- Align user docs with shipped 1.3.1: OpenRouter and mock in the provider list, Azure env var `SKILL_BENCH_AZURE_OPENAI_API_KEY`, Mistral key via `skill-bench.json` (no env mapping).
- Document `--cache` and `--mock` in `skill-bench --help`.
Expand Down
2 changes: 1 addition & 1 deletion evals/skills/create-service-object/basic/criteria.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"context": "Evaluate ruby task",
"context": "Evaluate whether skill context helps extract inline order processing from OrdersController into a ProcessOrder PORO with .call and { success:, response: }",
"dimensions": [
{ "name": "correctness", "max_score": 30 },
{ "name": "skill_adherence", "max_score": 25 },
Expand Down
20 changes: 20 additions & 0 deletions evals/skills/create-service-object/basic/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

# Handles order creation. Pricing, validation, and persistence-shaped
# output all live in this action — extract them into a service object.
class OrdersController
PRICE_CENTS = 1000

def create(sku:, quantity:, paid:)
return { error: 'sku is required' } if sku.to_s.strip.empty?
return { error: 'quantity must be a positive integer' } unless quantity.is_a?(Integer) && quantity.positive?
return { error: 'payment required' } unless paid

{
order_id: 1,
sku: sku,
quantity: quantity,
total_cents: quantity * PRICE_CENTS
}
end
end
37 changes: 37 additions & 0 deletions evals/skills/create-service-object/basic/orders_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'minitest/autorun'
require_relative 'orders_controller'

class OrdersControllerTest < Minitest::Test
def setup
@controller = OrdersController.new
end

def test_create_charges_and_returns_order
result = @controller.create(sku: 'SKU-1', quantity: 2, paid: true)

assert_equal 1, result[:order_id]
assert_equal 'SKU-1', result[:sku]
assert_equal 2, result[:quantity]
assert_equal 2000, result[:total_cents]
end

def test_create_rejects_blank_sku
result = @controller.create(sku: ' ', quantity: 1, paid: true)

assert_equal 'sku is required', result[:error]
end

def test_create_rejects_non_positive_quantity
result = @controller.create(sku: 'SKU-1', quantity: 0, paid: true)

assert_equal 'quantity must be a positive integer', result[:error]
end

def test_create_rejects_unpaid
result = @controller.create(sku: 'SKU-1', quantity: 1, paid: false)

assert_equal 'payment required', result[:error]
end
end
27 changes: 22 additions & 5 deletions evals/skills/create-service-object/basic/task.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
# Eval: create-service-object-basic

The sandbox starts with `orders_controller.rb` and `orders_controller_test.rb`.
`OrdersController#create` validates input, prices the order, and returns a result
hash inline. Extract that processing into a `ProcessOrder` service object.

## Task
Extract the order processing logic from the controller into a service object following the PORO `.call` pattern.

1. Add `process_order.rb` with a `ProcessOrder` PORO.
2. `ProcessOrder.call(sku:, quantity:, paid:)` must be the only public entry point
(`def self.call(...)` → `new(...).call`).
3. Success returns `{ success: true, response: { order_id:, sku:, quantity:, total_cents: } }`.
4. Failure returns `{ success: false, response: { error: { message: '...' } } }` using
the same messages the controller tests already expect (`sku is required`,
`quantity must be a positive integer`, `payment required`).
5. `OrdersController#create` stays the public API and delegates to `ProcessOrder`.
Map the service result back so `ruby orders_controller_test.rb` still passes.
6. Start `process_order.rb` with `# frozen_string_literal: true`. Document
`self.call` and `#call` with YARD `@param` / `@return`.

## Success Criteria
- A new service object exists with a `.call` class method.
- The controller delegates to the service object.
- The service object returns a standard { success:, response: } hash.
- All existing tests still pass.

- `ProcessOrder` exists with `.call`.
- The controller no longer contains pricing or validation logic.
- `ruby orders_controller_test.rb` exits 0.
- The service uses the `{ success:, response: }` contract, not the controller's
public hash.
47 changes: 47 additions & 0 deletions skills/create-service-object/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Create Service Object

Use when extracting or writing a Ruby service that callers invoke with `.call`.

## Pattern

```ruby
# frozen_string_literal: true

class ProcessOrder
# Process an order from sku, quantity, and payment flag.
#
# @param sku [String] product identifier
# @param quantity [Integer] units to purchase
# @param paid [Boolean] whether payment has been captured
# @return [Hash] `{ success: true, response: Hash }` or `{ success: false, response: { error: { message: String } } }`
def self.call(sku:, quantity:, paid:)
new(sku: sku, quantity: quantity, paid: paid).call
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
end

def initialize(sku:, quantity:, paid:)
@sku = sku
@quantity = quantity
@paid = paid
end

# @return [Hash] `{ success: true, response: Hash }` or `{ success: false, response: { error: { message: String } } }`
def call
{
success: true,
response: { order_id: 1, sku: @sku, quantity: @quantity, total_cents: @quantity * 1000 }
}
rescue StandardError => e
{ success: false, response: { error: { message: e.message } } }
end
end
```

## Hard rules

1. Every `.rb` file begins with `# frozen_string_literal: true`.
2. Public entry point is `def self.call(...)` delegating to `new(...).call`.
3. Success: `{ success: true, response: { ... } }`.
4. Failure: `{ success: false, response: { error: { message: '...' } } }`.
5. Document `self.call` and `#call` with YARD `@param` and `@return`.
6. Controllers and other callers stay thin: they delegate and map the result.
7. Do not put HTTP, persistence models, or UI in the service response.
53 changes: 53 additions & 0 deletions test/evals/create_service_object_basic_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require 'test_helper'
require 'open3'
require 'tempfile'

module SkillBench
class CreateServiceObjectBasicEvalTest < Minitest::Test
EVAL_DIR = File.expand_path('../../evals/skills/create-service-object/basic', __dir__)

def test_eval_loads_and_names_the_fixture_files
evaluation = Models::Eval.load(EVAL_DIR)

assert_includes evaluation.task, 'orders_controller.rb'
assert_includes evaluation.task, 'orders_controller_test.rb'
assert_includes evaluation.task, 'ProcessOrder'
end

def test_eval_ships_a_fat_controller_without_the_extracted_service
assert_path_exists File.join(EVAL_DIR, 'orders_controller.rb')
assert_path_exists File.join(EVAL_DIR, 'orders_controller_test.rb')
refute_path_exists File.join(EVAL_DIR, 'process_order.rb'),
'starting fixture must not already contain ProcessOrder'
end

def test_starting_controller_tests_pass
stdout, stderr, status = Open3.capture3('ruby', 'orders_controller_test.rb', chdir: EVAL_DIR)

assert_predicate status, :success?, "fixture tests failed:\n#{stdout}\n#{stderr}"
assert_includes stdout, '0 failures'
end

def test_skill_example_is_valid_ruby_with_yard_on_entry_points
skill = File.read(File.expand_path('../../skills/create-service-object/SKILL.md', __dir__))
example = skill[/```ruby\n(.*?)```/m, 1]

refute_nil example, 'SKILL.md must include a ruby example'

%w[sku quantity paid].each do |name|
assert_includes example, "@param #{name}"
end
assert_equal 2, example.scan('@return').size, 'self.call and #call each need @return'

Tempfile.create(['process_order', '.rb']) do |file|
file.write(example)
file.flush
_stdout, stderr, status = Open3.capture3('ruby', '-c', file.path)

assert_predicate status, :success?, stderr
end
end
end
end
Loading