import assert from 'node:assert/strict' import fs from 'node:fs/promises' import path from 'node:path' import { createHash } from 'node:crypto' import { exportSelectedConversationText } from '../../.tmp/rea-css-runtime/dist/artifacts/SelectedConversationText.js' const root = await fs.mkdtemp(path.resolve('.tmp/rea-session-export-test-')) const id = '11111111-1111-4111-8111-111111111111' const rows = [{ type: 'message', sessionId: id, role: 'user', content: 'test' }, { type: 'function_call', sessionId: id, callId: 'call', name: 'Read', arguments: '{"path":"test"}' }] const text = rows.map(row => JSON.stringify(row)).join('\n') + '\n' const first = path.join(root, 'project') await fs.mkdir(first) const file = path.join(first, `${id}.jsonl`) await fs.writeFile(file, text) await fs.writeFile(path.join(first, 'other.jsonl'), 'not valid JSON; must not be read') const result = await exportSelectedConversationText(root, id) assert.equal(result.operation, 'export_selected_conversation_text') assert.equal(result.authority, 'historical-reference') assert.equal(result.normalized_result.file.text, text) assert.equal(result.normalized_result.file.sha256, createHash('sha256').update(text).digest('hex')) assert.equal(result.normalized_result.file.records, 2) assert.equal(await fs.readFile(file, 'utf8'), text) await assert.rejects(exportSelectedConversationText(root, '../escape'), /UUID/) await assert.rejects(exportSelectedConversationText('relative', id), /absolute/) await fs.writeFile(file, '{invalid json}\n') await assert.rejects(exportSelectedConversationText(root, id), SyntaxError) await fs.writeFile(file, JSON.stringify({ sessionId: 'other' })) await assert.rejects(exportSelectedConversationText(root, id), /another session/) await fs.writeFile(file, text) const second = path.join(root, 'duplicate') await fs.mkdir(second) await fs.writeFile(path.join(second, `${id}.jsonl`), text) await assert.rejects(exportSelectedConversationText(root, id), /found 2/) const imageRoot = await fs.mkdtemp(path.resolve('.tmp/rea-session-images-test-')) const projects = path.join(imageRoot, 'projects') await fs.mkdir(projects) const image = Buffer.from('89504e470d0a1a0a', 'hex') const blobId = createHash('sha256').update(image).digest('hex') const blobDirectory = path.join(imageRoot, 'blobs', blobId.slice(0, 2)) await fs.mkdir(blobDirectory, { recursive: true }) const blobPath = path.join(blobDirectory, `${blobId}.png`) await fs.writeFile(blobPath, image) const imageRecord = { type: 'message', sessionId: id, role: 'user', content: [{ type: 'input_image', image: { type: 'image_blob_ref', blob_id: blobId, blob_path: blobPath, mime: 'image/png', size: image.length } }] } await fs.writeFile(path.join(projects, `${id}.jsonl`), JSON.stringify(imageRecord)) const exportedImages = await exportSelectedConversationText(projects, id, true) assert.equal(exportedImages.normalized_result.attachments[0].data_base64, image.toString('base64')) await fs.writeFile(blobPath, 'corrupt') await assert.rejects(exportSelectedConversationText(projects, id, true), /digest\/size/) imageRecord.content[0].image.blob_path = path.join(imageRoot, 'outside.png') await fs.writeFile(path.join(projects, `${id}.jsonl`), JSON.stringify(imageRecord)) await assert.rejects(exportSelectedConversationText(projects, id, true), /outside/) console.log('REA transcript export: exact selection, unchanged source, digest, malformed data and ambiguous matches passed.')