SPB Git

Repository Public MIT Selected

routes-helpers.test.mjs 4.8 KB 122 lines javascript

1/**2 * ─────────────────────────────────────────────3 *  SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 *  Author  : Simon-Pierre Boucher6 *  Contact : contact@spboucher.ai7 *  File    : test/unit/routes-helpers.test.mjs8 *  Purpose : Unit tests — split-diff row pairing, day grouping, research detection, search terms9 *  License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */1213import { describe, it, expect } from 'vitest';14import { splitRows, groupByDay, latestCommit } from '../../src/web/routes.mjs';15import { detectResearchSignals, sparkline } from '../../src/lib/overview.mjs';16import { queryTerms } from '../../src/lib/search.mjs';17import { sameStamps } from '../../src/stats/activity.mjs';18import { Cache } from '../../src/lib/cache.mjs';19import { mkdtempSync, rmSync } from 'node:fs';20import { tmpdir } from 'node:os';21import { join } from 'node:path';2223describe('splitRows', () => {24  it('pairs deletions with additions and keeps context on both sides', () => {25    const lines = [26      { type: 'ctx', text: 'a' },27      { type: 'del', text: 'b' },28      { type: 'del', text: 'c' },29      { type: 'add', text: 'B' },30      { type: 'ctx', text: 'd' },31      { type: 'add', text: 'E' },32      { type: 'meta', text: '\\ No newline' },33    ];34    const rows = splitRows(lines);35    expect(rows).toHaveLength(6);36    expect(rows[0]).toEqual({ left: lines[0], right: lines[0] });37    expect(rows[1]).toEqual({ left: lines[1], right: lines[3] });38    expect(rows[2]).toEqual({ left: lines[2], right: null });39    expect(rows[3]).toEqual({ left: lines[4], right: lines[4] });40    expect(rows[4]).toEqual({ left: null, right: lines[5] });41    expect(rows[5]).toEqual({ left: lines[6], right: null });42  });43});4445describe('groupByDay', () => {46  it('groups on the author calendar day, preserving order', () => {47    const days = groupByDay([48      { date: '2026-11-01T23:10:00-04:00', sha: '1' },49      { date: '2026-11-01T08:00:00-04:00', sha: '2' },50      { date: '2026-10-31T23:59:00-04:00', sha: '3' },51    ]);52    expect(days.map((d) => d.iso)).toEqual(['2026-11-01', '2026-10-31']);53    expect(days[0].commits).toHaveLength(2);54    expect(days[0].label).toMatch(/Nov 1, 2026/);55  });56});5758describe('detectResearchSignals', () => {59  it('flags a LaTeX paper with bibliography and figures', () => {60    const r = detectResearchSignals(['paper/main.tex', 'paper/refs.bib', 'paper/main.pdf', 'figures/fig1.png', 'src/model.py']);61    expect(r.likely).toBe(true);62    expect(r.figures).toBe(1);63  });64  it('does not flag an ordinary web app', () => {65    expect(detectResearchSignals(['src/index.ts', 'README.md', 'docs/guide.pdf']).likely).toBe(false);66  });67});6869describe('sparkline', () => {70  it('buckets the last 26 weeks and ignores older commits', () => {71    const now = 1_700_000_000;72    const s = sparkline([now - 100, now - 8 * 86400, now - 200 * 86400], now);73    expect(s.total).toBe(2);74    expect(s.counts[25]).toBe(1);75    expect(s.counts[24]).toBe(1);76    expect(s.path.startsWith('M')).toBe(true);77  });78});7980describe('queryTerms / sameStamps', () => {81  it('normalizes queries', () => {82    expect(queryTerms('  Git  Forge a x ')).toEqual(['git', 'forge']);83    expect(queryTerms('')).toEqual([]);84  });85  it('compares stamp maps', () => {86    expect(sameStamps({ a: '1' }, { a: '1' })).toBe(true);87    expect(sameStamps({ a: '1' }, { a: '2' })).toBe(false);88    expect(sameStamps({ a: '1' }, { a: '1', b: '2' })).toBe(false);89    expect(sameStamps(null, {})).toBe(false);90  });91});9293describe('latestCommit', () => {94  it('returns the most recent of the per-entry last commits', () => {95    const latest = latestCommit({96      a: { sha: '1', date: '2026-11-01T10:00:00+00:00', subject: 'old' },97      b: { sha: '2', date: '2026-11-09T08:00:00+00:00', subject: 'new' },98      c: { sha: '3', date: '2026-10-01T10:00:00+00:00', subject: 'older' },99    });100    expect(latest.sha).toBe('2');101  });102  it('is null for an empty or missing map', () => {103    expect(latestCommit({})).toBeNull();104    expect(latestCommit(undefined)).toBeNull();105  });106});107108describe('Cache.footprintAsync', () => {109  it('matches the synchronous footprint', async () => {110    const dir = mkdtempSync(join(tmpdir(), 'spbgit-cache-'));111    try {112      const cache = new Cache(dir);113      cache.set(cache.path('global', 'a.txt'), 'hello');114      cache.set(cache.repoPath('demo', 'abc123', 'b.json'), '{"x":1}');115      expect(await cache.footprintAsync()).toEqual(cache.footprint());116      expect((await cache.footprintAsync()).files).toBe(2);117    } finally {118      rmSync(dir, { recursive: true, force: true });119    }120  });121});122
Files main
Menu

Theme