
    =i                        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Z e	 ee
      j                         j                  j                        Zeej                  vrej                  j                  de       ddlmZmZmZmZmZ  ee
      j                  dz  Z ee
      j                  j                  dz  Z G d d      Z G d	 d
      Z G d d      Z G d d      Z G d d      Z G d d      Zy)z
Tests for impact_analyzer.py
    N)Path)analyzebuild_dependency_chainextract_endpointsextract_functionsextract_importsfixtureszimpact_analyzer.pyc                   .    e Zd ZdZd Zd Zd Zd Zd Zy)TestScenariosu   5개 핵심 시나리오 검증c                     t        t        dz        }t        |gt        t                    }d|d   v sJ d|d           y)uI   시나리오 1: sample_server.py 분석 시 /api/wiki/refine/start 포함sample_server.py	workspace/api/wiki/refine/startaffected_endpointsz<Expected /api/wiki/refine/start in affected_endpoints, got: NstrFIXTURES_DIRr   )selfserver_fileresults      A/home/jay/workspace/teams/shared/qc/tests/test_impact_analyzer.py#test_server_file_extracts_endpointsz1TestScenarios.test_server_file_extracts_endpoints'   sU    ,);;<+#l2CD %/C(DD	iI&QeJfIgh	iD    c                     t        t        dz        }t        |gt        t                    }d|d   v sJ d|d           y)u?   시나리오 2: sample_module.py 분석 시 extract_data 포함sample_module.pyr   extract_dataaffected_functionsz2Expected extract_data in affected_functions, got: Nr   )r   module_filer   s      r   #test_module_file_extracts_functionsz1TestScenarios.test_module_file_extracts_functions0   sU    ,);;<+#l2CD f%9::	_?G[@\?]^	_:r   c                     t        g t        t                    }|d   g k(  sJ |d   g k(  sJ |d   g k(  sJ |d   g k(  sJ |d   i k(  sJ y)uG   시나리오 3: 빈 파일 목록 → 빈 결과 반환 (에러 없음)r   changed_filesr   r   affected_importsdependency_chainN)r   r   r   r   r   s     r   test_empty_files_gracefulz'TestScenarios.test_empty_files_graceful9   sv    s<'89o&",,,*+r111*+r111()R///()R///r   c                 v    t        dgt        t                    }t        |t              sJ d|v sJ d|v sJ y)uC   시나리오 4: 존재하지 않는 파일 → 에러 없이 무시z/nonexistent/path/fake_file.pyr   r   r   N)r   r   r   
