| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { checkFim } from "./diff"; |
| 3 | |
| 4 | describe("checkFim", () => { |
| 5 | const testCases = [ |
| 6 | { |
| 7 | name: "simple insertion at cursor", |
| 8 | oldEditRange: ["function test() {", " ", "}"].join("\n"), |
| 9 | newEditRange: ["function test() {", " console.log('hello');", "}"].join( |
| 10 | "\n", |
| 11 | ), |
| 12 | cursorPosition: { line: 1, character: 2 }, |
| 13 | expected: { |
| 14 | isFim: true, |
| 15 | fimText: "console.log('hello');", |
| 16 | }, |
| 17 | }, |
| 18 | { |
| 19 | name: "not a FIM because insertion starts before { and new suffix is different from {}", |
| 20 | oldEditRange: ["function test() {}"].join("\n"), |
| 21 | newEditRange: ["function test() {console.log('test');}"].join("\n"), |
| 22 | cursorPosition: { line: 0, character: 16 }, |
| 23 | expected: { |
| 24 | isFim: false, |
| 25 | fimText: null, |
| 26 | }, |
| 27 | }, |
| 28 | { |
| 29 | name: "insertion with cursor at beginning", |
| 30 | oldEditRange: ["function test() {}"].join("\n"), |
| 31 | newEditRange: ["function test() {console.log('test');}"].join("\n"), |
| 32 | cursorPosition: { line: 0, character: 17 }, |
| 33 | expected: { |
| 34 | isFim: true, |
| 35 | fimText: "console.log('test');", |
| 36 | }, |
| 37 | }, |
| 38 | { |
| 39 | name: "not a FIM because text was deleted", |
| 40 | oldEditRange: ["function test() {", " return 42;", "}"].join("\n"), |
| 41 | newEditRange: ["function test() {", " console.log('hello');", "}"].join( |
| 42 | "\n", |
| 43 | ), |
| 44 | cursorPosition: { line: 1, character: 2 }, |
| 45 | expected: { |
| 46 | isFim: false, |
| 47 | fimText: null, |
| 48 | }, |
| 49 | }, |
| 50 | { |
| 51 | name: "not a FIM because text was changed before cursor", |
| 52 | oldEditRange: ["let x = 1;"].join("\n"), |
| 53 | newEditRange: ["const x = 1; // initialized x"].join("\n"), |
| 54 | cursorPosition: { line: 0, character: 8 }, |
| 55 | expected: { |
| 56 | isFim: false, |
| 57 | fimText: null, |
| 58 | }, |
| 59 | }, |
| 60 | { |
| 61 | name: "multi-line insertion at cursor", |
| 62 | oldEditRange: [ |
| 63 | "function process() {", |
| 64 | " ", |
| 65 | " return result;", |
| 66 | "}", |
| 67 | ].join("\n"), |
| 68 | newEditRange: [ |
| 69 | "function process() {", |
| 70 | " const data = fetchData();", |
| 71 | " const result = processData(data);", |
| 72 | " return result;", |
| 73 | "}", |
| 74 | ].join("\n"), |
| 75 | cursorPosition: { line: 1, character: 2 }, |
| 76 | expected: { |
| 77 | isFim: true, |
| 78 | fimText: |
| 79 | "const data = fetchData();\n const result = processData(data);", |
| 80 | }, |
| 81 | }, |
| 82 | { |
| 83 | name: "interesting case", |
| 84 | oldEditRange: [" }", "}", ""].join("\n"), |
| 85 | newEditRange: [" }", "}", "", "module.exports = Calculator;"].join("\n"), |
| 86 | cursorPosition: { line: 0, character: 0 }, |
| 87 | expected: { |
| 88 | isFim: false, |
| 89 | fimText: null, |
| 90 | }, |
| 91 | }, |
| 92 | { |
| 93 | name: "calculator.js divide", |
| 94 | oldEditRange: [ |
| 95 | " ", |
| 96 | " if (number === 0) {", |
| 97 | ' throw new Error("Cannot divide by zero");', |
| 98 | " }", |
| 99 | " this.result /= number;", |
| 100 | " return this;", |
| 101 | ].join("\n"), |
| 102 | newEditRange: [ |
| 103 | " if (typeof number !== 'number') {", |
| 104 | ' throw new Error("Invalid input: must be a number");', |
| 105 | " }", |
| 106 | " if (number === 0) {", |
| 107 | ' throw new Error("Cannot divide by zero");', |
| 108 | " }", |
| 109 | " this.result /= number;", |
| 110 | " return this;", |
| 111 | ].join("\n"), |
| 112 | cursorPosition: { line: 0, character: 4 }, |
| 113 | expected: { |
| 114 | isFim: true, |
| 115 | fimText: [ |
| 116 | "if (typeof number !== 'number') {", |
| 117 | ' throw new Error("Invalid input: must be a number");', |
| 118 | " }", |
| 119 | ].join("\n"), |
| 120 | }, |
| 121 | }, |
| 122 | ]; |
| 123 | |
| 124 | testCases.forEach( |
| 125 | ({ name, oldEditRange, newEditRange, cursorPosition, expected }) => { |
| 126 | it(name, () => { |
| 127 | const result = checkFim(oldEditRange, newEditRange, cursorPosition); |
| 128 | expect(result).toEqual(expected); |
| 129 | }); |
| 130 | }, |
| 131 | ); |
| 132 | }); |