import { describe, it, expect, vi, afterEach } from 'vitest' import { defineComponent, h } from 'vue' import { mount } from '@vue/test-utils' import { useReviewContent, type ReviewItem } from './useReviewContent' /** * Honesty guard: consumer-supplied `defaults` (placeholder / sample reviews) * are a DEV-ONLY affordance. In a production build the composable must render an * honest empty state instead of fabricated social proof — this is the fix for * the flagship SSR bug where a fabricated fallback was baked into the crawler * HTML while the client showed the real reviews. */ const SAMPLE_DEFAULTS: ReviewItem[] = [ { id: 'seed-1', platform: 'google', rating: 5, authorName: 'Sample Person', text: 'Placeholder review' }, ] function mountReviews(config: Parameters[0]) { let api!: ReturnType const Comp = defineComponent({ setup() { api = useReviewContent(config) return () => h('div') }, }) const wrapper = mount(Comp) return { api, wrapper } } afterEach(() => { vi.unstubAllEnvs() vi.unstubAllGlobals() vi.restoreAllMocks() delete (globalThis as Record).__DCS_CONTENT__ }) describe('useReviewContent honesty guard', () => { it('production build with no content define renders an empty state, NOT the defaults', () => { vi.stubEnv('DEV', false) // __DCS_CONTENT__ intentionally undefined (simulates a production SSR pass // where the define never reached the composable / no curated reviews). const { api, wrapper } = mountReviews({ sectionKey: 'testimonials', defaults: SAMPLE_DEFAULTS }) expect(api.reviews.value).toEqual([]) expect(api.hasReviews.value).toBe(false) expect(api.count.value).toBe(0) wrapper.unmount() }) it('production build with no reviews for the section renders empty, NOT the defaults', () => { vi.stubEnv('DEV', false) vi.stubGlobal('__DCS_CONTENT__', { global: {}, pages: {} }) const { api, wrapper } = mountReviews({ sectionKey: 'testimonials', defaults: SAMPLE_DEFAULTS }) expect(api.reviews.value).toEqual([]) expect(api.hasReviews.value).toBe(false) wrapper.unmount() }) it('production build DOES render the real curated reviews from the content define', () => { vi.stubEnv('DEV', false) vi.stubGlobal('__DCS_CONTENT__', { global: { 'reviews.testimonials.items': [ { id: 'real-1', platform: 'google', rating: 5, authorName: 'Real Reviewer', text: 'Genuine review' }, ], }, pages: {}, }) const { api, wrapper } = mountReviews({ sectionKey: 'testimonials', defaults: SAMPLE_DEFAULTS }) expect(api.reviews.value).toHaveLength(1) expect(api.reviews.value[0].authorName).toBe('Real Reviewer') expect(api.hasReviews.value).toBe(true) wrapper.unmount() }) it('dev build still surfaces the sample defaults (DX affordance)', () => { vi.stubEnv('DEV', true) const { api, wrapper } = mountReviews({ sectionKey: 'testimonials', defaults: SAMPLE_DEFAULTS }) expect(api.reviews.value).toHaveLength(1) expect(api.reviews.value[0].id).toBe('seed-1') wrapper.unmount() }) })