
    '|is[                         d Z ddlZddlZddlZddlmZ ddlmZ ddlZddl	m
Z
mZ ddlmZmZ  G d d      Z G d	 d
      Z G d d      Z G d d      Zedk(  r ej(                  edg       yy)aV  
Test for task-131.1: Project Memory Separation in team_prompts.py and dispatch.py

This test module validates the project-specific memory separation functionality:
- Ensures system tasks use WORKSPACE_ROOT/memory/
- Ensures project tasks use PROJECTS_ROOT/{project_id}/memory/
- Verifies path formats for reports, plans, and isolation rules
    N)Path)mock)build_prompt	TEAM_INFO)PROJECTS_ROOTregister_batch_taskc                   ^    e Zd ZdZej
                  d        Zd Zd Zd Z	d Z
d Zd Zd	 Zy
)&TestTeamPromptsProjectMemorySeparationz<Test team_prompts.py project memory separation functionalityc              #   d  K   t        j                         }t        j                         }t        |dd      j                  dd       t        |dd      j                  dd       t        |dd      j                  dd       t        |dd      j                  dd       t        |dd	      j                  dd       t        |d
dd      j                  dd       t        |d
d      j                  dd       ||d ddl}|j                  |d       |j                  |d       yw)z3Create temporary workspace and projects directoriesmemorytasksTparentsexist_okreportseventsteamsdev1dev3insuwikiplans	workspaceprojectsr   Nignore_errorstempfilemkdtempr   mkdirshutilrmtreeselfworkspace_tmpprojects_tmpr!   s       1/home/jay/workspace/teams/dev1/test_task_131_1.py	temp_dirsz0TestTeamPromptsProjectMemorySeparation.temp_dirs   s&     !((*'') 	]Hg.44TD4Q]Hi066td6S]Hh/55dT5R]GV,224$2O]GV,224$2O 	\:x;AA$Y]A^\:w/55dT5R '$
 	
 	m48l$7s   D.D0c                 <   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}d}|j                  d||d	d
      }|d    d| d}||v sJ d| d       	 ddd       y# 1 sw Y   yxY w)zKTest that system tasks (project_id=None) use WORKSPACE_ROOT/memory/reports/r   r   WORKSPACE_ROOTr   r   Ntask-1.1zSystem task test	dev1-teamnormallevel
project_id/memory/reports/.mdExpected report path  not found in prompt
r   patchdictosenviron	importlibprompts.team_promptsteam_promptsreloadr   )r$   r(   r;   team_prompts_moduletask_id	task_descpromptexpected_report_paths           r'   test_system_task_report_pathzCTestTeamPromptsProjectMemorySeparation.test_system_task_report_path5   s    ZZ__JJ"+K"8!*:!6
 	 >01 G*I(55WixD 6 F
 [)**:7)3G ! (61 '(<'==QR1-	 	 	   ABBc                 F   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}d}d}|j                  d	||d
|      }|d    d| d| d}||v sJ d| d       	 ddd       y# 1 sw Y   yxY w)zFTest that project tasks use PROJECTS_ROOT/{project_id}/memory/reports/r   r   r*   r   Ntask-2.1zProject task testr   r-   r.   r/   /r2   r3   r4   r5   r6   )	r$   r(   r;   r?   r@   rA   r1   rB   rC   s	            r'   test_project_task_report_pathzDTestTeamPromptsProjectMemorySeparation.test_project_task_report_pathQ   s    ZZ__JJ"+K"8!*:!6
 	 >01 G+I#J(55WixJ 6 F
 Z():,6FwisS ! (61 '(<'==QR1-	 	 	s   ABB c                 2   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}d}|j                  d||d	d
      }d| d}||v sJ d| d       	 ddd       y# 1 sw Y   yxY w)zRTest that system tasks (project_id=None) use teams/dev{N}/plan-{task_id}.md formatr   r   r*   r   Nr,   zSystem task with planr-   r.   r/   teams/dev1/plan-r3   Expected plan path r5   r6   )r$   r(   r;   r?   r@   rA   rB   expected_plan_paths           r'   test_system_task_plan_pathzATestTeamPromptsProjectMemorySeparation.test_system_task_plan_pathm   s    ZZ__JJ"+K"8!*:!6
 	 >01 G/I(55WixD 6 F $4G9C!@%/ %&8%99MN/'	 	 	s   ABBc                 <   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}d}d}|j                  d	||d
|      }d| d| d}||v sJ d| d       	 ddd       y# 1 sw Y   yxY w)zPTest that project tasks use projects/{project_id}/plans/plan-{task_id}.md formatr   r   r*   r   NrG   zProject task with planr   r-   r.   r/   	projects//plans/plan-r3   rL   r5   r6   	r$   r(   r;   r?   r@   rA   r1   rB   rM   s	            r'   test_project_task_plan_pathzBTestTeamPromptsProjectMemorySeparation.test_project_task_plan_path   s    ZZ__JJ"+K"8!*:!6
 	 >01 G0I#J(55WixJ 6 F $-ZLWIS!Q%/ %&8%99MN/)	 	 	rE   c                 <   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}d}d}|j                  d	||d
