| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.AgentFramework.Testing; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Assertion helpers for verifying prompt integrity without LLM invocation. |
| | | 7 | | /// Use in unit tests to catch prompt regressions (missing safety rules, deleted |
| | | 8 | | /// sections, forbidden patterns) at zero token cost. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <example> |
| | | 11 | | /// <code> |
| | | 12 | | /// [Fact] |
| | | 13 | | /// public void WriterPrompt_HasSafetyRules() |
| | | 14 | | /// { |
| | | 15 | | /// PromptAssert.Contains(WriterPrompt.Text, "ABSOLUTE RULE"); |
| | | 16 | | /// PromptAssert.ContainsInSection(WriterPrompt.Text, "### Critical", "meta-instruction-leak"); |
| | | 17 | | /// PromptAssert.ForbidsPattern(WriterPrompt.Text, @"TODO|FIXME|HACK"); |
| | | 18 | | /// } |
| | | 19 | | /// </code> |
| | | 20 | | /// </example> |
| | | 21 | | public static class PromptAssert |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Asserts that <paramref name="prompt"/> contains <paramref name="expected"/> |
| | | 25 | | /// using <see cref="StringComparison.OrdinalIgnoreCase"/>. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="prompt">The prompt text to inspect.</param> |
| | | 28 | | /// <param name="expected">The substring that must be present.</param> |
| | | 29 | | /// <exception cref="ArgumentException"> |
| | | 30 | | /// <paramref name="prompt"/> is <see langword="null"/> or |
| | | 31 | | /// <paramref name="expected"/> is <see langword="null"/> or empty. |
| | | 32 | | /// </exception> |
| | | 33 | | /// <exception cref="PromptAssertionException">The expected text was not found.</exception> |
| | | 34 | | public static void Contains(string prompt, string expected) |
| | 22 | 35 | | => Contains(prompt, expected, StringComparison.OrdinalIgnoreCase); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Asserts that <paramref name="prompt"/> contains <paramref name="expected"/> |
| | | 39 | | /// using the specified <paramref name="comparison"/>. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="prompt">The prompt text to inspect.</param> |
| | | 42 | | /// <param name="expected">The substring that must be present.</param> |
| | | 43 | | /// <param name="comparison">The comparison rule to use.</param> |
| | | 44 | | /// <exception cref="ArgumentException"> |
| | | 45 | | /// <paramref name="prompt"/> is <see langword="null"/> or |
| | | 46 | | /// <paramref name="expected"/> is <see langword="null"/> or empty. |
| | | 47 | | /// </exception> |
| | | 48 | | /// <exception cref="PromptAssertionException">The expected text was not found.</exception> |
| | | 49 | | public static void Contains(string prompt, string expected, StringComparison comparison) |
| | | 50 | | { |
| | 24 | 51 | | ValidatePrompt(prompt); |
| | 23 | 52 | | ValidateExpected(expected); |
| | | 53 | | |
| | 21 | 54 | | if (!prompt.Contains(expected, comparison)) |
| | | 55 | | { |
| | 2 | 56 | | throw new PromptAssertionException( |
| | 2 | 57 | | $"Expected prompt to contain '{expected}' but it was not found. " + |
| | 2 | 58 | | $"Prompt length: {prompt.Length} chars."); |
| | | 59 | | } |
| | 19 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Asserts that <paramref name="prompt"/> does NOT contain <paramref name="forbidden"/> |
| | | 64 | | /// using <see cref="StringComparison.OrdinalIgnoreCase"/>. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="prompt">The prompt text to inspect.</param> |
| | | 67 | | /// <param name="forbidden">The substring that must be absent.</param> |
| | | 68 | | /// <exception cref="ArgumentException"> |
| | | 69 | | /// <paramref name="prompt"/> is <see langword="null"/> or |
| | | 70 | | /// <paramref name="forbidden"/> is <see langword="null"/> or empty. |
| | | 71 | | /// </exception> |
| | | 72 | | /// <exception cref="PromptAssertionException">The forbidden text was found.</exception> |
| | | 73 | | public static void DoesNotContain(string prompt, string forbidden) |
| | | 74 | | { |
| | 2 | 75 | | ValidatePrompt(prompt); |
| | 2 | 76 | | ValidateExpected(forbidden, nameof(forbidden)); |
| | | 77 | | |
| | 2 | 78 | | var index = prompt.IndexOf(forbidden, StringComparison.OrdinalIgnoreCase); |
| | 2 | 79 | | if (index >= 0) |
| | | 80 | | { |
| | 1 | 81 | | var context = ExtractContext(prompt, index, forbidden.Length); |
| | 1 | 82 | | throw new PromptAssertionException( |
| | 1 | 83 | | $"Expected prompt to NOT contain '{forbidden}' but it was found at offset {index}. " + |
| | 1 | 84 | | $"Context: '...{context}...'"); |
| | | 85 | | } |
| | 1 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Asserts that <paramref name="prompt"/> does not match <paramref name="regexPattern"/>. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="prompt">The prompt text to inspect.</param> |
| | | 92 | | /// <param name="regexPattern">A regular expression pattern that must NOT match.</param> |
| | | 93 | | /// <exception cref="ArgumentException"> |
| | | 94 | | /// <paramref name="prompt"/> is <see langword="null"/> or |
| | | 95 | | /// <paramref name="regexPattern"/> is <see langword="null"/> or empty. |
| | | 96 | | /// </exception> |
| | | 97 | | /// <exception cref="PromptAssertionException">The pattern matched.</exception> |
| | | 98 | | public static void ForbidsPattern(string prompt, string regexPattern) |
| | | 99 | | { |
| | 2 | 100 | | ValidatePrompt(prompt); |
| | 2 | 101 | | ValidateExpected(regexPattern, nameof(regexPattern)); |
| | | 102 | | |
| | 2 | 103 | | var match = Regex.Match(prompt, regexPattern, RegexOptions.None); |
| | 2 | 104 | | if (match.Success) |
| | | 105 | | { |
| | 1 | 106 | | throw new PromptAssertionException( |
| | 1 | 107 | | $"Expected prompt to not match pattern '{regexPattern}' but found match: " + |
| | 1 | 108 | | $"'{match.Value}' at offset {match.Index}."); |
| | | 109 | | } |
| | 1 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Asserts that <paramref name="prompt"/> contains a markdown section header |
| | | 114 | | /// matching <paramref name="sectionHeader"/> (e.g., <c>"### Critical"</c>). |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="prompt">The prompt text to inspect.</param> |
| | | 117 | | /// <param name="sectionHeader">The markdown header text (including <c>#</c> prefix).</param> |
| | | 118 | | /// <exception cref="ArgumentException"> |
| | | 119 | | /// <paramref name="prompt"/> is <see langword="null"/> or |
| | | 120 | | /// <paramref name="sectionHeader"/> is <see langword="null"/> or empty. |
| | | 121 | | /// </exception> |
| | | 122 | | /// <exception cref="PromptAssertionException">The section header was not found.</exception> |
| | | 123 | | public static void HasSection(string prompt, string sectionHeader) |
| | | 124 | | { |
| | 2 | 125 | | ValidatePrompt(prompt); |
| | 2 | 126 | | ValidateExpected(sectionHeader, nameof(sectionHeader)); |
| | | 127 | | |
| | 2 | 128 | | if (FindSectionStart(prompt, sectionHeader) < 0) |
| | | 129 | | { |
| | 1 | 130 | | throw new PromptAssertionException( |
| | 1 | 131 | | $"Expected prompt to contain section '{sectionHeader}' but it was not found."); |
| | | 132 | | } |
| | 1 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Asserts that <paramref name="expected"/> appears within the markdown section |
| | | 137 | | /// that starts at <paramref name="sectionHeader"/>. The section ends at the next |
| | | 138 | | /// header of equal or higher level, or at the end of the prompt. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <param name="prompt">The prompt text to inspect.</param> |
| | | 141 | | /// <param name="sectionHeader">The markdown header that starts the section (e.g., <c>"### Critical"</c>).</param> |
| | | 142 | | /// <param name="expected">The text that must appear inside the section.</param> |
| | | 143 | | /// <exception cref="ArgumentException"> |
| | | 144 | | /// <paramref name="prompt"/> is <see langword="null"/> or |
| | | 145 | | /// <paramref name="sectionHeader"/> or <paramref name="expected"/> is <see langword="null"/> or empty. |
| | | 146 | | /// </exception> |
| | | 147 | | /// <exception cref="PromptAssertionException"> |
| | | 148 | | /// The section was not found, or the expected text was not in the section. |
| | | 149 | | /// </exception> |
| | | 150 | | public static void ContainsInSection(string prompt, string sectionHeader, string expected) |
| | | 151 | | { |
| | 5 | 152 | | ValidatePrompt(prompt); |
| | 5 | 153 | | ValidateExpected(sectionHeader, nameof(sectionHeader)); |
| | 5 | 154 | | ValidateExpected(expected); |
| | | 155 | | |
| | 5 | 156 | | var sectionStart = FindSectionStart(prompt, sectionHeader); |
| | 5 | 157 | | if (sectionStart < 0) |
| | | 158 | | { |
| | 1 | 159 | | throw new PromptAssertionException( |
| | 1 | 160 | | $"Section '{sectionHeader}' not found in prompt."); |
| | | 161 | | } |
| | | 162 | | |
| | 4 | 163 | | var sectionContent = ExtractSectionContent(prompt, sectionHeader, sectionStart); |
| | | 164 | | |
| | 4 | 165 | | if (!sectionContent.Contains(expected, StringComparison.OrdinalIgnoreCase)) |
| | | 166 | | { |
| | 1 | 167 | | var preview = sectionContent.Length > 200 |
| | 1 | 168 | | ? sectionContent[..200] |
| | 1 | 169 | | : sectionContent; |
| | 1 | 170 | | throw new PromptAssertionException( |
| | 1 | 171 | | $"Expected section '{sectionHeader}' to contain '{expected}' but it was not found. " + |
| | 1 | 172 | | $"Section content: '{preview}'"); |
| | | 173 | | } |
| | 3 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Asserts that <paramref name="firstSection"/> appears before |
| | | 178 | | /// <paramref name="secondSection"/> in <paramref name="prompt"/>. |
| | | 179 | | /// </summary> |
| | | 180 | | /// <param name="prompt">The prompt text to inspect.</param> |
| | | 181 | | /// <param name="firstSection">The section header that should come first.</param> |
| | | 182 | | /// <param name="secondSection">The section header that should come second.</param> |
| | | 183 | | /// <exception cref="ArgumentException"> |
| | | 184 | | /// <paramref name="prompt"/> is <see langword="null"/> or |
| | | 185 | | /// <paramref name="firstSection"/> or <paramref name="secondSection"/> is <see langword="null"/> or empty. |
| | | 186 | | /// </exception> |
| | | 187 | | /// <exception cref="PromptAssertionException"> |
| | | 188 | | /// A section was not found, or the order was reversed. |
| | | 189 | | /// </exception> |
| | | 190 | | public static void SectionOrder(string prompt, string firstSection, string secondSection) |
| | | 191 | | { |
| | 4 | 192 | | ValidatePrompt(prompt); |
| | 4 | 193 | | ValidateExpected(firstSection, nameof(firstSection)); |
| | 4 | 194 | | ValidateExpected(secondSection, nameof(secondSection)); |
| | | 195 | | |
| | 4 | 196 | | var idx1 = FindSectionStart(prompt, firstSection); |
| | 4 | 197 | | if (idx1 < 0) |
| | | 198 | | { |
| | 1 | 199 | | throw new PromptAssertionException( |
| | 1 | 200 | | $"Expected prompt to contain section '{firstSection}' but it was not found."); |
| | | 201 | | } |
| | | 202 | | |
| | 3 | 203 | | var idx2 = FindSectionStart(prompt, secondSection); |
| | 3 | 204 | | if (idx2 < 0) |
| | | 205 | | { |
| | 1 | 206 | | throw new PromptAssertionException( |
| | 1 | 207 | | $"Expected prompt to contain section '{secondSection}' but it was not found."); |
| | | 208 | | } |
| | | 209 | | |
| | 2 | 210 | | if (idx1 >= idx2) |
| | | 211 | | { |
| | 1 | 212 | | throw new PromptAssertionException( |
| | 1 | 213 | | $"Expected section '{firstSection}' to appear before '{secondSection}' " + |
| | 1 | 214 | | $"but order was reversed (first at offset {idx1}, second at offset {idx2})."); |
| | | 215 | | } |
| | 1 | 216 | | } |
| | | 217 | | |
| | | 218 | | private static void ValidatePrompt(string prompt) |
| | | 219 | | { |
| | 39 | 220 | | if (prompt is null) |
| | | 221 | | { |
| | 1 | 222 | | throw new ArgumentException("Prompt cannot be null.", nameof(prompt)); |
| | | 223 | | } |
| | 38 | 224 | | } |
| | | 225 | | |
| | | 226 | | private static void ValidateExpected(string value, string paramName = "expected") |
| | | 227 | | { |
| | 47 | 228 | | if (string.IsNullOrEmpty(value)) |
| | | 229 | | { |
| | 2 | 230 | | throw new ArgumentException("Value cannot be null or empty.", paramName); |
| | | 231 | | } |
| | 45 | 232 | | } |
| | | 233 | | |
| | | 234 | | private static string ExtractContext(string prompt, int matchIndex, int matchLength) |
| | | 235 | | { |
| | | 236 | | const int contextRadius = 20; |
| | 1 | 237 | | var start = Math.Max(0, matchIndex - contextRadius); |
| | 1 | 238 | | var end = Math.Min(prompt.Length, matchIndex + matchLength + contextRadius); |
| | 1 | 239 | | return prompt[start..end]; |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// Finds the start index of a markdown section header line. |
| | | 244 | | /// The header must appear at the start of a line. |
| | | 245 | | /// </summary> |
| | | 246 | | private static int FindSectionStart(string prompt, string sectionHeader) |
| | | 247 | | { |
| | | 248 | | // Check if the prompt starts with the header |
| | 14 | 249 | | if (prompt.StartsWith(sectionHeader, StringComparison.OrdinalIgnoreCase)) |
| | | 250 | | { |
| | | 251 | | // Verify it's a complete line match (next char is newline or end of string) |
| | 0 | 252 | | if (prompt.Length == sectionHeader.Length || |
| | 0 | 253 | | prompt[sectionHeader.Length] == '\n' || |
| | 0 | 254 | | prompt[sectionHeader.Length] == '\r') |
| | | 255 | | { |
| | 0 | 256 | | return 0; |
| | | 257 | | } |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | // Search for the header at the start of a line |
| | 14 | 261 | | var searchOffset = 0; |
| | 14 | 262 | | while (searchOffset < prompt.Length) |
| | | 263 | | { |
| | 14 | 264 | | var idx = prompt.IndexOf(sectionHeader, searchOffset, StringComparison.OrdinalIgnoreCase); |
| | 14 | 265 | | if (idx < 0) |
| | | 266 | | { |
| | 4 | 267 | | return -1; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | // Must be at the start of a line |
| | 10 | 271 | | if (idx == 0 || prompt[idx - 1] == '\n') |
| | | 272 | | { |
| | | 273 | | // Must be followed by newline, end of string, or whitespace to be a header |
| | 10 | 274 | | var endIdx = idx + sectionHeader.Length; |
| | 10 | 275 | | if (endIdx >= prompt.Length || |
| | 10 | 276 | | prompt[endIdx] == '\n' || |
| | 10 | 277 | | prompt[endIdx] == '\r') |
| | | 278 | | { |
| | 10 | 279 | | return idx; |
| | | 280 | | } |
| | | 281 | | } |
| | | 282 | | |
| | 0 | 283 | | searchOffset = idx + 1; |
| | | 284 | | } |
| | | 285 | | |
| | 0 | 286 | | return -1; |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Extracts the content of a section starting after the header line, |
| | | 291 | | /// ending at the next header of same or higher level, or end of string. |
| | | 292 | | /// </summary> |
| | | 293 | | private static string ExtractSectionContent(string prompt, string sectionHeader, int sectionStart) |
| | | 294 | | { |
| | 4 | 295 | | var headerLevel = CountHeaderLevel(sectionHeader); |
| | | 296 | | |
| | | 297 | | // Find the end of the header line |
| | 4 | 298 | | var contentStart = prompt.IndexOf('\n', sectionStart); |
| | 4 | 299 | | if (contentStart < 0) |
| | | 300 | | { |
| | 0 | 301 | | return string.Empty; |
| | | 302 | | } |
| | | 303 | | |
| | 4 | 304 | | contentStart++; // skip past the newline |
| | | 305 | | |
| | | 306 | | // Scan forward for the next header of same or higher level |
| | 4 | 307 | | var lines = prompt[contentStart..].Split('\n'); |
| | 4 | 308 | | var sectionEnd = contentStart; |
| | 80 | 309 | | foreach (var line in lines) |
| | | 310 | | { |
| | 38 | 311 | | var trimmed = line.TrimStart(); |
| | 38 | 312 | | if (trimmed.StartsWith('#')) |
| | | 313 | | { |
| | 8 | 314 | | var lineLevel = CountHeaderLevel(trimmed); |
| | 8 | 315 | | if (lineLevel <= headerLevel) |
| | | 316 | | { |
| | | 317 | | break; |
| | | 318 | | } |
| | | 319 | | } |
| | | 320 | | |
| | 34 | 321 | | sectionEnd += line.Length + 1; // +1 for the newline |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | // Clamp to prompt length |
| | 4 | 325 | | sectionEnd = Math.Min(sectionEnd, prompt.Length); |
| | | 326 | | |
| | 4 | 327 | | return prompt[contentStart..sectionEnd]; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | private static int CountHeaderLevel(string header) |
| | | 331 | | { |
| | 12 | 332 | | var count = 0; |
| | 100 | 333 | | foreach (var ch in header) |
| | | 334 | | { |
| | 44 | 335 | | if (ch == '#') |
| | | 336 | | { |
| | 32 | 337 | | count++; |
| | | 338 | | } |
| | | 339 | | else |
| | | 340 | | { |
| | | 341 | | break; |
| | | 342 | | } |
| | | 343 | | } |
| | | 344 | | |
| | 12 | 345 | | return count; |
| | | 346 | | } |
| | | 347 | | } |