import assert from "node:assert/strict";
|
import cryptoModule from "node:crypto";
|
import {createRequire} from "node:module";
|
|
globalThis.crypto ??= cryptoModule.webcrypto;
|
globalThis.CSS ??= {escape: (value) => String(value)};
|
const require = createRequire(import.meta.url);
|
const capture = require(process.argv[2]);
|
|
function config(uid, name) {
|
return {
|
creator_uid: uid,
|
creator_name: name,
|
dynamic_url: `https://space.bilibili.com/${uid}/dynamic`,
|
profile_url: `https://space.bilibili.com/${uid}`,
|
include_types: ["article", "text", "image"],
|
deadline_ms: 30000,
|
observation_interval_ms: 100,
|
stable_observations: 3,
|
};
|
}
|
|
function documentFixture(uid, body, imageUrl) {
|
const nodes = {
|
".opus-module-content": {innerText: body},
|
"h1": {innerText: "Full article"},
|
"time[datetime], [data-published-at]": {getAttribute: (name) => name === "datetime" ? "2026-08-12T10:00:00+08:00" : null},
|
".article-content, .opus-module-title": {},
|
};
|
return {
|
body: {innerText: "ordinary visible article"},
|
querySelector(selector) {
|
if (selector.includes(`data-mid=\"${uid}\"`)) return {};
|
return nodes[selector] ?? null;
|
},
|
querySelectorAll(selector) {
|
if (selector.includes("img[src]")) return [{naturalWidth: 100, naturalHeight: 80, currentSrc: imageUrl, src: imageUrl}];
|
return [];
|
},
|
};
|
}
|
|
let stableItem;
|
for (const [uid, name] of [["10001", "Creator A"], ["20002", "创作者乙"]]) {
|
const body = `完整正文-${name}\n`.repeat(100);
|
const result = capture.currentOpusSnapshot(
|
documentFixture(uid, body, "https://i1.hdslb.com/bfs/new_dyn/sample.png"),
|
{href: "https://www.bilibili.com/opus/sample_1"},
|
config(uid, name),
|
);
|
assert.equal(result.state, "READY");
|
assert.equal(result.item.body_text, body.trimEnd());
|
assert.deepEqual(result.item.original_image_candidates, ["https://i1.hdslb.com/bfs/new_dyn/sample.png"]);
|
if (uid === "10001") stableItem = result.item;
|
}
|
|
assert.ok(stableItem);
|
const sharedVectorDigest = await capture.acceptedSnapshotFingerprint([{
|
stable_id: "snap_1",
|
item_type: "article",
|
title: "Shared",
|
source_url: "https://www.bilibili.com/opus/snap_1",
|
published_at: "2026-08-12T02:00:00.000Z",
|
body_text: "正文\nline",
|
body_complete: true,
|
original_image_candidates: ["https://i1.hdslb.com/bfs/new_dyn/shared.png"],
|
}]);
|
assert.equal(sharedVectorDigest, "4F0616496B08F8537A1C9F17B48D53BC2F93A26EB15AB5977C6E891ABA411454");
|
const stableDigest = await capture.acceptedSnapshotFingerprint([stableItem]);
|
assert.match(stableDigest, /^[0-9A-F]{64}$/u);
|
assert.notEqual(
|
stableDigest,
|
await capture.acceptedSnapshotFingerprint([{...stableItem, body_text: `${stableItem.body_text}-changed`}]),
|
);
|
assert.notEqual(
|
stableDigest,
|
await capture.acceptedSnapshotFingerprint([stableItem, {...stableItem, stable_id: "sample_2", source_url: "https://www.bilibili.com/opus/sample_2"}]),
|
);
|
|
const samples = [
|
{state: "METADATA_NOT_READY", reason: "METADATA_NOT_READY", item: null},
|
{state: "READY", reason: "READY", item: stableItem},
|
{state: "READY", reason: "READY", item: stableItem},
|
{state: "DIMENSIONS_PENDING", reason: "DIMENSIONS_PENDING", item: null},
|
{state: "READY", reason: "READY", item: stableItem},
|
{state: "READY", reason: "READY", item: stableItem},
|
{state: "READY", reason: "READY", item: stableItem},
|
];
|
const stable = await capture.observeUntilStable(config("30003", "Stable"), async () => samples.shift(), async () => {});
|
assert.equal(stable.observations.length, 7);
|
|
await assert.rejects(
|
capture.observeUntilStable(config("30003", "Stable"), async () => ({state: "ACCESS_BLOCKED", reason: "ACCESS_BLOCKED", item: null}), async () => {}),
|
/E_ACCESS_CONTROL/u,
|
);
|
assert.throws(
|
() => capture.currentOpusSnapshot(documentFixture("10001", "body", "https://i1.hdslb.com/bfs/new_dyn/sample.png?token=synthetic"), {href: "https://www.bilibili.com/opus/sample_1"}, config("10001", "Creator A")),
|
/E_IMAGE_IDENTITY/u,
|
);
|
assert.throws(() => capture.validatePublicConfig({...config("10001", "Creator A"), dynamic_url: "https://space.bilibili.com/20002/dynamic"}), /E_PAGE_IDENTITY/u);
|
|
console.log(JSON.stringify({status: "PASS", creators: 2, readiness_observations: stable.observations.length, network_requests: 0}));
|