| 1 | // Generated by continue |
| 2 | |
| 3 | import { describe, expect, it, vi } from "vitest"; |
| 4 | import { ChatMessage } from "../index"; |
| 5 | import { generateLines, matchLine, streamLines } from "./util"; |
| 6 | |
| 7 | describe.skip("matchLine", () => { |
| 8 | it("should match empty lines if the first old line is also empty", () => { |
| 9 | const newLine = " "; |
| 10 | const oldLines = [" ", "some code"]; |
| 11 | const result = matchLine(newLine, oldLines); |
| 12 | expect(result).toEqual({ |
| 13 | matchIndex: 0, |
| 14 | isPerfectMatch: true, |
| 15 | newLine: "", |
| 16 | }); |
| 17 | }); |
| 18 | |
| 19 | it("should return matchIndex -1 if newLine is an end bracket and i > 4", () => { |
| 20 | const newLine = "}"; |
| 21 | const oldLines = ["line 1", "line 2", "line 3", "line 4", "line 5", "}"]; |
| 22 | const result = matchLine(newLine, oldLines); |
| 23 | expect(result).toEqual({ |
| 24 | matchIndex: -1, |
| 25 | isPerfectMatch: false, |
| 26 | newLine: "}", |
| 27 | }); |
| 28 | }); |
| 29 | |
| 30 | it("should match end bracket if within first 5 lines", () => { |
| 31 | const newLine = "}"; |
| 32 | const oldLines = ["line 1", "line 2", "line 3", "}", "line 5", "}"]; |
| 33 | const result = matchLine(newLine, oldLines); |
| 34 | expect(result).toEqual({ |
| 35 | matchIndex: 3, |
| 36 | isPerfectMatch: true, |
| 37 | newLine: "}", |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | it("should match lines perfectly", () => { |
| 42 | const newLine = "const a = 5;"; |
| 43 | const oldLines = ["let b = 6;", "const a = 5;", "console.log(a);"]; |
| 44 | const result = matchLine(newLine, oldLines); |
| 45 | expect(result).toEqual({ |
| 46 | matchIndex: 1, |
| 47 | isPerfectMatch: true, |
| 48 | newLine: "const a = 5;", |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | it("should match lines with tolerable differences", () => { |
| 53 | const newLine = "console.log(a);"; |
| 54 | const oldLines = ["const a = 5;", "console.log(b);", "console.log( a );"]; |
| 55 | const result = matchLine(newLine, oldLines); |
| 56 | expect(result).toEqual({ |
| 57 | matchIndex: 2, |
| 58 | isPerfectMatch: false, |
| 59 | newLine: "console.log(a);", |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | it("should return matchIndex -1 if no match found", () => { |
| 64 | const newLine = "new line that does not exist"; |
| 65 | const oldLines = ["line 1", "line 2", "line 3"]; |
| 66 | const result = matchLine(newLine, oldLines); |
| 67 | expect(result).toEqual({ |
| 68 | matchIndex: -1, |
| 69 | isPerfectMatch: false, |
| 70 | newLine: "new line that does not exist", |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | it("should fix indentation if permissible and line length > 8", () => { |
| 75 | const newLine = " const a = 5;"; |
| 76 | const oldLines = ["const a = 5;"]; |
| 77 | const result = matchLine(newLine, oldLines, false); |
| 78 | expect(result).toEqual({ |
| 79 | matchIndex: 0, |
| 80 | isPerfectMatch: true, |
| 81 | newLine: "const a = 5;", |
| 82 | }); |
| 83 | }); |
| 84 | |
| 85 | it("should not fix indentation if not permissible and line length is short", () => { |
| 86 | const newLine = " for"; |
| 87 | const oldLines = ["for"]; |
| 88 | const result = matchLine(newLine, oldLines, false); |
| 89 | expect(result).toEqual({ |
| 90 | matchIndex: 0, |
| 91 | isPerfectMatch: false, |
| 92 | newLine: " for", |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | it("should fix indentation even if line length <=8 when permissiveAboutIndentation is true", () => { |
| 97 | const newLine = " for"; |
| 98 | const oldLines = ["for"]; |
| 99 | const result = matchLine(newLine, oldLines, true); |
| 100 | expect(result).toEqual({ |
| 101 | matchIndex: 0, |
| 102 | isPerfectMatch: true, |
| 103 | newLine: "for", |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | it("should match lines that are empty strings", () => { |
| 108 | const newLine = ""; |
| 109 | const oldLines = [""]; |
| 110 | const result = matchLine(newLine, oldLines); |
| 111 | expect(result).toEqual({ |
| 112 | matchIndex: 0, |
| 113 | isPerfectMatch: true, |
| 114 | newLine: "", |
| 115 | }); |
| 116 | }); |
| 117 | }); |
| 118 | |
| 119 | describe("streamLines", () => { |
| 120 | it("should split chunks into lines correctly", async () => { |
| 121 | async function* streamCompletion(): AsyncGenerator<string> { |
| 122 | yield "line1\nline"; |
| 123 | yield "2\nline3\n"; |
| 124 | yield "line4"; |
| 125 | } |
| 126 | |
| 127 | const resultLines: string[] = []; |
| 128 | for await (const line of streamLines(streamCompletion())) { |
| 129 | resultLines.push(line); |
| 130 | } |
| 131 | |
| 132 | expect(resultLines).toEqual(["line1", "line2", "line3", "line4"]); |
| 133 | }); |
| 134 | |
| 135 | it("should handle ChatMessage chunks", async () => { |
| 136 | const messageChunk1: ChatMessage = { |
| 137 | role: "assistant", |
| 138 | content: "line1\nline", |
| 139 | }; |
| 140 | const messageChunk2: ChatMessage = { |
| 141 | role: "assistant", |
| 142 | content: "2\nline3\n", |
| 143 | }; |
| 144 | const messageChunk3: ChatMessage = { |
| 145 | role: "assistant", |
| 146 | content: "line4", |
| 147 | }; |
| 148 | |
| 149 | // const spy = vi.spyOn(messageContentModule, "renderChatMessage"); |
| 150 | |
| 151 | async function* streamCompletion(): AsyncGenerator<ChatMessage> { |
| 152 | yield messageChunk1; |
| 153 | yield messageChunk2; |
| 154 | yield messageChunk3; |
| 155 | } |
| 156 | |
| 157 | const resultLines: string[] = []; |
| 158 | for await (const line of streamLines(streamCompletion())) { |
| 159 | resultLines.push(line); |
| 160 | } |
| 161 | |
| 162 | expect(resultLines).toEqual(["line1", "line2", "line3", "line4"]); |
| 163 | // expect(spy).toHaveBeenCalledTimes(3); |
| 164 | }); |
| 165 | |
| 166 | it("should log lines if log parameter is true", async () => { |
| 167 | const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 168 | |
| 169 | async function* streamCompletion(): AsyncGenerator<string> { |
| 170 | yield "line1\nline2\n"; |
| 171 | yield "line3"; |
| 172 | } |
| 173 | |
| 174 | const resultLines: string[] = []; |
| 175 | for await (const line of streamLines(streamCompletion(), true)) { |
| 176 | resultLines.push(line); |
| 177 | } |
| 178 | |
| 179 | expect(resultLines).toEqual(["line1", "line2", "line3"]); |
| 180 | expect(consoleSpy).toHaveBeenCalledWith( |
| 181 | "Streamed lines: ", |
| 182 | "line1\nline2\nline3", |
| 183 | ); |
| 184 | |
| 185 | consoleSpy.mockRestore(); |
| 186 | }); |
| 187 | }); |
| 188 | |
| 189 | describe("generateLines", () => { |
| 190 | it("should yield the lines provided in the array", async () => { |
| 191 | const lines = ["line1", "line2", "line3"]; |
| 192 | const resultLines: string[] = []; |
| 193 | |
| 194 | for await (const line of generateLines(lines)) { |
| 195 | resultLines.push(line); |
| 196 | } |
| 197 | |
| 198 | expect(resultLines).toEqual(lines); |
| 199 | }); |
| 200 | }); |