@easy-web/core 1.3.3 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +9 -9
  3. package/src/components/Header.astro +205 -205
  4. package/src/components/HeaderCentered.astro +230 -230
  5. package/src/components/HeaderFlyout.astro +370 -370
  6. package/src/components/HeaderHideOnScroll.astro +234 -234
  7. package/src/components/LanguageSwitch.astro +82 -82
  8. package/src/components/NotFound.astro +115 -115
  9. package/src/components/PageShell.astro +82 -82
  10. package/src/components/SeoHead.astro +106 -106
  11. package/src/components/StructuredData.astro +1 -1
  12. package/src/components/ThemeToggle.astro +84 -84
  13. package/src/components/UniversalMedia.astro +106 -106
  14. package/src/components/__tests__/StructuredData.test.ts +1 -1
  15. package/src/components/__tests__/UniversalMedia.test.ts +65 -65
  16. package/src/contracts/__tests__/jsonld.test.ts +1 -1
  17. package/src/contracts/index.ts +1 -1
  18. package/src/contracts/jsonld.ts +1 -1
  19. package/src/i18n/__tests__/factory.test.ts +54 -54
  20. package/src/i18n/__tests__/localized-paths.test.ts +258 -258
  21. package/src/i18n/__tests__/routing-multi-locale.test.ts +89 -89
  22. package/src/i18n/__tests__/seo.test.ts +140 -140
  23. package/src/i18n/factory.ts +66 -66
  24. package/src/i18n/index.ts +5 -5
  25. package/src/i18n/localized-paths.ts +181 -181
  26. package/src/i18n/seo.ts +131 -131
  27. package/src/markdown/__tests__/__fixtures__/abs-src-assets.md +1 -1
  28. package/src/markdown/__tests__/__fixtures__/external-url.md +1 -1
  29. package/src/markdown/__tests__/__fixtures__/public-slash.md +1 -1
  30. package/src/markdown/__tests__/__fixtures__/rel-double-dot.md +1 -1
  31. package/src/markdown/__tests__/index.test.ts +60 -60
  32. package/src/markdown/index.ts +1 -1
  33. package/src/markdown/plugin.ts +42 -42
  34. package/src/schemas/__tests__/notFound.test.ts +32 -32
  35. package/src/schemas/notFound.ts +33 -33
  36. package/src/seo/__tests__/crawl-policy.test.ts +50 -50
  37. package/src/seo/__tests__/integration.test.ts +195 -195
  38. package/src/seo/__tests__/no-index-resolution.test.ts +53 -53
  39. package/src/seo/__tests__/robots-txt.test.ts +66 -66
  40. package/src/seo/__tests__/sitemap-options.test.ts +400 -400
  41. package/src/seo/crawl-policy.ts +36 -36
  42. package/src/seo/index.ts +254 -254
  43. package/src/seo/routes/robots-txt.ts +36 -36
