| 1 | import { describe, expect, test } from "bun:test"; |
| 2 | import { annotate, inlineSource, writeInline } from "../src/source.ts"; |
| 3 | |
| 4 | const PAGE = `<section> |
| 5 | <p class="lede" data-copy> |
| 6 | Already have a Vite app? One resource in{" "} |
| 7 | <code class="alc-code-inline">alchemy.run.ts</code> is the whole |
| 8 | integration &mdash; see <a href="/docs" class="link">the docs</a>. |
| 9 | </p> |
| 10 | <h1 data-copy>Reach for <span>it</span></h1> |
| 11 | </section> |
| 12 | `; |
| 13 | |
| 14 | describe("inline markdown", () => { |
| 15 | test("marks convertible elements", () => { |
| 16 | const out = annotate(PAGE, "f"); |
| 17 | expect(out).toContain('data-copy="f#0" data-copy-format="inline"'); |
| 18 | expect(out).toContain('data-copy="f#1">'); |
| 19 | }); |
| 20 | |
| 21 | test("reads content as inline markdown", () => { |
| 22 | expect(inlineSource(PAGE, 0).markdown).toBe( |
| 23 | "Already have a Vite app? One resource in `alchemy.run.ts` is the whole integration — see [the docs](/docs).", |
| 24 | ); |
| 25 | }); |
| 26 | |
| 27 | test("deleting inline code writes plain text", () => { |
| 28 | const base = inlineSource(PAGE, 0).markdown; |
| 29 | const out = writeInline(PAGE, 0, { |
| 30 | base, |
| 31 | markdown: base.replace("`alchemy.run.ts`", "your config"), |
| 32 | }); |
| 33 | expect(out).toContain("One resource in your config is the whole"); |
| 34 | expect(out).not.toContain("<code"); |
| 35 | }); |
| 36 | |
| 37 | test("backticks become code with the page's classes", () => { |
| 38 | const base = inlineSource(PAGE, 0).markdown; |
| 39 | const out = writeInline(PAGE, 0, { |
| 40 | base, |
| 41 | markdown: `${base} Run \`alchemy dev\` and **go**.`, |
| 42 | }); |
| 43 | expect(out).toContain( |
| 44 | 'Run <code class="alc-code-inline">alchemy dev</code> and <strong>go</strong>.', |
| 45 | ); |
| 46 | expect(out).toContain('<a href="/docs" class="link">the docs</a>'); |
| 47 | // Round trip. |
| 48 | const n = inlineSource(out, 0).markdown; |
| 49 | expect(n).toBe(`${base} Run \`alchemy dev\` and **go**.`); |
| 50 | }); |
| 51 | |
| 52 | test("escapes template-special characters", () => { |
| 53 | const base = inlineSource(PAGE, 0).markdown; |
| 54 | const out = writeInline(PAGE, 0, { |
| 55 | base, |
| 56 | markdown: 'Use {x} & <T> it\'s "ok"', |
| 57 | }); |
| 58 | expect(out).toContain(`Use &#123;x&#125; &amp; &lt;T&gt; it's "ok"`); |
| 59 | }); |
| 60 | |
| 61 | test("rejects stale edits", () => { |
| 62 | expect(() => |
| 63 | writeInline(PAGE, 0, { base: "old", markdown: "new" }), |
| 64 | ).toThrow(/changed/); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | test("line breaks round-trip as <br>", () => { |
| 69 | const PAGE2 = `<h2 data-copy>Reach for the\n better primitives.</h2>`; |
| 70 | const base = inlineSource(PAGE2, 0).markdown; |
| 71 | const out = writeInline(PAGE2, 0, { |
| 72 | base, |
| 73 | markdown: "Reach for the\nbetter `primitives`.", |
| 74 | }); |
| 75 | expect(out).toContain("Reach for the<br>better <code>primitives</code>."); |
| 76 | expect(inlineSource(out, 0).markdown).toBe( |
| 77 | "Reach for the\nbetter `primitives`.", |
| 78 | ); |
| 79 | }); |