
    r)j                        d Z ddlZddlmc mZ ddlZddlZddl	Z	ddl
Z
ddlZddlZej                  j                  ej                  j!                  ej                  j#                  e      d            ZdZej                  j!                  edd      ZedfdZej.                  j0                  ej.                  j3                  ej                  j5                  e       d	
       G d d                    Zy)u  
Slow acceptance test: verifies the full train_mlp → validate_strategy pipeline.

Marked @pytest.mark.slow — skipped by default in quick test runs.
Run with: /usr/bin/python3 -m pytest tests/test_acceptance_mlp.py -m slow -s

What it checks:
  1. train_mlp.py --smoke produces a valid weights artifact.
  2. The artifact loads, has the expected structure, and feature_cols match FEATURE_COLS.
  3. validate_strategy.py with the artifact's recommended_thresholds runs successfully
     and returns finite (non-NaN, non-None) metrics.
    Nz..z/usr/bin/python3datazCOINBASE_BTCUSD-6H.csvi,  c                 :    t        j                  | |dd|      }|S )NT)cwdcapture_outputtexttimeout)
subprocessrun)cmdr   r   results       D/Users/jameslopez/projects/TradingBot25/tests/test_acceptance_mlp.py_runr      s"    ^^TgF M    zdata CSV not found)reasonc                       e Zd Zd Zd Zy)TestAcceptanceMLPc                 f   t        |dz        }t        t        ddt        dd|g      }|j                  }d}||k(  }|st        j                  d|fd||f      d	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      t        j                  |      d
z  }t        j                  d|j                   d|j                         dz   d|iz  }t        t        j                  |            d x}x}}t         j"                  }|j$                  } ||      }	|	s t        j                  d      dz   dt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |	      dz  }t        t        j                  |            d x}x}}	t'        |      5 }
t)        j*                  |
      }d d d        j,                  }d} ||      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}x}}g }d}||v }|}|r|d   }t/        |      }d}||k\  }|}|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      nddz  }d d|iz  }|j1                  |       |rt        j                  d!fd"f      d#t        j                         v st        j                  t.              rt        j                  t.              nd#t        j                        t        j                  |      t        j                  |      d$z  }d%d&|iz  }|j1                  |       t        j2                  |d      i z  }d'd(|iz  }t        t        j                  |            d x}x}x}x}x}x}x}}|j,                  }d)} ||      }g d*}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }t        j                  d+|j-                  d)             d,z   d|iz  }t        t        j                  |            d x}x}x}x}}d-}|j,                  }d.}	i } ||	|      }||v }|st        j                  d|fd/||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |	      t        j                  |      t        j                  |      d0z  }d1d2|iz  }t        t        j                  |            d x}x}x}x}	x}}t4        j"                  j7                  dt8               dd3lm}m}  ||      }|d4   }||k(  }|st        j                  d|fd5||f      t        j                  |      d6t        j                         v st        j                  |      rt        j                  |      nd6d7z  }t        j                  d8      d9z   d:|iz  }t        t        j                  |            d x}}|d   D ]6  \  } }!t@        jB                  }t@        jD                  } ||       } ||      }"|"s]t        j                  d;      d<z   d=t        j                         v st        j                  t@              rt        j                  t@              nd=t        j                  |      d=t        j                         v st        j                  t@              rt        j                  t@              nd=t        j                  |      d>t        j                         v st        j                  |       rt        j                  |       nd>t        j                  |      t        j                  |"      d?z  }#t        t        j                  |#            d x}x}x}}"t@        jB                  }t@        jD                  } ||!      } ||      }"|"s]t        j                  d@      d<z   d=t        j                         v st        j                  t@              rt        j                  t@              nd=t        j                  |      d=t        j                         v st        j                  t@              rt        j                  t@              nd=t        j                  |      dAt        j                         v st        j                  |!      rt        j                  |!      ndAt        j                  |      t        j                  |"      d?z  }#t        t        j                  |#            d x}x}x}}"9 y # 1 sw Y   	9xY w)BNmlp_weights_TEST_6H.jsontools/train_mlp.py--data--smoke--outr   ==z2%(py2)s
{%(py2)s = %(py0)s.returncode
} == %(py5)sr   py0py2py5ztrain_mlp --smoke failed:


