Skip to content
Open
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
72 changes: 72 additions & 0 deletions packages/shared/src/__tests__/cards.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, it, expect } from 'vitest';
import { validateCardPlatforms, diffCardPlatforms } from '../cards';

describe('validateCardPlatforms', () => {
it('passes with valid platforms', () => {
const result = validateCardPlatforms(['github', 'linkedin']);
expect(result.valid).toBe(true);
expect(result.errors).toHaveLength(0);
});

it('fails with empty array', () => {
const result = validateCardPlatforms([]);
expect(result.valid).toBe(false);
expect(result.errors).toContain('At least one platform is required.');
});

it('fails with unknown platform', () => {
const result = validateCardPlatforms(['github', 'myspace']);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.includes('myspace'))).toBe(true);
});

it('fails with duplicate platforms', () => {
const result = validateCardPlatforms(['github', 'github']);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.includes('Duplicate'))).toBe(true);
});

it('passes with exactly 10 platforms', () => {
const platforms = ['github','linkedin','twitter','youtube','twitch',
'discord','devto','medium','dribbble','leetcode'];
const result = validateCardPlatforms(platforms);
expect(result.valid).toBe(true);
});

it('fails with more than 10 platforms', () => {
const platforms = ['github','linkedin','twitter','youtube','twitch',
'discord','devto','medium','dribbble','leetcode','npm'];
const result = validateCardPlatforms(platforms);
expect(result.valid).toBe(false);
expect(result.errors.some(e => e.includes('Maximum 10'))).toBe(true);
});

it('fails with all invalid platforms', () => {
const result = validateCardPlatforms(['myspace', 'bebo']);
expect(result.valid).toBe(false);
expect(result.errors.length).toBeGreaterThanOrEqual(2);
});
});

describe('diffCardPlatforms', () => {
it('correctly identifies added, removed, unchanged', () => {
const diff = diffCardPlatforms(['github', 'linkedin'], ['github', 'twitter']);
expect(diff.added).toEqual(['twitter']);
expect(diff.removed).toEqual(['linkedin']);
expect(diff.unchanged).toEqual(['github']);
});

it('handles empty old card', () => {
const diff = diffCardPlatforms([], ['github']);
expect(diff.added).toEqual(['github']);
expect(diff.removed).toEqual([]);
expect(diff.unchanged).toEqual([]);
});

it('handles identical cards', () => {
const diff = diffCardPlatforms(['github'], ['github']);
expect(diff.added).toEqual([]);
expect(diff.removed).toEqual([]);
expect(diff.unchanged).toEqual(['github']);
});
});
50 changes: 50 additions & 0 deletions packages/shared/src/cards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export type CardValidationResult = {
valid: boolean;
errors: string[];
};

const PLATFORMS = new Set([
'github', 'linkedin', 'twitter', 'instagram', 'youtube',
'twitch', 'discord', 'devto', 'hashnode', 'medium',
'dribbble', 'behance', 'figma', 'stackoverflow', 'leetcode',
'codepen', 'replit', 'npm', 'producthunt', 'website',
]);

export function validateCardPlatforms(platforms: string[]): CardValidationResult {
const errors: string[] = [];

if (platforms.length === 0) {
errors.push('At least one platform is required.');
}

if (platforms.length > 10) {
errors.push(`Maximum 10 platforms allowed, got ${platforms.length}.`);
}

const seen = new Set<string>();
for (const p of platforms) {
if (!PLATFORMS.has(p)) {
errors.push(`Unknown platform: "${p}".`);
}
if (seen.has(p)) {
errors.push(`Duplicate platform: "${p}".`);
}
seen.add(p);
}

return { valid: errors.length === 0, errors };
}

export function diffCardPlatforms(
oldCard: string[],
newCard: string[]
): { added: string[]; removed: string[]; unchanged: string[] } {
const oldSet = new Set(oldCard);
const newSet = new Set(newCard);

return {
added: newCard.filter(p => !oldSet.has(p)),
removed: oldCard.filter(p => !newSet.has(p)),
unchanged: oldCard.filter(p => newSet.has(p)),
};
}
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './platforms';
export * from './types';
export * from './cards';