# THIS FILE IS AUTOGENERATED FROM trailblazer-activity-dsl-linear/test/docs/activity_basics_test.rb
require "test_helper"

module X
  class DocsActivityTest < Minitest::Spec
    it "basic activity" do
      Memo = Struct.new(:options) do
        def save
          true
        end
      end

      #:memo-create
      module Memo::Operation
        class Create < Trailblazer::Operation
          step :validate
          #~body
          step :save
          left :handle_errors
          step :notify
          #~meths
          include T.def_steps(:validate, :save, :handle_errors, :notify)

          #:save
          def save(ctx, params:, **)
            memo = Memo.new(params[:memo])
            memo.save

            ctx[:model] = memo # you can write to the {ctx}.
          end
          #:save end

          def notify(ctx, **)
            true
          end

          #~body end
          def validate(ctx, params:, **) # home-made validation
            params.key?(:memo) &&
            params[:memo].key?(:text) &&
            params[:memo][:text].size > 9
            # return value matters!
          end
          #~meths end
        end
      end
      #:memo-create end

      #:memo-call
      result = Memo::Operation::Create.(
        params: {memo: {text: "Do not forget!"}}
      )

      result.success? # => true
      puts result.terminus.to_h[:semantic] #=> :success
      #:memo-call end

      #:memo-call-model
      result = Memo::Operation::Create.(
        params: {memo: {text: "Do not forget!"}}
      )

      #~ctx_to_result
      puts result[:model] #=> #<Memo id: 1 text: "Do not forget!">
      #:memo-call-model end
      #~ctx_to_result end

      assert_equal result.terminus.inspect, %(#<Trailblazer::Activity::Railway::End::Success semantic=:success>)
      #~ignore end
    end
  end
end