>assert %(py7)spy7zArtifact not writtenzd
>assert %(py7)s
{%(py7)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.path
}.exists
}(%(py5)s)
}osout)r   r   py4r   r"   version   )zI%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.get
}(%(py4)s)
} == %(py9)spayload)r   r   r%   py6py9zassert %(py11)spy11layers   in)z%(py3)s in %(py5)s)py3r   z%(py7)s)>=)z4%(py13)s
{%(py13)s = %(py9)s(%(py11)s)
} >= %(py16)slen)r*   r+   py13py16z%(py18)spy18zassert %(py21)spy21arch)1         r'   z%Smoke arch should be [49,8,4,1], got z
>assert %(py11)srecommended_thresholdstraining)zT%(py1)s in %(py11)s
{%(py11)s = %(py5)s
{%(py5)s = %(py3)s.get
}(%(py7)s, %(py9)s)
})py1r0   r   r"   r*   r+   zassert %(py13)sr3   )FEATURE_COLSload_mlp_artifactfeature_cols)z%(py1)s == %(py3)sr>   r=   r0   zArtifact feature_cols mismatch
>assert %(py5)sr   zW contains non-finite valuesz
>assert %(py10)s
{%(py10)s = %(py2)s
{%(py2)s = %(py0)s.all
}(%(py8)s
{%(py8)s = %(py5)s
{%(py5)s = %(py3)s.isfinite
}(%(py6)s)
})
}npW)r   r   r0   r   r)   py8py10zb contains non-finite valuesb)#strr   PYTHONDATA_6H
returncode
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_saferepr_format_assertmsgstdoutstderrAssertionError_format_explanationr#   pathexistsopenjsonloadgetr2   append_format_boolopsysinsertREPOstrategies.strategy_mlp_scoresr>   r?   rC   allisfinite)$selftmp_pathr$   r   @py_assert1@py_assert4@py_assert3@py_format6@py_format8@py_assert6fr(   @py_assert5@py_assert8@py_assert7@py_format10@py_format12@py_assert2@py_assert0@py_assert10@py_assert12@py_assert15@py_assert14@py_format17@py_format19@py_format20@py_format22@py_format14r>   r?   art@py_format4rD   rG   @py_assert9@py_format11s$                                       r   (test_smoke_train_produces_valid_artifactz:TestAcceptanceMLP.test_smoke_train_produces_valid_artifact$   sJ   (778v3)WcC D   	
A 	
 A% 	
 	
 A 	
 	
	6	
 	
   	
 	
 		  	
 	
 		 ! 	
 	
 		 %& 	
 	
  *&--6==/J	
 	
 	
 	
 	
 ww:w~~:~c":"::$:::::::r:::r:::w:::~::::::c:::c:::"::::::#Y!iilG  {{*9*{9%**%****%******w***w***{***9***%**********BxBx7"B78+<Bs+<'=BB'='BBBBBx7BBBxBBBBBB7BBB7BBBBBBB'=BBBBBBsBBBsBBB+<BBB'=BBBBBBBBBBBBBBBBB{{ 	
