1import { afterEach, describe, expect, it, vi } from 'vitest'2import { tikzCore } from '../artifacts/tikz/tikz-core.js'3import { createTikzAdapter } from '../src/adapters/tikz'4import tikzUnit from '../src/units/tikz'5const request = (source: string) => ({6 engine: 'tikz' as const, source, format: 'svg' as const, options: {},7 metadata: { title: '', description: '' },8 presentation: { background: 'transparent', padding: 0, frame: false },9})1011afterEach(() => {12 vi.restoreAllMocks()13 vi.unstubAllGlobals()14})1516describe('native TikZ edge adapter', () => {17 it('extracts a complete document and renders it through the edge core', async () => {18 const load = vi.spyOn(tikzCore, 'load').mockResolvedValue()19 const texify = vi.spyOn(tikzCore, 'texify').mockResolvedValue('<svg><path d="M0 0h1"/></svg>')20 const tikzAdapter = createTikzAdapter(async () => new Response('asset'))21 const source = String.raw`\documentclass{standalone}22\usepackage{tikz}23\begin{document}24\begin{tikzpicture}\draw (0,0) -- (1,1);\end{tikzpicture}25\end{document}`2627 const result = await tikzAdapter.render(request(source), new AbortController().signal)2829 expect(load).toHaveBeenCalledOnce()30 expect(texify).toHaveBeenCalledWith(expect.stringContaining('\\begin{tikzpicture}'), {31 addToPreamble: expect.stringContaining('\\usepackage{tikz}'),32 })33 expect(result).toMatchObject({ runtime: 'edge-wasm', contentType: 'image/svg+xml' })34 })3536 it('rejects oversized input before entering the serialized core', async () => {37 const tikzAdapter = createTikzAdapter(async () => new Response('asset'))38 const result = tikzAdapter.render(request('x'.repeat(256 * 1024 + 1)), new AbortController().signal)39 await expect(result).rejects.toMatchObject({ code: 'source_too_large', status: 413 })40 })4142 it('loads runtime files through the static-assets binding', async () => {43 const assetFetch = vi.fn(async (_input: RequestInfo | URL) => new Response('compressed asset'))44 vi.spyOn(tikzCore, 'load').mockImplementation(async fetchAsset => {45 const response = await fetchAsset('core.dump.gz')46 expect(await response.text()).toBe('compressed asset')47 })48 vi.spyOn(tikzCore, 'texify').mockResolvedValue('<svg><path d="M0 0h1"/></svg>')49 const fetchUnit = tikzUnit.fetch!50 const response = await fetchUnit(51 new Request('https://tikz.render.diagram.zip/v1/svg', {52 method: 'POST',53 headers: { 'Content-Type': 'application/json' },54 body: JSON.stringify({55 source: `\\begin{tikzpicture}\\draw (0,0) -- (${crypto.randomUUID().length},1);\\end{tikzpicture}`,56 options: {},57 metadata: { title: '', description: '' },58 presentation: { background: '', padding: 0, frame: false },59 }),60 }) as Parameters<typeof fetchUnit>[0],61 { RENDERER_BUILD: 'tikz-test-1', ASSETS: { fetch: assetFetch } } as unknown as Parameters<typeof fetchUnit>[1],62 { waitUntil() {}, passThroughOnException() {}, props: {} } as unknown as ExecutionContext,63 )6465 expect(response.status).toBe(200)66 expect(assetFetch).toHaveBeenCalledOnce()67 expect(new URL(String(assetFetch.mock.calls[0]?.[0])).pathname).toBe('/core.dump.gz')68 })69})