diff --git a/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx new file mode 100644 index 00000000..dca2e149 --- /dev/null +++ b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx @@ -0,0 +1,104 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import React from 'react'; +import BlogPostPage from './page'; + +vi.mock('../../../lib/seo', () => ({ + getSeoConfig: vi.fn().mockResolvedValue({ + brandNameFa: 'کنینا ایران', + canonicalBaseUrl: 'https://canina.ir', + ogImageUrl: '/assets/og.jpg', + defaultMetaDescription: 'توضیحات پیش‌فرض', + titleSeparator: '|', + titlePosition: 'suffix', + }), + formatPageTitle: vi.fn((title) => `${title} | کنینا ایران`), +})); + +vi.mock('../../../components/BlogPostClient', () => ({ + default: () =>
, +})); + +describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => { + beforeEach(() => { + vi.stubEnv('INTERNAL_API_URL', 'http://backend:3000/api'); + vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://canina.ir'); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + vi.unstubAllEnvs(); + }); + + it('injects FAQPage JSON-LD schema when article contains questions and answers', async () => { + const blogData = { + slug: 'joint-health', + title: 'سلامت مفاصل سگ', + content: ` +
+

مقدمه‌ای درباره مفاصل سگ.

+
+ آیا این مکمل برای سگ مسن مناسب است؟ +

بله، بسیار توصیه می‌شود و روند تخریب مفصل را کند می‌کند.

+
+
+ `, + createdAt: '2026-01-01', + publishedAt: '2026-01-01', + }; + + vi.stubGlobal( + 'fetch', + vi.fn().mockResolvedValue({ + ok: true, + json: async () => blogData, + }) + ); + + const jsx = await BlogPostPage({ params: Promise.resolve({ slug: 'joint-health' }) }); + expect(jsx).toBeDefined(); + + // Verify scripts in JSX tree + const scripts = React.Children.toArray(jsx.props.children).filter( + (child: any) => child?.type === 'script' && child?.props?.['type'] === 'application/ld+json' + ); + + // There should be 3 schemas: Article, Breadcrumb, and FAQPage + expect(scripts.length).toBe(3); + + const faqScript: any = scripts.find((s: any) => s.props['data-testid'] === 'faq-schema'); + expect(faqScript).toBeDefined(); + + const faqJson = JSON.parse(faqScript.props.dangerouslySetInnerHTML.__html); + expect(faqJson['@type']).toBe('FAQPage'); + expect(faqJson.mainEntity).toHaveLength(1); + expect(faqJson.mainEntity[0].name).toBe('آیا این مکمل برای سگ مسن مناسب است؟'); + }); + + it('does not inject FAQPage schema when article does not have FAQs', async () => { + const blogData = { + slug: 'nutrition', + title: 'تغذیه سگ', + content: '

یک مقاله ساده بدون سوال و جواب یا آکاردئون.

', + createdAt: '2026-01-01', + publishedAt: '2026-01-01', + }; + + vi.stubGlobal( + 'fetch', + vi.fn().mockResolvedValue({ + ok: true, + json: async () => blogData, + }) + ); + + const jsx = await BlogPostPage({ params: Promise.resolve({ slug: 'nutrition' }) }); + const scripts = React.Children.toArray(jsx.props.children).filter( + (child: any) => child?.type === 'script' && child?.props?.['type'] === 'application/ld+json' + ); + + // Only Article and Breadcrumb schemas + expect(scripts.length).toBe(2); + const faqScript = scripts.find((s: any) => s.props['data-testid'] === 'faq-schema'); + expect(faqScript).toBeUndefined(); + }); +}); diff --git a/frontend/application/app/blog/[slug]/page.tsx b/frontend/application/app/blog/[slug]/page.tsx index 03ca3731..49cef8ce 100644 --- a/frontend/application/app/blog/[slug]/page.tsx +++ b/frontend/application/app/blog/[slug]/page.tsx @@ -5,6 +5,7 @@ import BlogPostClient from "../../../components/BlogPostClient"; import { getSeoConfig, formatPageTitle } from "../../../lib/seo"; import { safeJsonLd } from "../../../lib/schema"; import { sanitizeBlogHtml } from "../../../lib/sanitizeBlogHtml"; +import { extractFaqsFromHtml, generateFAQPageSchema } from "../../../lib/extractFaqs"; function safeIso(val: any, fallback?: string): string | undefined { if (!val) return fallback; @@ -82,7 +83,7 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str const ogTitle = blog.ogTitle || title; const ogDesc = blog.ogDescription || description; - const isStaging = + const isStaging = process.env.NEXT_PUBLIC_APP_ENV === 'staging' || process.env.NODE_ENV !== 'production' || siteUrl.includes('stage') || @@ -267,6 +268,10 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug: ], }; + // SEO-P1-07: Auto-detect and inject FAQPage Schema when article content contains Q&As or accordion structures + const extractedFaqs = extractFaqsFromHtml(blog.content || ''); + const faqPageSchema = generateFAQPageSchema(extractedFaqs); + return ( <>