Replace a failed present card with the generic failed tool row; keep one event-authoritative card whichever of the call and its delivered event lands first. Use the host's real border token for the delivery card; include the visible description in the file button's accessible name and resolve its title path. Handle CommonMark angle-delimited image destinations and preserve titles; add transcript, prose-rewrite, and DOM regression tests. Update the change spec with review-driven acceptance commands, status, and evidence.
228 lines
12 KiB
TypeScript
228 lines
12 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { rewriteLocalImageUrls, rewriteProseLocalImageUrls } from '../src/client/markdown-images.ts'
|
|
import { rewriteLocalMarkdownLinks } from '../src/client/markdown-navigation.ts'
|
|
import type { SessionScope } from '../src/client/api.ts'
|
|
|
|
const ORIGIN = 'http://127.0.0.1:3080'
|
|
const scope: SessionScope = { sessionId: 'abc', cwd: '/repo' }
|
|
|
|
describe('rewriteLocalImageUrls', () => {
|
|
it('resolves relative destinations against the opened file directory', () => {
|
|
const md = '\n'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/docs/readme.md', ORIGIN)
|
|
expect(out).toContain(``)
|
|
expect(out).toContain(``)
|
|
})
|
|
|
|
it('expects the rewritten destination to be an absolute http URL MarkdownText accepts', () => {
|
|
const out = rewriteLocalImageUrls('', scope, '/repo/readme.md', ORIGIN)
|
|
const url = out.match(/\]\((http[^)]+)\)/)?.[1]
|
|
expect(url).toBeDefined()
|
|
expect(new URL(url as string).protocol).toBe('http:')
|
|
})
|
|
|
|
it('passes absolute local paths through unchanged', () => {
|
|
const md = ''
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/docs/readme.md', ORIGIN)
|
|
expect(out).toContain(``)
|
|
})
|
|
|
|
it('keeps remote http(s)/data/mailto destinations untouched', () => {
|
|
const md = '\n\n'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toContain('')
|
|
expect(out).toContain('')
|
|
expect(out).toContain('')
|
|
})
|
|
|
|
it('leaves in-page anchors untouched', () => {
|
|
const md = '\n\n[link](#top)'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toContain('')
|
|
})
|
|
|
|
it('rewrites reference-style image definitions', () => {
|
|
const md = '![logo][logo]\n\n[logo]: ./logo.svg'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toContain(`[logo]: ${ORIGIN}/sidebar/file?sessionId=abc&path=%2Frepo%2Flogo.svg&cwd=%2Frepo`)
|
|
// The inline reference itself is left for MarkdownText to combine.
|
|
expect(out).toContain('![logo][logo]')
|
|
})
|
|
|
|
it('normalizes `.`/`..` segments in relative destinations', () => {
|
|
const md = '\n'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/docs/sub/readme.md', ORIGIN)
|
|
expect(out).toContain(``)
|
|
expect(out).toContain(``)
|
|
expect(out).not.toContain('/.')
|
|
})
|
|
|
|
it('drops the cwd param when the scope has no cwd', () => {
|
|
const out = rewriteLocalImageUrls('', { sessionId: 'abc' }, '/repo/readme.md', ORIGIN)
|
|
expect(out).toContain(`sessionId=abc&path=%2Frepo%2Fimg.png`)
|
|
expect(out).not.toContain('cwd=')
|
|
})
|
|
|
|
it('handles windows absolute paths as local (not remote)', () => {
|
|
const md = ''
|
|
const out = rewriteLocalImageUrls(md, scope, 'C:\\repo\\docs\\readme.md', ORIGIN)
|
|
// A Windows drive path is absolute (not a remote scheme) → rewritten.
|
|
const url = out.match(/\]\(([^)]+)\)/)?.[1] as string
|
|
expect(url).toContain('/sidebar/file?')
|
|
expect(decodeURIComponent(url)).toContain('C:\\repo\\img.png')
|
|
})
|
|
|
|
it('does not rewrite image-looking text inside fenced code blocks', () => {
|
|
const md = '```\n\n```\n'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
// The fenced example must be preserved verbatim; only the real image is rewritten.
|
|
expect(out).toContain('```\n\n```')
|
|
expect(out).toContain('/sidebar/file?')
|
|
})
|
|
|
|
it('does not rewrite image-looking text inside inline code spans', () => {
|
|
const md = 'Use `` syntax.\n'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toContain('``')
|
|
expect(out).toContain('/sidebar/file?')
|
|
})
|
|
|
|
it('leaves every code-region form alone, tilde fences included', () => {
|
|
// The image pass had the SAME `~~~` blind spot as the link pass, and it
|
|
// predates this feature: on the 58d7f71 baseline
|
|
// `rewriteLocalImageUrls('~~~\n\n~~~')` already produced a
|
|
// `/sidebar/file` URL. Both passes now share one fence mask
|
|
// (`markdown-code.ts`).
|
|
const forms = [
|
|
'```\n\n```',
|
|
'````\n```\n\n```\n````',
|
|
'~~~\n\n~~~',
|
|
'~~~\nuse `` here\n~~~',
|
|
]
|
|
for (const form of forms) {
|
|
const out = rewriteLocalImageUrls(form, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out, form).toBe(form)
|
|
expect(out, form).not.toContain('/sidebar/file')
|
|
}
|
|
// A longer closing fence closes, and a real image after it is rewritten.
|
|
const after = rewriteLocalImageUrls('~~~\n\n~~~~\n\n', scope, '/repo/readme.md', ORIGIN)
|
|
expect(after).toContain('~~~\n\n~~~~')
|
|
expect(after).toMatch(/\[real\]\(.*\/sidebar\/file/)
|
|
// An unclosed fence runs to the end of the document.
|
|
expect(rewriteLocalImageUrls('~~~\n', scope, '/repo/readme.md', ORIGIN))
|
|
.toBe('~~~\n')
|
|
// A reference definition inside the fence is not harvested either.
|
|
const ref = rewriteLocalImageUrls('~~~\n![i][r]\n\n[r]: ./p.png\n~~~', scope, '/repo/readme.md', ORIGIN)
|
|
expect(ref).toBe('~~~\n![i][r]\n\n[r]: ./p.png\n~~~')
|
|
})
|
|
|
|
it('does not rewrite a reference definition used only by a plain link', () => {
|
|
// The `[manual][docs]` link references `[docs]: ./docs.md`, but that
|
|
// definition must NOT be redirected to /sidebar/file because it is not
|
|
// referenced by any image. Only image-referenced definitions are rewritten.
|
|
const md = '[manual][docs]\n\n![pic][pic-def]\n\n[docs]: ./docs.md\n[pic-def]: ./pic.png'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toContain('[docs]: ./docs.md')
|
|
expect(out).not.toMatch(/\[docs\]:.*\/sidebar\/file/)
|
|
// The image-referenced definition IS rewritten.
|
|
expect(out).toMatch(/\[pic-def\]:.*\/sidebar\/file/)
|
|
})
|
|
})
|
|
|
|
describe('rewriteLocalImageUrls: CommonMark angle destinations and titles (N7)', () => {
|
|
const MEDIA = (path: string): string => `${ORIGIN}/sidebar/file?sessionId=abc&path=${encodeURIComponent(path)}&cwd=%2Frepo`
|
|
|
|
it('rewrites an angle-delimited destination, including its space', () => {
|
|
const out = rewriteLocalImageUrls('', scope, '/repo/readme.md', ORIGIN)
|
|
// The delimiters are preserved; the URL-encoded path carries the space.
|
|
expect(out).toBe(``)
|
|
})
|
|
|
|
it('preserves an optional double-quoted title while rewriting the destination', () => {
|
|
const out = rewriteLocalImageUrls('', scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toBe(`} "the title")`)
|
|
})
|
|
|
|
it('preserves a multi-line double-quoted title', () => {
|
|
const md = ''
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toBe(`} "line one\nline two")`)
|
|
})
|
|
|
|
it('preserves a single-quoted title (it would otherwise not be rewritten at all)', () => {
|
|
const out = rewriteLocalImageUrls("", scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toBe(`} 'the title')`)
|
|
})
|
|
|
|
it('combines an angle destination with a title', () => {
|
|
const out = rewriteLocalImageUrls('', scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toBe(``)
|
|
})
|
|
|
|
it('leaves a remote angle destination and its title untouched', () => {
|
|
const md = ''
|
|
expect(rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)).toBe(md)
|
|
})
|
|
|
|
it('rewrites a reference definition with an angle destination, keeping its delimiters and title', () => {
|
|
const md = '![logo][logo]\n\n[logo]: <./logo mark.svg> "the logo"'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toContain(`[logo]: <${ORIGIN}/sidebar/file?sessionId=abc&path=%2Frepo%2Flogo+mark.svg&cwd=%2Frepo> "the logo"`)
|
|
})
|
|
})
|
|
|
|
describe('rewriteProseLocalImageUrls', () => {
|
|
const proseScope: SessionScope = { sessionId: 'abc', cwd: '/repo' }
|
|
const FILE_URL = (path: string, cwd = '/repo'): string =>
|
|
`${ORIGIN}/sidebar/file?sessionId=abc&path=${encodeURIComponent(path)}&cwd=${encodeURIComponent(cwd)}`
|
|
|
|
it('resolves a relative destination against the prose cwd (the row is not a file)', () => {
|
|
const out = rewriteProseLocalImageUrls('', proseScope, '/repo', ORIGIN)
|
|
expect(out).toBe(`})`)
|
|
})
|
|
|
|
it('leaves relative destinations untouched when the cwd is unknown', () => {
|
|
const md = '\n'
|
|
expect(rewriteProseLocalImageUrls(md, { sessionId: 'abc' }, undefined, ORIGIN)).toBe(md)
|
|
})
|
|
|
|
it('still serves an absolute local image when the cwd is unknown (no cwd param)', () => {
|
|
const out = rewriteProseLocalImageUrls('', { sessionId: 'abc' }, undefined, ORIGIN)
|
|
expect(out).toBe(``)
|
|
})
|
|
|
|
it('tolerates a trailing separator on the cwd', () => {
|
|
const out = rewriteProseLocalImageUrls('', proseScope, '/repo/', ORIGIN)
|
|
expect(out).toBe(`})`)
|
|
// Repeated separators collapse the same way.
|
|
const repeated = rewriteProseLocalImageUrls('', proseScope, '/repo///', ORIGIN)
|
|
expect(repeated).toBe(`})`)
|
|
})
|
|
|
|
it('resolves a relative destination against a Windows cwd', () => {
|
|
const out = rewriteProseLocalImageUrls('', { sessionId: 'abc', cwd: 'C:\\repo' }, 'C:\\repo\\', ORIGIN)
|
|
// A Windows root keeps its drive and backslash spelling after normalization.
|
|
expect(decodeURIComponent(out.replace(/\+/g, ' '))).toContain('C:\\repo\\cat.png')
|
|
})
|
|
|
|
it('rewrites a prose reference-style image against the cwd', () => {
|
|
const md = '![logo][logo]\n\n[logo]: ./logo.svg'
|
|
const out = rewriteProseLocalImageUrls(md, proseScope, '/repo', ORIGIN)
|
|
expect(out).toContain(`[logo]: ${FILE_URL('/repo/logo.svg')}`)
|
|
expect(out).toContain('![logo][logo]')
|
|
})
|
|
|
|
it('composes with the local-link rewrite: image first, then the link inside its label', () => {
|
|
const md = '[](./doc.md#intro)\n\n'
|
|
const out = rewriteLocalMarkdownLinks(
|
|
rewriteProseLocalImageUrls(md, proseScope, '/repo', ORIGIN),
|
|
undefined,
|
|
'/repo',
|
|
)
|
|
// The image destination became a media URL and the enclosing `.md` link is
|
|
// still claimed (its heading fragment survives as %23).
|
|
expect(out).toContain(`[})](./doc.md%23intro)`)
|
|
expect(out).toContain(`})`)
|
|
})
|
|
})
|