import { windowsUpdateScriptHash } from './script-console';
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\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', 'Nicht gemeldet', 'nur ein Ausschnitt', 'gespeicherte Ausgabe', '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\n'); expect(Array.from(bytes)).toEqual([239, 187, 191, 65, 13, 10]);
});
it('shows unknown restart state for an incomplete Windows Update summary', () => {
function rendered(hash: string) {
const result = { stdout: [], stderr: [], reboot_required: false, evidence: {
script_sha256: hash, windows_update_reboot_required: null
}};
return renderToStaticMarkup();
}
expect(rendered(windowsUpdateScriptHash)).toContain('Neustartbedarf unbekannt');
expect(rendered('ordinary script')).not.toContain('Neustartbedarf unbekannt');
});
it('decodes native PowerShell CP850 umlauts while preserving raw bytes and control escapes', () => {
const raw = outputBytes([132, 148, 129, 142, 153, 154, 225, 13, 10, 27]);
const original = Array.from(raw);
expect(outputText(raw, 'powershell').text).toBe('äöüÄÖÜß\n\\u001b');
expect(outputText(raw, 'powershell').encoding).toContain('Quellcodepage nicht separat belegt');
expect(outputText(raw, 'utf-8').text).toContain('�');
expect(Array.from(raw)).toEqual(original);
expect(outputText(outputBytes([156, 255]), 'cp850').text).toBe('£ ');
});
it('keeps valid UTF-8 PowerShell output and generic invalid UTF-8 on their existing paths', () => {
const raw = new TextEncoder().encode('Grüße äöüÄÖÜß');
expect(outputText(raw, 'powershell').text).toBe('Grüße äöüÄÖÜß');
expect(outputText(outputBytes([148])).text).toBe('�');
});
it('renders CRLF lines in UTF-8 and CP850 with standalone carriage returns visible', () => {
const utf8 = Array.from(new TextEncoder().encode('Grüße\r\n\r\nOK\rEnde\r\n'));
const cp850 = [71, 114, 129, 225, 101, 13, 10, 13, 10, 79, 75, 13, 69, 110, 100, 101, 13, 10];
for (const bytes of [utf8, cp850]) {
const raw = outputBytes(bytes);
expect(outputText(raw, 'powershell').text).toBe('Grüße\n\nOK\\u000dEnde\n');
expect(Array.from(raw)).toEqual(bytes);
}
});
it('selects the native PowerShell text view only from matching result evidence', () => {
const result = { stdout: [98, 148, 115, 97, 114, 116, 105, 103], stderr: [], evidence: {interpreter:'powershell',output_encoding:'raw_native_bytes'} };
const before = JSON.stringify(result);
const html = renderToStaticMarkup();
expect(html).toContain('bösartig');
expect(html).toContain('stdout Textdekodierung');
expect(html).toContain('Windows OEM 850');
expect(JSON.stringify(result)).toBe(before);
const other = renderToStaticMarkup();
expect(other).not.toContain('bösartig');
});
it('shows the stored exit code and permission message for a restricted log result', () => {
const html = renderToStaticMarkup();
expect(html).toContain('Exitcode 0');
expect(html).toContain('Leseberechtigung');
expect(html).not.toContain('herunterladen');
expect(html).not.toContain('Noch kein gespeichertes Ergebnis');
});