| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { Position } from "../.."; |
| 3 | import { |
| 4 | INSTINCT_EDITABLE_REGION_END_TOKEN, |
| 5 | INSTINCT_EDITABLE_REGION_START_TOKEN, |
| 6 | NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, |
| 7 | NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, |
| 8 | } from "../constants"; |
| 9 | import { |
| 10 | insertCursorToken, |
| 11 | insertEditableRegionTokensWithStaticRange, |
| 12 | } from "./utils"; |
| 13 | |
| 14 | describe("insertCursorToken", () => { |
| 15 | it("should insert cursor token at the specified position", () => { |
| 16 | const lines = ["line one", "line two", "line three"]; |
| 17 | const cursorPos: Position = { line: 1, character: 5 }; |
| 18 | const cursorToken = "|CURSOR|"; |
| 19 | |
| 20 | const result = insertCursorToken(lines, cursorPos, cursorToken); |
| 21 | |
| 22 | expect(result).toEqual(["line one", "line |CURSOR|two", "line three"]); |
| 23 | }); |
| 24 | |
| 25 | it("should handle cursor at the beginning of a line", () => { |
| 26 | const lines = ["line one", "line two", "line three"]; |
| 27 | const cursorPos: Position = { line: 1, character: 0 }; |
| 28 | const cursorToken = "|CURSOR|"; |
| 29 | |
| 30 | const result = insertCursorToken(lines, cursorPos, cursorToken); |
| 31 | |
| 32 | expect(result).toEqual(["line one", "|CURSOR|line two", "line three"]); |
| 33 | }); |
| 34 | |
| 35 | it("should handle cursor at the end of a line", () => { |
| 36 | const lines = ["line one", "line two", "line three"]; |
| 37 | const cursorPos: Position = { line: 1, character: 8 }; |
| 38 | const cursorToken = "|CURSOR|"; |
| 39 | |
| 40 | const result = insertCursorToken(lines, cursorPos, cursorToken); |
| 41 | |
| 42 | expect(result).toEqual(["line one", "line two|CURSOR|", "line three"]); |
| 43 | }); |
| 44 | |
| 45 | it("should not modify lines if cursor position is out of bounds (negative line)", () => { |
| 46 | const lines = ["line one", "line two", "line three"]; |
| 47 | const cursorPos: Position = { line: -1, character: 5 }; |
| 48 | const cursorToken = "|CURSOR|"; |
| 49 | |
| 50 | const result = insertCursorToken(lines, cursorPos, cursorToken); |
| 51 | |
| 52 | expect(result).toEqual(lines); |
| 53 | }); |
| 54 | |
| 55 | it("should not modify lines if cursor position is out of bounds (line too large)", () => { |
| 56 | const lines = ["line one", "line two", "line three"]; |
| 57 | const cursorPos: Position = { line: 3, character: 5 }; |
| 58 | const cursorToken = "|CURSOR|"; |
| 59 | |
| 60 | const result = insertCursorToken(lines, cursorPos, cursorToken); |
| 61 | |
| 62 | expect(result).toEqual(lines); |
| 63 | }); |
| 64 | |
| 65 | it("should cap the character position to the line length if it exceeds it", () => { |
| 66 | const lines = ["line one", "line two", "line three"]; |
| 67 | const cursorPos: Position = { line: 1, character: 100 }; |
| 68 | const cursorToken = "|CURSOR|"; |
| 69 | |
| 70 | const result = insertCursorToken(lines, cursorPos, cursorToken); |
| 71 | |
| 72 | expect(result).toEqual(["line one", "line two|CURSOR|", "line three"]); |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | describe("insertEditableRegionTokensWithStaticRange", () => { |
| 77 | it("should insert editable region tokens with default margins", () => { |
| 78 | const lines = ["line 0", "line 1", "line 2", "line 3", "line 4"]; |
| 79 | const cursorPos: Position = { line: 2, character: 3 }; |
| 80 | |
| 81 | const result = insertEditableRegionTokensWithStaticRange(lines, cursorPos); |
| 82 | |
| 83 | const expectedStart = Math.max(2 - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, 0); |
| 84 | const expectedEnd = Math.min( |
| 85 | 2 + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, |
| 86 | lines.length - 1, |
| 87 | ); |
| 88 | |
| 89 | const expected = [ |
| 90 | ...lines.slice(0, expectedStart), |
| 91 | INSTINCT_EDITABLE_REGION_START_TOKEN, |
| 92 | ...lines.slice(expectedStart, expectedEnd + 1), |
| 93 | INSTINCT_EDITABLE_REGION_END_TOKEN, |
| 94 | ...lines.slice(expectedEnd + 1), |
| 95 | ]; |
| 96 | |
| 97 | expect(result).toEqual(expected); |
| 98 | }); |
| 99 | |
| 100 | it("should insert editable region tokens with explicit range", () => { |
| 101 | const lines = ["line 0", "line 1", "line 2", "line 3", "line 4"]; |
| 102 | const cursorPos: Position = { line: 2, character: 3 }; |
| 103 | const editableRegionStart = 1; |
| 104 | const editableRegionEnd = 3; |
| 105 | |
| 106 | const result = insertEditableRegionTokensWithStaticRange( |
| 107 | lines, |
| 108 | cursorPos, |
| 109 | editableRegionStart, |
| 110 | editableRegionEnd, |
| 111 | ); |
| 112 | |
| 113 | const expected = [ |
| 114 | "line 0", |
| 115 | INSTINCT_EDITABLE_REGION_START_TOKEN, |
| 116 | "line 1", |
| 117 | "line 2", |
| 118 | "line 3", |
| 119 | INSTINCT_EDITABLE_REGION_END_TOKEN, |
| 120 | "line 4", |
| 121 | ]; |
| 122 | |
| 123 | expect(result).toEqual(expected); |
| 124 | }); |
| 125 | |
| 126 | it("should not modify lines if cursor position is out of bounds", () => { |
| 127 | const lines = ["line 0", "line 1", "line 2"]; |
| 128 | const cursorPos: Position = { line: 5, character: 3 }; |
| 129 | |
| 130 | const result = insertEditableRegionTokensWithStaticRange(lines, cursorPos); |
| 131 | |
| 132 | expect(result).toEqual(lines); |
| 133 | }); |
| 134 | }); |