| 1 | import { extractContentFromCodeBlock } from "./extractContentFromCodeBlocks"; |
| 2 | |
| 3 | describe("extractContentFromCodeBlock", () => { |
| 4 | it("should extract content from code block with language and filepath", () => { |
| 5 | const content = "```typescript src/main.ts\nconst x = 1;\n```"; |
| 6 | expect(extractContentFromCodeBlock(content, "src/main.ts")).toBe( |
| 7 | "const x = 1;", |
| 8 | ); |
| 9 | }); |
| 10 | |
| 11 | it("should extract content from code block with filepath only (no language)", () => { |
| 12 | const content = "```test.js\nclass Calculator { }\n```"; |
| 13 | expect(extractContentFromCodeBlock(content, "test.js")).toBe( |
| 14 | "class Calculator { }", |
| 15 | ); |
| 16 | }); |
| 17 | |
| 18 | it("should extract content from code block with line ranges", () => { |
| 19 | const content = "```js test.js (19-25)\nfunction test() {}\n```"; |
| 20 | expect(extractContentFromCodeBlock(content, "test.js")).toBe( |
| 21 | "function test() {}", |
| 22 | ); |
| 23 | }); |
| 24 | |
| 25 | it("should handle multiple code blocks and find the right one", () => { |
| 26 | const content = |
| 27 | "```typescript src/main.ts\nconst x = 1;\n```\n" + |
| 28 | "```test.js\nclass Calculator { }\n```\n" + |
| 29 | "```js utils.js (19-25)\nfunction test() {}\n```"; |
| 30 | |
| 31 | expect(extractContentFromCodeBlock(content, "src/main.ts")).toBe( |
| 32 | "const x = 1;", |
| 33 | ); |
| 34 | expect(extractContentFromCodeBlock(content, "test.js")).toBe( |
| 35 | "class Calculator { }", |
| 36 | ); |
| 37 | expect(extractContentFromCodeBlock(content, "utils.js")).toBe( |
| 38 | "function test() {}", |
| 39 | ); |
| 40 | }); |
| 41 | |
| 42 | it("should handle paths with special regex characters", () => { |
| 43 | const content = |
| 44 | "```typescript src/special+chars.ts[123]\nconst special = true;\n```"; |
| 45 | expect( |
| 46 | extractContentFromCodeBlock(content, "src/special+chars.ts[123]"), |
| 47 | ).toBe("const special = true;"); |
| 48 | }); |
| 49 | |
| 50 | it("should handle multiline content", () => { |
| 51 | const content = |
| 52 | "```typescript src/multi-line.ts\n" + |
| 53 | "function example() {\n" + |
| 54 | " const x = 1;\n" + |
| 55 | " return x + 2;\n" + |
| 56 | "}\n" + |
| 57 | "```"; |
| 58 | |
| 59 | const expected = |
| 60 | "function example() {\n" + " const x = 1;\n" + " return x + 2;\n" + "}"; |
| 61 | |
| 62 | expect(extractContentFromCodeBlock(content, "src/multi-line.ts")).toBe( |
| 63 | expected, |
| 64 | ); |
| 65 | }); |
| 66 | |
| 67 | it("should return undefined when path doesn't exist in any code block", () => { |
| 68 | const content = "```typescript src/main.ts\nconst x = 1;\n```"; |
| 69 | expect( |
| 70 | extractContentFromCodeBlock(content, "nonexistent.ts"), |
| 71 | ).toBeUndefined(); |
| 72 | }); |
| 73 | |
| 74 | it("should extract content when the path has directory structure", () => { |
| 75 | const content = |
| 76 | "```js src/components/Button.jsx\nconst Button = () => <button>Click me</button>;\n```"; |
| 77 | expect( |
| 78 | extractContentFromCodeBlock(content, "src/components/Button.jsx"), |
| 79 | ).toBe("const Button = () => <button>Click me</button>;"); |
| 80 | }); |
| 81 | |
| 82 | it("should match exact paths only", () => { |
| 83 | const content = |
| 84 | "```typescript src/utils.ts\nconst util = {};\n```\n" + |
| 85 | "```typescript src/utils/index.ts\nconst index = {};\n```"; |
| 86 | |
| 87 | expect(extractContentFromCodeBlock(content, "src/utils.ts")).toBe( |
| 88 | "const util = {};", |
| 89 | ); |
| 90 | expect(extractContentFromCodeBlock(content, "src/utils/index.ts")).toBe( |
| 91 | "const index = {};", |
| 92 | ); |
| 93 | }); |
| 94 | }); |