1
0
Fork 0
out-of-your-element-fork-th.../m2d/converters/emoji-sheet.test.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-02-01 03:38:17 +00:00
const assert = require("assert").strict
const {test} = require("supertape")
const {_convertImageStream} = require("./emoji-sheet")
2024-02-18 12:24:21 +00:00
const fs = require("fs")
2024-02-01 03:38:17 +00:00
const {Transform} = require("stream").Transform
/* c8 ignore next 7 */
function slow() {
if (process.argv.includes("--slow")) {
return test
} else {
return test.skip
}
}
class Meter extends Transform {
bytes = 0
_transform(chunk, encoding, cb) {
this.bytes += chunk.length
this.push(chunk)
cb()
}
}
/**
* @param {import("supertape").Test} t
2024-02-18 12:24:21 +00:00
* @param {string} path
2024-02-01 03:38:17 +00:00
* @param {number} totalSize
2024-03-01 04:28:14 +00:00
* @param {number => boolean} sizeCheck
2024-02-01 03:38:17 +00:00
*/
2024-03-01 04:28:14 +00:00
async function runSingleTest(t, path, totalSize, sizeCheck) {
2024-02-18 12:24:21 +00:00
const file = fs.createReadStream(path)
2024-02-01 03:38:17 +00:00
const meter = new Meter()
2024-02-18 12:24:21 +00:00
const p = file.pipe(meter)
2024-02-01 03:38:17 +00:00
const result = await _convertImageStream(p, () => {
2024-02-18 12:24:21 +00:00
file.pause()
file.emit("end")
2024-02-01 03:38:17 +00:00
})
2024-03-01 04:28:14 +00:00
t.equal(result.subarray(1, 4).toString("ascii"), "PNG", `test that this is a PNG file: ${result.toString("base64").slice(0, 100)}`)
2024-02-02 02:55:02 +00:00
/* c8 ignore next 5 */
2024-03-01 04:28:14 +00:00
if (sizeCheck(meter.bytes)) {
t.pass("read the correct amount of the file")
2024-02-01 03:38:17 +00:00
} else {
2024-03-01 04:28:14 +00:00
t.fail(`read too much or too little of the file, read: ${meter.bytes}, total: ${totalSize}`)
2024-02-01 03:38:17 +00:00
}
}
slow()("emoji-sheet: only partial file is read for APNG", async t => {
2024-03-01 04:28:14 +00:00
await runSingleTest(t, "test/res/butterfly.png", 2438998, n => n < 2438998 / 4) // should download less than 25% of the file
2024-02-01 03:38:17 +00:00
})
slow()("emoji-sheet: only partial file is read for GIF", async t => {
2024-03-01 04:28:14 +00:00
await runSingleTest(t, "test/res/butterfly.gif", 781223, n => n < 781223 / 4) // should download less than 25% of the file
})
slow()("emoji-sheet: entire file is read for static PNG", async t => {
await runSingleTest(t, "test/res/RLMgJGfgTPjIQtvvWZsYjhjy.png", 11301, n => n === 11301) // should download entire file
2024-02-01 03:38:17 +00:00
})