|      }d| d| d}||v sJ d| d       	 ddd       y# 1 sw Y   yxY w)zUTest dev3-team + project_id uses projects/{project_id}/plans/plan-{task_id}.md formatr   r   r*   r   Nztask-3.1zGLM project task with planr   	dev3-teamr.   r/   rP   rQ   r3   rL   z" not found in prompt for dev3-teamr6   rR   s	            r'   test_glm_project_plan_pathzATestTeamPromptsProjectMemorySeparation.test_glm_project_plan_path   s    ZZ__JJ"+K"8!*:!6
 	 >01 G4I#J(55WixJ 6 F $-ZLWIS!Q%/ %&8%99[\/)	 	 	rE   c                 V   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}d}d}|j                  d	||d
|      }|d    d| d}||v sJ d| d       d|v sJ d       	 ddd       y# 1 sw Y   yxY w)zPTest that project tasks use PROJECTS_ROOT in isolation rule (not WORKSPACE_ROOT)r   r   r*   r   NrG   zProject isolation testr   r-   r.   r/   rH   zExpected PROJECTS_ROOT path z not found in isolation ruleu   프로젝트 격리z'Project isolation rule marker not foundr6   )	r$   r(   r;   r?   r@   rA   r1   rB   expected_isolations	            r'   .test_project_isolation_rule_uses_projects_rootzUTestTeamPromptsProjectMemorySeparation.test_project_isolation_rule_uses_projects_root   s    ZZ__JJ"+K"8!*:!6
 	^ >01 G0I#J(55WixJ 6 F Z():,a8  &/ ./A.BB^_/ )F2]4]]2=	^ 	^ 	^s   ABB(c                    t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}d}|j                  d||d	d
      }|d    d|v sJ d       |d    d| d}||v sJ d| d       |d    d| d}||v sJ d| d       	 ddd       y# 1 sw Y   yxY w)zRTest that system tasks (project_id=None) use existing WORKSPACE_ROOT/memory/ pathsr   r   r*   r   Nr,   zSystem task paths testr-   r.   r/   z/memory/z4WORKSPACE_ROOT memory path not found for system taskz/memory/events/z.donezExpected done_file path r5   z/memory/tasks/r3   zExpected task_file path r6   )	r$   r(   r;   r?   r@   rA   rB   expected_done_fileexpected_task_files	            r'    test_system_task_paths_unchangedzGTestTeamPromptsProjectMemorySeparation.test_system_task_paths_unchanged   s+   ZZ__JJ"+K"8!*:!6
 !	 >01 G0I(55WixD 6 F
  ,-X6&@ F@
 %.k$:#;?7)SX!Y%/ *+=*>>RS/
 %.k$:#;>'RU!V%/ *+=*>>RS/?!	 !	 !	s   A;B==CN)__name__
__module____qualname____doc__pytestfixturer(   rD   rI   rN   rS   rV   rY   r]        r'   r
   r
      s@    F^^8 8488244 ^D#re   r
   c                   F    e Zd ZdZd Zej                  d        Zd Zd Z	y)#TestDispatchProjectMemorySeparationz8Test dispatch.py project memory separation functionalityc                 t    ddl }t        |d      sJ d       t        |j                  t              sJ d       y)z4Test that dispatch.py defines PROJECTS_ROOT variabler   Nr   z.dispatch.py must define PROJECTS_ROOT variablez%PROJECTS_ROOT should be a Path object)dispatchhasattr
isinstancer   r   )r$   dispatch_modules     r'   "test_projects_root_variable_existszFTestDispatchProjectMemorySeparation.test_projects_root_variable_exists  sA    *8 	
<	
8 /77> 	
3	
>re   c              #   8  K   t        j                         }t        j                         }t        |dd      j                  dd       t        |ddd      j                  dd       ||d dd	l}|j                  |d
       |j                  |d
       y	w)zFCreate temporary workspace and projects directories for dispatch testsr   batchesTr   r   r   r   r   Nr   r   r#   s       r'   temp_dispatch_dirsz6TestDispatchProjectMemorySeparation.temp_dispatch_dirs  s      !((*'') 	]Hi066td6S\:x;AA4 	B 	

 '$
 	
 	m48l$7s   BBc                    t         j                  j                  t        j                  |d   |d   d      5  ddl}ddl}|j                  |       d}d}d}d	}|j                  ||||
       t        |d         dz  dz  | dz  }|j                         sJ d| d       t        |dd      5 }	t        j                  |	      }
