162 lines
6.1 KiB
JavaScript
162 lines
6.1 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const toDataModuleUrl = (source) =>
|
|
`data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
|
|
|
|
const run = async () => {
|
|
const rawSource = fs.readFileSync(path.join(__dirname, "../utils/api.js"), "utf8");
|
|
const moduleBody = rawSource.slice(rawSource.indexOf("const successCodes"));
|
|
const prelude = `
|
|
const currentUser = {};
|
|
const genealogies = [];
|
|
const publicGenealogies = [];
|
|
const treeMembers = [];
|
|
const notifications = [];
|
|
const joinApplications = [];
|
|
const listFamilyFeedFixtures = () => [];
|
|
const listFamilyArticleFixtures = () => [];
|
|
const listFamilyAlbumFixtures = () => [];
|
|
const listCeremonyFixtures = () => [];
|
|
const listGrowthRecordFixtures = () => [];
|
|
const listNotificationFixtures = () => [];
|
|
const runtimeConfig = { baseUrl: "https://backend-api.ddxcjp.cn", clientId: "client-1", tenantId: "000000" };
|
|
const hasRemoteConfig = () => true;
|
|
const resolveRuntimeMode = () => "remote";
|
|
const AUTH_VERIFICATION_OPERATION = Object.freeze({});
|
|
const assertSmsCode = (value) => value;
|
|
const GENEALOGY_ACCESS_PRESET = Object.freeze({ MEMBER_ONLY: "MEMBER_ONLY" });
|
|
const fromApiGenealogyAccess = () => null;
|
|
const session = { getToken: () => "session-1", saveToken() {} };
|
|
`;
|
|
|
|
const requests = [];
|
|
let response;
|
|
globalThis.uni = {
|
|
request(options) {
|
|
requests.push(options);
|
|
queueMicrotask(() => options.success(response));
|
|
return { abort() {} };
|
|
},
|
|
};
|
|
const uploads = [];
|
|
globalThis.uni.uploadFile = (options) => {
|
|
const task = { abort() {} };
|
|
uploads.push({ options, task });
|
|
queueMicrotask(() => options.success({ statusCode: 200, data: JSON.stringify({ code: 200, data: null }) }));
|
|
return task;
|
|
};
|
|
const { appApi } = await import(toDataModuleUrl(`${prelude}\n${moduleBody}`));
|
|
const basePayload = {
|
|
uploadId: "upload-1",
|
|
fileName: "avatar.png",
|
|
fileMd5: "a".repeat(32),
|
|
totalSize: 1024,
|
|
totalChunks: 1,
|
|
chunkSize: 1024,
|
|
contentType: "image/png",
|
|
};
|
|
const completePayload = {
|
|
uploadId: "upload-1",
|
|
fileName: basePayload.fileName,
|
|
fileMd5: basePayload.fileMd5,
|
|
totalSize: basePayload.totalSize,
|
|
totalChunks: basePayload.totalChunks,
|
|
};
|
|
|
|
response = { statusCode: 200, data: { code: 200, data: { uploadId: null, instant: true, ossId: 900001 } } };
|
|
assert.deepStrictEqual(await appApi.initializeResumableUpload(basePayload), {
|
|
uploadId: null,
|
|
instant: true,
|
|
ossId: "900001",
|
|
url: "",
|
|
fileName: "",
|
|
});
|
|
assert.deepStrictEqual(requests.at(-1).data, basePayload);
|
|
assert.strictEqual(requests.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/app/files/resumable/init");
|
|
|
|
response = { statusCode: 200, data: { code: 200, data: { uploadId: null, instant: false, ossId: null } } };
|
|
await assert.rejects(
|
|
appApi.initializeResumableUpload(basePayload),
|
|
/uploadId/,
|
|
);
|
|
|
|
await appApi.uploadResumableChunk({
|
|
uploadId: "upload-1",
|
|
chunkIndex: 0,
|
|
chunkMd5: "b".repeat(32),
|
|
filePath: "/storage/emulated/0/avatar.png",
|
|
});
|
|
assert.strictEqual(uploads.length, 1);
|
|
assert.strictEqual(uploads[0].options.url, "https://backend-api.ddxcjp.cn/genealogy/app/files/resumable/chunk");
|
|
assert.strictEqual(uploads[0].options.filePath, "/storage/emulated/0/avatar.png");
|
|
assert.strictEqual(uploads[0].options.name, "file");
|
|
assert.strictEqual(uploads[0].options.timeout, 15000);
|
|
assert.deepStrictEqual(uploads[0].options.header, { clientid: "client-1", tenantId: "000000", Authorization: "Bearer session-1" });
|
|
assert.deepStrictEqual(uploads[0].options.formData, { uploadId: "upload-1", chunkIndex: "0", chunkMd5: "b".repeat(32) });
|
|
|
|
globalThis.uni.uploadFile = (options) => {
|
|
queueMicrotask(() => options.fail({ errMsg: "uploadFile:fail timeout" }));
|
|
return { abort() {} };
|
|
};
|
|
await assert.rejects(appApi.uploadResumableChunk({
|
|
uploadId: "upload-1",
|
|
chunkIndex: 0,
|
|
chunkMd5: "b".repeat(32),
|
|
filePath: "/storage/emulated/0/avatar.png",
|
|
}), (error) => error.code === "REQUEST_TIMEOUT");
|
|
|
|
const browserRequests = [];
|
|
class TestFormData {
|
|
entries = [];
|
|
append(name, value, fileName) { this.entries.push({ name, value, fileName }); }
|
|
}
|
|
globalThis.FormData = TestFormData;
|
|
globalThis.fetch = async (url, options) => {
|
|
browserRequests.push({ url, options });
|
|
return { status: 200, text: async () => JSON.stringify({ code: 200, data: null }) };
|
|
};
|
|
await appApi.uploadBrowserResumableChunk({
|
|
uploadId: "upload-1",
|
|
chunkIndex: 0,
|
|
chunkMd5: "b".repeat(32),
|
|
}, { name: "avatar.png" });
|
|
assert.strictEqual(browserRequests[0].url, "https://backend-api.ddxcjp.cn/genealogy/app/files/resumable/chunk");
|
|
assert.deepStrictEqual(browserRequests[0].options.body.entries, [
|
|
{ name: "uploadId", value: "upload-1", fileName: undefined },
|
|
{ name: "chunkIndex", value: "0", fileName: undefined },
|
|
{ name: "chunkMd5", value: "b".repeat(32), fileName: undefined },
|
|
{ name: "file", value: { name: "avatar.png" }, fileName: "avatar.png" },
|
|
]);
|
|
|
|
response = { statusCode: 200, data: { code: 200, data: { ossId: "900001", url: "https://oss.example/avatar.png", thumbnailUrl: "", fileName: "avatar.png" } } };
|
|
assert.deepStrictEqual(await appApi.completeResumableUpload(completePayload), {
|
|
ossId: "900001",
|
|
url: "https://oss.example/avatar.png",
|
|
thumbnailUrl: "",
|
|
fileName: "avatar.png",
|
|
});
|
|
assert.strictEqual(requests.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/app/files/resumable/complete");
|
|
await assert.rejects(
|
|
appApi.initializeResumableUpload({ ...basePayload, totalSize: 0 }),
|
|
/totalSize/,
|
|
);
|
|
await assert.rejects(
|
|
appApi.uploadResumableChunk({ uploadId: "upload-1", chunkIndex: -1, chunkMd5: "b".repeat(32), filePath: "/x" }),
|
|
/chunkIndex/,
|
|
);
|
|
|
|
delete globalThis.uni;
|
|
delete globalThis.fetch;
|
|
delete globalThis.FormData;
|
|
process.stdout.write("FILE-UPLOAD-API-RUNTIME-SMOKE PASS\n");
|
|
};
|
|
|
|
run().catch((error) => {
|
|
process.stderr.write(`${error.stack || error.message}\n`);
|
|
process.exit(1);
|
|
});
|