
    (iH                     "   d Z ddlZddlZddlZddlZddlZddlmZ ddlmZ dZ	dZ
ej                  j                  e
d      Zd Zd	 Zd
 Zd Zg Zdadad Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#e$dk(  r e#        yy)u   task-80.1 테스트: task-timer.py log 구조화 + anu-actions.log 이중기록

Tests the extended `log` command:
  - New parameters: --actor, --file, --action
  - Dual logging to anu-actions.log
  - Backward compatibility with old format
    N)datetime)Pathz(/home/jay/workspace/memory/task-timer.pyz /home/jay/workspace/memory/dailyzanu-actions.logc                     t         j                  t        dg| z   }t        j                  |dd      }|j
                  |j                  |j                  fS )zVRun task-timer.py log with given args.

    Returns (returncode, stdout, stderr).
    logT)capture_outputtext)sys
executableTIMER_SCRIPT
subprocessrun
returncodestdoutstderr)	args_listcmdresults      //home/jay/workspace/teams/dev1/test_task80_1.pyrun_log_cmdr      sF    
 >><
/)
;C^^C4@FfmmV]]::    c                      t        j                         j                  d      } t        j                  j                  t        |  d      S )z/Return the path for today's daily markdown log.z%Y-%m-%dz.md)r   nowstrftimeospathjoin	DAILY_DIR)date_strs    r   get_daily_log_pathr   $   s4    ||~&&z2H77<<	hZs#344r   c                     	 t        | dd      5 }|j                         cddd       S # 1 sw Y   yxY w# t        $ r g cY S w xY w)z=Read all lines from a file; return [] if file does not exist.rutf-8encodingN)open	readlinesFileNotFoundError)r   fs     r   read_file_linesr)   *   sF    $g. 	!!;;=	! 	! 	! 	s#   6 *	6 36 6 AAc                 h    t        |       }|| d D cg c]  }|j                  d       c}S c c}w )z@Return the last n lines of a file as a list of stripped strings.N
)r)   rstrip)r   nlinesls       r   last_n_linesr0   3   s0    D!E$)1"#J/qAHHTN///s   /Fc                      t         j                  j                  t              r.dat        t        dd      5 } | j                         addd       ydaday# 1 sw Y   yxY w)z;Back up anu-actions.log content (or note it did not exist).Tr!   r"   r#   NF)r   r   existsANU_ACTIONS_LOG_backup_existedr%   read_backup_contentr(   s    r   _backup_anu_actionsr8   B   sX     
ww~~o&/39 	'QffhO	' 	'  		' 	's   AA c                     t         r1t        t        dd      5 } | j                  t               ddd       yt
        j                  j                  t              rt        j                  t               yy# 1 sw Y   yxY w)z.Restore anu-actions.log to its pre-test state.wr"   r#   N)	r4   r%   r3   writer6   r   r   r2   remover7   s    r   _restore_anu_actionsr=   N   s\    /39 	%QGGO$	% 	% 77>>/*IIo& +	% 	%s   A66A?c                     t         j                  | ||f       |rdnd}|r"|s t        d| d|         t        d|        yt        d| d|         y)z2Record a test result and print immediate feedback.PASSFAILz  [z] z         Reason: N)_resultsappendprint)namepassedmessagestatuss       r   recordrH   X   s]    OOT67+,V6FvF82dV$%!'+,F82dV$%r   c                     | st        |      y)z8Raise AssertionError with message if condition is False.N)AssertionError)	conditionrF   s     r   assert_truerL   c   s    W%% r   c                     d} 	 t        dg      \  }}}t        |dk(  d| d|       	 t        j                  |      }t        |j                  d	      d
k(  d|       t        t                     }|D cg c]	  }d|v s| }}t        t        |      dkD  d       |d   }	t        d|	v d|	       t        d|	v d|	       t        d|	v d|	       t        | d       y# t        j                  $ r}t        d| d|      d}~ww xY wc c}w # t
        $ r }t        | dt        |             Y d}~yd}~ww xY w)zDTest 1: Basic backward compatibility - old format without new flags.u,   Test 1: 기본 하위 호환성 (old format)u   기존 형식 테스트r   Exit code should be 0, got 	. stderr=stdout is not valid JSON: 
