import { afterEach, describe, expect, it, vi } from 'vitest';
import robots from './robots';
import { GET as getBlogSitemap } from './sitemap-blog.xml/route';
import { GET as getWikiSitemap } from './sitemap-wiki.xml/route';
afterEach(() => {
vi.unstubAllGlobals();
vi.unstubAllEnvs();
});
describe('Sub-sitemaps and robots.ts integration (SEO-P1-10)', () => {
it('registers sitemap-blog.xml and sitemap-wiki.xml in robots.ts', () => {
vi.stubEnv('NEXT_PUBLIC_APP_ENV', 'production');
vi.stubEnv('NODE_ENV', 'production');
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://canina.ir');
const config = robots();
const sitemaps = Array.isArray(config.sitemap) ? config.sitemap : [config.sitemap];
expect(sitemaps).toContain('https://canina.ir/sitemap.xml');
expect(sitemaps).toContain('https://canina.ir/sitemap-products.xml');
expect(sitemaps).toContain('https://canina.ir/sitemap-categories.xml');
expect(sitemaps).toContain('https://canina.ir/sitemap-blog.xml');
expect(sitemaps).toContain('https://canina.ir/sitemap-wiki.xml');
});
it('generates sitemap-blog.xml correctly with published blogs and image tags', async () => {
vi.stubEnv('INTERNAL_API_URL', 'http://backend:3000/api');
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://canina.ir');
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
data: [
{
slug: 'puppy-nutrition',
title: 'تغذیه توله سگ',
status: 'PUBLISHED',
updatedAt: '2026-10-12T12:00:00.000Z',
publishedAt: '2026-10-10T12:00:00.000Z',
contentUpdatedAt: '2026-10-11T12:00:00.000Z',
featured: true,
imageUrl: '/uploads/puppy.jpg',
},
{
slug: 'hidden-blog',
title: 'مقاله پیشنویس',
status: 'DRAFT',
},
],
meta: { lastPage: 1 },
}),
});
vi.stubGlobal('fetch', fetchMock);
const res = await getBlogSitemap();
expect(res.status).toBe(200);
const xml = await res.text();
expect(xml).toContain('https://canina.ir/blog/puppy-nutrition');
expect(xml).toContain('0.9');
expect(xml).toContain('2026-10-11T12:00:00.000Z');
expect(xml).toContain('https://canina.ir/uploads/puppy.jpg');
expect(xml).toContain('تغذیه توله سگ');
expect(xml).not.toContain('hidden-blog');
});
it('generates sitemap-wiki.xml correctly with scientific terms', async () => {
vi.stubEnv('INTERNAL_API_URL', 'http://backend:3000/api');
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://canina.ir');
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
data: [
{
key: 'omega-3',
term: 'امگا ۳',
updatedAt: '2026-09-01T10:00:00.000Z',
},
],
meta: { lastPage: 1 },
}),
});
vi.stubGlobal('fetch', fetchMock);
const res = await getWikiSitemap();
expect(res.status).toBe(200);
const xml = await res.text();
expect(xml).toContain('https://canina.ir/wiki/omega-3');
expect(xml).toContain('0.8');
});
});