| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { ContinueErrorReason } from "../../util/errors"; |
| 3 | import { trimEmptyLines, validateSingleEdit } from "./findAndReplaceUtils"; |
| 4 | |
| 5 | describe("validateSingleEdit", () => { |
| 6 | describe("valid inputs", () => { |
| 7 | it("should not throw for valid non-empty strings", () => { |
| 8 | expect(() => { |
| 9 | validateSingleEdit("old", "new"); |
| 10 | }).not.toThrow(); |
| 11 | }); |
| 12 | |
| 13 | it("should allow empty old_string for file creation", () => { |
| 14 | expect(() => { |
| 15 | validateSingleEdit("", "new content"); |
| 16 | }).not.toThrow(); |
| 17 | }); |
| 18 | |
| 19 | it("should allow empty new_string for deletion", () => { |
| 20 | expect(() => { |
| 21 | validateSingleEdit("delete me", ""); |
| 22 | }).not.toThrow(); |
| 23 | }); |
| 24 | |
| 25 | it("should allow multiline strings", () => { |
| 26 | expect(() => { |
| 27 | validateSingleEdit("line1\nline2", "newline1\nnewline2"); |
| 28 | }).not.toThrow(); |
| 29 | }); |
| 30 | |
| 31 | it("should allow special characters", () => { |
| 32 | expect(() => { |
| 33 | validateSingleEdit("/[a-z]+/g", "/[A-Z]+/g"); |
| 34 | }).not.toThrow(); |
| 35 | }); |
| 36 | }); |
| 37 | |
| 38 | describe("invalid inputs", () => { |
| 39 | it("should throw error when old_string is null", () => { |
| 40 | expect(() => { |
| 41 | validateSingleEdit(null as any, "new"); |
| 42 | }).toThrowError( |
| 43 | expect.objectContaining({ |
| 44 | reason: ContinueErrorReason.FindAndReplaceMissingOldString, |
| 45 | }), |
| 46 | ); |
| 47 | }); |
| 48 | |
| 49 | it("should throw error when old_string is undefined", () => { |
| 50 | expect(() => { |
| 51 | validateSingleEdit(undefined as any, "new"); |
| 52 | }).toThrowError( |
| 53 | expect.objectContaining({ |
| 54 | reason: ContinueErrorReason.FindAndReplaceMissingOldString, |
| 55 | }), |
| 56 | ); |
| 57 | }); |
| 58 | |
| 59 | it("should throw error when new_string is undefined", () => { |
| 60 | expect(() => { |
| 61 | validateSingleEdit("old", undefined as any); |
| 62 | }).toThrowError( |
| 63 | expect.objectContaining({ |
| 64 | reason: ContinueErrorReason.FindAndReplaceMissingNewString, |
| 65 | }), |
| 66 | ); |
| 67 | }); |
| 68 | |
| 69 | it("should throw error when old_string and new_string are identical", () => { |
| 70 | expect(() => { |
| 71 | validateSingleEdit("same", "same"); |
| 72 | }).toThrowError( |
| 73 | expect.objectContaining({ |
| 74 | reason: ContinueErrorReason.FindAndReplaceIdenticalOldAndNewStrings, |
| 75 | }), |
| 76 | ); |
| 77 | }); |
| 78 | |
| 79 | it("should throw error when both are empty strings", () => { |
| 80 | expect(() => { |
| 81 | validateSingleEdit("", ""); |
| 82 | }).toThrowError( |
| 83 | expect.objectContaining({ |
| 84 | reason: ContinueErrorReason.FindAndReplaceIdenticalOldAndNewStrings, |
| 85 | }), |
| 86 | ); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | describe("error messages with context", () => { |
| 91 | it("should include edit number in error messages when index provided", () => { |
| 92 | expect(() => { |
| 93 | validateSingleEdit(null as any, "new", 2); |
| 94 | }).toThrowError( |
| 95 | expect.objectContaining({ |
| 96 | reason: ContinueErrorReason.FindAndReplaceMissingOldString, |
| 97 | }), |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | it("should include edit number for new_string errors", () => { |
| 102 | expect(() => { |
| 103 | validateSingleEdit("old", undefined as any, 0); |
| 104 | }).toThrowError( |
| 105 | expect.objectContaining({ |
| 106 | reason: ContinueErrorReason.FindAndReplaceMissingNewString, |
| 107 | }), |
| 108 | ); |
| 109 | }); |
| 110 | |
| 111 | it("should include edit number for identical strings error", () => { |
| 112 | expect(() => { |
| 113 | validateSingleEdit("same", "same", 4); |
| 114 | }).toThrowError( |
| 115 | expect.objectContaining({ |
| 116 | reason: ContinueErrorReason.FindAndReplaceIdenticalOldAndNewStrings, |
| 117 | }), |
| 118 | ); |
| 119 | }); |
| 120 | |
| 121 | it("should not include context when index not provided", () => { |
| 122 | expect(() => { |
| 123 | validateSingleEdit("same", "same"); |
| 124 | }).toThrowError( |
| 125 | expect.objectContaining({ |
| 126 | reason: ContinueErrorReason.FindAndReplaceIdenticalOldAndNewStrings, |
| 127 | }), |
| 128 | ); |
| 129 | }); |
| 130 | }); |
| 131 | }); |
| 132 | |
| 133 | describe("trimEmptyLines", () => { |
| 134 | it("trims leading empty lines from start", () => { |
| 135 | const input = ["", "", "a", "", "b"]; |
| 136 | const result = trimEmptyLines({ lines: input, fromEnd: false }); |
| 137 | expect(result).toEqual(["a", "", "b"]); |
| 138 | }); |
| 139 | |
| 140 | it("trims trailing empty lines from end", () => { |
| 141 | const input = ["a", "", "b", "", ""]; |
| 142 | const result = trimEmptyLines({ lines: input, fromEnd: true }); |
| 143 | expect(result).toEqual(["a", "", "b"]); |
| 144 | }); |
| 145 | |
| 146 | it("returns empty array when all lines are empty", () => { |
| 147 | const input = ["", "", ""]; |
| 148 | const result = trimEmptyLines({ lines: input, fromEnd: false }); |
| 149 | expect(result).toEqual([]); |
| 150 | }); |
| 151 | }); |