ddd       d
v sJ d       t        |
d         dkD  sJ d       |
d   d   }|d   |k(  sJ |d   |k(  sJ |d    d| d| d}|d   |k(  sJ d| d|d           	 ddd       y# 1 sw Y   xxY w# 1 sw Y   yxY w)zCTest that register_batch_task accepts and uses project_id parameterr   r   r*   r   Nzbatch-20260302-001r,   r-   r   r1   r   ro   .jsonBatch file  was not createdrutf-8encodingr   z'Batch data should contain 'tasks' arrayzTasks array should not be emptyr@   teamrH   r2   r3   report_pathExpected report_path , got )r   r7   r8   r9   r:   r;   ri   r>   r   r   existsopenjsonloadlen)r$   rp   r;   rl   batch_idr@   team_idr1   
batch_filef
batch_data
task_entryrC   s                r'   (test_register_batch_task_with_project_idzLTestDispatchProjectMemorySeparation.test_register_batch_task_with_project_id&  s   ZZ__JJ"4["A!3J!?
 .	 ._-+H G!G#J //'7z 0  '45 Je$%  $$&R+j\AQ(RR& j#8 *A!YYq\
* j(S*SS(z'*+a/R1RR/#G,Q/Ji(G333f%000 &j12!J<?OPWyX[\ ! m,0DD '(<'=VJ}D]C^_DY.	 .	>* *?.	 .	s%   A:E2D?A-E?E	EEc                 (   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddl}|j                  |       d}d}d}|j                  |||       t        |d         d	z  d
z  | dz  }|j                         sJ d| d       t        |dd      5 }t        j                  |      }	ddd       	d   d   }
d| d}|
d   |k(  sJ d| d|
d           	 ddd       y# 1 sw Y   9xY w# 1 sw Y   yxY w)zETest that register_batch_task works without project_id (system tasks)r   r   r*   r   Nzbatch-20260302-002ztask-1.2r-   r   ro   rs   rt   ru   rv   rw   rx   r   zmemory/reports/r3   r{   r|   r}   )r   r7   r8   r9   r:   r;   ri   r>   r   r   r~   r   r   r   )r$   rp   r;   rl   r   r@   r   r   r   r   r   rC   s               r'   +test_register_batch_task_without_project_idzOTestDispatchProjectMemorySeparation.test_register_batch_task_without_project_idX  s`   ZZ__JJ"4["A!3J!?
 #	 ._-+H G!G //'7K '45 Je$%  $$&R+j\AQ(RR& j#8 *A!YYq\
* $G,Q/J%4WIS#A m,0DD '(<'=VJ}D]C^_DC#	 #	8* *9#	 #	s$   A6D.C<.D<D	DDN)
r^   r_   r`   ra   rm   rb   rc   rp   r   r   rd   re   r'   rg   rg     s-    B	
 ^^8 8*0d%re   rg   c                   L    e Zd ZdZej
                  d        Zd Zd Zd Z	d Z
y)TestPathFormatsz;Test path format consistency across teams and project typesc              #     K   t        j                         }t        j                         }t        |dd      j                  dd       t        |dd      j                  dd       t        |dd      j                  dd       t        |dd      j                  dd       t        |dd	      j                  dd       t        |d
dd      j                  dd       t        |d
d      j                  dd       t        |ddd      j                  dd       t        |dd      j                  dd       ||d ddl}|j                  |d       |j                  |d       yw)z2Create temporary directories for path format testsr   r   Tr   r   r   r   dev2r   r   r   project2r   r   Nr   r   r#   s       r'   temp_path_dirszTestPathFormats.temp_path_dirs  so     !((*'') 	]Hg.44TD4Q]Hi066td6S]GV,224$2O]GV,224$2O]GV,224$2O 	\:x;AA4 	B 	
 	\:w/55dT5R\:x;AA4 	B 	
 	\:w/55dT5R '$
 	
 	m48l$7s   E+E-c                 &   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}|j                  d|dd	      }d
|v sJ |d    d|v sJ 	 ddd       y# 1 sw Y   yxY w)z Test dev1-team system task pathsr   r   r*   r   Nr,   r-   	Test taskrr   rK   r2   r6   r$   r   r;   r?   r@   rB   s         r'   test_dev1_team_system_pathsz+TestPathFormats.test_dev1_team_system_paths      ZZ__JJ"0"=!/
!;
 	N >01 G(55Wkd 6 F &///$[122BCvMMM#	N 	N 	N   ABBc                 &   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}|j                  d|dd	      }d
|v sJ |d    d|v sJ 	 ddd       y# 1 sw Y   yxY w)z Test dev2-team system task pathsr   r   r*   r   Nr,   	dev2-teamr   rr   zteams/dev2/plan-r2   r6   r   s         r'   test_dev2_team_system_pathsz+TestPathFormats.test_dev2_team_system_paths  r   r   c                 &   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}|j                  d|dd	      }d
|v sJ |d    d|v sJ 	 ddd       y# 1 sw Y   yxY w)z Test dev3-team system task pathsr   r   r*   r   Nr,   rU   r   rr   zteams/dev3/plan-r2   r6   r   s         r'   test_dev3_team_system_pathsz+TestPathFormats.test_dev3_team_system_paths  r   r   c                 n   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}d}dD ]D  }|j                  ||d	|
      }|d    d| d}d| d}	||v s
J | d       |	|v r<J | d        	 ddd       y# 1 sw Y   yxY w)z0Test that all teams use consistent project pathsr   r   r*   r   Nr   r,   )r-   r   rU   r   rr   rH   r2   rP   rQ   z  should use projects report pathz should use projects plan pathr6   )
r$   r   r;   r?   r1   r@   r   rB   expected_reportexpected_plans
             r'   test_all_teams_project_pathsz,TestPathFormats.test_all_teams_project_paths  s   ZZ__JJ"0"=!/
!;
 	 >01#J GB ,99Wkj :  &j12!J<?OP   #,J<| D&&0 i?@0 %. i=>.	 	 	s   AB+B++B4N)r^   r_   r`   ra   rb   rc   r   r   r   r   r   rd   re   r'   r   r     s3    E^^8 8@N*N*N* re   r   c                   @    e Zd ZdZej
                  d        Zd Zd Zy)TestMemorySeparationIntegrationzAIntegration tests for memory separation across system and projectc              #   ^  K   t        j                         }t        j                         }dD ]  }t        ||      j                  dd       ! dD ]'  }dD ]   }t        |||      j                  dd       " ) ||d ddl}|j                  |d	       |j                  |d	       yw)
