@@ -97,6 +97,36 @@ defmodule Durable.CompensationTest do
9797 end
9898 end
9999
100+ # Workflow where first compensation succeeds but second fails (partial compensation)
101+ defmodule PartialCompensationWorkflow do
102+ use Durable
103+ use Durable.Helpers
104+
105+ workflow "partial_compensation" do
106+ step ( :step_one , [ compensate: :undo_one ] , fn data ->
107+ { :ok , assign ( data , :one , true ) }
108+ end )
109+
110+ step ( :step_two , [ compensate: :undo_two ] , fn data ->
111+ { :ok , assign ( data , :two , true ) }
112+ end )
113+
114+ step ( :fail_step , fn _data ->
115+ raise "Step failed"
116+ end )
117+
118+ # This compensation runs first (reverse order) and succeeds
119+ compensate ( :undo_two , fn data ->
120+ { :ok , assign ( data , :two_undone , true ) }
121+ end )
122+
123+ # This compensation runs second and fails
124+ compensate ( :undo_one , fn _data ->
125+ raise "Compensation for step_one failed"
126+ end )
127+ end
128+ end
129+
100130 describe "compensation DSL" do
101131 test "compensate macro creates compensation definition" do
102132 { :ok , workflow_def } = BookTripWorkflow . __workflow_definition__ ( "book_trip" )
@@ -195,6 +225,25 @@ defmodule Durable.CompensationTest do
195225
196226 assert compensation_steps == [ ]
197227 end
228+
229+ test "records partial success when some compensations succeed and others fail" do
230+ { :ok , execution } = create_and_execute_workflow ( PartialCompensationWorkflow , % { } )
231+
232+ # Workflow should be in compensation_failed state
233+ assert execution . status == :compensation_failed
234+
235+ # Should have results for both compensations
236+ results = execution . compensation_results
237+ assert length ( results ) == 2
238+
239+ # First compensation (undo_two) should succeed, second (undo_one) should fail
240+ [ first_comp , second_comp ] = results
241+ assert first_comp [ "step" ] == "step_two"
242+ assert first_comp [ "result" ] [ "status" ] == "completed"
243+
244+ assert second_comp [ "step" ] == "step_one"
245+ assert second_comp [ "result" ] [ "status" ] == "failed"
246+ end
198247 end
199248
200249 # Helper function to create and execute workflow
0 commit comments