stdout=NrG   loggedExpected status='logged', got uH   Daily log should contain '기존 형식 테스트' but it was not found.[anu]z=Daily log line should contain '[anu]' (default actor). Line: [-]z:Daily log line should contain '[-]' (default file). Line: [note]z?Daily log line should contain '[note]' (default action). Line: TFr   rL   jsonloadsJSONDecodeErrorrJ   getr)   r   lenrH   str
rD   rcr   r   dataedaily_linesr/   matchinglines
             r   test_01_basic_backward_compatrf   m   sz   9D"$(*C)DEFFB!G)"YvjA	C	V::f%D 	DHHX&(2,TH5	7 &&8&:;*M!.G1.LAMMCMA%V	X |GtOKD8T	VETMHQ	SH$MdXV	X 	tT1 ## 	V #=aS	&!TUU	V N"  $tUCF##$sL   %D C$ 8D 7	DDAD $D7DDD 	D>D99D>c                     d} 	 t        g d      \  }}}t        |dk(  d| d|       	 t        j                  |      }t        |j                  d	      d
k(  d|       t        t                     }|D cg c]	  }d|v s| }}t        t        |      dkD  d       |d   }	t        d|	v d|	       t        d|	v d|	       t        d|	v d|	       t        d|	v d|	       t        | d       y# t        j                  $ r}t        d| d|      d}~ww xY wc c}w # t
        $ r }t        | dt        |             Y d}~yd}~ww xY w)z;Test 2: Full structured log with --actor, --file, --action.u9   Test 2: 전체 구조화 로그 (--actor --file --action))   한정 위임 규칙 추가--actoranu--filez	CLAUDE.md--actioneditr   rN   rO   rP   rQ   NrG   rR   rS   rh   uL   Daily log should contain '한정 위임 규칙 추가' but it was not found.rT   rU   z-Daily log line should contain '[anu]'. Line: z[CLAUDE.md]z3Daily log line should contain '[CLAUDE.md]'. Line: z[edit]z.Daily log line should contain '[edit]'. Line: z1Daily log line should contain the message. Line: TFrX   r_   s
             r   test_02_full_structured_logrn      s   FD$$( *
 FF 	B!G)"YvjA	C	V::f%D 	DHHX&(2,TH5	7 &&8&:;*Q!.Kq.PAQQCMA%Z	\ |GtO;D8D	FMT)A$J	LH$<THE	G1T9?xH	J 	tT+ ## 	V #=aS	&!TUU	V R  $tUCF##$sM   &D' C6  8D' 8	D"D"A/D' 6D	DDD' '	E0EEc                     d} 	 t        g d      \  }}}t        |dk(  d| d|       	 t        j                  |      }t        |j                  d	      d
k(  d|       t        t                     }|D cg c]	  }d|v s| }}t        t        |      dkD  d       |d   }	t        d|	v d|	       t        d|	v d|	       t        d|	v d|	       t        | d       y# t        j                  $ r}t        d| d|      d}~ww xY wc c}w # t
        $ r }t        | dt        |             Y d}~yd}~ww xY w)z4Test 3: Partial parameters - only --action provided.u*   Test 3: 부분 파라미터 (--action 만))   리뷰 완료rl   reviewr   rN   rO   rP   rQ   NrG   rR   rS   rp   u)   Daily log should contain '리뷰 완료'.rT   rU   z&Default actor '[anu]' expected. Line: rV   z#Default file '[-]' expected. Line: z[review]z+Explicit action '[review]' expected. Line: TFrX   r_   s
             r   "test_03_partial_params_only_actionrr      ss   7D$()PQFFB!G)"YvjA	C	V::f%D 	DHHX&(2,TH5	7 &&8&:;*C!o.BACCCMA%7	9 |GtO4TH=	?ETM1$:	<J$&9$B	D 	tT) ## 	V #=aS	&!TUU	V D  $tUCF##$sM   &D C%  8D 8	DDAD %D8D		DD 	D?D::D?c            	         d} 	 d}t        |ddddddg      \  }}}t        |d	k(  d
| d|       t        t        j                  j	                  t
              dt
                t        t
              }|D cg c]  }||v s|j                  d       }}t        t        |      d	kD  d| d       |d   }t        t        j                  d|      d|       t        d|v d|       t        d|v d|       t        d|v d|       t        ||v d|       t        | d       yc c}w # t        $ r }	t        | dt        |	             Y d}	~	yd}	~	ww xY w)z?Test 4: anu-actions.log creation and format after any log call.u)   Test 4: anu-actions.log 생성 및 형식u   anu-actions-log-형식-검증ri   rj   rl   testrk   ztest.pyr   rN   rO    anu-actions.log should exist at r+   'anu-actions.log should contain marker ''.rT   z(^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]z@Line should start with a timestamp [YYYY-MM-DD HH:MM:SS]. Line: rU   z3anu-actions.log line should contain '[anu]'. Line: z[test]z=anu-actions.log line should contain '[test]' (action). Line: z	[test.py]z>anu-actions.log line should contain '[test.py]' (file). Line: z7anu-actions.log line should contain the message. Line: TFN)r   rL   r   r   r2   r3   r)   r,   r]   rematchrH   rJ   r^   
