diff --git a/test/directory.test.mjs b/test/directory.test.mjs index 5ed9f92..b784843 100644 --- a/test/directory.test.mjs +++ b/test/directory.test.mjs @@ -18,3 +18,41 @@ test('directory text is escaped and repository names cannot inject URLs or paths assert.throws(() => validateDirectory({ ...input, projects: [{ ...project, repository: '../../private' }] }), /Invalid repository/); assert.throws(() => validateDirectory({ ...input, projects: [project, { ...project, repository: 'EXAMPLE' }] }), /Duplicate/); }); + +test('validates root input properties', () => { + const validProject = { repository: 'example', name: 'Example', description: 'Example', scope: 'Scope' }; + const baseInput = { schema_version: 'freedom.source-directory/v1', organization: 'FreeTWAI', projects: [validProject] }; + + // Valid should not throw + validateDirectory(baseInput); + + // Missing or invalid schema + assert.throws(() => validateDirectory(null), /Expected the FreeTWAI public source directory/); + assert.throws(() => validateDirectory({ ...baseInput, schema_version: 'invalid' }), /Expected the FreeTWAI public source directory/); + + // Wrong organization + assert.throws(() => validateDirectory({ ...baseInput, organization: 'Other' }), /Expected the FreeTWAI public source directory/); + + // Invalid projects array + assert.throws(() => validateDirectory({ ...baseInput, projects: null }), /Expected the FreeTWAI public source directory/); + + // > 100 projects + const tooManyProjects = Array.from({ length: 101 }, (_, i) => ({ ...validProject, repository: `repo-${i}` })); + assert.throws(() => validateDirectory({ ...baseInput, projects: tooManyProjects }), /Expected the FreeTWAI public source directory/); +}); + +test('validates project properties', () => { + const validProject = { repository: 'example', name: 'Example', description: 'Example', scope: 'Scope' }; + const baseInput = { schema_version: 'freedom.source-directory/v1', organization: 'FreeTWAI', projects: [] }; + + // Missing repository (null/undefined/empty) + assert.throws(() => validateDirectory({ ...baseInput, projects: [null] }), /Invalid repository name/); + assert.throws(() => validateDirectory({ ...baseInput, projects: [{ ...validProject, repository: undefined }] }), /Invalid repository name/); + + // Invalid string values, empty strings, strings > 500 chars + for (const key of ['name', 'description', 'scope']) { + assert.throws(() => validateDirectory({ ...baseInput, projects: [{ ...validProject, [key]: undefined }] }), new RegExp(`Invalid ${key}`)); + assert.throws(() => validateDirectory({ ...baseInput, projects: [{ ...validProject, [key]: ' ' }] }), new RegExp(`Invalid ${key}`)); + assert.throws(() => validateDirectory({ ...baseInput, projects: [{ ...validProject, [key]: 'a'.repeat(501) }] }), new RegExp(`Invalid ${key}`)); + } +}); diff --git a/test/privacy.test.mjs b/test/privacy.test.mjs index 3d3ef4d..4ecb346 100644 --- a/test/privacy.test.mjs +++ b/test/privacy.test.mjs @@ -40,3 +40,48 @@ test('does not swallow sentence-ending periods into links', () => { const html = renderPrivacyPage(page); assert.doesNotMatch(html, /href="[^"]*\."/); }); + +test('validates section and block properties', () => { + const basePage = structuredClone(page); + + // Invalid section objects (empty string header, missing/empty blocks) + basePage.zh.sections[0].h = ' '; + assert.throws(() => validatePrivacyPage(basePage), /Invalid zh section/); + basePage.zh.sections[0].h = 'Header'; + basePage.zh.sections[0].blocks = undefined; + assert.throws(() => validatePrivacyPage(basePage), /Invalid zh section/); + basePage.zh.sections[0].blocks = []; + assert.throws(() => validatePrivacyPage(basePage), /Invalid zh section/); + + // Invalid blocks (missing p and ul, or empty arrays, or non-string items in ul) + basePage.zh.sections[0].blocks = [{}]; + assert.throws(() => validatePrivacyPage(basePage), /Invalid zh block in "Header"/); + basePage.zh.sections[0].blocks = [{ p: ' ' }]; + assert.throws(() => validatePrivacyPage(basePage), /Invalid zh block in "Header"/); + basePage.zh.sections[0].blocks = [{ ul: [] }]; + assert.throws(() => validatePrivacyPage(basePage), /Invalid zh block in "Header"/); + basePage.zh.sections[0].blocks = [{ ul: ['valid', ' '] }]; + assert.throws(() => validatePrivacyPage(basePage), /Invalid zh block in "Header"/); + basePage.zh.sections[0].blocks = [{ ul: ['valid', 123] }]; + assert.throws(() => validatePrivacyPage(basePage), /Invalid zh block in "Header"/); +}); + +test('validates effective date', () => { + const basePage = structuredClone(page); + + basePage.effective_date = undefined; + assert.throws(() => validatePrivacyPage(basePage), /Invalid effective date/); + + basePage.effective_date = '2026-9-28'; + assert.throws(() => validatePrivacyPage(basePage), /Invalid effective date/); +}); + +test('handles missing eyebrow fallback', () => { + const basePage = structuredClone(page); + + delete basePage.zh.eyebrow; + const html = renderPrivacyPage(basePage); + + // Check that the eyebrow paragraph is rendered empty instead of "undefined" + assert.match(html, /
<\/p>/); +});