|
| 1 | +import json |
1 | 2 | from unittest.mock import MagicMock, patch |
2 | 3 |
|
3 | 4 | import pytest |
@@ -37,3 +38,62 @@ async def test_chunks(): |
37 | 38 | assert mock_chunker.config == mock_config |
38 | 39 | mock_chunker.chunk.assert_called() |
39 | 40 | assert mock_chunker.chunk.call_count == 2 |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.asyncio |
| 44 | +async def test_chunks_pipe(capsys): |
| 45 | + # Mock the Config object |
| 46 | + mock_config = MagicMock(spec=Config) |
| 47 | + mock_config.chunk_size = 2000 |
| 48 | + mock_config.overlap_ratio = 0.2 |
| 49 | + mock_config.files = ["file1.py"] |
| 50 | + mock_config.pipe = True |
| 51 | + |
| 52 | + # Mock the TreeSitterChunker |
| 53 | + mock_chunker = TreeSitterChunker(mock_config) |
| 54 | + mock_chunker.chunk = MagicMock() |
| 55 | + _chunks = [ |
| 56 | + Chunk("chunk1_file1", Point(0, 1), Point(0, 12), path="file1.py", id="c1"), |
| 57 | + Chunk("chunk2_file1", Point(1, 1), Point(1, 12), path="file1.py", id="c2"), |
| 58 | + ] |
| 59 | + mock_chunker.chunk.side_effect = [ |
| 60 | + _chunks, |
| 61 | + ] |
| 62 | + with patch( |
| 63 | + "vectorcode.subcommands.chunks.TreeSitterChunker", return_value=mock_chunker |
| 64 | + ): |
| 65 | + # Call the chunks function |
| 66 | + result = await chunks(mock_config) |
| 67 | + |
| 68 | + # Assertions |
| 69 | + assert result == 0 |
| 70 | + assert mock_chunker.config == mock_config |
| 71 | + mock_chunker.chunk.assert_called() |
| 72 | + assert mock_chunker.chunk.call_count == 1 |
| 73 | + |
| 74 | + captured = capsys.readouterr() |
| 75 | + output = json.loads(captured.out.strip()) |
| 76 | + assert output == [ |
| 77 | + [ |
| 78 | + { |
| 79 | + "text": "chunk1_file1", |
| 80 | + "start": { |
| 81 | + "row": 0, |
| 82 | + "column": 1, |
| 83 | + }, |
| 84 | + "end": {"row": 0, "column": 12}, |
| 85 | + "path": "file1.py", |
| 86 | + "chunk_id": "c1", |
| 87 | + }, |
| 88 | + { |
| 89 | + "text": "chunk2_file1", |
| 90 | + "start": { |
| 91 | + "row": 1, |
| 92 | + "column": 1, |
| 93 | + }, |
| 94 | + "end": {"row": 1, "column": 12}, |
| 95 | + "path": "file1.py", |
| 96 | + "chunk_id": "c2", |
| 97 | + }, |
| 98 | + ] |
| 99 | + ] |
0 commit comments