|
| 1 | +import assert from 'assert'; |
| 2 | +import fs from 'fs'; |
| 3 | +import os from 'os'; |
| 4 | +import path from 'path'; |
| 5 | +import { createRequire } from 'module'; |
| 6 | + |
| 7 | +const require = createRequire(import.meta.url); |
| 8 | +const { cmdConvertSession } = require('../../cli/session-convert'); |
| 9 | + |
| 10 | +function writeJsonl(filePath, records) { |
| 11 | + const lines = records.map((r) => JSON.stringify(r)); |
| 12 | + fs.writeFileSync(filePath, `${lines.join('\n')}\n`, 'utf-8'); |
| 13 | +} |
| 14 | + |
| 15 | +function listFiles(dirPath) { |
| 16 | + return fs.readdirSync(dirPath).filter(Boolean).sort(); |
| 17 | +} |
| 18 | + |
| 19 | +test('convert-session converts codex jsonl to claude jsonl', async () => { |
| 20 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codexmate-convert-')); |
| 21 | + const inputPath = path.join(tmpDir, 'input.jsonl'); |
| 22 | + const outDir = path.join(tmpDir, 'out'); |
| 23 | + fs.mkdirSync(outDir); |
| 24 | + |
| 25 | + writeJsonl(inputPath, [ |
| 26 | + { type: 'session_meta', timestamp: '2026-04-29T00:00:00.000Z', payload: { id: 'sess-1', cwd: '/repo' } }, |
| 27 | + { type: 'response_item', timestamp: '2026-04-29T00:00:01.000Z', payload: { type: 'message', role: 'user', content: 'hi' } }, |
| 28 | + { type: 'response_item', timestamp: '2026-04-29T00:00:02.000Z', payload: { type: 'message', role: 'assistant', content: 'hello' } } |
| 29 | + ]); |
| 30 | + |
| 31 | + await cmdConvertSession( |
| 32 | + ['--from', 'codex', '--to', 'claude', '--file', inputPath, '--output', `${outDir}/`], |
| 33 | + { resolveSessionFilePath: () => inputPath } |
| 34 | + ); |
| 35 | + |
| 36 | + const files = listFiles(outDir); |
| 37 | + assert.deepStrictEqual(files, ['claude-session-sess-1.jsonl']); |
| 38 | + const content = fs.readFileSync(path.join(outDir, files[0]), 'utf-8').trim(); |
| 39 | + const records = content.split('\n').map((line) => JSON.parse(line)); |
| 40 | + assert.strictEqual(records.length, 2); |
| 41 | + assert.strictEqual(records[0].type, 'user'); |
| 42 | + assert.strictEqual(records[0].sessionId, 'sess-1'); |
| 43 | + assert.strictEqual(records[0].cwd, '/repo'); |
| 44 | + assert.strictEqual(records[0].message.content, 'hi'); |
| 45 | + assert.strictEqual(records[1].type, 'assistant'); |
| 46 | + assert.strictEqual(records[1].message.content, 'hello'); |
| 47 | +}); |
| 48 | + |
| 49 | +test('convert-session converts claude jsonl to codex jsonl', async () => { |
| 50 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codexmate-convert-')); |
| 51 | + const inputPath = path.join(tmpDir, 'input.jsonl'); |
| 52 | + const outDir = path.join(tmpDir, 'out'); |
| 53 | + fs.mkdirSync(outDir); |
| 54 | + |
| 55 | + writeJsonl(inputPath, [ |
| 56 | + { type: 'user', timestamp: '2026-04-29T00:00:01.000Z', sessionId: 'sess-2', cwd: '/repo', message: { content: 'hi' } }, |
| 57 | + { type: 'assistant', timestamp: '2026-04-29T00:00:02.000Z', sessionId: 'sess-2', cwd: '/repo', message: { content: 'hello' } } |
| 58 | + ]); |
| 59 | + |
| 60 | + await cmdConvertSession( |
| 61 | + ['--from', 'claude', '--to', 'codex', '--file', inputPath, '--output', `${outDir}/`], |
| 62 | + { resolveSessionFilePath: () => inputPath } |
| 63 | + ); |
| 64 | + |
| 65 | + const files = listFiles(outDir); |
| 66 | + assert.deepStrictEqual(files, ['codex-session-sess-2.jsonl']); |
| 67 | + const content = fs.readFileSync(path.join(outDir, files[0]), 'utf-8').trim(); |
| 68 | + const records = content.split('\n').map((line) => JSON.parse(line)); |
| 69 | + assert.strictEqual(records[0].type, 'session_meta'); |
| 70 | + assert.strictEqual(records[0].payload.id, 'sess-2'); |
| 71 | + assert.strictEqual(records[0].payload.cwd, '/repo'); |
| 72 | + assert.strictEqual(records[1].type, 'response_item'); |
| 73 | + assert.strictEqual(records[1].payload.role, 'user'); |
| 74 | + assert.strictEqual(records[2].payload.role, 'assistant'); |
| 75 | +}); |
| 76 | + |
0 commit comments