|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
1 | 3 | import { EnvironmentId } from "@t3tools/contracts"; |
2 | 4 | import { act, type ComponentProps, type ReactNode } from "react"; |
3 | 5 | import { renderToStaticMarkup } from "react-dom/server"; |
@@ -73,6 +75,45 @@ function codeButton(renderer: ReactTestRenderer, label: string) { |
73 | 75 | return button.props as ComponentProps<typeof Button>; |
74 | 76 | } |
75 | 77 |
|
| 78 | +describe("ChatMarkdown bare anchor placeholders", () => { |
| 79 | + it.each(["<A>", "<a>", "<a >"])( |
| 80 | + "preserves unmatched %s without linking later blocks", |
| 81 | + (token) => { |
| 82 | + const text = `- **"From ${token}"** appears in the header.\n\n- **Tests:** cover inheritance.\n\nThe deferred move continues on B.\n\nSee <a href="https://example.com">the link</a>.`; |
| 83 | + const document = new DOMParser().parseFromString( |
| 84 | + renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={text} />), |
| 85 | + "text/html", |
| 86 | + ); |
| 87 | + |
| 88 | + expect(document.querySelector("strong")?.textContent).toBe(`"From ${token}"`); |
| 89 | + expect([...document.querySelectorAll("a")].map((link) => link.textContent)).toEqual([ |
| 90 | + "the link", |
| 91 | + ]); |
| 92 | + expect(document.querySelectorAll("li")).toHaveLength(2); |
| 93 | + expect( |
| 94 | + [...document.querySelectorAll("p")].map((paragraph) => paragraph.textContent), |
| 95 | + ).toContain("The deferred move continues on B."); |
| 96 | + }, |
| 97 | + ); |
| 98 | + |
| 99 | + it("preserves paired HTML anchors, details, markdown links, and inline code", () => { |
| 100 | + const text = |
| 101 | + 'Bare <a>label</a>, <a id="section"></a>, `<A>`, and [docs](https://example.com).\n\n<details><summary>More</summary>Details</details>'; |
| 102 | + const document = new DOMParser().parseFromString( |
| 103 | + renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={text} />), |
| 104 | + "text/html", |
| 105 | + ); |
| 106 | + |
| 107 | + expect([...document.querySelectorAll("a")].map((link) => link.textContent)).toEqual([ |
| 108 | + "label", |
| 109 | + "", |
| 110 | + "docs", |
| 111 | + ]); |
| 112 | + expect(document.querySelector("code")?.textContent).toBe("<A>"); |
| 113 | + expect(document.querySelector("[data-markdown-details]")?.textContent).toContain("More"); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
76 | 117 | describe("ChatMarkdown context references", () => { |
77 | 118 | it("renders text and image references through the chip renderer, with readable fallback", async () => { |
78 | 119 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
|
0 commit comments