fix: fill missing audience profile buckets

This commit is contained in:
admin123 2026-05-18 17:40:21 +08:00
parent 26ae3bb4b6
commit c8287e8d8e
2 changed files with 56 additions and 1 deletions

View File

@ -122,7 +122,7 @@ function readDistributionValue(
return (
readProfileDistributionItems(profile, key).find(
(candidate) => candidate.label === label
)?.value ?? ""
)?.value ?? "0%"
);
}

View File

@ -95,4 +95,59 @@ describe("audience-profile-csv", () => {
expect(rowLine).not.toContain("request-failed");
expect(rowLine).not.toContain("timeout");
});
test("fills missing fixed distribution buckets with zero for successful profiles", () => {
const csv = buildAudienceProfileCsv([
{
profiles: {
audience: { status: "success" },
fans: { status: "success" },
longtimeFans: {
age: [
{ label: "18-23", value: "11.1%" },
{ label: "24-30", value: "33.3%" },
{ label: "31-40", value: "55.6%" }
],
cityTier: [
{ label: "一线城市", value: "10%" },
{ label: "二线城市", value: "20%" },
{ label: "三线城市", value: "40%" },
{ label: "四线城市", value: "30%" }
],
crowd: [
{ label: "精致妈妈", value: "30%" },
{ label: "新锐白领", value: "20%" },
{ label: "资深中产", value: "10%" },
{ label: "都市蓝领", value: "20%" },
{ label: "小镇中老年", value: "10%" },
{ label: "小镇青年", value: "10%" }
],
status: "success"
}
},
record: {
authorId: "123",
authorName: "达人 A",
status: "success"
}
} satisfies AudienceProfileExportRow
]);
expect(readCsvValue(csv, "铁粉画像-41-50占比")).toBe("0%");
expect(readCsvValue(csv, "铁粉画像-50+占比")).toBe("0%");
expect(readCsvValue(csv, "铁粉画像-新一线城市占比")).toBe("0%");
expect(readCsvValue(csv, "铁粉画像-五线城市占比")).toBe("0%");
expect(readCsvValue(csv, "铁粉画像-都市银发占比")).toBe("0%");
expect(readCsvValue(csv, "铁粉画像-Z世代占比")).toBe("0%");
});
});
function readCsvValue(csv: string, header: string): string {
const [headerLine, rowLine] = csv.split("\n");
const headers = headerLine.split(",");
const values = rowLine.split(",");
const index = headers.indexOf(header);
expect(index).toBeGreaterThanOrEqual(0);
return values[index] ?? "";
}