Files
creator-hub/web/tests/comment-aggregation-api.test.cjs
T

81 lines
2.8 KiB
JavaScript

const assert = require("node:assert/strict");
const fs = require("node:fs");
const Module = require("node:module");
const path = require("node:path");
const test = require("node:test");
const ts = require("../node_modules/typescript");
const target = path.resolve(__dirname, "../src/services/api.ts");
let response;
const calls = [];
const originalLoad = Module._load;
Module._load = function (request, parent, isMain) {
if (parent?.filename === target && request === "@/requestErrorConfig")
return {
request: async (url) => {
calls.push(url);
return response;
},
};
return originalLoad.call(this, request, parent, isMain);
};
const source = ts.transpileModule(fs.readFileSync(target, "utf8"), {
compilerOptions: { module: ts.ModuleKind.CommonJS },
}).outputText;
const mod = new Module(target, module);
mod.filename = target;
mod.paths = module.paths;
mod._compile(source, target);
const { getList, resourcePaths } = mod.exports;
test("comment aggregation transmits source, account, hours and pagination to dedicated endpoint", async () => {
response = { data: [{ id: "comment-1" }], total: 201, has_next: true };
const result = await getList({
resource: "creator-comment-aggregation",
page: 3,
pageSize: 25,
filters: {
source_type: "owned",
source_id: "account/1",
hours: 6,
unsupported: "ignored",
},
});
const query = new URL(calls.at(-1), "https://example.test");
assert.equal(query.pathname, "/creator/comments/aggregation");
assert.equal(query.searchParams.get("source_type"), "owned");
assert.equal(query.searchParams.get("source_id"), "account/1");
assert.equal(query.searchParams.get("hours"), "6");
assert.equal(query.searchParams.get("page"), "3");
assert.equal(query.searchParams.get("page_size"), "25");
assert.equal(query.searchParams.has("unsupported"), false);
assert.deepEqual(result, {
data: [{ id: "comment-1" }],
total: 201,
hasNext: true,
});
response = { data: [], total: 0, has_next: false };
assert.deepEqual(
await getList({
resource: "creator-comment-aggregation",
page: 1,
pageSize: 25,
filters: { source_type: "competitor", source_id: undefined, hours: 24 },
}),
{ data: [], total: 0, hasNext: false },
);
assert.equal(
new URL(calls.at(-1), "https://example.test").searchParams.has("source_id"),
false,
);
assert.equal(resourcePaths["creator-comments"], "/creator/comments");
});
test("menu and page header expose comment aggregation", () => {
const metadata = fs.readFileSync(
path.resolve(__dirname, "../src/utils/metadata.tsx"),
"utf8",
);
assert.match(metadata, /path: '\/creator\/comments', name: '评论聚合'/);
assert.match(metadata, /p === '\/creator\/comments', title: '评论聚合'/);
});