|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseIncompleteMarkdown } from "../lib/parse-incomplete-markdown"; |
| 3 | + |
| 4 | +describe("parseIncompleteMarkdown - word-internal underscores", () => { |
| 5 | + describe("underscores as word separators", () => { |
| 6 | + it("should handle single underscore between words", () => { |
| 7 | + const input = "hello_world"; |
| 8 | + const result = parseIncompleteMarkdown(input); |
| 9 | + expect(result).toBe("hello_world"); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should handle multiple underscores between words", () => { |
| 13 | + const input = "hello_world_test"; |
| 14 | + const result = parseIncompleteMarkdown(input); |
| 15 | + expect(result).toBe("hello_world_test"); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should handle CONSTANT_CASE", () => { |
| 19 | + const input = "MAX_VALUE"; |
| 20 | + const result = parseIncompleteMarkdown(input); |
| 21 | + expect(result).toBe("MAX_VALUE"); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should handle multiple snake_case words in text", () => { |
| 25 | + const input = "The user_name and user_email are required"; |
| 26 | + const result = parseIncompleteMarkdown(input); |
| 27 | + expect(result).toBe("The user_name and user_email are required"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should handle underscore in URLs", () => { |
| 31 | + const input = "Visit https://example.com/path_with_underscore"; |
| 32 | + const result = parseIncompleteMarkdown(input); |
| 33 | + expect(result).toBe("Visit https://example.com/path_with_underscore"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should handle numbers with underscores", () => { |
| 37 | + const input = "The value is 1_000_000"; |
| 38 | + const result = parseIncompleteMarkdown(input); |
| 39 | + expect(result).toBe("The value is 1_000_000"); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("incomplete italic formatting", () => { |
| 44 | + it("should complete italic at word boundary", () => { |
| 45 | + const input = "_italic text"; |
| 46 | + const result = parseIncompleteMarkdown(input); |
| 47 | + expect(result).toBe("_italic text_"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should complete italic with punctuation", () => { |
| 51 | + const input = "This is _italic"; |
| 52 | + const result = parseIncompleteMarkdown(input); |
| 53 | + expect(result).toBe("This is _italic_"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should complete italic before newline", () => { |
| 57 | + const input = "_italic\n"; |
| 58 | + const result = parseIncompleteMarkdown(input); |
| 59 | + expect(result).toBe("_italic_\n"); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe("edge cases", () => { |
| 64 | + it("should handle underscore at end of word (ambiguous case)", () => { |
| 65 | + const input = "word_"; |
| 66 | + const result = parseIncompleteMarkdown(input); |
| 67 | + expect(result).toBe("word_"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should handle leading underscore in identifier", () => { |
| 71 | + const input = "_privateVariable"; |
| 72 | + const result = parseIncompleteMarkdown(input); |
| 73 | + expect(result).toBe("_privateVariable_"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should handle code with underscores in markdown", () => { |
| 77 | + const input = "Use `variable_name` in your code"; |
| 78 | + const result = parseIncompleteMarkdown(input); |
| 79 | + expect(result).toBe("Use `variable_name` in your code"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should handle mixed snake_case and italic", () => { |
| 83 | + const input = "The variable_name is _important"; |
| 84 | + const result = parseIncompleteMarkdown(input); |
| 85 | + expect(result).toBe("The variable_name is _important_"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should not modify complete italic pairs", () => { |
| 89 | + const input = "_complete italic_ and some_other_text"; |
| 90 | + const result = parseIncompleteMarkdown(input); |
| 91 | + expect(result).toBe("_complete italic_ and some_other_text"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should handle underscore in code blocks", () => { |
| 95 | + const input = "```\nfunction_name()\n```"; |
| 96 | + const result = parseIncompleteMarkdown(input); |
| 97 | + expect(result).toBe("```\nfunction_name()\n```"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should handle HTML attributes with underscores", () => { |
| 101 | + const input = '<div data_attribute="value">'; |
| 102 | + const result = parseIncompleteMarkdown(input); |
| 103 | + expect(result).toBe('<div data_attribute="value">'); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("real-world scenarios", () => { |
| 108 | + it("should handle Python-style names", () => { |
| 109 | + const input = "__init__ and __main__ are special"; |
| 110 | + const result = parseIncompleteMarkdown(input); |
| 111 | + expect(result).toBe("__init__ and __main__ are special"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should handle markdown in sentences with snake_case", () => { |
| 115 | + const input = "The user_id field stores the _unique identifier"; |
| 116 | + const result = parseIncompleteMarkdown(input); |
| 117 | + expect(result).toBe("The user_id field stores the _unique identifier_"); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should handle the original bug report case", () => { |
| 121 | + const input = `hello_world |
| 122 | +
|
| 123 | +<a href="example_link"/>`; |
| 124 | + const result = parseIncompleteMarkdown(input); |
| 125 | + expect(result).toBe(input); |
| 126 | + expect(result).not.toMatch(/hello_world_/); |
| 127 | + expect(result).not.toMatch(/_$/); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments