56 lines
1.8 KiB
JavaScript
56 lines
1.8 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");
|
|
},
|
|
},
|
|
};
|
|
globalThis.uni = {
|
|
getFileInfo(options) {
|
|
options.success({ size: 4, digest: "a".repeat(32) });
|
|
},
|
|
};
|
|
assert.deepStrictEqual(await pickAndUploadImage(), {
|
|
ossId: "2060000000000000001",
|
|
url: "https://files.example/image.jpg",
|
|
thumbnailUrl: "",
|
|
fileName: "image.jpg",
|
|
});
|
|
delete globalThis.plus;
|
|
delete globalThis.uni;
|
|
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);
|
|
});
|