Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions test/directory.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
}
});
45 changes: 45 additions & 0 deletions test/privacy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 class="eyebrow"><\/p>/);
});
Loading