|
| 1 | +import type { Page } from '@playwright/test' |
| 2 | + |
| 3 | +// Minimal valid 10x5 PNG (landscape) |
| 4 | +export function createLandscapePng(): Buffer { |
| 5 | + return createPngBuffer(10, 5, [255, 0, 0]) // red |
| 6 | +} |
| 7 | + |
| 8 | +// Minimal valid 5x10 PNG (portrait) |
| 9 | +export function createPortraitPng(): Buffer { |
| 10 | + return createPngBuffer(5, 10, [0, 0, 255]) // blue |
| 11 | +} |
| 12 | + |
| 13 | +// Creates a minimal valid PNG with given dimensions and solid color |
| 14 | +function createPngBuffer( |
| 15 | + width: number, |
| 16 | + height: number, |
| 17 | + rgb: [number, number, number], |
| 18 | +): Buffer { |
| 19 | + // Build raw scanlines: each row = filter byte (0) + RGB pixels |
| 20 | + const rawData: number[] = [] |
| 21 | + for (let y = 0; y < height; y++) { |
| 22 | + rawData.push(0) // filter: none |
| 23 | + for (let x = 0; x < width; x++) { |
| 24 | + rawData.push(rgb[0], rgb[1], rgb[2]) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const deflated = deflateRaw(Buffer.from(rawData)) |
| 29 | + |
| 30 | + const chunks: Buffer[] = [] |
| 31 | + // Signature |
| 32 | + chunks.push(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) |
| 33 | + // IHDR |
| 34 | + chunks.push(createChunk('IHDR', ihdrData(width, height))) |
| 35 | + // IDAT |
| 36 | + chunks.push(createChunk('IDAT', deflated)) |
| 37 | + // IEND |
| 38 | + chunks.push(createChunk('IEND', Buffer.alloc(0))) |
| 39 | + |
| 40 | + return Buffer.concat(chunks) |
| 41 | +} |
| 42 | + |
| 43 | +function ihdrData(width: number, height: number): Buffer { |
| 44 | + const buf = Buffer.alloc(13) |
| 45 | + buf.writeUInt32BE(width, 0) |
| 46 | + buf.writeUInt32BE(height, 4) |
| 47 | + buf[8] = 8 // bit depth |
| 48 | + buf[9] = 2 // color type: RGB |
| 49 | + buf[10] = 0 // compression |
| 50 | + buf[11] = 0 // filter |
| 51 | + buf[12] = 0 // interlace |
| 52 | + return buf |
| 53 | +} |
| 54 | + |
| 55 | +function createChunk(type: string, data: Buffer): Buffer { |
| 56 | + const length = Buffer.alloc(4) |
| 57 | + length.writeUInt32BE(data.length, 0) |
| 58 | + const typeBytes = Buffer.from(type, 'ascii') |
| 59 | + const crcInput = Buffer.concat([typeBytes, data]) |
| 60 | + const crc = Buffer.alloc(4) |
| 61 | + crc.writeUInt32BE(crc32(crcInput), 0) |
| 62 | + return Buffer.concat([length, typeBytes, data, crc]) |
| 63 | +} |
| 64 | + |
| 65 | +function crc32(buf: Buffer): number { |
| 66 | + let crc = 0xffffffff |
| 67 | + for (let i = 0; i < buf.length; i++) { |
| 68 | + crc ^= buf[i] |
| 69 | + for (let j = 0; j < 8; j++) { |
| 70 | + crc = crc & 1 ? (crc >>> 1) ^ 0xedb88320 : crc >>> 1 |
| 71 | + } |
| 72 | + } |
| 73 | + return (crc ^ 0xffffffff) >>> 0 |
| 74 | +} |
| 75 | + |
| 76 | +// Minimal zlib deflate: stored blocks (no compression) |
| 77 | +function deflateRaw(data: Buffer): Buffer { |
| 78 | + const chunks: Buffer[] = [] |
| 79 | + // zlib header |
| 80 | + chunks.push(Buffer.from([0x78, 0x01])) |
| 81 | + |
| 82 | + const maxBlock = 65535 |
| 83 | + let offset = 0 |
| 84 | + while (offset < data.length) { |
| 85 | + const remaining = data.length - offset |
| 86 | + const blockSize = Math.min(remaining, maxBlock) |
| 87 | + const isLast = offset + blockSize >= data.length |
| 88 | + const header = Buffer.alloc(5) |
| 89 | + header[0] = isLast ? 0x01 : 0x00 |
| 90 | + header.writeUInt16LE(blockSize, 1) |
| 91 | + header.writeUInt16LE(blockSize ^ 0xffff, 3) |
| 92 | + chunks.push(header) |
| 93 | + chunks.push(data.subarray(offset, offset + blockSize)) |
| 94 | + offset += blockSize |
| 95 | + } |
| 96 | + |
| 97 | + // Adler-32 checksum |
| 98 | + let a = 1, |
| 99 | + b = 0 |
| 100 | + for (let i = 0; i < data.length; i++) { |
| 101 | + a = (a + data[i]) % 65521 |
| 102 | + b = (b + a) % 65521 |
| 103 | + } |
| 104 | + const adler = Buffer.alloc(4) |
| 105 | + adler.writeUInt32BE((b << 16) | a, 0) |
| 106 | + chunks.push(adler) |
| 107 | + |
| 108 | + return Buffer.concat(chunks) |
| 109 | +} |
| 110 | + |
| 111 | +export async function uploadTestImage( |
| 112 | + page: Page, |
| 113 | + name: string, |
| 114 | + buffer: Buffer, |
| 115 | +) { |
| 116 | + const fileInput = page.locator('input[type="file"]').first() |
| 117 | + await fileInput.setInputFiles({ |
| 118 | + name, |
| 119 | + mimeType: 'image/png', |
| 120 | + buffer, |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +export async function uploadMultipleTestImages( |
| 125 | + page: Page, |
| 126 | + files: Array<{ name: string; buffer: Buffer }>, |
| 127 | +) { |
| 128 | + const fileInput = page.locator('input[type="file"]').first() |
| 129 | + await fileInput.setInputFiles( |
| 130 | + files.map((f) => ({ |
| 131 | + name: f.name, |
| 132 | + mimeType: 'image/png', |
| 133 | + buffer: f.buffer, |
| 134 | + })), |
| 135 | + ) |
| 136 | +} |
0 commit comments