-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_markdown_conversion.py
More file actions
169 lines (132 loc) · 5.37 KB
/
test_markdown_conversion.py
File metadata and controls
169 lines (132 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import asyncio
import os
import time
import logging
import sys
from dotenv import load_dotenv
import unittest
from unittest.mock import patch, MagicMock
from src.agents.orchestrator_agent import OrchestratorAgent
from src.agents.content_writer_agent import ContentWriterAgent
from src.agents.document_structure_agent import DocumentStructureAgent
from src.models.report import ReportSection, ReportStructure
class TestMarkdownConversion(unittest.TestCase):
"""Test the markdown conversion functionality in the content writer agent."""
@classmethod
def setUpClass(cls):
"""Set up test environment."""
# Setup logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Load environment variables
load_dotenv('.env.local')
# Check for required API keys
if not os.getenv('OPENAI_API_KEY'):
raise Exception("Error: OPENAI_API_KEY not found in .env.local")
# Create output directory
os.makedirs('output', exist_ok=True)
os.makedirs('output/images', exist_ok=True)
os.makedirs('output/test', exist_ok=True)
async def test_markdown_conversion(self):
"""Test the conversion of all markdown elements to DOCX."""
# Initialize content writer agent
writer_agent = ContentWriterAgent()
# Create a predefined section with all markdown elements
section = ReportSection(
title="Markdown Conversion Test",
content=self._generate_test_markdown()
)
# Create a simple structure with one section
structure = ReportStructure(
title="Markdown Conversion Test",
sections=[
section
],
metadata={"template_type": "standard", "max_pages": 5, "test_mode": True}
)
# Sample research data
research = [
{
"title": "Markdown Test",
"content": "This is sample research content for testing markdown conversion.",
"source": "Test Source",
"credibility_score": 0.9
}
]
# Execute content writer
output_path = await writer_agent.execute({
"structure": structure,
"research": research,
"include_images": True
})
# Verify results
self.assertTrue(os.path.exists(output_path), f"Output file not found: {output_path}")
file_size = os.path.getsize(output_path) / 1024 # Convert to KB
print(f"Success! Test document generated at: {output_path}")
print(f"File size: {file_size:.2f} KB")
return output_path
def _generate_test_markdown(self):
"""Generate test markdown content with all supported elements."""
return """
# Main Heading
This is a paragraph with **bold text**, *italic text*, and `code`. This tests the basic formatting.
This paragraph has **bold text with *nested italic* inside it** and also *italic text with **nested bold** inside it*.
## Subheading Level 2
### Subheading Level 3
Here's a link to [OpenAI](https://openai.com) for testing link formatting. This sentence has **bold** and a [link](https://example.com) in the same paragraph.
#### Lists
Unordered list:
- Item 1 with **bold text**
- Item 2 with *italic text*
- Item 3 with `code` and [a link](https://example.com)
Numbered list with formatting:
1. First item with **bold**
2. Second item with *italic*
3. Third item with `code`
List with custom numbering (should be normalized):
41. This should be properly numbered as 1
42. This should be properly numbered as 2
43. This should be properly numbered as 3
#### Table
| Header 1 | Header 2 | Header 3 |
|----------|----------|----------|
| **Bold** | *Italic* | `Code` |
| [Link](https://example.com) | Mixed **bold** and *italic* | Data 3 |
| Data 4 | Data 5 | Data 6 |
#### Blockquote
> This is a blockquote to test how blockquotes are formatted in the document.
> It includes **bold text** and *italic text* to test inline formatting.
#### Code Block
```python
def hello_world():
print("Hello, World!")
return True
```
#### Image

#### Combined Elements
1. **Bold item** with a [link](https://example.com)
2. *Italic item* with `code snippet`
3. Mixed **bold** and *italic* formatting in the same line
Final paragraph with combined **bold**, *italic*, and `code` elements to test inline formatting handling.
"""
async def run_test():
"""Run the markdown conversion test."""
test = TestMarkdownConversion()
TestMarkdownConversion.setUpClass()
await test.test_markdown_conversion()
if __name__ == "__main__":
print("Starting markdown conversion test...")
start_time = time.time()
try:
asyncio.run(run_test())
except KeyboardInterrupt:
print("\nTest interrupted by user")
except Exception as e:
print(f"\nUnexpected error: {str(e)}")
logging.exception("Detailed error information:")
finally:
end_time = time.time()
print(f"\nTotal execution time: {end_time - start_time:.2f} seconds")