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

55 lines
1.4 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-02-18 12:24:21 +00:00
async function runSingleTest(t, path, totalSize) {
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
})
t.equal(result.subarray(1, 4).toString("ascii"), "PNG", `result was not a PNG file: ${result.toString("base64")}`)
2024-02-02 02:55:02 +00:00
/* c8 ignore next 5 */
2024-02-01 03:38:17 +00:00
if (meter.bytes < totalSize / 4) { // should download less than 25% of each file
t.pass("intentionally read partial file")
} else {
t.fail(`read more than 25% of file, read: ${meter.bytes}, total: ${totalSize}`)
}
}
slow()("emoji-sheet: only partial file is read for APNG", async t => {
2024-02-18 12:24:21 +00:00
await runSingleTest(t, "test/res/butterfly.png", 2438998)
2024-02-01 03:38:17 +00:00
})
slow()("emoji-sheet: only partial file is read for GIF", async t => {
2024-02-18 12:24:21 +00:00
await runSingleTest(t, "test/res/butterfly.gif", 781223)
2024-02-01 03:38:17 +00:00
})