6 	
{6" 	
m 	
"m3 	
 	
"m 	
 	
	6	
 	
   	
 	
 		  	
 	
 		  	
 	
 		 " 	
 	
 		 # 	
 	
 		 '4 	
 	
  4GKK4G3HI	
 	
 	
 	
 	
 	
 (F7;;FzF2F;z2+FF'+FFFFF'+FFFF'FFFFFF7FFF7FFF;FFFzFFF2FFF+FFFFFFFFF4 R$>"T"l2TTT"lTTT"TTTTTTlTTTlTTTT4TTTTTTTMDAq66I"++I+a.I6.)I)II+IIIIIII2III2III6IIIIII"III"III+IIIIIIaIIIaIII.III)IIIIII66I"++I+a.I6.)I)II+IIIIIII2III2III6IIIIII"III"III+IIIIIIaIIIaIII.III)IIIIIII " Ys   n&&n0c                 |   t        |dz        }t        t        ddt        dd|g      }|j                  }d}||k(  }|st        j                  d|fd||f      d	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      t        j                  |      d
z  }t        j                  d|j                         dz   d|iz  }t        t        j                  |            d x}x}}t        |      5 }	t!        j"                  |	      }
d d d        
d   d   }||j%                  dd      |j%                  dd      |j%                  dd      |j%                  dd      |j%                  dd      |j%                  dd      dddd
}t        |dz        }t        |d      5 }	t!        j&                  ||	       d d d        t        t        ddt        ddd |gd!"      }|j                  }d}||k(  }|st        j                  d|fd||f      d#t        j                         v st        j                  |      rt        j                  |      nd#t        j                  |      t        j                  |      d
z  }t        j                  d$|j(                   d%|j                         dz   d|iz  }t        t        j                  |            d x}x}}|j(                  }d&}||v }|st        j                  d'|fd(||f      t        j                  |      d)t        j                         v st        j                  |      rt        j                  |      nd)d*z  }t        j                  d+|d d,        d-z   d.|iz  }t        t        j                  |            d x}}|j+                         D ]B  d/j-                         v st/        fd0d1D              s+t1        j2                  d2        D y # 1 sw Y   xY w# 1 sw Y   OxY w)3Nr   r   r   r   r   r   r   r   train_resultr   ztrain_mlp failed:
r!   r"   r<   r;   !i_long_entry_activation_thresholdg         i_long_exit_activation_threshold-i_long_exit_activation_confirmation_thresholdi_use_long_exit_confirmationg      ?i_use_long_entry_confirmationFi_trailing_stop_thresholdg     @)
mlp_weights_filer   r   r   r   r   r   i_regime_windowi_regime_entry_min_scorei_mvrv_suppress_bearztest_params.jsonwzstrategies/validate_strategy.pyz--strategy_filezstrategy_mlp_scores.pyz--params_filex   )r   validate_resultzvalidate_strategy failed:
r    zTotal Tradesr.   )z%(py1)s in %(py3)sout_textrA   z!Expected metrics in output, got:
i  rB   r   nanc              3   &   K   | ]  }|v  
 y w)N ).0kwlines     r   	<genexpr>zNTestAcceptanceMLP.test_validate_strategy_runs_with_artifact.<locals>.<genexpr>i   s     ,^B]BR4ZB]s   )CalmarSharpezP&LzNaN found in metric line: )rH   r   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rT   rU   rV   rY   rZ   r[   r\   dumprS   
splitlinesloweranypytestfail)re   rf   r$   r   rg   rh   ri   rj   rk   rm   r(   recparamsparams_filer   r   rt   rs   r   r   s                      @r   )test_validate_strategy_runs_with_artifactz;TestAcceptanceMLP.test_validate_strategy_runs_with_artifact?   s   (778V%9%w	7CI J&& 	
! 	
&!+ 	
 	
&! 	
 	
	6	
 	
   	
 	
 		  	
 	
 		 ' 	
 	
 		 +, 	
 	
  ","5"5!67	
 	
 	
 	
 	
 #Y!iilG j!":; #149\^a1b038Z\_0`=@WWEtvy=z,/GG4RTW,X-0WW5TV[-\),1Lc)R (/$)
 (%778+s#qIIfa  $ 5g7[	 

  )) 	
Q 	
)Q. 	
 	
)Q 	
 	
	6	
 	
   	
 	
 		  	
 	
 		 * 	
 	
 		 ./ 	
 	
  */*@*@)AODZDZC[\	
 	
 	
 	
 	
 #)) 	
~) 	
 	
~ 	
 	
 		  	
 	
	6	
 	
  "* 	
 	
 		 "* 	
 	
  1%40AB	
 	
 	
 	
 	
 '')D

$,^B],^)^8?@ *C Y  $#s    P$P1$P.1P;N)__name__
__module____qualname__r   r   r   r   r   r   r   !   s    J6+Ar   r   )__doc__builtinsrN   _pytest.assertion.rewrite	assertionrewriterL   rZ   r#   r	   r_   numpyrC   r   rW   abspathjoindirname__file__ra   rI   rJ   r   markslowskipifrX   r   r   r   r   <module>r      s      	  
  	wwrww||BGGOOH$=tDE	
'',,tV%=
>   w//8LMGA GA N GAr   