import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import fs from 'fs/promises'; import path from 'path'; import os from 'os'; import { fileURLToPath, pathToFileURL } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const PROJECT_ROOT = path.join(__dirname, '..'); describe('scripts/regenerate_gold_from_cache.mjs', () => { let tmpDir; let cacheDir; let goldPath; let ragPath; const origEnv = { ...process.env }; beforeEach(async () => { tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'regen-cache-')); cacheDir = path.join(tmpDir, 'cache'); goldPath = path.join(tmpDir, 'pipeline_gold.jsonl'); ragPath = path.join(tmpDir, 'rag_chunks.jsonl'); process.env.PIPELINE_CACHE_DIR = cacheDir; process.env.GOLD_PATH = goldPath; process.env.RAG_CHUNKS_PATH = ragPath; await fs.mkdir(cacheDir, { recursive: true }); await fs.mkdir(path.dirname(goldPath), { recursive: true }); await fs.mkdir(path.dirname(ragPath), { recursive: true }); }); afterEach(async () => { process.env = { ...origEnv }; await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => {}); }); it('reconstructs gold from cache with reward/verifier ok', async () => { // write chunks const chunk = { id: 'c1', content: 'chunk content', source: { meta: 1 } }; await fs.writeFile(ragPath, JSON.stringify(chunk) + '\n', 'utf8'); // questions cache await fs.writeFile( path.join(cacheDir, 'questions.jsonl'), JSON.stringify({ chunk_id: 'c1', questions: ['What is X?'], question_ids: ['q1'], ts: Date.now(), }) + '\n', 'utf8', ); // generations cache await fs.writeFile( path.join(cacheDir, 'generations.jsonl'), JSON.stringify({ chunk_id: 'c1', question_id: 'q1', gen_id: 'g1', answer: 'Answer text', raw: 'Answer text', ts: Date.now(), }) + '\n', 'utf8', ); // verification cache (ok) await fs.writeFile( path.join(cacheDir, 'verifications.jsonl'), JSON.stringify({ chunk_id: 'c1', question_id: 'q1', gen_id: 'g1', ok: true, score: 'PASS', raw: '...', ts: Date.now(), }) + '\n', 'utf8', ); // reward cache (ok) await fs.writeFile( path.join(cacheDir, 'rewards.jsonl'), JSON.stringify({ chunk_id: 'c1', question_id: 'q1', gen_id: 'g1', ok: true, score: 0.9, raw: '0.9', ts: Date.now(), }) + '\n', 'utf8', ); const mod = await import(pathToFileURL(path.join(PROJECT_ROOT, 'scripts', 'regenerate_gold_from_cache.mjs'))); await mod.main(); const out = await fs.readFile(goldPath, 'utf8'); const lines = out.split('\n').filter(Boolean); expect(lines).toHaveLength(1); const rec = JSON.parse(lines[0]); expect(rec.question).toBe('What is X?'); expect(rec.sample.answer).toBe('Answer text'); expect(rec.verifier.ok).toBe(true); expect(rec.reward.score).toBe(0.9); }); });