新增: - 书库导入导出(library_import/library_export)及测试 - 图片变体生成(image_variants)与响应式图片(responsiveImage) - 书籍搜索(bookSearch)+ BookSearch/LibrarySearchGrid 组件 - 小组件运行时(widgetRuntime)与静态检查(widgetLint) - 上传缓存中间件(upload_cache)与字体 CSS 提取脚本 其它: - 后端 handlers/services 全量调整 - 前端页面、组件、库函数与配置更新
72 lines
2.9 KiB
TypeScript
72 lines
2.9 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { afterEach, describe, it } from "node:test";
|
|
import { discussionJsonLd, questionJsonLd, serializeJsonLd } from "./jsonLd.ts";
|
|
|
|
const prev = {
|
|
SITE_URL: process.env.SITE_URL,
|
|
NODE_ENV: process.env.NODE_ENV,
|
|
};
|
|
|
|
afterEach(() => {
|
|
if (prev.SITE_URL === undefined) delete process.env.SITE_URL;
|
|
else process.env.SITE_URL = prev.SITE_URL;
|
|
process.env.NODE_ENV = prev.NODE_ENV;
|
|
});
|
|
|
|
describe("serializeJsonLd", () => {
|
|
it("尖括号不会提前闭合 script", () => {
|
|
const raw = serializeJsonLd({ text: `a</script><script>alert(1)` });
|
|
assert.equal(raw.includes("</script>"), false);
|
|
assert.equal(raw.includes("\\u003c"), true);
|
|
const parsed = JSON.parse(raw);
|
|
assert.equal(parsed.text, "a</script><script>alert(1)");
|
|
});
|
|
});
|
|
|
|
describe("questionJsonLd", () => {
|
|
it("采纳答案不重复进 suggestedAnswer", () => {
|
|
process.env.SITE_URL = "https://bbs.example.com";
|
|
process.env.NODE_ENV = "production";
|
|
const data = questionJsonLd({
|
|
title: "怎么排序",
|
|
text: "正文",
|
|
createdAt: "2026-09-01T00:00:00Z",
|
|
author: { id: 3, name: "甲" },
|
|
postId: 9,
|
|
accepted: { id: 4, floor: 1, content: "用索引", created_at: "2026-09-02T00:00:00Z", author: { id: 5, name: "乙" } },
|
|
answers: [
|
|
{ id: 4, floor: 1, content: "用索引", created_at: "2026-09-02T00:00:00Z", author: { id: 5, name: "乙" } },
|
|
{ id: 6, floor: 2, content: "先 EXPLAIN", created_at: "2026-09-03T00:00:00Z" },
|
|
],
|
|
});
|
|
assert.ok(data);
|
|
const q = data.mainEntity as { acceptedAnswer: { text: string; url: string }; suggestedAnswer: { text: string; url: string }[] };
|
|
assert.equal(q.acceptedAnswer.text, "用索引");
|
|
assert.equal(q.acceptedAnswer.url, "https://bbs.example.com/post/9#comment-1");
|
|
assert.deepEqual(q.suggestedAnswer.map((a) => a.text), ["先 EXPLAIN"]);
|
|
assert.equal(q.suggestedAnswer[0].url, "https://bbs.example.com/post/9#comment-2");
|
|
assert.equal(data["@type"], "QAPage");
|
|
});
|
|
});
|
|
|
|
describe("discussionJsonLd", () => {
|
|
it("评论分页不改讨论实体地址", () => {
|
|
process.env.SITE_URL = "https://bbs.example.com";
|
|
process.env.NODE_ENV = "production";
|
|
const data = discussionJsonLd({
|
|
title: "闲聊",
|
|
createdAt: "2026-09-01T00:00:00Z",
|
|
author: { id: 1, name: "丙" },
|
|
postId: 8,
|
|
pageUrl: "https://bbs.example.com/post/8?page=2",
|
|
comments: [{ id: 2, floor: 21, content: "可见楼层", created_at: "2026-09-01T01:00:00Z" }],
|
|
});
|
|
assert.equal(data?.url, "https://bbs.example.com/post/8");
|
|
const page = data?.mainEntityOfPage as { url: string };
|
|
assert.equal(page.url, "https://bbs.example.com/post/8?page=2");
|
|
const firstComment = (data?.comment as { text: string; url: string }[])[0];
|
|
assert.equal(firstComment.text, "可见楼层");
|
|
assert.equal(firstComment.url, "https://bbs.example.com/post/8#comment-21");
|
|
});
|
|
});
|