Repository navigation
feat: fixture-backed create-service-object eval #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
evals/skills/create-service-object/basic/orders_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
37
evals/skills/create-service-object/basic/orders_controller_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.