|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { test, expect } from '@playwright/test'; |
| 3 | + |
| 4 | +type AtlasBaseline = { |
| 5 | + occupancy: number[][]; |
| 6 | + protectedCells: Record<string, string>; |
| 7 | +}; |
| 8 | + |
| 9 | +const baseline = JSON.parse( |
| 10 | + readFileSync( |
| 11 | + new URL( |
| 12 | + './fixtures/neko-pause-cat-atlas-baseline.json', |
| 13 | + import.meta.url, |
| 14 | + ), |
| 15 | + 'utf8', |
| 16 | + ), |
| 17 | +) as AtlasBaseline; |
| 18 | + |
| 19 | +test('built-in cat atlas preserves approved cells and uses brown closed-eye arcs', async ({ |
| 20 | + page, |
| 21 | +}) => { |
| 22 | + await page.goto('/'); |
| 23 | + |
| 24 | + const result = await page.evaluate(async (protectedCells) => { |
| 25 | + const CELL_WIDTH = 192; |
| 26 | + const CELL_HEIGHT = 208; |
| 27 | + const COLUMNS = 8; |
| 28 | + const ROWS = 11; |
| 29 | + const CLOSED_EYE_RECTS = { |
| 30 | + '0/2-left': { row: 0, column: 2, x: 45, y: 48, width: 48, height: 42 }, |
| 31 | + '0/2-right': { row: 0, column: 2, x: 99, y: 48, width: 48, height: 42 }, |
| 32 | + '6/4-closed': { row: 6, column: 4, x: 42, y: 48, width: 52, height: 44 }, |
| 33 | + // Source inspection places these arcs at y=94..106, not inside the |
| 34 | + // original y=48..89 estimate. |
| 35 | + '8/5-left': { row: 8, column: 5, x: 45, y: 82, width: 48, height: 42 }, |
| 36 | + '8/5-right': { row: 8, column: 5, x: 105, y: 82, width: 48, height: 42 }, |
| 37 | + } as const; |
| 38 | + |
| 39 | + const response = await fetch('/assets/cat/neko-pause-cat.webp', { |
| 40 | + cache: 'no-store', |
| 41 | + }); |
| 42 | + if (!response.ok) { |
| 43 | + throw new Error(`Failed to load cat atlas: ${response.status}`); |
| 44 | + } |
| 45 | + |
| 46 | + const bitmap = await createImageBitmap(await response.blob()); |
| 47 | + const canvas = document.createElement('canvas'); |
| 48 | + canvas.width = bitmap.width; |
| 49 | + canvas.height = bitmap.height; |
| 50 | + const context = canvas.getContext('2d', { willReadFrequently: true }); |
| 51 | + if (!context) { |
| 52 | + throw new Error('Canvas 2D context unavailable'); |
| 53 | + } |
| 54 | + context.drawImage(bitmap, 0, 0); |
| 55 | + bitmap.close(); |
| 56 | + |
| 57 | + const hexDigest = async (pixels: Uint8ClampedArray) => |
| 58 | + Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256', pixels))) |
| 59 | + .map((value) => value.toString(16).padStart(2, '0')) |
| 60 | + .join(''); |
| 61 | + |
| 62 | + const occupancy: number[][] = []; |
| 63 | + const changedProtectedCells: string[] = []; |
| 64 | + for (let row = 0; row < ROWS; row += 1) { |
| 65 | + const occupancyRow: number[] = []; |
| 66 | + for (let column = 0; column < COLUMNS; column += 1) { |
| 67 | + const cell = context.getImageData( |
| 68 | + column * CELL_WIDTH, |
| 69 | + row * CELL_HEIGHT, |
| 70 | + CELL_WIDTH, |
| 71 | + CELL_HEIGHT, |
| 72 | + ); |
| 73 | + let occupied = 0; |
| 74 | + for (let offset = 3; offset < cell.data.length; offset += 4) { |
| 75 | + if (cell.data[offset] !== 0) { |
| 76 | + occupied += 1; |
| 77 | + } |
| 78 | + } |
| 79 | + occupancyRow.push(occupied); |
| 80 | + |
| 81 | + const key = `${row}/${column}`; |
| 82 | + const approvedHash = protectedCells[key]; |
| 83 | + if (approvedHash) { |
| 84 | + const computedHash = await hexDigest(cell.data); |
| 85 | + if (computedHash !== approvedHash) { |
| 86 | + changedProtectedCells.push(key); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + occupancy.push(occupancyRow); |
| 91 | + } |
| 92 | + |
| 93 | + const isBrownArc = (red: number, green: number, blue: number, alpha: number) => |
| 94 | + alpha >= 192 && |
| 95 | + red >= 55 && |
| 96 | + red <= 170 && |
| 97 | + green >= 35 && |
| 98 | + green <= 125 && |
| 99 | + blue >= 20 && |
| 100 | + blue <= 90 && |
| 101 | + red > green && |
| 102 | + green >= blue; |
| 103 | + |
| 104 | + const inspectClosedEye = (rect: (typeof CLOSED_EYE_RECTS)[keyof typeof CLOSED_EYE_RECTS]) => { |
| 105 | + const pixels = context.getImageData( |
| 106 | + rect.column * CELL_WIDTH + rect.x, |
| 107 | + rect.row * CELL_HEIGHT + rect.y, |
| 108 | + rect.width, |
| 109 | + rect.height, |
| 110 | + ).data; |
| 111 | + const brown = new Uint8Array(rect.width * rect.height); |
| 112 | + let lightFill = false; |
| 113 | + |
| 114 | + for (let index = 0; index < brown.length; index += 1) { |
| 115 | + const offset = index * 4; |
| 116 | + const red = pixels[offset] ?? 0; |
| 117 | + const green = pixels[offset + 1] ?? 0; |
| 118 | + const blue = pixels[offset + 2] ?? 0; |
| 119 | + const alpha = pixels[offset + 3] ?? 0; |
| 120 | + brown[index] = Number(isBrownArc(red, green, blue, alpha)); |
| 121 | + lightFill ||= alpha >= 192 && red >= 205 && green >= 205 && blue >= 205; |
| 122 | + } |
| 123 | + |
| 124 | + let largestConnectedArc = 0; |
| 125 | + const visited = new Uint8Array(brown.length); |
| 126 | + for (let start = 0; start < brown.length; start += 1) { |
| 127 | + if (brown[start] === 0 || visited[start] === 1) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + const stack = [start]; |
| 131 | + visited[start] = 1; |
| 132 | + let connected = 0; |
| 133 | + while (stack.length > 0) { |
| 134 | + const index = stack.pop(); |
| 135 | + if (index === undefined) { |
| 136 | + break; |
| 137 | + } |
| 138 | + connected += 1; |
| 139 | + const x = index % rect.width; |
| 140 | + const y = Math.floor(index / rect.width); |
| 141 | + const neighbors = [ |
| 142 | + x > 0 ? index - 1 : -1, |
| 143 | + x + 1 < rect.width ? index + 1 : -1, |
| 144 | + y > 0 ? index - rect.width : -1, |
| 145 | + y + 1 < rect.height ? index + rect.width : -1, |
| 146 | + ]; |
| 147 | + for (const neighbor of neighbors) { |
| 148 | + if ( |
| 149 | + neighbor >= 0 && |
| 150 | + brown[neighbor] === 1 && |
| 151 | + visited[neighbor] === 0 |
| 152 | + ) { |
| 153 | + visited[neighbor] = 1; |
| 154 | + stack.push(neighbor); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + largestConnectedArc = Math.max(largestConnectedArc, connected); |
| 159 | + } |
| 160 | + |
| 161 | + return { |
| 162 | + brownArc: largestConnectedArc >= 12, |
| 163 | + lightFill, |
| 164 | + }; |
| 165 | + }; |
| 166 | + |
| 167 | + return { |
| 168 | + dimensions: { width: canvas.width, height: canvas.height }, |
| 169 | + occupancy, |
| 170 | + changedProtectedCells, |
| 171 | + closedEyes: Object.fromEntries( |
| 172 | + Object.entries(CLOSED_EYE_RECTS).map(([key, rect]) => [ |
| 173 | + key, |
| 174 | + inspectClosedEye(rect), |
| 175 | + ]), |
| 176 | + ), |
| 177 | + }; |
| 178 | + }, baseline.protectedCells); |
| 179 | + |
| 180 | + expect(result.dimensions).toEqual({ width: 1536, height: 2288 }); |
| 181 | + expect(result.occupancy).toEqual(baseline.occupancy); |
| 182 | + expect(result.changedProtectedCells).toEqual([]); |
| 183 | + expect(result.closedEyes).toEqual({ |
| 184 | + '0/2-left': { brownArc: true, lightFill: false }, |
| 185 | + '0/2-right': { brownArc: true, lightFill: false }, |
| 186 | + '6/4-closed': { brownArc: true, lightFill: false }, |
| 187 | + '8/5-left': { brownArc: true, lightFill: false }, |
| 188 | + '8/5-right': { brownArc: true, lightFill: false }, |
| 189 | + }); |
| 190 | +}); |
0 commit comments