| 1 | import { describe, expect, test, vi } from "vitest"; |
| 2 | import { DiffLine } from ".."; |
| 3 | import { NEXT_EDIT_MODELS } from "../llm/constants"; |
| 4 | import { |
| 5 | convertNextEditModelNameToEnum, |
| 6 | isNextEditTest, |
| 7 | isWhitespaceOnlyDeletion, |
| 8 | } from "./utils"; |
| 9 | |
| 10 | describe("isNextEditTest", () => { |
| 11 | test("returns true when NEXT_EDIT_TEST_ENABLED is 'true'", () => { |
| 12 | vi.stubEnv("NEXT_EDIT_TEST_ENABLED", "true"); |
| 13 | expect(isNextEditTest()).toBe(true); |
| 14 | }); |
| 15 | |
| 16 | test("returns false when NEXT_EDIT_TEST_ENABLED is 'false'", () => { |
| 17 | vi.stubEnv("NEXT_EDIT_TEST_ENABLED", "false"); |
| 18 | expect(isNextEditTest()).toBe(false); |
| 19 | }); |
| 20 | |
| 21 | test("returns false when NEXT_EDIT_TEST_ENABLED is undefined", () => { |
| 22 | vi.stubEnv("NEXT_EDIT_TEST_ENABLED", undefined); |
| 23 | expect(isNextEditTest()).toBe(false); |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | describe("isWhitespaceOnlyDeletion", () => { |
| 28 | test("returns true for whitespace-only deletions", () => { |
| 29 | const diffLines: DiffLine[] = [ |
| 30 | { type: "old", line: " " }, |
| 31 | { type: "old", line: "\t" }, |
| 32 | ]; |
| 33 | expect(isWhitespaceOnlyDeletion(diffLines)).toBe(true); |
| 34 | }); |
| 35 | |
| 36 | test("returns false for non-whitespace deletions", () => { |
| 37 | const diffLines: DiffLine[] = [ |
| 38 | { type: "old", line: "code" }, |
| 39 | { type: "old", line: " " }, |
| 40 | ]; |
| 41 | expect(isWhitespaceOnlyDeletion(diffLines)).toBe(false); |
| 42 | }); |
| 43 | |
| 44 | test("returns false for mixed content", () => { |
| 45 | const diffLines: DiffLine[] = [ |
| 46 | { type: "old", line: " " }, |
| 47 | { type: "old", line: "code" }, |
| 48 | ]; |
| 49 | expect(isWhitespaceOnlyDeletion(diffLines)).toBe(false); |
| 50 | }); |
| 51 | |
| 52 | test("returns false for mixed type", () => { |
| 53 | const diffLines: DiffLine[] = [ |
| 54 | { type: "old", line: " " }, |
| 55 | { type: "new", line: "" }, |
| 56 | ]; |
| 57 | expect(isWhitespaceOnlyDeletion(diffLines)).toBe(false); |
| 58 | }); |
| 59 | |
| 60 | test("returns true for empty array", () => { |
| 61 | const diffLines: DiffLine[] = []; |
| 62 | expect(isWhitespaceOnlyDeletion(diffLines)).toBe(true); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe("convertNextEditModelNameToEnum", () => { |
| 67 | test("returns correct enum for exact matches", () => { |
| 68 | Object.values(NEXT_EDIT_MODELS).forEach((model) => { |
| 69 | expect(convertNextEditModelNameToEnum(model)).toBe(model); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | test("returns correct enum when model name is contained in a longer string", () => { |
| 74 | expect(convertNextEditModelNameToEnum("inception/mercury-coder")).toBe( |
| 75 | NEXT_EDIT_MODELS.MERCURY_CODER, |
| 76 | ); |
| 77 | expect(convertNextEditModelNameToEnum("blah/instinct")).toBe( |
| 78 | NEXT_EDIT_MODELS.INSTINCT, |
| 79 | ); |
| 80 | }); |
| 81 | |
| 82 | test("returns undefined for non-matching model names", () => { |
| 83 | expect(convertNextEditModelNameToEnum("unknown-model")).toBeUndefined(); |
| 84 | expect(convertNextEditModelNameToEnum("random-string")).toBeUndefined(); |
| 85 | expect(convertNextEditModelNameToEnum("")).toBeUndefined(); |
| 86 | }); |
| 87 | |
| 88 | test("returns first match when multiple models could match", () => { |
| 89 | const modelName = "model-that-contains-multiple-matches"; |
| 90 | const result = convertNextEditModelNameToEnum(modelName); |
| 91 | |
| 92 | if (result !== undefined) { |
| 93 | expect(Object.values(NEXT_EDIT_MODELS)).toContain(result); |
| 94 | } |
| 95 | }); |
| 96 | }); |