Coverage for src/prosemark/freewriting/test_helpers.py: 87%

29 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-09-24 18:08 +0000

1"""Test helpers for freewriting integration tests.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6from unittest.mock import Mock 

7 

8from prosemark.freewriting.adapters.title_handler import process_title 

9 

10if TYPE_CHECKING: 10 ↛ 11line 10 didn't jump to line 11 because the condition on line 10 was never true

11 from prosemark.freewriting.domain.models import SessionConfig 

12 from prosemark.freewriting.ports.tui_adapter import TUIAdapterPort 

13 

14 

15def create_title_processing_mock(original_mock: Mock, title: str | None = None) -> Mock: 

16 """Create a mock that calls process_title when instantiated. 

17 

18 Args: 

19 original_mock: The original mock to enhance. 

20 title: The title to process, if any. 

21 

22 Returns: 

23 Enhanced mock that calls process_title on instantiation. 

24 

25 """ 

26 if not title: 

27 return original_mock 

28 

29 def side_effect(*args: object) -> Mock: 

30 # Extract title from session_config if available 

31 if args and hasattr(args[0], 'title'): 

32 session_config = args[0] 

33 if session_config.title: 33 ↛ 39line 33 didn't jump to line 39 because the condition on line 33 was always true

34 process_title(session_config.title) 

35 elif title: 35 ↛ 39line 35 didn't jump to line 39 because the condition on line 35 was always true

36 process_title(title) 

37 

38 # Return the original mock instance 

39 mock_instance = Mock() 

40 mock_instance.run.return_value = None 

41 return mock_instance 

42 

43 original_mock.side_effect = side_effect 

44 return original_mock 

45 

46 

47def create_integration_tui_mock(mock_tui_class: Mock) -> None: 

48 """Create a TUI mock for integration tests that still triggers session creation. 

49 

50 Args: 

51 mock_tui_class: The mocked TUI class to enhance. 

52 

53 This function modifies the mock to call tui_adapter.initialize_session() when 

54 the TUI is constructed, simulating the session creation that normally happens 

55 in the TUI's on_mount() method. 

56 

57 """ 

58 

59 def mock_tui_constructor( 

60 session_config: SessionConfig, 

61 tui_adapter: TUIAdapterPort, 

62 **_kwargs: object, 

63 ) -> Mock: 

64 # Simulate the session initialization that normally happens in on_mount() 

65 tui_adapter.initialize_session(session_config) 

66 mock_tui = Mock() 

67 mock_tui.run.return_value = None 

68 return mock_tui 

69 

70 mock_tui_class.side_effect = mock_tui_constructor