|
| 1 | + |
| 2 | +import { vi } from "vitest"; |
| 3 | + |
| 4 | +import { createRgbaImage } from "../../../test/testUtils.js"; |
| 5 | +import { encode } from "../encode.js"; |
| 6 | +import { encodeBase64 } from "../encodeBase64.js"; |
| 7 | + |
| 8 | +test("basic image (png)",()=>{ |
| 9 | + const image = testUtils.createGreyImage([ |
| 10 | + [0, 0, 0, 0, 0], |
| 11 | + [0, 255, 255, 255, 0], |
| 12 | + [0, 255, 0, 255, 0], |
| 13 | + [0, 255, 255, 255, 0], |
| 14 | + [255, 0, 255, 0, 255], |
| 15 | + ]); |
| 16 | + const base64Url = encodeBase64(image,'png'); |
| 17 | + |
| 18 | + expect(base64Url).toBe("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAAAAACoBHk5AAAAFklEQVR4XmNggID///+DSCCEskHM/wCAnQr2TY5mOAAAAABJRU5ErkJggg=="); |
| 19 | +}); |
| 20 | + |
| 21 | +test("basic image 2 (jpeg)",()=>{ |
| 22 | + const image = testUtils.createGreyImage([ |
| 23 | + [255, 255, 255, 255, 255], |
| 24 | + [255, 0, 0, 0, 255], |
| 25 | + [255, 0, 0, 0, 255], |
| 26 | + [255, 0, 0, 0, 255], |
| 27 | + [255, 255, 255, 255, 255], |
| 28 | + ]); |
| 29 | + const format = 'jpeg' |
| 30 | + const base64 = encodeBase64(image,format); |
| 31 | + const base64Data = Buffer.from(encode(image,{format})) |
| 32 | + .toString('base64'); |
| 33 | + expect(typeof base64).toBe('string'); |
| 34 | + expect(base64Data).toBe(base64.slice( base64.indexOf(',') + 1)); |
| 35 | +}); |
| 36 | + |
| 37 | +test("legacy image-js test",()=>{ |
| 38 | + const image = createRgbaImage([[ |
| 39 | + 255, |
| 40 | + 0, |
| 41 | + 0, |
| 42 | + 255, // red |
| 43 | + 0, |
| 44 | + 255, |
| 45 | + 0, |
| 46 | + 255,// green |
| 47 | + 0, |
| 48 | + 0, |
| 49 | + 255, |
| 50 | + 255 // blue |
| 51 | + ],[255, |
| 52 | + 255, |
| 53 | + 0, |
| 54 | + 255,// yellow |
| 55 | + 255, |
| 56 | + 0, |
| 57 | + 255, |
| 58 | + 255,// magenta |
| 59 | + 0, |
| 60 | + 255, |
| 61 | + 255, |
| 62 | + 255,// cyan |
| 63 | + ],[ 0, |
| 64 | + 0, |
| 65 | + 0, |
| 66 | + 255,// black |
| 67 | + 255, |
| 68 | + 255, |
| 69 | + 255, |
| 70 | + 255, // white |
| 71 | + 127, |
| 72 | + 127, |
| 73 | + 127, |
| 74 | + 255,//grey |
| 75 | + ]]); |
| 76 | + const format = 'jpeg'; |
| 77 | + const url = encodeBase64(image,format); |
| 78 | + const base64Data = Buffer.from(encode(image,{format})) |
| 79 | + .toString('base64') |
| 80 | + expect(typeof url).toBe('string'); |
| 81 | + expect(base64Data).toBe(url.slice(url.indexOf(',') + 1)); |
| 82 | +}) |
| 83 | +test("browser testing",()=>{ |
| 84 | + const image = testUtils.createGreyImage([ |
| 85 | + [255, 255, 255, 255, 255], |
| 86 | + [255, 0, 0, 0, 255], |
| 87 | + [255, 0, 0, 0, 255], |
| 88 | + [255, 0, 0, 0, 255], |
| 89 | + [255, 255, 255, 255, 255], |
| 90 | + ]); |
| 91 | + const base64Node = encodeBase64(image,'jpg'); |
| 92 | + vi.stubGlobal('window',()=>{ |
| 93 | + const base64Browser = encodeBase64(image,'jpg'); |
| 94 | + expect(base64Browser).not.toBe(base64Node); |
| 95 | + }) |
| 96 | + vi.stubGlobal('document',()=>{ |
| 97 | + const base64Browser = encodeBase64(image,'jpg'); |
| 98 | + expect(base64Browser).not.toBe(base64Node); |
| 99 | + }) |
| 100 | +}) |
0 commit comments