|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +package com.microsoft.copilot.eclipse.ui.chat; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 8 | + |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +/** |
| 15 | + * Unit tests for the private static parsing helpers in {@link ThinkingBlock}: {@code stripTrailingNewlines} and |
| 16 | + * {@code parseSections}. Exercises the helpers via reflection so the production visibility stays untouched. |
| 17 | + * |
| 18 | + * <p>Primary goal: guard CRLF handling so a {@code \r\n}-terminated body or title boundary does not leak a stray |
| 19 | + * {@code \r} into the rendered text. |
| 20 | + */ |
| 21 | +class ThinkingBlockParseTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + void stripTrailingNewlines_stripsLfCrAndCrlf() throws Exception { |
| 25 | + assertEquals("body", invokeStripTrailingNewlines("body\n")); |
| 26 | + assertEquals("body", invokeStripTrailingNewlines("body\r")); |
| 27 | + assertEquals("body", invokeStripTrailingNewlines("body\r\n")); |
| 28 | + assertEquals("body", invokeStripTrailingNewlines("body\r\n\r\n")); |
| 29 | + assertEquals("body", invokeStripTrailingNewlines("body\n\r\n")); |
| 30 | + assertEquals("body", invokeStripTrailingNewlines("body")); |
| 31 | + assertEquals("", invokeStripTrailingNewlines("")); |
| 32 | + assertEquals("", invokeStripTrailingNewlines("\r\n\r\n")); |
| 33 | + // Internal newlines and leading whitespace must survive untouched. |
| 34 | + assertEquals(" code\n more", invokeStripTrailingNewlines(" code\n more\r\n")); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void parseSections_handlesCrlfTitleBoundary() throws Exception { |
| 39 | + String raw = "intro body\r\n**Plan**\r\nplan body\r\n**Next**\r\nnext body\r\n"; |
| 40 | + List<?> sections = invokeParseSections(raw); |
| 41 | + assertEquals(3, sections.size()); |
| 42 | + |
| 43 | + assertNull(title(sections.get(0))); |
| 44 | + assertEquals("intro body", body(sections.get(0))); |
| 45 | + |
| 46 | + assertEquals("Plan", title(sections.get(1))); |
| 47 | + assertEquals("plan body", body(sections.get(1))); |
| 48 | + |
| 49 | + assertEquals("Next", title(sections.get(2))); |
| 50 | + assertEquals("next body", body(sections.get(2))); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void parseSections_handlesLfOnlyTitleBoundary() throws Exception { |
| 55 | + // Existing LF behavior must remain unchanged. |
| 56 | + String raw = "intro\n**Plan**\nplan body"; |
| 57 | + List<?> sections = invokeParseSections(raw); |
| 58 | + assertEquals(2, sections.size()); |
| 59 | + assertNull(title(sections.get(0))); |
| 60 | + assertEquals("intro", body(sections.get(0))); |
| 61 | + assertEquals("Plan", title(sections.get(1))); |
| 62 | + assertEquals("plan body", body(sections.get(1))); |
| 63 | + } |
| 64 | + |
| 65 | + private static String invokeStripTrailingNewlines(String input) throws Exception { |
| 66 | + Method m = ThinkingBlock.class.getDeclaredMethod("stripTrailingNewlines", String.class); |
| 67 | + m.setAccessible(true); |
| 68 | + return (String) m.invoke(null, input); |
| 69 | + } |
| 70 | + |
| 71 | + private static List<?> invokeParseSections(String raw) throws Exception { |
| 72 | + Method m = ThinkingBlock.class.getDeclaredMethod("parseSections", String.class); |
| 73 | + m.setAccessible(true); |
| 74 | + return (List<?>) m.invoke(null, raw); |
| 75 | + } |
| 76 | + |
| 77 | + private static String title(Object parsedSection) throws Exception { |
| 78 | + Method m = parsedSection.getClass().getDeclaredMethod("title"); |
| 79 | + m.setAccessible(true); |
| 80 | + return (String) m.invoke(parsedSection); |
| 81 | + } |
| 82 | + |
| 83 | + private static String body(Object parsedSection) throws Exception { |
| 84 | + Method m = parsedSection.getClass().getDeclaredMethod("body"); |
| 85 | + m.setAccessible(true); |
| 86 | + return (String) m.invoke(parsedSection); |
| 87 | + } |
| 88 | +} |
0 commit comments