z2Create temporary directories for integration tests)zmemory/tasksmemory/reportszmemory/eventsz
teams/dev1z
teams/dev2z
teams/dev3Tr   )r   r   )r   r   r   r   Nr   r   )r$   r%   r&   subdirprojr!   s         r'   temp_integration_dirsz5TestMemorySeparationIntegration.temp_integration_dirs	  s      !((*'') t 	KF'--dT-J	K - 	TD5 T\4066td6ST	T
 '$
 	
 	m48l$7s   B+B-c                 v   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}|j                  d|dd	      }|j                  d|d
d	      }|d    d|v sJ |d   |vsd|vsJ |d    d|v sJ 	 ddd       y# 1 sw Y   yxY w)zHTest that system and project tasks have completely isolated memory pathsr   r   r*   r   Nr,   r-   zSystem taskrr   zProject taskr   z/memory/reportsu   격리	/insuwikir6   )r$   r   r;   r?   r@   system_promptproject_prompts          r'   &test_system_and_project_tasks_isolatedzFTestMemorySeparationIntegration.test_system_and_project_tasks_isolated!  s    ZZ__JJ"7"D!6z!B
 	U >01 G 0<<Wm = M 1==Wn > N
 ,K89I]ZZZ(4MIX]jMjjj ,J78	BnTTT5	U 	U 	Us   A-B//B8c                 t   t         j                  j                  t        j                  |d   |d   d      5  ddl}ddlm} |j                  |       d}|j                  d|dd	
      }|j                  d|dd
      }|d    d|v sJ |d    d|v sJ d	|v sJ d|v sJ 	 ddd       y# 1 sw Y   yxY w)z7Test that different projects use different memory pathsr   r   r*   r   Nr,   r-   zInsuWiki taskr   rr   zProject2 taskr   r   z	/project2r6   )r$   r   r;   r?   r@   insuwiki_promptproject2_prompts          r'   *test_multiple_projects_use_different_pathszJTestMemorySeparationIntegration.test_multiple_projects_use_different_paths?  s    ZZ__JJ"7"D!6z!B
 	1 >01 G 2>>Wo* ? O 2>>Wo* ? O
 ,J78	BoUUU+J78	BoUUU 0000007	1 	1 	1s   A,B..B7N)	r^   r_   r`   ra   rb   rc   r   r   r   rd   re   r'   r   r     s&    K^^8 8.U<1re   r   __main__z-v)ra   r   r9   r   pathlibr   unittestr   rb   r<   r   r   ri   r   r   r
   rg   r   r   r^   main__file__rd   re   r'   <module>r      s|     	     9 7h hV{ {|C CLV1 V1r zFKK4 ! re   