Files
jiapuapp/tests/file-upload-api-runtime-smoke.js
T
2026-07-27 06:50:23 +08:00

176 lines
6.8 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 = [];
const singleUploads = [];
globalThis.plus = {
uploader: {
createUpload(url, options, callback) {
const upload = {
headers: {},
files: [],
setRequestHeader(key, value) { this.headers[key] = value; },
addFile(filePath, fileOptions) { this.files.push({ filePath, fileOptions }); },
start() { queueMicrotask(() => callback({ responseText: JSON.stringify({ code: 200, data: null }) }, 200)); },
abort() {},
};
uploads.push({ url, options, upload });
return upload;
},
},
};
globalThis.uni.uploadFile = (options) => {
singleUploads.push(options);
queueMicrotask(() => options.success({
statusCode: 200,
data: JSON.stringify({ code: 200, data: { ossId: "900002", url: "https://oss.example/single.png", thumbnailUrl: "", fileName: "single.png" } }),
}));
return { abort() {} };
};
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].url, "https://backend-api.ddxcjp.cn/genealogy/app/files/resumable/chunk?uploadId=upload-1&chunkIndex=0&chunkMd5=" + "b".repeat(32));
assert.deepStrictEqual(uploads[0].upload.headers, { clientid: "client-1", tenantId: "000000", Authorization: "Bearer session-1" });
assert.deepStrictEqual(uploads[0].upload.files, [{ filePath: "/storage/emulated/0/avatar.png", fileOptions: { key: "file" } }]);
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");
assert.deepStrictEqual(await appApi.uploadSingleFile({ filePath: "/storage/emulated/0/single.png" }), {
ossId: "900002",
url: "https://oss.example/single.png",
thumbnailUrl: "",
fileName: "single.png",
});
assert.strictEqual(singleUploads.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/app/files/upload");
assert.strictEqual(singleUploads.at(-1).name, "file");
assert.strictEqual(singleUploads.at(-1).filePath, "/storage/emulated/0/single.png");
assert.deepStrictEqual(singleUploads.at(-1).header, { clientid: "client-1", tenantId: "000000", Authorization: "Bearer session-1" });
response = { statusCode: 200, data: { code: 200, data: null } };
await appApi.createFileReference({
bizType: "family_feed",
bizName: "family feed image",
bizTable: "gen_family_feed",
bizId: "900013001",
bizField: "media_oss_ids",
ossId: "900002",
usageScene: "feed_image",
});
assert.strictEqual(requests.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/app/files/reference");
assert.deepStrictEqual(requests.at(-1).data, {
bizType: "family_feed",
bizName: "family feed image",
bizTable: "gen_family_feed",
bizId: 900013001,
bizField: "media_oss_ids",
ossId: 900002,
usageScene: "feed_image",
});
await appApi.deleteFileReference({ bizTable: "gen_family_feed", bizId: "900013001", bizField: "media_oss_ids" });
assert.strictEqual(requests.at(-1).url, "https://backend-api.ddxcjp.cn/genealogy/app/files/reference?bizTable=gen_family_feed&bizId=900013001&bizField=media_oss_ids");
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.plus;
delete globalThis.uni;
process.stdout.write("FILE-UPLOAD-API-RUNTIME-SMOKE PASS\n");
};
run().catch((error) => {
process.stderr.write(`${error.stack || error.message}\n`);
process.exit(1);
});