star-chart-search-enhancer/tests/market-id-extractor.test.ts

58 lines
1.8 KiB
TypeScript

import { JSDOM } from "jsdom";
import { describe, expect, test } from "vitest";
import { extractAuthorIdFromRow } from "../src/content/market/id-extractor";
import { createListSignature } from "../src/content/market/list-signature";
describe("market id extractor", () => {
test("extracts authorId from a detail link inside the row", () => {
const row = createRow(
'<a href="https://xingtu.cn/ad/creator/author-homepage/douyin-video/6629661559960371207">达人详情</a>'
);
expect(extractAuthorIdFromRow(row)).toEqual({
authorId: "6629661559960371207",
source: "detail-link",
success: true
});
});
test("extracts authorId from fallback row attributes", () => {
const row = createRow(
'<button data-author-id="7312345678901234567">查看</button>'
);
expect(extractAuthorIdFromRow(row)).toEqual({
authorId: "7312345678901234567",
source: "attribute",
success: true
});
});
test("returns an explicit missing-author-id result when no stable source exists", () => {
const row = createRow('<span>没有达人 id</span>');
expect(extractAuthorIdFromRow(row)).toEqual({
authorId: null,
reason: "missing-author-id",
success: false
});
});
test("builds a deterministic list signature from authorIds and url state", () => {
expect(
createListSignature({
authorIds: ["111", "222", "333"],
url: "https://xingtu.cn/ad/creator/market?page=2&keyword=test"
})
).toBe("/ad/creator/market?page=2&keyword=test::111,222,333");
});
});
function createRow(innerHtml: string) {
const dom = new JSDOM(
`<table><tbody><tr><td>${innerHtml}</td></tr></tbody></table>`
);
return dom.window.document.querySelector("tr") as HTMLTableRowElement;
}