|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Generate shard planning overview files from manifest.json |
| 5 | + * |
| 6 | + * Reads a manifest.json file produced by plan-shards.js and generates: |
| 7 | + * - overview.txt: Human-readable shard summary |
| 8 | + * - overview.json: JSON format with stats |
| 9 | + * - files-<i>.txt: Test file list for each shard |
| 10 | + */ |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | +const path = require('path'); |
| 14 | + |
| 15 | +const outDir = process.argv[2] || path.join('reports', 'shards'); |
| 16 | +const timingsPath = process.argv[3] || 'timings-current.json'; |
| 17 | +const manifestPath = path.join(outDir, 'manifest.json'); |
| 18 | + |
| 19 | +if (!fs.existsSync(manifestPath)) { |
| 20 | + console.error(`❌ Manifest file not found: ${manifestPath}`); |
| 21 | + process.exit(1); |
| 22 | +} |
| 23 | + |
| 24 | +const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); |
| 25 | +const bins = Array.isArray(manifest.bins) ? manifest.bins : []; |
| 26 | + |
| 27 | +// Build known/unknown stats using timings file if present |
| 28 | +let known = new Set(); |
| 29 | +try { |
| 30 | + const timings = JSON.parse(fs.readFileSync(timingsPath, 'utf8')); |
| 31 | + for (const f of (timings.files || [])) { |
| 32 | + if (f && f.file) { |
| 33 | + known.add(String(f.file).trim()); |
| 34 | + } |
| 35 | + } |
| 36 | +} catch (err) { |
| 37 | + console.warn(`⚠️ Could not load timings from ${timingsPath}: ${err.message}`); |
| 38 | +} |
| 39 | + |
| 40 | +// Calculate statistics |
| 41 | +let totalTests = 0; |
| 42 | +let knownCount = 0; |
| 43 | +let unknownCount = 0; |
| 44 | + |
| 45 | +bins.forEach(bin => { |
| 46 | + const names = (bin.items || []) |
| 47 | + .map(it => typeof it === 'string' ? it : (it && it.name) || it) |
| 48 | + .filter(Boolean); |
| 49 | + |
| 50 | + totalTests += names.length; |
| 51 | + names.forEach(name => { |
| 52 | + if (known.has(name)) { |
| 53 | + knownCount++; |
| 54 | + } else { |
| 55 | + unknownCount++; |
| 56 | + } |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +// Generate overview data |
| 61 | +const overview = bins.map((bin, i) => ({ |
| 62 | + shard: i, |
| 63 | + count: (bin.items || []).length, |
| 64 | + totalSec: Number(bin.total || 0) |
| 65 | +})); |
| 66 | + |
| 67 | +// Write overview.txt (human-readable) |
| 68 | +const lines = overview.map(o => |
| 69 | + `#${o.shard}\tcount=${o.count}\ttotal=${o.totalSec.toFixed(2)}s` |
| 70 | +); |
| 71 | +fs.writeFileSync( |
| 72 | + path.join(outDir, 'overview.txt'), |
| 73 | + lines.join('\n') + '\n' |
| 74 | +); |
| 75 | + |
| 76 | +// Write overview.json (structured data) |
| 77 | +const overviewData = { |
| 78 | + shards: bins.length, |
| 79 | + overview, |
| 80 | + totals: bins.map(b => b.total), |
| 81 | + counts: bins.map(b => (b.items || []).length), |
| 82 | + stats: { |
| 83 | + totalTests, |
| 84 | + knownCount, |
| 85 | + unknownCount, |
| 86 | + knownPercentage: totalTests > 0 ? ((knownCount / totalTests) * 100).toFixed(1) : 0 |
| 87 | + } |
| 88 | +}; |
| 89 | +fs.writeFileSync( |
| 90 | + path.join(outDir, 'overview.json'), |
| 91 | + JSON.stringify(overviewData, null, 2) |
| 92 | +); |
| 93 | + |
| 94 | +// Write files-<i>.txt (test list per shard) |
| 95 | +bins.forEach((bin, i) => { |
| 96 | + const names = (bin.items || []) |
| 97 | + .map(it => typeof it === 'string' ? it : (it && it.name) || it) |
| 98 | + .filter(Boolean); |
| 99 | + |
| 100 | + fs.writeFileSync( |
| 101 | + path.join(outDir, `files-${i}.txt`), |
| 102 | + names.join('\n') + (names.length ? '\n' : '') |
| 103 | + ); |
| 104 | +}); |
| 105 | + |
| 106 | +console.log(`✅ Wrote overview.txt, overview.json and ${bins.length} shard files to ${outDir}`); |
| 107 | +console.log(` Total tests: ${totalTests} (${knownCount} with timing data, ${unknownCount} unknown)`); |
0 commit comments