import assert from 'node:assert/strict' import test from 'node:test' import { loadTestModule } from './load-test-module.mjs' const { LARGE_LOG_CHAR_THRESHOLD, LARGE_LOG_LINE_THRESHOLD, countLogLines, createLogLine, formatLogLine, formatLogLines, formatSelectedLogRows, isLargeLogPayload, } = await loadTestModule(`export * from './lib/logs'`) const renderSelector = 'newRenderPipelineStateWithDescriptor:error:' const computeSelector = 'newComputePipelineStateWithDescriptor:options:reflection:error:' const precisionNote = (selector, code) => `[QEMU] reims-vgpu: ${selector}: metal2vulkan-runtime made it with a precision note (metal2vulkan-degrade-${code}): its answers there can differ from Metal's on this device; the first of its kind, each make that carries it counted` test('successful render and compute precision notes keep their text as informational logs', () => { for (const line of [ precisionNote(renderSelector, 'device-denormals'), precisionNote(computeSelector, 'simdgroup-access-order'), ]) { const row = createLogLine({ line }) assert.equal(row.level, 'info') assert.equal(row.line, line) } }) test('pipeline refusals and fatal errors retain their error level', () => { for (const line of [ `[QEMU] reims-vgpu: platform call ${renderSelector}: the runtime refused it`, '[QEMU] reims-vgpu: fatal: Vulkan device was lost', `${precisionNote(renderSelector, 'device-denormals')}; error: failed to submit the command buffer`, ]) { assert.equal(createLogLine({ line }).level, 'error') } }) test('formatLogLine and formatLogLines emit single-line entries', () => { const row = createLogLine({ ts: '12:34:56', source: 'qemu', line: 'booting kernel' }) assert.equal(formatLogLine(row), '12:34:56 [qemu] booting kernel') assert.equal( formatLogLines([ row, createLogLine({ ts: '12:34:57', source: 'app', line: 'guest ready' }), ]), '12:34:56 [qemu] booting kernel\n12:34:57 [app] guest ready', ) }) test('countLogLines handles empty strings, CRLF, and trailing newlines', () => { assert.equal(countLogLines(''), 0) assert.equal(countLogLines('\n'), 0) assert.equal(countLogLines('one line'), 1) assert.equal(countLogLines('a\nb\n'), 2) assert.equal(countLogLines('a\r\nb\r\nc'), 3) }) test('isLargeLogPayload enforces line and character thresholds', () => { assert.equal(LARGE_LOG_LINE_THRESHOLD, 20) assert.equal(LARGE_LOG_CHAR_THRESHOLD, 2000) const nineteenLines = Array.from({ length: 19 }, (_, i) => `line ${i + 1}`).join('\n') const twentyLines = Array.from({ length: 20 }, (_, i) => `line ${i + 1}`).join('\n') assert.equal(isLargeLogPayload(''), false) assert.equal(isLargeLogPayload(nineteenLines), false) assert.equal(isLargeLogPayload(`${nineteenLines}\n`), false) assert.equal(isLargeLogPayload(twentyLines), true) assert.equal(isLargeLogPayload('x'.repeat(1999)), false) assert.equal(isLargeLogPayload('x'.repeat(2000)), true) }) test('formatSelectedLogRows normalizes multi-row DOM selections into single lines', () => { const normalized = formatSelectedLogRows([ { ts: ' 09:00:01 ', source: ' host ', line: ' indented message' }, { ts: '09:00:02', source: 'qemu', line: 'vgpu ready' }, ]) assert.equal( normalized, '09:00:01 [host] indented message\n09:00:02 [qemu] vgpu ready', ) }) test('formatSelectedLogRows flattens embedded LF, CRLF, and CR breaks within a log row', () => { const normalized = formatSelectedLogRows([ { ts: '00:00:00', source: 'app', line: 'first\ncontinued\n\ntrailing\n' }, { ts: '00:00:01', source: 'qemu', line: 'win\r\nsecond line\r\n' }, { ts: '00:00:02', source: 'host', line: '\rcr\rjoined' }, ]) assert.equal( normalized, '00:00:00 [app] first continued trailing\n00:00:01 [qemu] win second line\n00:00:02 [host] cr joined', ) assert.equal(countLogLines(normalized), 3) assert.equal( formatLogLine({ ts: '00:00:03', source: 'app', line: 'alpha\r\nbeta\ngamma' }), '00:00:03 [app] alpha beta gamma', ) })