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