import { describe, expect, it } from 'vitest' import { DESK_SCAN, buildDesk } from './desk' import type { DocumentRecord, PubInfo } from './types' const pub: PubInfo = { uri: 'at://did:plc:publisher/site.standard.publication/self', did: 'did:plc:publisher', handle: 'pub.example.com', pds: 'https://pds.example.com', record: { $type: 'site.standard.publication', url: 'https://pub.example.com/blog', name: 'A publication', }, verified: true, } const words = (n: number) => Array.from({ length: n }, () => 'word').join(' ') function doc( rkey: string, value: Partial & { title?: string } = {}, ): { uri: string; value: DocumentRecord } { return { uri: `at://did:plc:publisher/site.standard.document/${rkey}`, value: { $type: 'site.standard.document', site: pub.uri, title: rkey, publishedAt: '2026-01-01T00:00:00.000Z', ...value, } as DocumentRecord, } } describe('buildDesk', () => { it('tallies tags and averages reading time', () => { const data = buildDesk( [ doc('old', { publishedAt: '2025-01-01T00:00:00.000Z', tags: ['old'] }), doc('new', { publishedAt: '2026-02-01T00:00:00.000Z', tags: ['atproto', 'atproto'], textContent: words(500), }), doc('middle', { publishedAt: '2026-01-15T00:00:00.000Z', tags: ['atproto', 'design'], textContent: words(200), }), ], { subscribers: 54, posts: 11 }, ) expect(data.averageMinutes).toBe(2) expect(data).toMatchObject({ subscribers: 54, posts: 11, scanned: 3, truncated: false }) }) it('leaves an unread count null instead of inventing a zero', () => { const data = buildDesk([doc('one')], { subscribers: null, posts: null }) expect(data.subscribers).toBeNull() expect(data.posts).toBeNull() }) it('drops records with no usable title', () => { const data = buildDesk([doc('ok'), doc('bad', { title: ' ' })], { subscribers: 0, posts: 2, }) expect(data.scanned).toBe(2) }) it('marks a scan that filled its cap as truncated', () => { const records = Array.from({ length: DESK_SCAN }, (_, i) => doc(`doc${i}`)) const data = buildDesk(records, { subscribers: 1, posts: 1 }) expect(data.truncated).toBe(true) }) })