import { describe, expect, it } from 'vitest' import { articleContributors, articleDetails, articleMetaLine, articleTags, formatDate, isoDate, readingMinutes, } from './article' import type { DocumentRecord } from './types' const base: DocumentRecord = { $type: 'site.standard.document', site: 'at://did:plc:publisher/site.standard.publication/self', title: 'A post', publishedAt: '2026-08-20T04:00:00.000Z', } const words = (n: number) => Array.from({ length: n }, () => 'word').join(' ') describe('isoDate', () => { it('normalizes a datetime', () => { expect(isoDate('2026-08-20T04:00:00.000Z')).toBe('2026-08-20T04:00:00.000Z') }) it('rejects anything that is not a date', () => { expect(isoDate('not a date')).toBeUndefined() expect(isoDate(42)).toBeUndefined() expect(isoDate(undefined)).toBeUndefined() }) }) describe('readingMinutes', () => { it('rounds up to whole minutes', () => { expect(readingMinutes(words(332))).toBe(2) expect(readingMinutes(words(103))).toBe(1) }) it('stays silent for a body too short to estimate', () => { expect(readingMinutes(words(49))).toBeUndefined() expect(readingMinutes('')).toBeUndefined() expect(readingMinutes(undefined)).toBeUndefined() }) }) describe('articleTags', () => { it('trims, strips a leading hash and deduplicates', () => { expect(articleTags({ tags: [' atproto ', '#spaces', 'atproto', ''] })).toEqual([ 'atproto', 'spaces', ]) }) it('drops entries that are not short strings', () => { expect(articleTags({ tags: ['ok', 42, 'x'.repeat(65)] })).toEqual(['ok']) }) it('caps the list', () => { expect(articleTags({ tags: Array.from({ length: 12 }, (_, i) => `tag${i}`) })).toHaveLength( 8, ) }) it('is empty for a record without tags', () => { expect(articleTags({ tags: undefined })).toEqual([]) expect(articleTags({ tags: 'atproto' })).toEqual([]) }) }) describe('articleContributors', () => { it('names a contributor by display name, or by DID when there is none', () => { expect( articleContributors({ contributors: [ { did: 'did:plc:alice', displayName: 'Alice' }, { did: 'did:plc:bob' }, ], }), ).toEqual([ { did: 'did:plc:alice', name: 'Alice' }, { did: 'did:plc:bob', name: 'did:plc:bob' }, ]) }) it('keeps a role and drops entries without a DID', () => { expect( articleContributors({ contributors: [{ did: 'did:plc:alice', displayName: 'Alice', role: 'editor' }, {}], }), ).toEqual([{ did: 'did:plc:alice', name: 'Alice', role: 'editor' }]) }) it('caps the list', () => { expect( articleContributors({ contributors: Array.from({ length: 12 }, (_, i) => ({ did: `did:plc:${i}` })), }), ).toHaveLength(8) }) }) describe('articleDetails', () => { it('carries the fields a record actually has', () => { expect( articleDetails({ ...base, updatedAt: '2026-08-21T00:00:00.000Z', textContent: words(500), tags: ['atproto'], contributors: [{ did: 'did:plc:alice', displayName: 'Alice' }], }), ).toEqual({ publishedAt: '2026-08-20T04:00:00.000Z', updatedAt: '2026-08-21T00:00:00.000Z', readingMinutes: 3, tags: ['atproto'], contributors: [{ did: 'did:plc:alice', name: 'Alice' }], }) }) it('does not call an unchanged or older timestamp an update', () => { expect(articleDetails({ ...base, updatedAt: base.publishedAt }).updatedAt).toBeUndefined() expect(articleDetails({ ...base, updatedAt: '2026-01-01T00:00:00.000Z' }).updatedAt).toBeUndefined() }) }) describe('formatDate', () => { it('formats the date the record chose, in UTC', () => { expect(formatDate('2026-08-20T04:00:00.000Z', 'en-US')).toBe('Aug 20, 2026') }) }) describe('articleMetaLine', () => { it('prefers the update date and adds reading time and contributors', () => { expect( articleMetaLine( { publishedAt: '2026-08-20T04:00:00.000Z', updatedAt: '2026-08-21T00:00:00.000Z', readingMinutes: 3, tags: [], contributors: [{ did: 'did:plc:alice', name: 'Alice' }], }, 'en-US', ), ).toBe('Updated Aug 21, 2026 · 3 min read · with Alice') }) it('falls back to the publish date', () => { expect( articleMetaLine({ publishedAt: '2026-08-20T04:00:00.000Z', tags: [], contributors: [] }, 'en-US'), ).toBe('Published Aug 20, 2026') }) it('joins three names and counts the rest', () => { expect( articleMetaLine( { tags: [], contributors: [ { did: 'did:plc:a', name: 'A' }, { did: 'did:plc:b', name: 'B' }, { did: 'did:plc:c', name: 'C' }, { did: 'did:plc:d', name: 'D' }, ], }, 'en-US', ), ).toBe('with A, B, C and 1 more') }) it('is empty when the record has nothing to say', () => { expect(articleMetaLine({ tags: [], contributors: [] }, 'en-US')).toBe('') }) })