/** * A repository is addressed by name and identified by its record key. The * rows a listing puts in hand carry both, and every screen reads one of them. */ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { account, primeFromCache, repoKeyOf, repoList, repoRecord, spaceOf, } from '../src/lib/git.js'; const DID = 'did:plc:owner'; const HOST = 'pds.example.com'; /** @param {string} name @param {string} rkey @param {string} [space] */ const row = (name, rkey, space) => ({ uri: space ? `${space}/${DID}/dev.pdsjs.git.repo/${rkey}` : `at://${DID}/dev.pdsjs.git.repo/${rkey}`, cid: `bafy${rkey}`, value: { name, refs: [{ name: 'refs/heads/main', sha: 'a'.repeat(40) }], ...(space ? { space } : {}), }, }); /** @param {Array>} rows */ function prime(rows) { const held = new Map([ [ 'git-ui:discovery', JSON.stringify({ host: HOST, did: DID, pds: `https://${HOST}`, handle: 'owner.test', }), ], ['git-ui:repos', JSON.stringify({ host: HOST, rows })], ]); globalThis.localStorage = /** @type {any} */ ({ getItem: (/** @type {string} */ key) => held.get(key) ?? null, setItem: () => {}, }); globalThis.location = /** @type {any} */ ({ hostname: HOST }); return primeFromCache(); } beforeEach(() => { account.did = ''; account.pds = ''; account.handle = ''; }); afterEach(() => { globalThis.localStorage = /** @type {any} */ (undefined); globalThis.location = /** @type {any} */ (undefined); }); describe('the rows a listing puts in hand', () => { it('names a repository by its record, not by its key', () => { const listed = prime([row('demo-app', '3mup7vnsuqc3f')]); expect(listed?.map((repo) => repo.name)).toEqual(['demo-app']); expect(repoList().map((repo) => repo.name)).toEqual(['demo-app']); }); it('finds a repository by the name a page addresses it with', () => { prime([row('demo-app', '3mup7vnsuqc3f')]); expect(repoRecord('demo-app')?.name).toBe('demo-app'); expect(repoRecord('3mup7vnsuqc3f')).toBe(null); }); it('gives the record key the pack reader and the config are at', () => { prime([row('demo-app', '3mup7vnsuqc3f')]); expect(repoKeyOf('demo-app')).toBe('3mup7vnsuqc3f'); }); it('keeps a private repository of the same name apart from a public one', () => { const space = `at://${DID}/space/dev.pdsjs.git.space/secret`; prime([row('demo-app', '3mpublickey'), row('secret', 'secret', space)]); expect(spaceOf('secret')).toBe(space); expect(repoKeyOf('secret', space)).toBe('secret'); expect(repoKeyOf('demo-app')).toBe('3mpublickey'); }); it('falls back to the key for a record carrying no name', () => { const bare = row('demo-app', '3mbarekey'); bare.value = /** @type {any} */ ({ refs: [] }); prime([bare]); expect(repoKeyOf('3mbarekey')).toBe('3mbarekey'); }); });