Files
canina/frontend/application/app/blog/[slug]/timestamp-metadata.test.tsx
T

74 lines
4.0 KiB
TypeScript

import React from 'react';
import { afterEach, expect, it, vi } from 'vitest';
import { renderToStaticMarkup } from 'react-dom/server';
import BlogPostPage, { generateMetadata } from './page';
import { validContentUpdatedAt } from '../../../lib/blogDates';
vi.mock('../../../lib/seo', () => ({
getSeoConfig: async () => ({ brandNameFa: 'کنینا', canonicalBaseUrl: 'https://canina.ir', ogImageUrl: 'https://canina.ir/default.webp' }),
formatPageTitle: (title: string) => title,
}));
vi.mock('../../../components/BlogPostClient', () => ({
default: ({ blog, publishedDate }: { blog: any; publishedDate: string }) => <div>
<span>تاریخ انتشار: {publishedDate}</span>
{validContentUpdatedAt(blog) && <span>آخرین به‌روزرسانی</span>}
</div>,
}));
afterEach(() => { vi.unstubAllGlobals(); vi.unstubAllEnvs(); });
const published = '2026-10-29T07:11:40.925Z';
async function readDates(fields: Record<string, unknown>) {
vi.stubEnv('NEXT_PUBLIC_APP_ENV', 'staging');
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({
title: 'Timestamp contract', content: '<p>Article</p>', ...fields,
}) }));
const params = Promise.resolve({ slug: 'timestamp-contract' });
const metadata = await generateMetadata({ params });
const jsx = await BlogPostPage({ params });
const script: any = React.Children.toArray(jsx.props.children)[0];
return { og: metadata.openGraph as any, robots: metadata.robots,
article: JSON.parse(script.props.dangerouslySetInnerHTML.__html), html: renderToStaticMarkup(jsx) };
}
it.each([null, undefined, 'invalid', published, '2026-10-28T00:00:00Z'])(
'uses real publication for unedited/invalid/chronologically inapplicable content date %s', async (contentUpdatedAt) => {
const { og, article, html, robots } = await readDates({ publishedAt: published,
contentUpdatedAt, updatedAt: '2026-10-30T09:00:00Z', imageUrl: '/attached.webp' });
expect(og.publishedTime).toBe(published);
expect(og.modifiedTime).toBe(published);
expect(article.datePublished).toBe(published);
expect(article.dateModified).toBe(published);
expect(html).not.toContain('آخرین به‌روزرسانی');
// Stage noindex remains an independent HTTP acceptance check; no robots change in this slice.
});
it('uses actual substantive update consistently without changing publication', async () => {
const modified = '2026-10-29T12:00:00.000Z';
const { og, article, html } = await readDates({ publishedAt: published, contentUpdatedAt: modified });
expect(og.modifiedTime).toBe(modified);
expect(article.dateModified).toBe(modified);
expect(article.datePublished).toBe(published);
expect(html).toContain('آخرین به‌روزرسانی');
});
it.each([null, 'invalid'])('uses the valid existing createdAt fallback when publishedAt is %s', async (publishedAt) => {
const { og, article } = await readDates({ publishedAt, createdAt: published });
expect(og.publishedTime).toBe(published);
expect(og.modifiedTime).toBe(published);
expect(article.datePublished).toBe(published);
expect(article.dateModified).toBe(published);
});
it.each([{}, { publishedAt: 'invalid', createdAt: 'invalid', contentUpdatedAt: '2026-10-30T00:00:00Z' }])(
'does not invent rendering-time publication or modification when unknown: %j', async (fields) => {
const { og, article } = await readDates(fields);
expect(og.publishedTime).toBeUndefined();
expect(og.modifiedTime).toBeUndefined();
expect(article).not.toHaveProperty('datePublished');
expect(article).not.toHaveProperty('dateModified');
});
it('formats visible publication using Persian calendar and Tehran rather than server timezone', async () => {
const { html } = await readDates({ publishedAt: '2026-10-29T21:00:00Z' });
const expected = new Intl.DateTimeFormat('fa-IR-u-ca-persian', {
year: 'numeric', month: 'numeric', day: 'numeric', timeZone: 'Asia/Tehran',
}).format(new Date('2026-10-29T21:00:00Z'));
expect(html).toContain(`تاریخ انتشار: ${expected}`);
});