rD   markerr`   r   r   r.   r/   rd   re   rb   s
             r   test_04_anu_actions_logr|      s   6D%$0(ui	*
 FF 	B!G)"YvjA	C 	BGGNN?3..?@	B  0,1AqVq[AHHTNAACMA%5fXR@	B | 	BHHH$ONthW	YGtOA$J	LH$KD8T	VK4'LTHU	WFdNEdXN	P 	tT% B&  $tUCF##$s1   A4D1 8	D,D,BD1 ,D1 1	E:EEc            	         d} 	 d}t        |ddddddg      \  }}}t        |dk(  d	| d
|       	 t        j                  |      }t        |j                  d      dk(  d|       dj                  t        t                           }t        ||v d| d       |j                  d      }|j                  |      }	t        |dk7  d       t        |	|kD  d| d|	        t        | d       y# t        j                  $ r}t        d| d|      d}~ww xY w# t
        $ r }t        | dt        |             Y d}~yd}~ww xY w)z;Test 5: Existing --type flag routes to the correct section.u)   Test 5: 기존 --type 플래그 호환성u   결정-사항-테스트-마커--typedecisionri   rj   rl   r   rN   rO   rP   rQ   NrG   rR   rS    z.Daily log should contain the decision marker 'rw   u   ## 의사결정rT   uF   Daily log should have '## 의사결정' section after --type decision.uB   Marker should appear after '## 의사결정' section. section_pos=z, marker_pos=TF)r   rL   rY   rZ   r[   rJ   r\   r   r)   r   findrfindrH   r^   )
rD   r{   r`   r   r   ra   rb   daily_contentdecision_section_pos
marker_poss
             r   test_05_type_flag_still_worksr     s   6D&$1(ju
	*
 FF 	B!G)"YvjA	C	V::f%D 	DHHX&(2,TH5	7 0B0D EFFm+<VHBG	I
  -112CD"((0
(B.T	VJ!55/0j\K	L 	tT- ## 	V #=aS	&!TUU	V.  $tUCF##$s;   -D C/ B'D /DDDD 	E$D??Ec            
         d} 	 t         j                  j                  t              rt	        t              ng }t        |      }g d}|D ]/  \  }}t        |d|g      \  }}}t        |dk(  d| d| d|       1 t        t         j                  j                  t              dt                t	        t              }	t        |	      }
t        |
|t        |      z   k\  d	t        |       d
| d|
        dj                  |	      }|D ]  \  }}t        ||v d| d        t        | d       y# t        $ r }t        | dt        |             Y d}~yd}~ww xY w)zJTest 6: Multiple sequential log calls accumulate lines in anu-actions.log.u1   Test 6: 순차적 로그 누적 (anu-actions.log)))u   첫 번째 누적 테스트r5   )u   두 번째 누적 테스트r;   )u   세 번째 누적 테스트verifyrl   r   zlog call failed for 'z': rc=z	, stderr=ru   z.anu-actions.log should have grown by at least z lines. Before=z, After=r   z anu-actions.log should contain 'z' after sequential calls.TFN)r   r   r2   r3   r)   r]   r   rL   r   rH   rJ   r^   )rD   lines_beforecount_beforemessagesmsgactionr`   r   r   lines_aftercount_aftercontent_after_rb   s                 r   +test_06_multiple_sequential_logs_accumulater   :  sr   >D"$;=77>>/;Z7`b<(
 $ 	LKC!,c:v-F!GBa'uF2$izJL	L
 	BGGNN?3..?@	B &o6+&K<#h-#??<S]O L"^8K=:	;
 , 	SFC},23%7PQS	S 	tT $tUCF##$s   D+D0 0	E9EEc                  R   d} 	 t        g d      \  }}}t        |dk(  d| d|       	 t        j                  |      }t        |j                  d	      d
