scriptCat/yuntu/yuntuReportFilling/yuntuReportFilling.test.js
2026-04-13 21:08:30 +08:00

218 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const test = require("node:test");
const assert = require("node:assert/strict");
const api = require("./yuntuReportFilling.user.js");
test("isTargetCreateReportRequest matches the segmented market create endpoint", () => {
assert.equal(
api.isTargetCreateReportRequest(
"https://yuntu.oceanengine.com/product_node/v2/api/segmentedMarket/createSegmentedMarket?aadvid=1648829117232140",
"POST",
),
true,
);
assert.equal(
api.isTargetCreateReportRequest(
"https://yuntu.oceanengine.com/product_node/v2/api/industry/insightBrandStats?aadvid=1648829117232140",
"POST",
),
false,
);
});
test("isSupportedPageUrl covers both creation and detail pages", () => {
assert.equal(
api.isSupportedPageUrl(
"https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketcreation?aadvid=1648829117232140",
),
true,
);
assert.equal(
api.isSupportedPageUrl(
"https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketDetail/marketOverview?aadvid=1710507483282439&reportId=441518",
),
true,
);
assert.equal(
api.isSupportedPageUrl(
"https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketList?aadvid=1648829117232140",
),
true,
);
assert.equal(
api.isSupportedPageUrl(
"https://yuntu.oceanengine.com/yuntu_brand/ecom/product/otherPage?aadvid=1710507483282439",
),
false,
);
});
test("getSupportedPageType identifies list, creation, and detail pages", () => {
assert.equal(
api.getSupportedPageType(
"https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketList?aadvid=1648829117232140",
),
"list",
);
assert.equal(
api.getSupportedPageType(
"https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketcreation?aadvid=1648829117232140",
),
"creation",
);
assert.equal(
api.getSupportedPageType(
"https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketDetail/marketOverview?aadvid=1710507483282439&reportId=441518",
),
"detail",
);
});
test("getButtonUiState disables the button on the list page and enables it on action pages with payload", () => {
assert.deepEqual(
api.getButtonUiState({
pageType: "list",
hasPayload: true,
isSubmitting: false,
}),
{
shouldRender: true,
disabled: true,
},
);
assert.deepEqual(
api.getButtonUiState({
pageType: "detail",
hasPayload: true,
isSubmitting: false,
}),
{
shouldRender: true,
disabled: false,
},
);
assert.deepEqual(
api.getButtonUiState({
pageType: null,
hasPayload: true,
isSubmitting: false,
}),
{
shouldRender: false,
disabled: true,
},
);
});
test("createRouteChangeWatcher triggers a sync callback after pushState", () => {
const events = [];
const listeners = new Map();
const root = {
history: {
pushState() {
events.push("pushState");
},
replaceState() {
events.push("replaceState");
},
},
addEventListener(name, handler) {
listeners.set(name, handler);
},
removeEventListener(name) {
listeners.delete(name);
},
setTimeout(callback) {
callback();
return 1;
},
clearTimeout() {},
};
const dispose = api.createRouteChangeWatcher(root, () => {
events.push("sync");
});
root.history.pushState({}, "", "/next");
assert.deepEqual(events, ["pushState", "sync"]);
dispose();
events.length = 0;
root.history.replaceState({}, "", "/again");
assert.deepEqual(events, ["replaceState"]);
});
test("getLocalApiBaseCandidates prefers localhost and falls back to 127.0.0.1", () => {
assert.deepEqual(api.getLocalApiBaseCandidates(), [
"http://localhost:3000",
"http://127.0.0.1:3000",
]);
});
test("extractReportId returns response.data.reportId when present", () => {
assert.equal(
api.extractReportId({
data: {
reportId: "987654321",
},
}),
"987654321",
);
assert.equal(api.extractReportId({ data: {} }), null);
});
test("shiftDateBackOneYear keeps month/day and clamps leap day", () => {
assert.equal(api.shiftDateBackOneYear("2025-03-01"), "2024-03-01");
assert.equal(api.shiftDateBackOneYear("2024-02-29"), "2023-02-28");
});
test("buildAutoCopyPayload deep clones payload, shifts top-level dates, and appends the year range to name", () => {
const original = {
name: "测试(近一年)",
startTime: "2025-03-01",
endTime: "2026-02-28",
nested: {
startTime: "SHOULD_NOT_CHANGE",
},
};
const copied = api.buildAutoCopyPayload(original);
assert.deepEqual(copied, {
name: "测试近一年2024-2025",
startTime: "2024-03-01",
endTime: "2025-02-28",
nested: {
startTime: "SHOULD_NOT_CHANGE",
},
});
assert.notEqual(copied, original);
assert.notEqual(copied.nested, original.nested);
});
test("buildPersistRequest keeps the response info unchanged for backend parsing", () => {
const responseInfo = {
code: 0,
message: "success",
data: {
reportId: "report-001",
status: "SUCCESS",
},
};
const request = api.buildPersistRequest(responseInfo);
assert.deepEqual(request, responseInfo);
assert.notEqual(request, responseInfo);
assert.notEqual(request.data, responseInfo.data);
});