|
| 1 | +/** |
| 2 | + * Audit image resolution in gridset files |
| 3 | + * |
| 4 | + * This test audits a gridset file to ensure all images in the ZIP |
| 5 | + * are being resolved correctly by the processor. |
| 6 | + */ |
| 7 | + |
| 8 | +import { GridsetProcessor } from '../src/processors/gridsetProcessor'; |
| 9 | +import { resolveGrid3CellImage } from '../src/processors/gridset/resolver'; |
| 10 | +import path from 'node:path'; |
| 11 | + |
| 12 | +interface AuditResult { |
| 13 | + totalCells: number; |
| 14 | + cellsWithDeclaredImages: number; |
| 15 | + cellsWithResolvedImages: number; |
| 16 | + cellsWithoutResolvedImages: number; |
| 17 | + actualImageFilesInZip: number; |
| 18 | + resolvedImagePaths: string[]; |
| 19 | + unresolvedCells: Array<{ |
| 20 | + label: string; |
| 21 | + x: number; |
| 22 | + y: number; |
| 23 | + imageName: string; |
| 24 | + page: string; |
| 25 | + }>; |
| 26 | + imageFilesInZip: string[]; |
| 27 | + resolvedImagesNotInZip: string[]; |
| 28 | +} |
| 29 | + |
| 30 | +function countImageFilesInZip(entries: string[]): string[] { |
| 31 | + // Count all image files in the ZIP (excluding XML files) |
| 32 | + const imageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.svg', '.webp']; |
| 33 | + return entries.filter((entry) => { |
| 34 | + const ext = entry.toLowerCase().split('.').pop(); |
| 35 | + return ext && imageExtensions.includes(`.${ext}`); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +async function auditGridsetImages(gridsetPath: string): Promise<AuditResult> { |
| 40 | + const processor = new GridsetProcessor(); |
| 41 | + |
| 42 | + // Load the gridset |
| 43 | + const tree = await processor.loadIntoTree(gridsetPath); |
| 44 | + |
| 45 | + // Get all entries from the ZIP for manual inspection |
| 46 | + // We need to access the internal ZIP entries |
| 47 | + const fs = await import('node:fs'); |
| 48 | + const AdmZip = (await import('adm-zip')).default; |
| 49 | + const zip = new AdmZip(gridsetPath); |
| 50 | + const allEntries = zip.getEntries().map((e: any) => e.entryName); |
| 51 | + |
| 52 | + const imageFilesInZip = countImageFilesInZip(allEntries); |
| 53 | + |
| 54 | + const resolvedImagePaths = new Set<string>(); |
| 55 | + const unresolvedCells: AuditResult['unresolvedCells'] = []; |
| 56 | + |
| 57 | + let totalCells = 0; |
| 58 | + let cellsWithDeclaredImages = 0; |
| 59 | + let cellsWithResolvedImages = 0; |
| 60 | + |
| 61 | + // Audit each page |
| 62 | + for (const [pageId, page] of Object.entries(tree.pages)) { |
| 63 | + for (const button of page.buttons) { |
| 64 | + totalCells++; |
| 65 | + |
| 66 | + if (button.image || button.resolvedImageEntry) { |
| 67 | + cellsWithDeclaredImages++; |
| 68 | + } |
| 69 | + |
| 70 | + if (button.resolvedImageEntry) { |
| 71 | + cellsWithResolvedImages++; |
| 72 | + resolvedImagePaths.add(button.resolvedImageEntry); |
| 73 | + } else if (button.image) { |
| 74 | + // Has image name but couldn't resolve |
| 75 | + const cellX = (button.parameters as any)?.cellX; |
| 76 | + const cellY = (button.parameters as any)?.cellY; |
| 77 | + unresolvedCells.push({ |
| 78 | + label: button.label, |
| 79 | + x: cellX, |
| 80 | + y: cellY, |
| 81 | + imageName: button.image, |
| 82 | + page: pageId, |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Check for resolved images that aren't actually in the ZIP |
| 89 | + const resolvedImagesNotInZip = Array.from(resolvedImagePaths).filter( |
| 90 | + (img) => !allEntries.includes(img) && !allEntries.includes(img.replace(/^Grids\//, '')) |
| 91 | + ); |
| 92 | + |
| 93 | + return { |
| 94 | + totalCells, |
| 95 | + cellsWithDeclaredImages, |
| 96 | + cellsWithResolvedImages, |
| 97 | + cellsWithoutResolvedImages: cellsWithDeclaredImages - cellsWithResolvedImages, |
| 98 | + actualImageFilesInZip: imageFilesInZip.length, |
| 99 | + resolvedImagePaths: Array.from(resolvedImagePaths), |
| 100 | + unresolvedCells, |
| 101 | + imageFilesInZip, |
| 102 | + resolvedImagesNotInZip, |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +describe('Gridset Image Audit', () => { |
| 107 | + const exampleGridset = path.join(process.cwd(), 'examples/example-images.gridset'); |
| 108 | + |
| 109 | + test('should resolve all images that exist in the ZIP', async () => { |
| 110 | + const audit = await auditGridsetImages(exampleGridset); |
| 111 | + |
| 112 | + console.log('\n=== Gridset Image Audit ==='); |
| 113 | + console.log(`Total cells: ${audit.totalCells}`); |
| 114 | + console.log(`Cells with declared images: ${audit.cellsWithDeclaredImages}`); |
| 115 | + console.log(`Cells with resolved images: ${audit.cellsWithResolvedImages}`); |
| 116 | + console.log(`Cells without resolved images: ${audit.cellsWithoutResolvedImages}`); |
| 117 | + console.log(`Actual image files in ZIP: ${audit.actualImageFilesInZip}`); |
| 118 | + console.log(`Unique resolved image paths: ${audit.resolvedImagePaths.length}`); |
| 119 | + console.log(`Resolved images not found in ZIP: ${audit.resolvedImagesNotInZip.length}`); |
| 120 | + |
| 121 | + if (audit.unresolvedCells.length > 0) { |
| 122 | + console.log(`\nUnresolved cells (${audit.unresolvedCells.length}):`); |
| 123 | + audit.unresolvedCells.forEach((cell) => { |
| 124 | + console.log(` - "${cell.label}" at (${cell.x}, ${cell.y}): ${cell.imageName}`); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + if (audit.resolvedImagesNotInZip.length > 0) { |
| 129 | + console.log(`\nResolved images not in ZIP (${audit.resolvedImagesNotInZip.length}):`); |
| 130 | + audit.resolvedImagesNotInZip.forEach((img) => { |
| 131 | + console.log(` - ${img}`); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + console.log('\n=== Sample resolved images ==='); |
| 136 | + audit.resolvedImagePaths.slice(0, 10).forEach((img) => { |
| 137 | + console.log(` - ${img}`); |
| 138 | + }); |
| 139 | + if (audit.resolvedImagePaths.length > 10) { |
| 140 | + console.log(` ... and ${audit.resolvedImagePaths.length - 10} more`); |
| 141 | + } |
| 142 | + |
| 143 | + console.log('\n=== Sample image files in ZIP ==='); |
| 144 | + audit.imageFilesInZip.slice(0, 10).forEach((img) => { |
| 145 | + console.log(` - ${img}`); |
| 146 | + }); |
| 147 | + if (audit.imageFilesInZip.length > 10) { |
| 148 | + console.log(` ... and ${audit.imageFilesInZip.length - 10} more`); |
| 149 | + } |
| 150 | + |
| 151 | + // The resolved images should be a subset of images in the ZIP |
| 152 | + expect(audit.resolvedImagesNotInZip.length).toBe(0); |
| 153 | + |
| 154 | + // Log the summary |
| 155 | + console.log('\n=== Summary ==='); |
| 156 | + console.log(`✓ All ${audit.cellsWithResolvedImages} resolved images exist in the ZIP`); |
| 157 | + console.log( |
| 158 | + `✓ ${audit.cellsWithDeclaredImages - audit.cellsWithResolvedImages} cells could not be resolved` |
| 159 | + ); |
| 160 | + console.log( |
| 161 | + `✓ ${audit.actualImageFilesInZip - audit.resolvedImagePaths.length} images in ZIP are not referenced by cells` |
| 162 | + ); |
| 163 | + }, 30000); |
| 164 | +}); |
0 commit comments