git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/markdown.test.ts
import {expect, test} from 'bun:test'
import {inline, linkSessions, markdown} from '@/lib/markdown'
// Vnodes as plain data: the tag and the text inside, enough to see the shape.
type Node = {tag?: string; children?: unknown[]; text?: unknown; attrs?: Record<string, unknown>}
const shape = (x: unknown): unknown => {
if (Array.isArray(x)) return x.map(shape)
if (x == null || typeof x !== 'object') return x
const n = x as Node
const raw = n.children ?? (n.text != null ? [n.text] : [])
const kids = Array.isArray(raw) ? raw : [raw]
if (n.tag === '#') return n.children ?? n.text
return {[String(n.tag)]: kids.map(shape).filter((k) => k !== null && k !== undefined)}
}
const flat = (x: unknown): string => JSON.stringify(shape(x))
test('inline marks become elements and stay text', () => {
const s = flat(inline('run `cargo test`, **then** look at *this* [page](https://x.org) or <script>'))
expect(s).toContain('{"code":["cargo test"]}')
expect(s).toContain('{"strong":["then"]}')
expect(s).toContain('{"em":["this"]}')
expect(s).toContain('{"a":[{"span":["page"]}]}')
expect(s).toContain('<script>')
})
test('a link goes only to http(s)', () => {
expect(flat(inline('[x](javascript:alert(1))'))).not.toContain('"a"')
})
test('blocks: headings, lists, fenced code, paragraphs', () => {
const s = flat(markdown('# Title\n\n- one\n- two\n\n```rust\nfn main() {}\n```\n\nThe end.'))
expect(s).toContain('"h3"')
expect(s).toContain('{"ul":[{"li":["one"]},{"li":["two"]}]}')
// Highlighted: the keyword in its own span, the rest as text.
expect(s).toContain('{"pre":[{"code":[{"span":["fn"]}," ","main","() {}"]}]}')
expect(s).toContain('{"p":["The end."]}')
})
test('a table: its head, its rows, and how each column is aligned', () => {
const md = 'Results:\n\n| Test | Result |\n|---|--:|\n| `account_row` | ok |\n| a \\| b | **fail** |\n\nDone.'
const s = flat(markdown(md))
expect(s).toContain('{"thead":[{"tr":[{"th":["Test"]},{"th":["Result"]}]}]}')
expect(s).toContain('{"tr":[{"td":[{"code":["account_row"]}]},{"td":["ok"]}]}')
expect(s).toContain('{"td":["a | b"]}')
expect(s).toContain('{"td":[{"strong":["fail"]}]}')
expect(s).toContain('{"p":["Done."]}')
})
test('a pipe in a sentence is not a table', () => {
expect(flat(markdown('use a | b here\nand more'))).not.toContain('table')
})
test('a table straight after a sentence is still a table', () => {
const s = flat(markdown('All green:\n| Test | Result |\n|---|---|\n| a | ok |'))
expect(s).toContain('{"p":["All green:"]}')
expect(s).toContain('{"th":["Test"]}')
})
test('a session the text names links to it, in this window; an unknown one stays text', () => {
linkSessions((id) => id === 's3' || id === 's123')
try {
const s = JSON.stringify(inline('ask S123 and `session-s3`, not s9 or AWS s3x, see [it](/sessions/s3)'))
expect(s.match(/"href":"\/sessions\/s123"/g)?.length).toBe(1)
expect(s.match(/"href":"\/sessions\/s3"/g)?.length).toBe(2)
expect(s).not.toContain('/sessions/s9')
expect(s).not.toContain('_blank')
expect(flat(inline('s9 and s3x'))).toBe('["s9 and s3x"]')
} finally {
linkSessions(() => false)
}
})
test('an analysis fence is a panel of Markdown, its recommendation set apart', () => {
const md = 'Found it.\n\n```analysis\n### Problem\nThe gate **fails**.\n### Evidence\n- 2 of 2 runs\n### Recommendation\nFix the cause.\n```\n\nWhich?'
const s = flat(markdown(md))
expect(s).not.toContain('"pre"')
expect(s).toContain('{"strong":["fails"]}')
expect(s).toContain('{"ul":[{"li":["2 of 2 runs"]}]}')
expect(s).toContain('{"div":[{"h5":["Recommendation"]},{"p":["Fix the cause."]}]}')
expect(s).toContain('{"p":["Which?"]}')
})
test('a new-session fence is the same panel, marked as a proposal', () => {
const s = JSON.stringify(markdown('```new-session\n### The wish\nA new page.\n### Recommendation\nA new session.\n```'))
expect(s).toContain('md__analysis--new-session')
expect(s).toContain('New session?')
expect(s).not.toContain('"pre"')
})
test('an impact fence is the same panel, what it needs first set apart at its foot', () => {
const md = '```impact\n### What changes\nThe coach can ask for a picture.\n### Before it works\nOne key on the hub.\n```'
const s = JSON.stringify(markdown(md))
expect(s).toContain('md__analysis--impact')
expect(s).toContain('Once it lands')
expect(s).not.toContain('"pre"')
expect(flat(markdown(md))).toContain('{"div":[{"h5":["Before it works"]},{"p":["One key on the hub."]}]}')
})