167 lines
7.9 KiB
JavaScript
167 lines
7.9 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
const { test } = require("node:test");
|
|
const ts = require("typescript");
|
|
|
|
function loadModule(relativePath, mocks = {}, globals = {}) {
|
|
const source = fs.readFileSync(path.join(__dirname, "..", relativePath), "utf8");
|
|
const compiled = ts.transpileModule(source, {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX },
|
|
}).outputText;
|
|
const loadedModule = { exports: {} };
|
|
vm.runInNewContext(compiled, {
|
|
module: loadedModule, exports: loadedModule.exports, process, URLSearchParams,
|
|
require: (name) => name in mocks ? mocks[name] : require(name),
|
|
...globals,
|
|
});
|
|
return loadedModule.exports;
|
|
}
|
|
|
|
const page = {
|
|
title: "Page title",
|
|
meta: { seo_title: "Standard title", search_description: "Standard description" },
|
|
seo_title_override: "Custom title | Shiytech",
|
|
seo_description_override: "Custom description",
|
|
canonical_url: "https://www.shiytech.cn/products",
|
|
og_image: { url: "https://cms.shiytech.cn/media/share.png", title: "Share image" },
|
|
schema_json: { "@type": "CollectionPage" },
|
|
};
|
|
|
|
test("SEO metadata includes absolute title, canonical and social image", () => {
|
|
const { createPageMetadata } = loadModule("services/seo.service.ts", { "./api-client": {} });
|
|
const metadata = createPageMetadata(page, "Fallback");
|
|
assert.equal(metadata.title.absolute, page.seo_title_override);
|
|
assert.equal(metadata.description, page.seo_description_override);
|
|
assert.equal(metadata.alternates.canonical, page.canonical_url);
|
|
assert.equal(metadata.openGraph.images[0].url, page.og_image.url);
|
|
assert.equal(metadata.twitter.card, "summary_large_image");
|
|
});
|
|
|
|
test("SEO falls back to standard Wagtail fields and handles missing pages", () => {
|
|
const { createPageMetadata } = loadModule("services/seo.service.ts", { "./api-client": {} });
|
|
const metadata = createPageMetadata({ ...page, seo_title_override: "", seo_description_override: "", og_image: null }, "Fallback");
|
|
assert.equal(metadata.title.absolute, page.meta.seo_title);
|
|
assert.equal(metadata.description, page.meta.search_description);
|
|
assert.equal(metadata.twitter.card, "summary");
|
|
assert.equal(createPageMetadata(null, "Fallback").title, "Fallback");
|
|
});
|
|
|
|
test("SEO lookup requests every SEO field and encodes slugs", async () => {
|
|
let requested;
|
|
const { getPageSEO, SEO_FIELDS } = loadModule("services/seo.service.ts", {
|
|
"./api-client": { fetchAPI: async (url) => { requested = url; return { items: [page] }; } },
|
|
});
|
|
assert.equal(await getPageSEO("products.ProductPage", "a&b"), page);
|
|
const query = new URLSearchParams(requested.split("?")[1]);
|
|
assert.equal(query.get("slug"), "a&b");
|
|
assert.equal(query.get("fields"), SEO_FIELDS);
|
|
for (const field of ["seo_title", "search_description", "og_image", "canonical_url", "schema_json"]) {
|
|
assert.ok(SEO_FIELDS.split(",").includes(field));
|
|
}
|
|
});
|
|
|
|
test("JSON-LD escapes script boundaries and suppresses empty data", async () => {
|
|
const schema = { "@type": "WebPage", description: "</script><script>alert(1)</script>" };
|
|
const { default: PageSEO } = loadModule("components/layout/PageSEO.tsx", {
|
|
"@/services/seo.service": { getPageSEO: async () => ({ schema_json: schema }) },
|
|
});
|
|
const element = await PageSEO({ type: "home.HomePage" });
|
|
const json = element.props.dangerouslySetInnerHTML.__html;
|
|
assert.ok(!json.includes("<"));
|
|
assert.deepEqual(JSON.parse(json), schema);
|
|
const empty = loadModule("components/layout/PageSEO.tsx", {
|
|
"@/services/seo.service": { getPageSEO: async () => ({ schema_json: {} }) },
|
|
});
|
|
assert.equal(await empty.default({ type: "home.HomePage" }), null);
|
|
});
|
|
|
|
test("page fetches share the publish cache tag without changing snippet tags", async () => {
|
|
const calls = [];
|
|
const { fetchAPI } = loadModule("services/api-client.ts", {}, {
|
|
fetch: async (url, options) => { calls.push(options); return { ok: true, json: async () => ({}) }; },
|
|
});
|
|
await fetchAPI("/api/v2/pages/?type=home.HomePage", { tags: ["home-page", "wagtail-pages"] });
|
|
assert.equal(calls[0].next.tags.join(","), "wagtail-pages,home-page");
|
|
await fetchAPI("/api/v1/custom/core/site-settings/", { tags: ["site-settings"] });
|
|
assert.equal(calls[1].next.tags.join(","), "site-settings");
|
|
});
|
|
|
|
test("authenticated webhook expires shared cache immediately and rejects unauthorized requests", async () => {
|
|
const calls = [];
|
|
const { POST } = loadModule("app/api/revalidate/route.ts", {
|
|
"next/cache": { revalidateTag: (tag, profile) => calls.push({ tag, profile }) },
|
|
}, { process: { env: { REVALIDATE_SECRET: "test-secret" } } });
|
|
const request = (authorization) => ({
|
|
headers: { get: () => authorization },
|
|
json: async () => ({ tags: ["wagtail-pages"] }),
|
|
});
|
|
assert.equal((await POST(request("Bearer test-secret"))).status, 200);
|
|
assert.equal(calls[0].tag, "wagtail-pages");
|
|
assert.equal(calls[0].profile.expire, 0);
|
|
assert.equal((await POST(request("Bearer invalid"))).status, 401);
|
|
assert.equal(calls.length, 1);
|
|
});
|
|
|
|
test("all CMS page routes wire metadata and JSON-LD to the correct page model", async () => {
|
|
const routes = [
|
|
["app/page.tsx", "home.HomePage", undefined],
|
|
["app/products/page.tsx", "products.ProductIndexPage", "products"],
|
|
["app/solutions/page.tsx", "solutions.SolutionIndexPage", "solutions"],
|
|
["app/cases/page.tsx", "cases.CaseStudyIndexPage", "cases"],
|
|
["app/blog/page.tsx", "blog.BlogIndexPage", "blog"],
|
|
["app/contact/page.tsx", "core.ContactPage", "contact"],
|
|
["app/privacy-policy/page.tsx", "core.SimpleContentPage", "privacy-policy"],
|
|
["app/products/[slug]/page.tsx", "products.ProductPage", "test-slug"],
|
|
["app/solutions/[slug]/page.tsx", "solutions.SolutionPage", "test-slug"],
|
|
["app/cases/[slug]/page.tsx", "cases.CaseStudyPage", "test-slug"],
|
|
["app/blog/[slug]/page.tsx", "blog.BlogPage", "test-slug"],
|
|
];
|
|
const { createPageMetadata } = loadModule("services/seo.service.ts", { "./api-client": {} });
|
|
const PageSEO = () => null;
|
|
const service = {
|
|
getHomePage: async () => page, getContactPage: async () => page,
|
|
getProductList: async () => [], getSolutionList: async () => [],
|
|
getCaseStudyList: async () => [], getBlogList: async () => [],
|
|
getProductBySlug: async () => page, getSolutionBySlug: async () => page,
|
|
getCaseStudyBySlug: async () => page, getBlogBySlug: async () => page,
|
|
getSimpleContentPageBySlug: async () => page,
|
|
};
|
|
const mocks = {
|
|
"@/components/layout/PageSEO": { default: PageSEO },
|
|
"@/blocks/BlockRenderer": { BlockRenderer: () => null },
|
|
"@/services/preview.service": { getPreviewOrFallback: async (type, fields, fallback) => fallback() },
|
|
};
|
|
for (const name of ["page", "product", "solution", "case", "blog", "legal"]) {
|
|
mocks[`@/services/${name}.service`] = service;
|
|
}
|
|
function findSEO(element) {
|
|
if (!element || typeof element !== "object") return null;
|
|
if (element.type === PageSEO) return element;
|
|
const children = element.props?.children;
|
|
for (const child of Array.isArray(children) ? children : [children]) {
|
|
const found = findSEO(child);
|
|
if (found) return found;
|
|
}
|
|
return null;
|
|
}
|
|
for (const [file, expectedType, expectedSlug] of routes) {
|
|
const calls = [];
|
|
const route = loadModule(file, {
|
|
...mocks,
|
|
"@/services/seo.service": {
|
|
createPageMetadata,
|
|
getPageSEO: async (type, slug) => { calls.push({ type, slug }); return page; },
|
|
},
|
|
});
|
|
const props = { params: Promise.resolve({ slug: "test-slug" }) };
|
|
assert.equal((await route.generateMetadata(props)).title.absolute, page.seo_title_override, file);
|
|
assert.equal(calls[0].type, expectedType, file);
|
|
assert.equal(calls[0].slug, expectedSlug, file);
|
|
const element = findSEO(await route.default(props));
|
|
assert.ok(element, file);
|
|
assert.equal(element.props.type, expectedType, file);
|
|
assert.equal(element.props.slug, expectedSlug, file);
|
|
}
|
|
}); |