| 1 | // Generated by continue |
| 2 | |
| 3 | import { addIndentation } from "./streamDiffLines"; |
| 4 | import type { DiffLine } from "../index"; |
| 5 | |
| 6 | describe("addIndentation", () => { |
| 7 | async function* mockDiffLineGenerator(lines: DiffLine[]) { |
| 8 | for (const line of lines) { |
| 9 | yield line; |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | test("should add indentation to each line", async () => { |
| 14 | const lines: DiffLine[] = [ |
| 15 | { type: "same", line: "Line 1" }, |
| 16 | { type: "new", line: "Line 2" }, |
| 17 | { type: "old", line: "Line 3" }, |
| 18 | ]; |
| 19 | const indentation = " "; |
| 20 | |
| 21 | const generator = addIndentation(mockDiffLineGenerator(lines), indentation); |
| 22 | |
| 23 | const result: DiffLine[] = []; |
| 24 | for await (const diffLine of generator) { |
| 25 | result.push(diffLine); |
| 26 | } |
| 27 | |
| 28 | expect(result).toEqual([ |
| 29 | { type: "same", line: " Line 1" }, |
| 30 | { type: "new", line: " Line 2" }, |
| 31 | { type: "old", line: " Line 3" }, |
| 32 | ]); |
| 33 | }); |
| 34 | |
| 35 | test("should handle empty generator", async () => { |
| 36 | const lines: DiffLine[] = []; |
| 37 | const indentation = "\t"; |
| 38 | |
| 39 | const generator = addIndentation(mockDiffLineGenerator(lines), indentation); |
| 40 | |
| 41 | const result: DiffLine[] = []; |
| 42 | for await (const diffLine of generator) { |
| 43 | result.push(diffLine); |
| 44 | } |
| 45 | |
| 46 | expect(result).toEqual([]); |
| 47 | }); |
| 48 | |
| 49 | test("should handle no indentation", async () => { |
| 50 | const lines: DiffLine[] = [{ type: "same", line: "Line 1" }]; |
| 51 | const indentation = ""; |
| 52 | |
| 53 | const generator = addIndentation(mockDiffLineGenerator(lines), indentation); |
| 54 | |
| 55 | const result: DiffLine[] = []; |
| 56 | for await (const diffLine of generator) { |
| 57 | result.push(diffLine); |
| 58 | } |
| 59 | |
| 60 | expect(result).toEqual([{ type: "same", line: "Line 1" }]); |
| 61 | }); |
| 62 | |
| 63 | test("should propagate errors from the source generator", async () => { |
| 64 | async function* errorGenerator(): AsyncGenerator<DiffLine> { |
| 65 | throw new Error("Test error"); |
| 66 | } |
| 67 | const indentation = " "; |
| 68 | |
| 69 | const generator = addIndentation(errorGenerator(), indentation); |
| 70 | |
| 71 | await expect(async () => { |
| 72 | for await (const _ of generator) { |
| 73 | // Consume the generator |
| 74 | } |
| 75 | }).rejects.toThrow("Test error"); |
| 76 | }); |
| 77 | }); |