isinstancedictr%   s     r   test_nonexistent_file_ignoredz+TestScenarios.test_nonexistent_file_ignoredC   sH    -.,'

 &$'''#v---#v---r   c                    t        t        dz        t        t        dz        t        t        dz        g}t        j                         }t        |t        t                     t        j                         |z
  }|dk  sJ d|dd       y	)
u#   시나리오 5: 30초 이내 완료r   r   sample_utils.pyr      zExecution took z.2fzs, expected < 30sN)r   r   timer   )r   filesstartelapseds       r    test_execution_within_30_secondsz.TestScenarios.test_execution_within_30_secondsN   s~     112112001
 		\!23))+%|Mwsm;LMM|r   N)	__name__
__module____qualname____doc__r   r    r&   r*   r2    r   r   r   r   $   s!    )i_0	.Nr   r   c                       e Zd Zd Zd Zd Zy)TestExtractFunctionsFromASTc                 R    t        t        t        dz              }d|v sJ d|v sJ y )Nr,   helper_funcformat_outputr   r   r   r%   s     r   !test_extracts_top_level_functionsz=TestExtractFunctionsFromAST.test_extracts_top_level_functionsd   s3    "3|6G'G#HI&&&&(((r   c                 j    t        t        t        dz              }d|v sJ d|v sJ d|v sJ d|v sJ y )Nr   r   process_dataDataProcessorzDataProcessor.runr=   r%   s     r   test_extracts_class_methodsz7TestExtractFunctionsFromAST.test_extracts_class_methodsi   sO    "3|6H'H#IJ''''''&((("f,,,r   c                 (    t        d      }|g k(  sJ y N/nonexistent/file.py)r   r%   s     r   #test_nonexistent_file_returns_emptyz?TestExtractFunctionsFromAST.test_nonexistent_file_returns_emptyp       "#9:||r   N)r3   r4   r5   r>   rB   rF   r7   r   r   r9   r9   b   s    )
-r   r9   c                       e Zd Zd Zd Zd Zy)TestExtractImportsFromASTc                 F    t        t        t        dz              }d|v sJ y )Nr   sample_utilsr   r   r   r%   s     r   test_extracts_from_importsz4TestExtractImportsFromAST.test_extracts_from_importsw   s%     \4F%F!GH'''r   c                 R    t        t        t        dz              }d|v sJ d|v sJ y )Nsample_importer.pysample_modulerK   rL   r%   s     r   test_extracts_plain_importsz5TestExtractImportsFromAST.test_extracts_plain_imports{   s3     \4H%H!IJ&((('''r   c                 (    t        d      }|g k(  sJ y rD   )r   r%   s     r   rF   z=TestExtractImportsFromAST.test_nonexistent_file_returns_empty   s     !78||r   N)r3   r4   r5   rM   rQ   rF   r7   r   r   rI   rI   u   s    ((
r   rI   c                   $    e Zd Zd Zd Zd Zd Zy)TestExtractEndpointsc                 ^    t        t        t        dz              }d|v sJ d|v sJ d|v sJ y )Nr   r   z/api/wiki/refine/resumez/api/statusr   r   r   r%   s     r   'test_extracts_path_comparison_endpointsz<TestExtractEndpoints.test_extracts_path_comparison_endpoints   sA    "3|6H'H#IJ'6111(F222&&&r   c                 Z    t        t        t        dz              }t        |      dk\  sJ y )Nr      )r   r   r   lenr%   s     r   !test_extracts_all_three_endpointsz6TestExtractEndpoints.test_extracts_all_three_endpoints   s)    "3|6H'H#IJ6{ar   c                 (    t        d      }|g k(  sJ y rD   )r   r%   s     r   rF   z8TestExtractEndpoints.test_nonexistent_file_returns_empty   rG   r   c                 H    t        t        t        dz              }|g k(  sJ y )Nr,   rV   r%   s     r   )test_file_without_endpoints_returns_emptyz>TestExtractEndpoints.test_file_without_endpoints_returns_empty   s#    "3|6G'G#HI||r   N)r3   r4   r5   rW   r[   rF   r^   r7   r   r   rT   rT      s    ' r   rT   c                       e Zd Zd Zd Zd Zy)TestBuildDependencyChainc                    t        t        dz        }t        |gt        t                    }||v sJ ||   }|D cg c]!  }t        j                  j                  |      # c}t        fddD              s
J d        y c c}w )Nr   r   c              3   &   K   | ]  }|v  
 y w)Nr7   ).0nameimporter_namess     r   	<genexpr>zMTestBuildDependencyChain.test_finds_files_importing_module.<locals>.<genexpr>   s      
'+DN"
s   )r   rO   z"Expected importers in chain, got: )r   r   r   ospathbasenameany)r   r   chain	importerspre   s        @r   !test_finds_files_importing_modulez:TestBuildDependencyChain.test_finds_files_importing_module   s    ,);;<&}L@QRe###+&	7@A!"''**1-A 
/Y
 
 	A//?@	A 
 Bs   &Bc                 F    t        g t        t                    }|i k(  sJ y )Nr   )r   r   r   r   rk   s     r   $test_empty_files_returns_empty_chainz=TestBuildDependencyChain.test_empty_files_returns_empty_chain   s    &rS5FG{{r   c                 ^    t        dgt        t                    }t        |t              sJ y )NrE   r   )r   r   r   r(   r)   rp   s     r   (test_nonexistent_file_returns_empty_listzATestBuildDependencyChain.test_nonexistent_file_returns_empty_list   s'    &(>'?3|K\]%&&&r   N)r3   r4   r5   rn   rq   rs   r7   r   r   r`   r`      s    	A'r   r`   c                   (    e Zd ZdZdefdZdefdZy)TestCLIOutputJSONu!   CLI 실행 시 JSON 파일 출력tmp_pathc                    |dz  }t        t        dz        }t        j                  t        j
                  t        t              d|dt        |      dt        t              gddd      }|j                  d	k(  sJ d
|j                          |j                         sJ d       t        |      5 }t        j                  |      }d d d        dv sJ d|v sJ d|v sJ d|v sJ d|v sJ y # 1 sw Y   (xY w)Nimpact.jsonr   --files--output--workspaceTr-   capture_outputtexttimeoutr   zCLI failed: z Output JSON file was not createdr"   r   r   r#   r$   )r   r   
subprocessrunsys
executableANALYZER_PATH
returncodestderrexistsopenjsonload)r   rv   output_filer   r   fdatas          r   test_cli_produces_json_outputz/TestCLIOutputJSON.test_cli_produces_json_output   s   .,);;<M"K L!	  
    A%Efmm_'EE%!!#G%GG#+ 	 !99Q<D	  $&&&#t+++#t+++!T)))!T)))	  	 s   /C,,C5c                 R   |dz  }t        t        dz        }t        j                  t        j
                  t        t              d|dt        |      dt        t              gddd       t        |      5 }t        j                  |      }d d d        d	d
   v sJ y # 1 sw Y   xY w)Nrx   r   ry   rz   r{   Tr-   r|   r   r   )
r   r   r   r   r   r   r   r   r   r   )r   rv   r   r   r   r   s         r    test_cli_json_contains_endpointsz2TestCLIOutputJSON.test_cli_json_contains_endpoints   s    .,);;<M"K L!	  	
  + 	 !99Q<D	  (40D+EEEE	  	 s   5BB&N)r3   r4   r5   r6   r   r   r   r7   r   r   ru   ru      s     +*d *@F Fr   ru   )r6   r   rg   r   r   r.   pathlibr   pytestr   __file__resolveparent_PARENTrh   insertimpact_analyzerr   r   r   r   r   r   r   r   r9   rI   rT   r`   ru   r7   r   r   <module>r      s     	  
   
d8n$$&--44
5
#((HHOOAw  H~$$z1X%%,,/CC6N 6N| &   *' ',:F :Fr   