k(  d|       t        |j                  d      dk(  d|       t        |j                  d      dk(  d|       t        |j                  d      dk(  d|       t        | d       y# t        j                  $ r}t        d| d|      d}~ww xY w# t
        $ r }t        | dt        |             Y d}~yd}~ww xY w)z8Test 7: JSON output reflects actor, file, action fields.u6   Test 7: JSON 출력에 actor/file/action 필드 포함)u   JSON 필드 검증ri   dev1rk   	README.mdrl   creater   rN   rO   rP   rQ   NrG   rR   rS   actorr   z*Expected actor='dev1' in JSON output, got filer   z.Expected file='README.md' in JSON output, got r   r   z-Expected action='create' in JSON output, got TF)	r   rL   rY   rZ   r[   rJ   r\   rH   r^   )rD   r`   r   r   ra   rb   s         r   .test_07_json_output_contains_actor_file_actionr   b  sG   CD$( *
 FF 	B!G)"YvjA	C	V::f%D 	DHHX&(2,TH5	7DHHW%/8A	CDHHV$3<THE	GDHHX&(2;D8D	F 	tT ## 	V #=aS	&!TUU	V  $tUCF##$s;   &C= C  BC= C:$C55C::C= =	D&D!!D&c                      d} 	 t        g       \  }}}t        |dk7  d| d       t        | d       y# t        $ r }t        | dt	        |             Y d}~yd}~ww xY w)zCTest 8: Calling log with no message should exit with non-zero code.u1   Test 8: 메시지 없이 호출 시 오류 종료r   z9Expected non-zero exit code when message is missing, got .TFNr   rL   rH   rJ   r^   rD   r`   r   r   rb   s        r   %test_08_missing_message_exits_nonzeror     sd    >D$(_FFB!GGt1M	OtT $tUCF##$s   .3 	AAAc                      d} 	 t        g d      \  }}}t        |dk7  d| d       t        | d       y# t        $ r }t        | dt	        |             Y d}~yd}~ww xY w)	z<Test 9: Invalid --type value should exit with non-zero code.u.   Test 9: 잘못된 --type 값 시 오류 종료)u	   테스트r~   invalid_xyzr   z4Expected non-zero exit code for invalid --type, got r   TFNr   r   s        r   "test_09_invalid_type_exits_nonzeror     sf    ;D$()OPFFB!GB2$aH	JtT $tUCF##$s   05 	AAAc                  F   d} 	 d}t        |g      \  }}}t        |dk(  d| d|       t        t        j                  j	                  t
              dt
                t        t
              }|D cg c]  }||v s|j                  d       }}t        t        |      dkD  d| d	       |d
   }t        d|v d|       t        d|v d|       t        d|v d|       t        | d       yc c}w # t        $ r }	t        | dt        |	             Y d}	~	yd}	~	ww xY w)zITest 10: anu-actions.log uses defaults even when no --actor/--file given.u5   Test 10: anu-actions.log 기본값 (플래그 없음)u   기본값-anu-actions-검증r   rN   rO   ru   r+   rv   rw   rT   rU   z9Default actor '[anu]' expected in anu-actions.log. Line: rV   z6Default file '[-]' expected in anu-actions.log. Line: rW   z;Default action '[note]' expected in anu-actions.log. Line: TFN)r   rL   r   r   r2   r3   r)   r,   r]   rH   rJ   r^   rz   s
             r   2test_10_anu_actions_log_format_no_actor_file_flagsr     sA   BD$/(&2FFB!G)"YvjA	CBGGNN?3..?@	B  0,1AqVq[AHHTNAACMA%5fXR@	B |GtOGxP	RETMDTHM	OH$I$R	T 	tT B  $tUCF##$s1   A.C7 2	C2<C2A"C7 2C7 7	D  DD c                     t        d       t        d       t        d       t        dt                t        dt                t        dt                t        dt	                       t                t
        j                  j                  t              s't        dt                t        j                  d       t                	 t        d	       t                t                t                t                t                t!                t#                t%                t'                t)                t+                t                t        d       t,        D  cg c]
  } | d
   s	|  }} t,        D  cg c]
  } | d
   r	|  }} t/        t,              }t        dt/        |       d| dt/        |       d| d	       |r?t                t        d       |D ]%  \  }}}t        d|        |st        d|        ' t        d       t        j                  |sd       y d
       y # t+                w xY wc c} w c c} w )Nz<============================================================uB   task-80.1 테스트: task-timer.py log 구조화 + anu-actions.logzTIMER_SCRIPT  : zDAILY_DIR     : zANU_ACTIONS   : zTODAY LOG     : z"FATAL: task-timer.py not found at    z-- Running tests --   z	Summary: /z	 passed, z failedzFailed tests:z  - z    r   )rC   r   r   r3   r   r   r   r2   r	   exitr8   rf   rn   rr   r|   r   r   r   r   r   r   r=   rA   r]   )r!   rE   failedtotalrD   r   r   s          r   mainr     s   	(O	
NO	(O	\N
+,	YK
()	_-
./	/12
34	G 77>>,'2<.AB #$%'#%*,!%'3568-/*,:< 		G	(O!*AQqTa*F*!.A1a.F.ME	Ic&k]!E7)CK=%
PQo" 	$LD!SD- SEl#	$ 
(OHHfQ$!$# 	 +.s%   A/H1 #
I .I <
II1H=__main__)r   )%__doc__rY   r   rx   r   r	   r   pathlibr   r   r   r   r   r3   r   r   r)   r0   rA   r6   r4   r8   r=   rH   rL   rf   rn   rr   r|   r   r   r   r   r   r   r   __name__ r   r   <module>r      s     	 	  
  9.	'',,y*;<;50 	'&&%$P'$T!$H($V)$X%$P$B	$	$$F1%h zF r   