SPB Git

Repository Public MIT Selected

badge.test.mjs 5.2 KB 111 lines javascript

1/**2 * ─────────────────────────────────────────────3 *  SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 *  Author  : Simon-Pierre Boucher6 *  Contact : contact@spboucher.ai7 *  File    : test/unit/badge.test.mjs8 *  Purpose : Unit tests — metric badge specs, options, colours and SVG output9 *  License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */1213import { describe, it, expect } from 'vitest';14import {15  renderBadge, repoBadgeSpec, siteBadgeSpec, withBadgeOptions, normalizeColor, freshnessColor, textWidth,16  REPO_BADGE_METRICS, SITE_BADGE_METRICS,17} from '../../src/render/badge.mjs';1819const overview = {20  commitCount: 1234,21  lastPush: new Date(Date.now() - 3 * 3600_000).toISOString(),22  spark: { counts: [0, 2, 5, 0, 1] },23  languages: [{ name: 'JavaScript', color: '#f1e05a', percent: 71.4 }, { name: 'CSS', color: '#663399', percent: 28.6 }],24  sizeBytes: 5 * 1024 * 1024,25  branchCount: 3,26  tagCount: 0,27  license: 'MIT',28  cloneCount: 42,29};3031describe('badge specs', () => {32  it('covers every documented repository metric', () => {33    for (const metric of REPO_BADGE_METRICS) {34      const spec = repoBadgeSpec(overview, metric);35      expect(spec, metric).toBeTruthy();36      expect(spec.value.length).toBeGreaterThan(0);37      expect(spec.color).toMatch(/^#[0-9a-f]{6}$/);38    }39    expect(repoBadgeSpec(overview, 'stars')).toBeNull();40  });4142  it('formats values from the overview', () => {43    expect(repoBadgeSpec(overview, 'commits').value).toBe('1,234');44    expect(repoBadgeSpec(overview, 'last-commit').value).toBe('3 h ago');45    expect(repoBadgeSpec(overview, 'language')).toMatchObject({ value: 'JavaScript 71%', color: '#f1e05a' });46    expect(repoBadgeSpec(overview, 'size').value).toBe('5.0 MB');47    expect(repoBadgeSpec(overview, 'activity')).toMatchObject({ value: '8 commits in 5 weeks', bars: [0, 2, 5, 0, 1] });48  });4950  it('degrades on an empty repository', () => {51    const empty = { commitCount: 0, lastPush: null, spark: null, languages: [], sizeBytes: 0, branchCount: 0, tagCount: 0, license: null, cloneCount: 0 };52    expect(repoBadgeSpec(empty, 'last-commit').value).toBe('never');53    expect(repoBadgeSpec(empty, 'language').value).toBe('none');54    expect(repoBadgeSpec(empty, 'license').value).toBe('none');55    expect(repoBadgeSpec(empty, 'activity').bars).toHaveLength(26);56  });5758  it('site metrics', () => {59    for (const metric of SITE_BADGE_METRICS) expect(siteBadgeSpec({ repos: 190, commits: 5000, languages: 30 }, metric)).toBeTruthy();60    expect(siteBadgeSpec({ repos: 190, commits: 5000, languages: 30 }, 'commits').value).toBe('5,000');61    expect(siteBadgeSpec({}, 'nope')).toBeNull();62  });63});6465describe('badge options and colours', () => {66  it('accepts only known styles and hex colours', () => {67    const spec = { label: 'commits', value: '1', color: '#3b3fa3' };68    expect(withBadgeOptions(spec, { style: 'for-the-badge', color: 'f00', label: 'x'.repeat(80) }))69      .toMatchObject({ style: 'for-the-badge', color: '#ff0000', label: 'x'.repeat(40) });70    expect(withBadgeOptions(spec, { style: 'plastic"><script>', color: 'red;}' })).toMatchObject({ style: 'flat', color: '#3b3fa3', label: 'commits' });71    expect(withBadgeOptions(spec, { label: ['a', 'b'] }).label).toBe('commits');72  });7374  it('normalizes colours', () => {75    expect(normalizeColor('#ABC')).toBe('#aabbcc');76    expect(normalizeColor('0c8599')).toBe('#0c8599');77    expect(normalizeColor('url(x)')).toBeNull();78  });7980  it('colours freshness from green to red', () => {81    const now = Date.parse('2026-11-17T00:00:00Z');82    expect(freshnessColor('2026-11-16T00:00:00Z', now)).toBe('#2e9e4f');83    expect(freshnessColor('2025-01-01T00:00:00Z', now)).toBe('#c0392b');84    expect(freshnessColor(null, now)).toBe('#8b93a3');85  });8687  it('estimates wider text as wider', () => {88    expect(textWidth('commits')).toBeGreaterThan(textWidth('ill'));89    expect(textWidth('abc', { bold: true })).toBeGreaterThan(textWidth('abc'));90  });91});9293describe('renderBadge', () => {94  it('produces a standalone, escaped SVG in every style', () => {95    for (const style of ['flat', 'flat-square', 'for-the-badge']) {96      const svg = renderBadge({ label: 'a<b', value: '"x"&y', color: '#3b3fa3', style });97      expect(svg.startsWith('<svg xmlns="http://www.w3.org/2000/svg"')).toBe(true);98      expect(svg.endsWith('</svg>')).toBe(true);99      expect(svg).not.toMatch(/<b|"x"&y/);100      expect(svg).toMatch(/&lt;B|&lt;b/);101    }102    expect(renderBadge({ label: 'k', value: 'v', style: 'for-the-badge' })).toContain('height="28"');103  });104105  it('draws one bar per week for the activity chart and uses dark ink on light fills', () => {106    const svg = renderBadge({ label: 'activity', value: '8 commits', color: '#3b3fa3', bars: [0, 2, 5, 0, 1] });107    expect(svg.match(/<rect x="\d+" y="[\d.]+" width="2"/g)).toHaveLength(5);108    expect(renderBadge({ label: 'language', value: 'JavaScript', color: '#f1e05a' })).toContain('fill="#1b1f2a"');109  });110});111
Files main
Menu

Theme