Skip to content

Commit 253e6f5

Browse files
authored
Improve PlanChunking validation and tests (#34)
1 parent 9d2fd06 commit 253e6f5

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Tests/ToolImplementationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ public void SuggestTranspilerTargets_WithInvalidMemberId_ReturnsError()
13751375
Assert.Equal("error", response.GetProperty("status").GetString());
13761376
}
13771377

1378-
[Fact(Skip = "PlanChunking tool currently returns error for valid inputs")]
1378+
[Fact]
13791379
public void PlanChunking_WithValidMember_ReturnsChunkPlan()
13801380
{
13811381
// Arrange - find a method from the test assembly
@@ -1536,7 +1536,7 @@ internal record ChunkInfo(int StartLine, int EndLine, int EstimatedChars);
15361536

15371537
internal record ChunkPlanResult(
15381538
string MemberId,
1539-
List<ChunkInfo> Chunks,
1539+
ChunkInfo[] Chunks,
15401540
int TotalLines,
15411541
int EstimatedChars,
15421542
int TargetChunkSize,

Tools/PlanChunking.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public static string PlanChunking(string memberId, int targetChunkSize = 6000, i
2626
throw new ArgumentException($"Invalid member ID: {memberId}");
2727
}
2828

29+
if (targetChunkSize <= 0)
30+
{
31+
throw new ArgumentException("targetChunkSize must be positive", nameof(targetChunkSize));
32+
}
33+
34+
if (overlap <= 0)
35+
{
36+
throw new ArgumentException("overlap must be positive", nameof(overlap));
37+
}
38+
2939
// Get the source document to analyze
3040
var document = decompilerService.DecompileMember(memberId, includeHeader: true);
3141
var totalLines = document.TotalLines;
@@ -36,7 +46,7 @@ public static string PlanChunking(string memberId, int targetChunkSize = 6000, i
3646
{
3747
result = new ChunkPlanResult(
3848
memberId,
39-
new List<ChunkInfo>(),
49+
Array.Empty<ChunkInfo>(),
4050
0,
4151
0,
4252
targetChunkSize,
@@ -46,15 +56,11 @@ public static string PlanChunking(string memberId, int targetChunkSize = 6000, i
4656
else
4757
{
4858
// Estimate average characters per line based on the document
49-
var divisor = Math.Min(totalLines, 10);
59+
var divisor = Math.Max(1, Math.Min(totalLines, 10));
5060
int avgCharsPerLine;
5161
try
5262
{
5363
var sampleSlice = decompilerService.GetSourceSlice(memberId, 1, divisor);
54-
if (divisor == 0)
55-
{
56-
divisor = 1;
57-
}
5864
avgCharsPerLine = sampleSlice.Code.Length / divisor;
5965
}
6066
catch
@@ -70,10 +76,10 @@ public static string PlanChunking(string memberId, int targetChunkSize = 6000, i
7076
// Calculate target lines per chunk
7177
var targetLinesPerChunk = Math.Max(1, targetChunkSize / avgCharsPerLine);
7278

73-
if (overlap >= targetLinesPerChunk || overlap < 0)
79+
if (overlap >= targetLinesPerChunk)
7480
{
7581
throw new ArgumentException(
76-
$"Overlap must be between 0 and {targetLinesPerChunk - 1}",
82+
$"Overlap must be less than {targetLinesPerChunk}",
7783
nameof(overlap));
7884
}
7985

@@ -101,7 +107,7 @@ public static string PlanChunking(string memberId, int targetChunkSize = 6000, i
101107

102108
result = new ChunkPlanResult(
103109
memberId,
104-
chunks,
110+
chunks.ToArray(),
105111
totalLines,
106112
totalLines * avgCharsPerLine,
107113
targetChunkSize,
@@ -118,7 +124,7 @@ internal record ChunkInfo(int StartLine, int EndLine, int EstimatedChars);
118124

119125
internal record ChunkPlanResult(
120126
string MemberId,
121-
List<ChunkInfo> Chunks,
127+
ChunkInfo[] Chunks,
122128
int TotalLines,
123129
int EstimatedChars,
124130
int TargetChunkSize,

0 commit comments

Comments
 (0)