65 lines
2.1 KiB
JavaScript
65 lines
2.1 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const run = async () => {
|
|
const rawSource = fs.readFileSync(path.join(__dirname, "../utils/resumable-image-upload.js"), "utf8");
|
|
const source = rawSource
|
|
.replace('import { appApi } from "@/utils/api.js";', "const appApi = globalThis.__appApi;")
|
|
.replace('import { calcMD5Bytes } from "@/utils/md5.js";', "const calcMD5Bytes = () => \"a\".repeat(32);");
|
|
globalThis.__appApi = {
|
|
initializeResumableUpload: async () => ({
|
|
instant: true,
|
|
ossId: "2060000000000000001",
|
|
url: "https://files.example/image.jpg",
|
|
fileName: "image.jpg",
|
|
}),
|
|
};
|
|
const moduleUrl = `data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
|
|
const { pickAndUploadImage, toConsumerOssId } = await import(moduleUrl);
|
|
|
|
assert.strictEqual(toConsumerOssId("900001"), 900001);
|
|
assert.throws(() => toConsumerOssId("0"), (error) => error.code === "OSS_ID_INVALID");
|
|
assert.throws(() => toConsumerOssId("9007199254740992"), (error) => error.code === "OSS_ID_UNSAFE");
|
|
await assert.rejects(pickAndUploadImage(), (error) => error.code === "IMAGE_PICK_UNAVAILABLE");
|
|
|
|
globalThis.plus = {
|
|
gallery: {
|
|
pick(success) {
|
|
success("/tmp/image.jpg");
|
|
},
|
|
},
|
|
io: {
|
|
resolveLocalFileSystemURL(_path, success) {
|
|
success({
|
|
file(done) {
|
|
done({ name: "image.jpg", size: 4, type: "image/jpeg" });
|
|
},
|
|
});
|
|
},
|
|
FileReader: class {
|
|
readAsArrayBuffer() {
|
|
this.result = new Uint8Array([1, 2, 3, 4]).buffer;
|
|
this.onloadend({ target: { result: this.result } });
|
|
}
|
|
},
|
|
},
|
|
};
|
|
assert.deepStrictEqual(await pickAndUploadImage(), {
|
|
ossId: "2060000000000000001",
|
|
url: "https://files.example/image.jpg",
|
|
thumbnailUrl: "",
|
|
fileName: "image.jpg",
|
|
});
|
|
delete globalThis.plus;
|
|
delete globalThis.__appApi;
|
|
process.stdout.write("RESUMABLE-IMAGE-UPLOAD-RUNTIME-SMOKE PASS\n");
|
|
};
|
|
|
|
run().catch((error) => {
|
|
process.stderr.write(`${error.stack || error.message}\n`);
|
|
process.exit(1);
|
|
});
|