import { describe, expect, it, vi } from 'vitest'; import { createTid } from '../src/repo.js'; import { createSpaceBrowser } from '../src/space-browser.js'; const SPACE = 'at://did:plc:authority/social.grain.space/3lgrainspace'; /** @param {Array<{collection: string, rkey: string}>} records */ function browserOver(records) { return createSpaceBrowser( /** @type {any} */ ({ async listAllSpaceRecords() { return records.map((record) => ({ ...record, cid: 'bafyrecord' })); }, }), ); } /** @param {string} iso */ function tidAt(iso) { vi.useFakeTimers(); vi.setSystemTime(new Date(iso)); const tid = createTid(); vi.useRealTimers(); return tid; } describe('space browser countRecords', () => { it('reports the newest write per collection', async () => { // Minted in time order: createTid is monotonic, so a TID asked for an // earlier time than the last one comes back with the last one's time. const gallery = tidAt('2026-05-02'); const firstPhoto = tidAt('2026-06-01'); const middlePhoto = tidAt('2026-07-04'); const newestPhoto = tidAt('2026-08-09'); const browser = browserOver([ { collection: 'social.grain.photo', rkey: firstPhoto }, { collection: 'social.grain.photo', rkey: newestPhoto }, { collection: 'social.grain.photo', rkey: middlePhoto }, { collection: 'social.grain.gallery', rkey: gallery }, ]); expect(await browser.countRecords(SPACE)).toEqual([ { collection: 'social.grain.gallery', count: 1, lastRecordAt: Date.parse('2026-05-02'), }, { collection: 'social.grain.photo', count: 3, lastRecordAt: Date.parse('2026-08-09'), }, ]); }); it('counts a record whose rkey is not a TID, and reads no time from it', async () => { const browser = browserOver([ { collection: 'social.grain.profile', rkey: 'self' }, ]); expect(await browser.countRecords(SPACE)).toEqual([ { collection: 'social.grain.profile', count: 1, lastRecordAt: null }, ]); }); it('returns nothing for a space that holds no records', async () => { expect(await browserOver([]).countRecords(SPACE)).toEqual([]); }); });