import { expect, it, vi } from 'vitest'; import { renderToStaticMarkup } from 'react-dom/server'; import { ConsoleOutput, outputBytes, outputText } from './console-output'; it('renders hostile output as inert text, reports real status and preserves exact raw bytes', () => { const fetch = vi.fn(); vi.stubGlobal('fetch', fetch); try { const hostile = '\r\n\x1b[2J\u202eapprove', raw = Array.from(new TextEncoder().encode(hostile)); const job = { state: 'unknown', attempt_id: 'fixed-attempt', revision: 4, cancel_requested: true, result: { stdout: raw, stderr: [], exit_code: null, error_code: 'TIMEOUT', output_truncated: true, reboot_required: false } }; const before = JSON.stringify(job), html = renderToStaticMarkup(); for (const expected of ['unknown', 'fixed-attempt', 'TIMEOUT', 'unknown / nicht gemeldet', 'nur ein Ausschnitt', 'keinen Live-Ausgabestream', 'Abbruch angefordert', 'neuen genehmigten Plan', '<script>', '\\u000d', '\\u001b', '\\u202e', JSON.stringify(raw)]) expect(html).toContain(expected); expect(html).not.toMatch(/<(script|img|iframe|form|input)\b/i); expect(html).not.toContain('onclick='); expect(fetch).not.toHaveBeenCalled(); expect(JSON.stringify(job)).toBe(before); } finally { vi.unstubAllGlobals(); } }); it('distinguishes no result, empty bytes, invalid encoding, invalid bytes and missing truncation evidence', () => { const pending = renderToStaticMarkup(); expect(pending).toContain('Noch kein gespeichertes Ergebnis'); expect(pending).not.toContain('(leere Ausgabe)'); const partial = renderToStaticMarkup(); expect(partial).toContain('kein gültiges UTF-8'); expect(partial).toContain('Vollständigkeit ist nicht bestätigt'); expect(partial).toContain('(leere Ausgabe)'); const bad = renderToStaticMarkup(); expect(bad.match(/Keine verlässliche Textansicht/g)).toHaveLength(2); for (const raw of [undefined, 'hello', [1.5], [-1], [256]]) expect(() => outputBytes(raw)).toThrow(); const bytes = outputBytes([239, 187, 191, 65, 13, 10]); expect(outputText(bytes).text).toBe('\\ufeffA\\u000d\n'); expect(Array.from(bytes)).toEqual([239, 187, 191, 65, 13, 10]); });