| 1 | export interface CompletionTestCase { |
| 2 | original: string; // Text with |cur| and |till| markers |
| 3 | completion: string; // Text to insert/overwrite |
| 4 | appliedCompletion?: string | null; |
| 5 | cursorMarker?: string; |
| 6 | tillMarker?: string; |
| 7 | } |
| 8 | |
| 9 | export interface ProcessedTestCase { |
| 10 | input: { |
| 11 | lastLineOfCompletionText: string; |
| 12 | currentText: string; |
| 13 | cursorPosition: number; |
| 14 | }; |
| 15 | expectedResult: { |
| 16 | completionText: string; |
| 17 | range?: { |
| 18 | start: number; |
| 19 | end: number; |
| 20 | }; |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Transforms human-readable test case into input and expected results. |
| 26 | * |
| 27 | * - `original`: Your original text with |cur| marking where the cursor is before completion, |
| 28 | * and |till| marking where the cursor should be after accepting completion (and this is |
| 29 | * the end of the actual applied completion) |
| 30 | * - `completion`: LLM completion output |
| 31 | * - `appliedCompletion` (optional): part of the LLM completion output that is actually applied |
| 32 | * (written between |cur| and |till| in the original) |
| 33 | * |
| 34 | * For example, you have this line: |
| 35 | * |
| 36 | * console.log("<cursor here>"); |
| 37 | * |
| 38 | * and expect it to be completed this way: |
| 39 | * |
| 40 | * console.log("foo: ", bar<cursor here>); |
| 41 | * |
| 42 | * with your completion coming from LLM being: `'foo: ", bar<cursor here>);'` |
| 43 | * |
| 44 | * Your input to this function should be: |
| 45 | * - original: `'console.log("|cur|"|till|);'` |
| 46 | * - completion: `'foo: ", bar);'` |
| 47 | * - appliedCompletion: `'foo: ", bar'` |
| 48 | * |
| 49 | * Output: input and expected output of {@link core/autocomplete/util/processSingleLineCompletion/processSingleLineCompletion|processSingleLineCompletion()} |
| 50 | * |
| 51 | */ |
| 52 | export function processTestCase({ |
| 53 | original, |
| 54 | completion, |
| 55 | appliedCompletion = null, |
| 56 | cursorMarker = "|cur|", |
| 57 | tillMarker = "|till|", |
| 58 | }: CompletionTestCase): ProcessedTestCase { |
| 59 | // Validate cursor marker |
| 60 | if (!original.includes(cursorMarker)) { |
| 61 | throw new Error("Cursor marker not found in original text"); |
| 62 | } |
| 63 | |
| 64 | const cursorPos = original.indexOf(cursorMarker); |
| 65 | original = original.replace(cursorMarker, ""); |
| 66 | |
| 67 | let tillPos = original.indexOf(tillMarker); |
| 68 | if (tillPos < 0) { |
| 69 | tillPos = cursorPos; |
| 70 | } else { |
| 71 | original = original.replace(tillMarker, ""); |
| 72 | } |
| 73 | |
| 74 | // Calculate currentText based on what's between cursor and till marker |
| 75 | const currentText = original.substring(cursorPos); |
| 76 | |
| 77 | return { |
| 78 | input: { |
| 79 | lastLineOfCompletionText: completion, |
| 80 | currentText, |
| 81 | cursorPosition: cursorPos, |
| 82 | }, |
| 83 | expectedResult: { |
| 84 | completionText: appliedCompletion || completion, |
| 85 | range: |
| 86 | cursorPos === tillPos |
| 87 | ? undefined |
| 88 | : { |
| 89 | start: cursorPos, |
| 90 | end: tillPos, |
| 91 | }, |
| 92 | }, |
| 93 | }; |
| 94 | } |