OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git-v3 / test / push-index.test.js

51 lines · javascript ref:
/**
 * push-index: record attribution, list ordering/dedup, disk persistence +
 * restart survival. Powers the public profile "Repos pushed" list.
 */

import test from 'brittle'
import { mkdtemp, rm, mkdir, readFile } from '#node:fs/promises'
import { tmpdir } from '#node:os'
import { join } from '#node:path'

function fresh (tag) { return import('../lib/services/push-index.js?fresh=' + tag) }

test('push-index: record, dedup/order, persistence + restart', async (t) => {
  const root = await mkdtemp(join(tmpdir(), 'pi-'))
  const storage = join(root, 's')
  await mkdir(storage, { recursive: true })
  t.teardown(() => rm(root, { recursive: true, force: true }))

  const m = await fresh('1')
  await m.configurePushIndex(storage)
  m.recordPush('alice', 'repo-a', 'sha1', 1000)
  m.recordPush('alice', 'repo-b', 'sha2', 3000)
  m.recordPush('alice', 'repo-a', 'sha3', 5000) // newer push to repo-a
  m.recordPush('bob', 'repo-c', 'sha4', 2000)
  m.recordPush(null, 'repo-x') // unattributable → ignored
  m.recordPush('carol', null) // no repo → ignored

  const alice = m.listUserPushes('alice')
  t.is(alice.length, 2, 'alice pushed 2 distinct repos')
  t.is(alice[0].repo, 'repo-a', 'most-recent push first')
  t.is(alice[0].count, 2, 'repo-a push counted twice')
  t.is(alice[0].lastSha, 'sha3', 'lastSha updated to newest')
  t.is(alice[0].firstPush, 1000, 'firstPush preserved')
  t.is(alice[0].lastPush, 5000, 'lastPush updated')
  t.is(m.listUserPushes('bob').length, 1, 'bob has 1 repo')
  t.is(m.listUserPushes('nobody').length, 0, 'unknown user → empty')
  t.is(m.listUserPushes('carol').length, 0, 'null-repo push was ignored')

  await m.flushPushIndex()
  const disk = JSON.parse(await readFile(join(storage, 'push-index.json'), 'utf8'))
  t.ok(disk.users.alice && disk.users.alice['repo-a'], 'persisted to disk')

  // Restart: a fresh module (empty in-memory) must load the disk index.
  const m2 = await fresh('2')
  await m2.configurePushIndex(storage)
  const alice2 = m2.listUserPushes('alice')
  t.is(alice2.length, 2, 'survived restart')
  t.is(alice2[0].repo, 'repo-a', 'order preserved after restart')
  t.is(alice2[0].count, 2, 'count preserved after restart')
})