11import { describe , it , expect , vi } from 'vitest' ;
22import { seoPlugin } from './index.js' ;
3- import type { BuildContext } from '../../plugin/types.js' ;
3+ import * as robotsModule from './robots.js' ;
4+ import * as sitemapModule from './sitemap.js' ;
45
56describe ( 'seoPlugin' , ( ) => {
67 it ( 'should have correct plugin name' , ( ) => {
@@ -14,22 +15,30 @@ describe('seoPlugin', () => {
1415 expect ( typeof plugin . postBuild ) . toBe ( 'function' ) ;
1516 } ) ;
1617
17- it ( 'should skip sitemap if not configured' , async ( ) => {
18+ it ( 'should skip when not configured' , async ( ) => {
1819 const plugin = seoPlugin ( ) ;
1920 const consoleSpy = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
21+ const sitemapSpy = vi . spyOn ( sitemapModule , 'generateSitemap' ) . mockResolvedValue ( undefined ) ;
22+ const robotsSpy = vi . spyOn ( robotsModule , 'generateRobots' ) . mockResolvedValue ( undefined ) ;
2023
2124 const mockContext = {
2225 config : { } ,
2326 } as any ;
2427
2528 await plugin . postBuild ! ( mockContext as any ) ;
2629
27- expect ( consoleSpy ) . toHaveBeenCalledWith ( '[seo] Sitemap not configured, skipping' ) ;
30+ expect ( consoleSpy ) . toHaveBeenCalledWith ( '[seo] No SEO generation configured, skipping' ) ;
31+ expect ( sitemapSpy ) . not . toHaveBeenCalled ( ) ;
32+ expect ( robotsSpy ) . not . toHaveBeenCalled ( ) ;
2833
2934 consoleSpy . mockRestore ( ) ;
35+ sitemapSpy . mockRestore ( ) ;
36+ robotsSpy . mockRestore ( ) ;
3037 } ) ;
3138
3239 it ( 'should call generateSitemap when configured' , async ( ) => {
40+ const sitemapSpy = vi . spyOn ( sitemapModule , 'generateSitemap' ) . mockResolvedValue ( undefined ) ;
41+
3342 const plugin = seoPlugin ( {
3443 sitemap : { hostname : 'https://example.com' } ,
3544 } ) ;
@@ -47,5 +56,38 @@ describe('seoPlugin', () => {
4756
4857 // Should not throw
4958 await expect ( plugin . postBuild ! ( mockContext as any ) ) . resolves . toBeUndefined ( ) ;
59+ expect ( sitemapSpy ) . toHaveBeenCalledWith ( mockContext , { hostname : 'https://example.com' } ) ;
60+
61+ sitemapSpy . mockRestore ( ) ;
62+ } ) ;
63+
64+ it ( 'should call generateRobots when configured' , async ( ) => {
65+ const robotsSpy = vi . spyOn ( robotsModule , 'generateRobots' ) . mockResolvedValue ( undefined ) ;
66+
67+ const plugin = seoPlugin ( {
68+ robots : { disallow : [ '/admin' ] } ,
69+ } ) ;
70+
71+ const mockContext = {
72+ config : {
73+ seo : {
74+ robots : {
75+ disallow : [ '/admin' ] ,
76+ } ,
77+ } ,
78+ } ,
79+ outDir : '/tmp/dist' ,
80+ routes : { } ,
81+ allContent : { } ,
82+ } as any ;
83+
84+ await expect ( plugin . postBuild ! ( mockContext as any ) ) . resolves . toBeUndefined ( ) ;
85+ expect ( robotsSpy ) . toHaveBeenCalledWith (
86+ mockContext ,
87+ { disallow : [ '/admin' ] } ,
88+ undefined
89+ ) ;
90+
91+ robotsSpy . mockRestore ( ) ;
5092 } ) ;
5193} ) ;
0 commit comments