Files
omniai-server/scripts/ossRouteContract.test.js
2026-06-02 13:14:10 +08:00

132 lines
4.2 KiB
JavaScript

const assert = require("node:assert/strict");
const { createRequire } = require("node:module");
const nodeRequire = createRequire(__filename);
const contextPath = "../src/routes/context.js";
const ossRoutePath = "../src/routes/oss.js";
const ossClientPath = "../src/ossClient.js";
function passThrough(_req, _res, next) {
next();
}
function createMockResponse() {
const res = {};
res.status = (statusCode) => {
res.statusCode = statusCode;
return res;
};
res.json = (body) => {
res.body = body;
return res;
};
return res;
}
function loadOssRouter(ossClient) {
const express = nodeRequire("express");
const router = express.Router();
const contextResolvedPath = nodeRequire.resolve(contextPath);
const ossRouteResolvedPath = nodeRequire.resolve(ossRoutePath);
const ossClientResolvedPath = nodeRequire.resolve(ossClientPath);
const originalContextModule = nodeRequire.cache[contextResolvedPath];
const originalOssRouteModule = nodeRequire.cache[ossRouteResolvedPath];
const originalOssClientModule = nodeRequire.cache[ossClientResolvedPath];
delete nodeRequire.cache[ossRouteResolvedPath];
nodeRequire.cache[contextResolvedPath] = {
id: contextResolvedPath,
filename: contextResolvedPath,
loaded: true,
exports: {
requireAuth: passThrough,
},
};
nodeRequire.cache[ossClientResolvedPath] = {
id: ossClientResolvedPath,
filename: ossClientResolvedPath,
loaded: true,
exports: ossClient,
};
nodeRequire(ossRoutePath).registerOssRoutes(router);
return {
router,
restore() {
if (originalContextModule) nodeRequire.cache[contextResolvedPath] = originalContextModule;
else delete nodeRequire.cache[contextResolvedPath];
if (originalOssRouteModule) nodeRequire.cache[ossRouteResolvedPath] = originalOssRouteModule;
else delete nodeRequire.cache[ossRouteResolvedPath];
if (originalOssClientModule) nodeRequire.cache[ossClientResolvedPath] = originalOssClientModule;
else delete nodeRequire.cache[ossClientResolvedPath];
},
};
}
function getRouteHandler(router, method, routePath) {
const layer = router.stack.find(
(candidate) => candidate.route?.path === routePath && candidate.route.methods[method.toLowerCase()],
);
const handler = layer?.route?.stack.at(-1)?.handle;
if (!handler) throw new Error(`Route not found: ${method.toUpperCase()} ${routePath}`);
return handler;
}
async function uploadWithScope(scope, mimeType) {
const putCalls = [];
const { router, restore } = loadOssRouter({
isOssConfigured: () => true,
putObject: async (objectKey, body, contentType, headers) => {
putCalls.push({ objectKey, body, contentType, headers });
},
createSignedReadUrl: (objectKey) => `https://signed.example.com/${objectKey}?Expires=86400`,
});
try {
const handler = getRouteHandler(router, "post", "/oss/upload");
const res = createMockResponse();
await handler(
{
body: {
dataUrl: `data:${mimeType};base64,${Buffer.from("{}").toString("base64")}`,
mimeType,
scope,
},
user: { id: "admin-1" },
},
res,
);
return { res, putCalls };
} finally {
restore();
}
}
async function main() {
process.env.OSS_PUBLIC_BASE_URL = "https://cdn.example.com";
const imageUpload = await uploadWithScope("community-case-cover", "image/png");
assert.equal(imageUpload.res.statusCode, 201);
assert.match(imageUpload.putCalls[0].objectKey, /^community\/images\/.+\.png$/);
assert.equal(imageUpload.res.body.ossKey, imageUpload.putCalls[0].objectKey);
assert.equal(imageUpload.res.body.url, `https://cdn.example.com/${imageUpload.putCalls[0].objectKey}`);
assert.equal(
imageUpload.res.body.signedUrl,
`https://signed.example.com/${imageUpload.putCalls[0].objectKey}?Expires=86400`,
);
const workflowUpload = await uploadWithScope("community-case-workflow", "application/json");
assert.equal(workflowUpload.res.statusCode, 201);
assert.match(workflowUpload.putCalls[0].objectKey, /^community\/canvas\/.+\.json$/);
assert.equal(workflowUpload.putCalls[0].contentType, "application/json");
console.log("oss route contract tests passed");
}
main().catch((error) => {
console.error(error);
process.exit(1);
});