@@ -1,400 +1,400 @@
1
- import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'
2
-
3
- const { sitemapSpy } = vi.hoisted(() => ({ sitemapSpy: vi.fn() }))
4
-
5
- vi.mock('@astrojs/sitemap', () => ({
6
- default: (opts: unknown) => {
7
- sitemapSpy(opts)
8
- return { name: '@astrojs/sitemap', hooks: {} }
9
- },
10
- }))
11
-
12
- const easyWebSeo = (await import('../index.js')).default
13
-
14
- type SitemapOptions = {
15
- filter: (page: string) => boolean
16
- serialize: (item: { url: string; links?: { lang: string; url: string }[] }) => {
17
- url: string
18
- links?: { lang: string; url: string }[]
19
- }
20
- namespaces?: { xhtml?: boolean }
21
- i18n?: { defaultLocale: string; locales: Record<string, string> }
22
- }
23
-
24
- type Integration = ReturnType<typeof easyWebSeo>
25
-
26
- const SITE = 'https://harleyrentflorida.de'
27
-
28
- const baseConfig = () => ({
29
- site: new URL(SITE),
30
- base: '/',
31
- trailingSlash: 'ignore',
32
- build: { format: 'directory' },
33
- i18n: { locales: ['de', 'en'], defaultLocale: 'de', routing: {} },
34
- integrations: [],
35
- })
36
-
37
- const run = (
38
- integration: Integration,
39
- config: Record<string, unknown> = baseConfig(),
40
- ) => {
41
- const updateConfig = vi.fn()
42
- const logger = { error: vi.fn(), warn: vi.fn(), info: vi.fn() }
43
- const hook = integration.hooks['astro:config:setup'] as unknown as (p: unknown) => void
44
-
45
- hook({ config, updateConfig, injectRoute: vi.fn(), logger })
46
-
47
- return { updateConfig, logger }
48
- }
49
-
50
- const sitemapOptions = (): SitemapOptions => sitemapSpy.mock.calls[0]?.[0] as SitemapOptions
51
-
52
- beforeEach(() => {
53
- sitemapSpy.mockClear()
54
- })
55
-
56
- afterEach(() => {
57
- delete process.env.EASY_WEB_SEO_NO_INDEX
58
- delete process.env.EASY_WEB_SEO_TRAILING_SLASH
59
- vi.restoreAllMocks()
60
- })
61
-
62
- describe('trailing-slash style resolution', () => {
63
- it('publishes "always" for Astro directory output so SeoHead matches the sitemap', () => {
64
- run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }))
65
-
66
- expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('always')
67
- })
68
-
69
- it('publishes "never" for file output', () => {
70
- run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
71
- ...baseConfig(),
72
- build: { format: 'file' },
73
- })
74
-
75
- expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('never')
76
- })
77
-
78
- it('publishes "never" when trailingSlash is explicitly never', () => {
79
- run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
80
- ...baseConfig(),
81
- trailingSlash: 'never',
82
- })
83
-
84
- expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('never')
85
- })
86
-
87
- it('lets an explicit trailingSlash outrank the output shape', () => {
88
- run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
89
- ...baseConfig(),
90
- build: { format: 'file' },
91
- trailingSlash: 'always',
92
- })
93
-
94
- expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('always')
95
- })
96
-
97
- it('warns and assumes slashes for build.format "preserve", which emits both shapes', () => {
98
- const { logger } = run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
99
- ...baseConfig(),
100
- build: { format: 'preserve' },
101
- })
102
-
103
- expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('always')
104
- expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('preserve'))
105
- })
106
-
107
- it('emits sitemap URLs in the same form it publishes for the canonical', () => {
108
- run(
109
- easyWebSeo({
110
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
111
- localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
112
- }),
113
- )
114
-
115
- const links = sitemapOptions().serialize({ url: `${SITE}/datenschutz/` }).links ?? []
116
-
117
- expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('always')
118
- for (const link of links) {
119
- expect(link.url.endsWith('/')).toBe(true)
120
- }
121
- })
122
-
123
- it('emits sitemap URLs without a slash under file output, matching that canonical form', () => {
124
- run(
125
- easyWebSeo({
126
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
127
- localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
128
- }),
129
- { ...baseConfig(), build: { format: 'file' } },
130
- )
131
-
132
- const links = sitemapOptions().serialize({ url: `${SITE}/datenschutz` }).links ?? []
133
-
134
- expect(links.map((l) => l.url)).toEqual([`${SITE}/datenschutz`, `${SITE}/en/privacy`])
135
- })
136
- })
137
-
138
- describe('sitemap crawl-policy filter', () => {
139
- it('excludes /admin/ from the sitemap, matching the robots.txt Disallow', () => {
140
- run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }))
141
-
142
- const { filter } = sitemapOptions()
143
-
144
- expect(filter(`${SITE}/admin/`)).toBe(false)
145
- expect(filter(`${SITE}/datenschutz/`)).toBe(true)
146
- })
147
-
148
- it('composes a caller filter with AND semantics', () => {
149
- run(
150
- easyWebSeo({
151
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
152
- filter: (page) => !page.includes('/bikes/'),
153
- }),
154
- )
155
-
156
- const { filter } = sitemapOptions()
157
-
158
- expect(filter(`${SITE}/bikes/fatboy/`)).toBe(false)
159
- expect(filter(`${SITE}/about/`)).toBe(true)
160
- })
161
-
162
- it('does not let a caller filter re-admit a crawl-policy blocked path', () => {
163
- run(
164
- easyWebSeo({
165
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
166
- filter: () => true,
167
- }),
168
- )
169
-
170
- expect(sitemapOptions().filter(`${SITE}/admin/`)).toBe(false)
171
- })
172
-
173
- it('registers no sitemap at all in noIndex mode, since robots.txt blocks everything', () => {
174
- const { updateConfig } = run(easyWebSeo({ noIndex: true, sitemapLocales: { de: 'de-DE', en: 'en-US' } }))
175
-
176
- expect(sitemapSpy).not.toHaveBeenCalled()
177
- expect(updateConfig).not.toHaveBeenCalled()
178
- })
179
-
180
- it('enables the xhtml namespace so xhtml:link alternates are valid', () => {
181
- run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }))
182
-
183
- expect(sitemapOptions().namespaces).toEqual({ xhtml: true })
184
- })
185
- })
186
-
187
- describe('localizedPaths serialization', () => {
188
- const withGroups = () =>
189
- easyWebSeo({
190
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
191
- localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
192
- })
193
-
194
- it('injects reciprocal alternates onto both members of a translated-slug group', () => {
195
- run(withGroups())
196
- const { serialize } = sitemapOptions()
197
-
198
- const expected = [
199
- { lang: 'de-DE', url: `${SITE}/datenschutz/` },
200
- { lang: 'en-US', url: `${SITE}/en/privacy/` },
201
- ]
202
-
203
- expect(serialize({ url: `${SITE}/datenschutz/` }).links).toEqual(expected)
204
- expect(serialize({ url: `${SITE}/en/privacy/` }).links).toEqual(expected)
205
- })
206
-
207
- it('includes a self-reference in each group member link set', () => {
208
- run(withGroups())
209
- const { serialize } = sitemapOptions()
210
-
211
- const links = serialize({ url: `${SITE}/datenschutz/` }).links ?? []
212
-
213
- expect(links.map((l) => l.url)).toContain(`${SITE}/datenschutz/`)
214
- })
215
-
216
- it('leaves equal-slug pages that @astrojs/sitemap already paired untouched', () => {
217
- run(withGroups())
218
- const { serialize } = sitemapOptions()
219
-
220
- const autoPaired = {
221
- url: `${SITE}/impressum/`,
222
- links: [
223
- { lang: 'de-DE', url: `${SITE}/impressum/` },
224
- { lang: 'en-US', url: `${SITE}/en/impressum/` },
225
- ],
226
- }
227
-
228
- expect(serialize(autoPaired)).toEqual(autoPaired)
229
- })
230
-
231
- it('never drops an entry by returning undefined', () => {
232
- run(withGroups())
233
- const { serialize } = sitemapOptions()
234
-
235
- expect(serialize({ url: `${SITE}/admin/` })).toBeDefined()
236
- })
237
-
238
- it('re-renders every <loc> into the served form so it matches the canonical', () => {
239
- run(
240
- easyWebSeo({
241
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
242
- localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
243
- }),
244
- { ...baseConfig(), trailingSlash: 'never' },
245
- )
246
-
247
- const { serialize } = sitemapOptions()
248
-
249
- expect(serialize({ url: `${SITE}/about/` }).url).toBe(`${SITE}/about`)
250
- expect(serialize({ url: `${SITE}/about` }).url).toBe(`${SITE}/about`)
251
- })
252
-
253
- it('normalises auto-paired alternates too, not just the loc', () => {
254
- run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
255
- ...baseConfig(),
256
- trailingSlash: 'never',
257
- })
258
-
259
- const serialized = sitemapOptions().serialize({
260
- url: `${SITE}/impressum/`,
261
- links: [
262
- { lang: 'de-DE', url: `${SITE}/impressum/` },
263
- { lang: 'en-US', url: `${SITE}/en/impressum/` },
264
- ],
265
- })
266
-
267
- expect(serialized.links?.map((l) => l.url)).toEqual([
268
- `${SITE}/impressum`,
269
- `${SITE}/en/impressum`,
270
- ])
271
- })
272
- })
273
-
274
- describe('localizedPaths validation', () => {
275
- const expectThrow = (options: Parameters<typeof easyWebSeo>[0], message: string | RegExp) => {
276
- expect(() => run(easyWebSeo(options))).toThrow(message)
277
- }
278
-
279
- it('rejects a group missing a configured locale', () => {
280
- expectThrow(
281
- { sitemapLocales: { de: 'de-DE', en: 'en-US' }, localizedPaths: [{ de: '/datenschutz/' }] },
282
- /missing the locale\(s\): en/,
283
- )
284
- })
285
-
286
- it('rejects a group naming an unknown locale', () => {
287
- expectThrow(
288
- {
289
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
290
- localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/', fr: '/fr/confidentialite/' }],
291
- },
292
- /unknown locale\(s\): fr/,
293
- )
294
- })
295
-
296
- it('rejects a path that is not absolute', () => {
297
- expectThrow(
298
- {
299
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
300
- localizedPaths: [{ de: 'datenschutz/', en: '/en/privacy/' }],
301
- },
302
- /must be an absolute path/,
303
- )
304
- })
305
-
306
- it('rejects a path carrying a query string', () => {
307
- expectThrow(
308
- {
309
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
310
- localizedPaths: [{ de: '/datenschutz/?x=1', en: '/en/privacy/' }],
311
- },
312
- /must not contain a query string or hash/,
313
- )
314
- })
315
-
316
- it('rejects the same path being claimed by two groups', () => {
317
- expectThrow(
318
- {
319
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
320
- localizedPaths: [
321
- { de: '/datenschutz/', en: '/en/privacy/' },
322
- { de: '/datenschutz/', en: '/en/data-protection/' },
323
- ],
324
- },
325
- /claimed by both/,
326
- )
327
- })
328
-
329
- it('rejects localizedPaths when astro i18n is not configured', () => {
330
- expect(() =>
331
- run(easyWebSeo({ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }] }), {
332
- ...baseConfig(),
333
- i18n: undefined,
334
- }),
335
- ).toThrow(/requires `i18n` to be configured/)
336
- })
337
-
338
- it('accepts a valid group', () => {
339
- expect(() =>
340
- run(
341
- easyWebSeo({
342
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
343
- localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
344
- }),
345
- ),
346
- ).not.toThrow()
347
- })
348
- })
349
-
350
- describe('localizedPaths build-time verification', () => {
351
- const registeredValidator = (updateConfig: ReturnType<typeof vi.fn>) => {
352
- const patch = updateConfig.mock.calls[0]?.[0] as { integrations: Integration[] }
353
- return patch.integrations
354
- }
355
-
356
- it('registers the validator after @astrojs/sitemap so it can observe serialize', () => {
357
- const { updateConfig } = run(
358
- easyWebSeo({
359
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
360
- localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
361
- }),
362
- )
363
-
364
- const names = registeredValidator(updateConfig).map((i) => i.name)
365
-
366
- expect(names).toEqual(['@astrojs/sitemap', '@easy-web/seo:localized-paths-validation'])
367
- })
368
-
369
- it('fails the build when a declared path was never emitted', () => {
370
- const { updateConfig } = run(
371
- easyWebSeo({
372
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
373
- localizedPaths: [{ de: '/datenschutz/', en: '/en/typo/' }],
374
- }),
375
- )
376
-
377
- const validator = registeredValidator(updateConfig)[1] as Integration
378
- const done = validator.hooks['astro:build:done'] as unknown as () => void
379
-
380
- expect(() => done()).toThrow(/no such page was built/)
381
- })
382
-
383
- it('passes when every declared path was emitted', () => {
384
- const { updateConfig } = run(
385
- easyWebSeo({
386
- sitemapLocales: { de: 'de-DE', en: 'en-US' },
387
- localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
388
- }),
389
- )
390
-
391
- const { serialize } = sitemapOptions()
392
- serialize({ url: `${SITE}/datenschutz/` })
393
- serialize({ url: `${SITE}/en/privacy/` })
394
-
395
- const validator = registeredValidator(updateConfig)[1] as Integration
396
- const done = validator.hooks['astro:build:done'] as unknown as () => void
397
-
398
- expect(() => done()).not.toThrow()
399
- })
400
- })
1
+ import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'
2
+
3
+ const { sitemapSpy } = vi.hoisted(() => ({ sitemapSpy: vi.fn() }))
4
+
5
+ vi.mock('@astrojs/sitemap', () => ({
6
+ default: (opts: unknown) => {
7
+ sitemapSpy(opts)
8
+ return { name: '@astrojs/sitemap', hooks: {} }
9
+ },
10
+ }))
11
+
12
+ const easyWebSeo = (await import('../index.js')).default
13
+
14
+ type SitemapOptions = {
15
+ filter: (page: string) => boolean
16
+ serialize: (item: { url: string; links?: { lang: string; url: string }[] }) => {
17
+ url: string
18
+ links?: { lang: string; url: string }[]
19
+ }
20
+ namespaces?: { xhtml?: boolean }
21
+ i18n?: { defaultLocale: string; locales: Record<string, string> }
22
+ }
23
+
24
+ type Integration = ReturnType<typeof easyWebSeo>
25
+
26
+ const SITE = 'https://harleyrentflorida.de'
27
+
28
+ const baseConfig = () => ({
29
+ site: new URL(SITE),
30
+ base: '/',
31
+ trailingSlash: 'ignore',
32
+ build: { format: 'directory' },
33
+ i18n: { locales: ['de', 'en'], defaultLocale: 'de', routing: {} },
34
+ integrations: [],
35
+ })
36
+
37
+ const run = (
38
+ integration: Integration,
39
+ config: Record<string, unknown> = baseConfig(),
40
+ ) => {
41
+ const updateConfig = vi.fn()
42
+ const logger = { error: vi.fn(), warn: vi.fn(), info: vi.fn() }
43
+ const hook = integration.hooks['astro:config:setup'] as unknown as (p: unknown) => void
44
+
45
+ hook({ config, updateConfig, injectRoute: vi.fn(), logger })
46
+
47
+ return { updateConfig, logger }
48
+ }
49
+
50
+ const sitemapOptions = (): SitemapOptions => sitemapSpy.mock.calls[0]?.[0] as SitemapOptions
51
+
52
+ beforeEach(() => {
53
+ sitemapSpy.mockClear()
54
+ })
55
+
56
+ afterEach(() => {
57
+ delete process.env.EASY_WEB_SEO_NO_INDEX
58
+ delete process.env.EASY_WEB_SEO_TRAILING_SLASH
59
+ vi.restoreAllMocks()
60
+ })
61
+
62
+ describe('trailing-slash style resolution', () => {
63
+ it('publishes "always" for Astro directory output so SeoHead matches the sitemap', () => {
64
+ run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }))
65
+
66
+ expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('always')
67
+ })
68
+
69
+ it('publishes "never" for file output', () => {
70
+ run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
71
+ ...baseConfig(),
72
+ build: { format: 'file' },
73
+ })
74
+
75
+ expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('never')
76
+ })
77
+
78
+ it('publishes "never" when trailingSlash is explicitly never', () => {
79
+ run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
80
+ ...baseConfig(),
81
+ trailingSlash: 'never',
82
+ })
83
+
84
+ expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('never')
85
+ })
86
+
87
+ it('lets an explicit trailingSlash outrank the output shape', () => {
88
+ run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
89
+ ...baseConfig(),
90
+ build: { format: 'file' },
91
+ trailingSlash: 'always',
92
+ })
93
+
94
+ expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('always')
95
+ })
96
+
97
+ it('warns and assumes slashes for build.format "preserve", which emits both shapes', () => {
98
+ const { logger } = run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
99
+ ...baseConfig(),
100
+ build: { format: 'preserve' },
101
+ })
102
+
103
+ expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('always')
104
+ expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('preserve'))
105
+ })
106
+
107
+ it('emits sitemap URLs in the same form it publishes for the canonical', () => {
108
+ run(
109
+ easyWebSeo({
110
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
111
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
112
+ }),
113
+ )
114
+
115
+ const links = sitemapOptions().serialize({ url: `${SITE}/datenschutz/` }).links ?? []
116
+
117
+ expect(process.env.EASY_WEB_SEO_TRAILING_SLASH).toBe('always')
118
+ for (const link of links) {
119
+ expect(link.url.endsWith('/')).toBe(true)
120
+ }
121
+ })
122
+
123
+ it('emits sitemap URLs without a slash under file output, matching that canonical form', () => {
124
+ run(
125
+ easyWebSeo({
126
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
127
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
128
+ }),
129
+ { ...baseConfig(), build: { format: 'file' } },
130
+ )
131
+
132
+ const links = sitemapOptions().serialize({ url: `${SITE}/datenschutz` }).links ?? []
133
+
134
+ expect(links.map((l) => l.url)).toEqual([`${SITE}/datenschutz`, `${SITE}/en/privacy`])
135
+ })
136
+ })
137
+
138
+ describe('sitemap crawl-policy filter', () => {
139
+ it('excludes /admin/ from the sitemap, matching the robots.txt Disallow', () => {
140
+ run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }))
141
+
142
+ const { filter } = sitemapOptions()
143
+
144
+ expect(filter(`${SITE}/admin/`)).toBe(false)
145
+ expect(filter(`${SITE}/datenschutz/`)).toBe(true)
146
+ })
147
+
148
+ it('composes a caller filter with AND semantics', () => {
149
+ run(
150
+ easyWebSeo({
151
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
152
+ filter: (page) => !page.includes('/bikes/'),
153
+ }),
154
+ )
155
+
156
+ const { filter } = sitemapOptions()
157
+
158
+ expect(filter(`${SITE}/bikes/fatboy/`)).toBe(false)
159
+ expect(filter(`${SITE}/about/`)).toBe(true)
160
+ })
161
+
162
+ it('does not let a caller filter re-admit a crawl-policy blocked path', () => {
163
+ run(
164
+ easyWebSeo({
165
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
166
+ filter: () => true,
167
+ }),
168
+ )
169
+
170
+ expect(sitemapOptions().filter(`${SITE}/admin/`)).toBe(false)
171
+ })
172
+
173
+ it('registers no sitemap at all in noIndex mode, since robots.txt blocks everything', () => {
174
+ const { updateConfig } = run(easyWebSeo({ noIndex: true, sitemapLocales: { de: 'de-DE', en: 'en-US' } }))
175
+
176
+ expect(sitemapSpy).not.toHaveBeenCalled()
177
+ expect(updateConfig).not.toHaveBeenCalled()
178
+ })
179
+
180
+ it('enables the xhtml namespace so xhtml:link alternates are valid', () => {
181
+ run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }))
182
+
183
+ expect(sitemapOptions().namespaces).toEqual({ xhtml: true })
184
+ })
185
+ })
186
+
187
+ describe('localizedPaths serialization', () => {
188
+ const withGroups = () =>
189
+ easyWebSeo({
190
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
191
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
192
+ })
193
+
194
+ it('injects reciprocal alternates onto both members of a translated-slug group', () => {
195
+ run(withGroups())
196
+ const { serialize } = sitemapOptions()
197
+
198
+ const expected = [
199
+ { lang: 'de-DE', url: `${SITE}/datenschutz/` },
200
+ { lang: 'en-US', url: `${SITE}/en/privacy/` },
201
+ ]
202
+
203
+ expect(serialize({ url: `${SITE}/datenschutz/` }).links).toEqual(expected)
204
+ expect(serialize({ url: `${SITE}/en/privacy/` }).links).toEqual(expected)
205
+ })
206
+
207
+ it('includes a self-reference in each group member link set', () => {
208
+ run(withGroups())
209
+ const { serialize } = sitemapOptions()
210
+
211
+ const links = serialize({ url: `${SITE}/datenschutz/` }).links ?? []
212
+
213
+ expect(links.map((l) => l.url)).toContain(`${SITE}/datenschutz/`)
214
+ })
215
+
216
+ it('leaves equal-slug pages that @astrojs/sitemap already paired untouched', () => {
217
+ run(withGroups())
218
+ const { serialize } = sitemapOptions()
219
+
220
+ const autoPaired = {
221
+ url: `${SITE}/impressum/`,
222
+ links: [
223
+ { lang: 'de-DE', url: `${SITE}/impressum/` },
224
+ { lang: 'en-US', url: `${SITE}/en/impressum/` },
225
+ ],
226
+ }
227
+
228
+ expect(serialize(autoPaired)).toEqual(autoPaired)
229
+ })
230
+
231
+ it('never drops an entry by returning undefined', () => {
232
+ run(withGroups())
233
+ const { serialize } = sitemapOptions()
234
+
235
+ expect(serialize({ url: `${SITE}/admin/` })).toBeDefined()
236
+ })
237
+
238
+ it('re-renders every <loc> into the served form so it matches the canonical', () => {
239
+ run(
240
+ easyWebSeo({
241
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
242
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
243
+ }),
244
+ { ...baseConfig(), trailingSlash: 'never' },
245
+ )
246
+
247
+ const { serialize } = sitemapOptions()
248
+
249
+ expect(serialize({ url: `${SITE}/about/` }).url).toBe(`${SITE}/about`)
250
+ expect(serialize({ url: `${SITE}/about` }).url).toBe(`${SITE}/about`)
251
+ })
252
+
253
+ it('normalises auto-paired alternates too, not just the loc', () => {
254
+ run(easyWebSeo({ sitemapLocales: { de: 'de-DE', en: 'en-US' } }), {
255
+ ...baseConfig(),
256
+ trailingSlash: 'never',
257
+ })
258
+
259
+ const serialized = sitemapOptions().serialize({
260
+ url: `${SITE}/impressum/`,
261
+ links: [
262
+ { lang: 'de-DE', url: `${SITE}/impressum/` },
263
+ { lang: 'en-US', url: `${SITE}/en/impressum/` },
264
+ ],
265
+ })
266
+
267
+ expect(serialized.links?.map((l) => l.url)).toEqual([
268
+ `${SITE}/impressum`,
269
+ `${SITE}/en/impressum`,
270
+ ])
271
+ })
272
+ })
273
+
274
+ describe('localizedPaths validation', () => {
275
+ const expectThrow = (options: Parameters<typeof easyWebSeo>[0], message: string | RegExp) => {
276
+ expect(() => run(easyWebSeo(options))).toThrow(message)
277
+ }
278
+
279
+ it('rejects a group missing a configured locale', () => {
280
+ expectThrow(
281
+ { sitemapLocales: { de: 'de-DE', en: 'en-US' }, localizedPaths: [{ de: '/datenschutz/' }] },
282
+ /missing the locale\(s\): en/,
283
+ )
284
+ })
285
+
286
+ it('rejects a group naming an unknown locale', () => {
287
+ expectThrow(
288
+ {
289
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
290
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/', fr: '/fr/confidentialite/' }],
291
+ },
292
+ /unknown locale\(s\): fr/,
293
+ )
294
+ })
295
+
296
+ it('rejects a path that is not absolute', () => {
297
+ expectThrow(
298
+ {
299
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
300
+ localizedPaths: [{ de: 'datenschutz/', en: '/en/privacy/' }],
301
+ },
302
+ /must be an absolute path/,
303
+ )
304
+ })
305
+
306
+ it('rejects a path carrying a query string', () => {
307
+ expectThrow(
308
+ {
309
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
310
+ localizedPaths: [{ de: '/datenschutz/?x=1', en: '/en/privacy/' }],
311
+ },
312
+ /must not contain a query string or hash/,
313
+ )
314
+ })
315
+
316
+ it('rejects the same path being claimed by two groups', () => {
317
+ expectThrow(
318
+ {
319
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
320
+ localizedPaths: [
321
+ { de: '/datenschutz/', en: '/en/privacy/' },
322
+ { de: '/datenschutz/', en: '/en/data-protection/' },
323
+ ],
324
+ },
325
+ /claimed by both/,
326
+ )
327
+ })
328
+
329
+ it('rejects localizedPaths when astro i18n is not configured', () => {
330
+ expect(() =>
331
+ run(easyWebSeo({ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }] }), {
332
+ ...baseConfig(),
333
+ i18n: undefined,
334
+ }),
335
+ ).toThrow(/requires `i18n` to be configured/)
336
+ })
337
+
338
+ it('accepts a valid group', () => {
339
+ expect(() =>
340
+ run(
341
+ easyWebSeo({
342
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
343
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
344
+ }),
345
+ ),
346
+ ).not.toThrow()
347
+ })
348
+ })
349
+
350
+ describe('localizedPaths build-time verification', () => {
351
+ const registeredValidator = (updateConfig: ReturnType<typeof vi.fn>) => {
352
+ const patch = updateConfig.mock.calls[0]?.[0] as { integrations: Integration[] }
353
+ return patch.integrations
354
+ }
355
+
356
+ it('registers the validator after @astrojs/sitemap so it can observe serialize', () => {
357
+ const { updateConfig } = run(
358
+ easyWebSeo({
359
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
360
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
361
+ }),
362
+ )
363
+
364
+ const names = registeredValidator(updateConfig).map((i) => i.name)
365
+
366
+ expect(names).toEqual(['@astrojs/sitemap', '@easy-web/seo:localized-paths-validation'])
367
+ })
368
+
369
+ it('fails the build when a declared path was never emitted', () => {
370
+ const { updateConfig } = run(
371
+ easyWebSeo({
372
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
373
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/typo/' }],
374
+ }),
375
+ )
376
+
377
+ const validator = registeredValidator(updateConfig)[1] as Integration
378
+ const done = validator.hooks['astro:build:done'] as unknown as () => void
379
+
380
+ expect(() => done()).toThrow(/no such page was built/)
381
+ })
382
+
383
+ it('passes when every declared path was emitted', () => {
384
+ const { updateConfig } = run(
385
+ easyWebSeo({
386
+ sitemapLocales: { de: 'de-DE', en: 'en-US' },
387
+ localizedPaths: [{ de: '/datenschutz/', en: '/en/privacy/' }],
388
+ }),
389
+ )
390
+
391
+ const { serialize } = sitemapOptions()
392
+ serialize({ url: `${SITE}/datenschutz/` })
393
+ serialize({ url: `${SITE}/en/privacy/` })
394
+
395
+ const validator = registeredValidator(updateConfig)[1] as Integration
396
+ const done = validator.hooks['astro:build:done'] as unknown as () => void
397
+
398
+ expect(() => done()).not.toThrow()
399
+ })
400
+ })