Repository Public MIT Selected
1/**2 * ─────────────────────────────────────────────3 * SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 * Author : Simon-Pierre Boucher6 * Contact : contact@spboucher.ai7 * File : test/unit/archive.test.mjs8 * Purpose : Unit tests — archive builds (single build, size ceiling, no leftovers) and transient cache pruning9 * License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */1213import { describe, it, expect, beforeAll, afterAll } from 'vitest';14import { execFileSync } from 'node:child_process';15import { mkdtempSync, rmSync, writeFileSync, readdirSync, existsSync, mkdirSync, utimesSync, statSync } from 'node:fs';16import { tmpdir } from 'node:os';17import { join, dirname } from 'node:path';18import { randomBytes } from 'node:crypto';19import { sendArchive } from '../../src/git/archive.mjs';20import { Cache } from '../../src/lib/cache.mjs';2122let root;23let ctx;24let sha;2526/** Minimal stand-in for a Fastify reply. */27function fakeReply() {28 const r = { status: 200, headers: {}, body: undefined };29 r.code = (c) => { r.status = c; return r; };30 r.header = (k, v) => { r.headers[k.toLowerCase()] = v; return r; };31 r.send = (b) => { r.body = b; b?.destroy?.(); return r; };32 return r;33}3435beforeAll(() => {36 root = mkdtempSync(join(tmpdir(), 'spbgit-archive-'));37 const repo = join(root, 'demo');38 mkdirSync(repo);39 const git = (...args) => execFileSync('git', args, { cwd: repo, stdio: 'pipe' }).toString().trim();40 git('init', '-q');41 writeFileSync(join(repo, 'README.md'), '# demo\n');42 writeFileSync(join(repo, 'noise.bin'), randomBytes(400_000)); // incompressible: the archive is > 400 kB43 git('add', '.');44 git('-c', 'user.name=t', '-c', 'user.email=t@example.org', 'commit', '-q', '-m', 'init');45 sha = git('rev-parse', 'HEAD');46 ctx = { repos: { dir: () => repo }, cache: new Cache(join(root, 'cache')), config: { publicUrl: 'https://git.example.org' } };47});4849afterAll(() => rmSync(root, { recursive: true, force: true }));5051const cacheFiles = () => readdirSync(dirname(ctx.cache.repoPath('demo', sha, 'x')));5253describe('sendArchive', () => {54 it('builds one archive for concurrent requests and leaves no temporary file', async () => {55 const replies = await Promise.all(Array.from({ length: 6 }, () => sendArchive(ctx, 'demo', sha, 'tar.gz', fakeReply(), 'demo-main')));56 for (const r of replies) {57 expect(r.status).toBe(200);58 expect(r.headers['content-length']).toBeGreaterThan(400_000);59 }60 expect(cacheFiles()).toEqual(['archive.tar.gz']);61 });6263 it('refuses an archive above the ceiling, removes the partial file and remembers the verdict', async () => {64 const first = await sendArchive(ctx, 'demo', sha, 'zip', fakeReply(), 'demo-main', { maxBytes: 50_000 });65 expect(first.status).toBe(413);66 expect(first.body.error.code).toBe('archive_too_large');67 expect(first.body.error.message).toContain('git clone https://git.example.org/demo.git');68 expect(cacheFiles().sort()).toEqual(['archive.tar.gz', 'oversize-zip.json']);69 const again = await sendArchive(ctx, 'demo', sha, 'zip', fakeReply(), 'demo-main', { maxBytes: 50_000 });70 expect(again.status).toBe(413);71 expect(cacheFiles().some((f) => f.endsWith('.tmp'))).toBe(false);72 });7374 it('answers 500 and leaves nothing behind when git fails', async () => {75 const bad = 'f'.repeat(40);76 const r = await sendArchive(ctx, 'demo', bad, 'zip', fakeReply(), 'demo-x');77 expect(r.status).toBe(500);78 expect(readdirSync(dirname(ctx.cache.repoPath('demo', bad, 'x')))).toEqual([]);79 });8081 it('rejects unknown formats', async () => {82 expect((await sendArchive(ctx, 'demo', sha, 'rar', fakeReply(), 'demo')).status).toBe(400);83 });84});8586describe('Cache#pruneTransient', () => {87 it('drops abandoned temporary files and archives not served recently, keeps everything else', async () => {88 const cache = new Cache(join(root, 'cache2'));89 const p = (name) => cache.repoPath('r', 'a'.repeat(40), name);90 const now = Date.now();91 const put = (name, ageMs, size = 10) => {92 mkdirSync(dirname(p(name)), { recursive: true });93 writeFileSync(p(name), Buffer.alloc(size));94 const t = new Date(now - ageMs);95 utimesSync(p(name), t, t);96 };97 const H = 3600_000;98 put('archive.zip.deadbeef.tmp', 5 * H, 1000);99 put('archive.zip.cafecafe.tmp', 0.2 * H);100 put('hl-v3-a.json.0badf00d.tmp', 5 * H, 7);101 put('archive.tar.gz', 30 * H, 2000);102 put('archive.zip', 2 * H);103 put('hl-v3-a.json', 300 * H);104 put('oversize-zip.json', 300 * H);105 const res = await cache.pruneTransient('r', { tmpMaxAgeMs: H, archiveMaxAgeMs: 24 * H }, now);106 expect(res).toEqual({ files: 3, bytes: 3007 });107 expect(readdirSync(dirname(p('x'))).sort()).toEqual(['archive.zip', 'archive.zip.cafecafe.tmp', 'hl-v3-a.json', 'oversize-zip.json']);108 expect(await cache.pruneTransient('missing', { tmpMaxAgeMs: H, archiveMaxAgeMs: H }, now)).toEqual({ files: 0, bytes: 0 });109 expect(existsSync(p('archive.zip'))).toBe(true);110 expect(statSync(p('archive.zip')).size).toBe(10);111 });112});113