| 1 | import { ctxItemToRifWithContents } from "./util"; |
| 2 | import { ContextItemWithId, RangeInFileWithContents } from "../index"; |
| 3 | |
| 4 | describe("ctxItemToRifWithContents", () => { |
| 5 | it("should parse start and end lines from the item name when format is valid", () => { |
| 6 | const item: ContextItemWithId = { |
| 7 | id: { providerTitle: "testProvider", itemId: "1" }, |
| 8 | name: "myFunction(10-20)", |
| 9 | content: "function content", |
| 10 | description: "test description", |
| 11 | uri: { type: "file", value: "/path/to/file" }, |
| 12 | }; |
| 13 | |
| 14 | const expected: RangeInFileWithContents = { |
| 15 | filepath: "/path/to/file", |
| 16 | range: { |
| 17 | start: { line: 10, character: 0 }, |
| 18 | end: { line: 20, character: 0 }, |
| 19 | }, |
| 20 | contents: "function content", |
| 21 | }; |
| 22 | |
| 23 | const result = ctxItemToRifWithContents(item); |
| 24 | expect(result).toEqual(expected); |
| 25 | }); |
| 26 | |
| 27 | it("should set startLine and endLine to 0 when name format is invalid", () => { |
| 28 | const item: ContextItemWithId = { |
| 29 | id: { providerTitle: "testProvider", itemId: "2" }, |
| 30 | name: "myFunction", |
| 31 | content: "function content", |
| 32 | description: "test description", |
| 33 | uri: { type: "file", value: "/path/to/file" }, |
| 34 | }; |
| 35 | |
| 36 | const expected: RangeInFileWithContents = { |
| 37 | filepath: "/path/to/file", |
| 38 | range: { |
| 39 | start: { line: 0, character: 0 }, |
| 40 | end: { line: 0, character: 0 }, |
| 41 | }, |
| 42 | contents: "function content", |
| 43 | }; |
| 44 | |
| 45 | const result = ctxItemToRifWithContents(item); |
| 46 | expect(result).toEqual(expected); |
| 47 | }); |
| 48 | |
| 49 | it("should handle missing uri by setting filepath to empty string", () => { |
| 50 | const item: ContextItemWithId = { |
| 51 | id: { providerTitle: "testProvider", itemId: "3" }, |
| 52 | name: "myFunction(10-20)", |
| 53 | content: "function content", |
| 54 | description: "test description", |
| 55 | }; |
| 56 | |
| 57 | const expected: RangeInFileWithContents = { |
| 58 | filepath: "", |
| 59 | range: { |
| 60 | start: { line: 10, character: 0 }, |
| 61 | end: { line: 20, character: 0 }, |
| 62 | }, |
| 63 | contents: "function content", |
| 64 | }; |
| 65 | |
| 66 | const result = ctxItemToRifWithContents(item); |
| 67 | expect(result).toEqual(expected); |
| 68 | }); |
| 69 | |
| 70 | it("should handle uri with undefined value by setting filepath to empty string", () => { |
| 71 | const item: ContextItemWithId = { |
| 72 | id: { providerTitle: "testProvider", itemId: "4" }, |
| 73 | name: "myFunction(10-20)", |
| 74 | content: "function content", |
| 75 | description: "test description", |
| 76 | }; |
| 77 | |
| 78 | const expected: RangeInFileWithContents = { |
| 79 | filepath: "", |
| 80 | range: { |
| 81 | start: { line: 10, character: 0 }, |
| 82 | end: { line: 20, character: 0 }, |
| 83 | }, |
| 84 | contents: "function content", |
| 85 | }; |
| 86 | |
| 87 | const result = ctxItemToRifWithContents(item); |
| 88 | expect(result).toEqual(expected); |
| 89 | }); |
| 90 | |
| 91 | it("should handle invalid line numbers gracefully", () => { |
| 92 | const item: ContextItemWithId = { |
| 93 | id: { providerTitle: "testProvider", itemId: "6" }, |
| 94 | name: "myFunction(invalid-lines)", |
| 95 | content: "function content", |
| 96 | description: "test description", |
| 97 | uri: { type: "file", value: "/path/to/file" }, |
| 98 | }; |
| 99 | |
| 100 | const result = ctxItemToRifWithContents(item); |
| 101 | |
| 102 | expect(result.range.start.line).toBeNaN(); |
| 103 | expect(result.range.end.line).toBeNaN(); |
| 104 | }); |
| 105 | |
| 106 | it("should handle missing closing parenthesis in name", () => { |
| 107 | const item: ContextItemWithId = { |
| 108 | id: { providerTitle: "testProvider", itemId: "7" }, |
| 109 | name: "myFunction(10-20", |
| 110 | content: "function content", |
| 111 | description: "test description", |
| 112 | uri: { type: "file", value: "/path/to/file" }, |
| 113 | }; |
| 114 | |
| 115 | const expected: RangeInFileWithContents = { |
| 116 | filepath: "/path/to/file", |
| 117 | range: { |
| 118 | start: { line: 10, character: 0 }, |
| 119 | end: { line: 20, character: 0 }, |
| 120 | }, |
| 121 | contents: "function content", |
| 122 | }; |
| 123 | |
| 124 | const result = ctxItemToRifWithContents(item); |
| 125 | expect(result).toEqual(expected); |
| 126 | }); |
| 127 | |
| 128 | it("should handle name with multiple '-' characters", () => { |
| 129 | const item: ContextItemWithId = { |
| 130 | id: { providerTitle: "testProvider", itemId: "8" }, |
| 131 | name: "myFunction(10-20-30)", |
| 132 | content: "function content", |
| 133 | description: "test description", |
| 134 | uri: { type: "file", value: "/path/to/file" }, |
| 135 | }; |
| 136 | |
| 137 | const expected: RangeInFileWithContents = { |
| 138 | filepath: "/path/to/file", |
| 139 | range: { |
| 140 | start: { line: 10, character: 0 }, |
| 141 | end: { line: 20, character: 0 }, |
| 142 | }, |
| 143 | contents: "function content", |
| 144 | }; |
| 145 | |
| 146 | const result = ctxItemToRifWithContents(item); |
| 147 | expect(result).toEqual(expected); |
| 148 | }); |
| 149 | }); |