import { describe, expect, it } from 'vitest' import { type PublicationSummary, type SubscriptionRow, filterSubscriptions, formatSubscriptionDate, hostOf, parseSubscription, publicationFallback, safeHttpUrl, sortSubscriptions, subscriptionStats, } from './subscription-list' const collection = 'site.standard.graph.subscription' const record = (rkey: string, value: unknown, collectionName = collection) => ({ uri: `at://did:plc:me/${collectionName}/${rkey}`, value, }) describe('parseSubscription', () => { it('keeps the rkey, publication and a valid createdAt', () => { expect( parseSubscription( record('3kabc', { $type: collection, publication: 'at://did:plc:pub/site.standard.publication/self', createdAt: '2026-02-03T04:05:06.000Z', }), ), ).toEqual({ uri: `at://did:plc:me/${collection}/3kabc`, rkey: '3kabc', publication: 'at://did:plc:pub/site.standard.publication/self', createdAt: '2026-02-03T04:05:06.000Z', }) }) it('drops a createdAt that does not parse but keeps the subscription', () => { expect( parseSubscription( record('3kabc', { publication: 'at://did:plc:pub/site.standard.publication/self', createdAt: 'not a date', }), )?.createdAt, ).toBeUndefined() }) it('rejects a record with no publication, or a publication that is not an at-uri', () => { expect(parseSubscription(record('a', {}))).toBeNull() expect(parseSubscription(record('b', { publication: 'https://example.com' }))).toBeNull() expect( parseSubscription(record('c', { publication: 'at://did:plc:pub/wrong/self' })), ).toBeNull() }) it('rejects the wrong collection or a malformed record uri', () => { expect( parseSubscription( record('a', { publication: 'at://did:plc:pub/site.standard.publication/self' }, 'other'), ), ).toBeNull() expect( parseSubscription({ uri: 'not-an-at-uri', value: { publication: 'at://did:plc:pub/site.standard.publication/self' }, }), ).toBeNull() }) }) const row = ( rkey: string, pub: Partial | undefined, createdAt?: string, ): SubscriptionRow => ({ uri: `at://did:plc:me/${collection}/${rkey}`, rkey, publication: `at://did:plc:${rkey}/site.standard.publication/self`, createdAt, pub: pub ? { uri: `at://did:plc:${rkey}/site.standard.publication/self`, name: pub.name ?? rkey, ...pub, } : undefined, }) describe('filterSubscriptions', () => { const rows = [ row('one', { name: 'Atproto Notes', description: 'Protocol writing', url: 'https://atproto.example.com/blog', host: 'atproto.example.com', }), row('two', { name: 'Standard Site', description: 'The publication network', url: 'https://standard.site', host: 'standard.site', }), ] it('matches case-insensitively across name, description, URL and host', () => { expect(filterSubscriptions(rows, 'atproto').map((r) => r.rkey)).toEqual(['one']) expect(filterSubscriptions(rows, 'PROTOCOL').map((r) => r.rkey)).toEqual(['one']) expect(filterSubscriptions(rows, 'standard.site').map((r) => r.rkey)).toEqual(['two']) expect(filterSubscriptions(rows, 'example.com/blog').map((r) => r.rkey)).toEqual(['one']) }) it('requires every whitespace-separated term', () => { expect(filterSubscriptions(rows, 'protocol writing').map((r) => r.rkey)).toEqual(['one']) expect(filterSubscriptions(rows, 'protocol standard')).toEqual([]) }) it('returns every row for an empty query and none for a miss', () => { expect(filterSubscriptions(rows, ' ')).toHaveLength(2) expect(filterSubscriptions(rows, 'nobody')).toEqual([]) }) }) describe('sortSubscriptions', () => { const rows = [ row('b', { name: 'Beta', host: 'z.example.com' }, '2026-01-02T00:00:00Z'), row('a', { name: 'Alpha', host: 'a.example.com' }, '2026-03-02T00:00:00Z'), row('c', { name: 'Gamma', host: 'm.example.com' }, '2026-02-02T00:00:00Z'), row('d', undefined, undefined), ] it('sorts by name and leaves the input alone', () => { const input = [...rows] expect(sortSubscriptions(rows, 'name').map((r) => r.rkey)).toEqual(['a', 'b', 'c', 'd']) expect(rows).toEqual(input) }) it('sorts by host and puts rows with no host last', () => { expect(sortSubscriptions(rows, 'site').map((r) => r.rkey)).toEqual(['a', 'c', 'b', 'd']) }) it('sorts newest and oldest, with rows that have no date last in both', () => { expect(sortSubscriptions(rows, 'newest').map((r) => r.rkey)).toEqual(['a', 'c', 'b', 'd']) expect(sortSubscriptions(rows, 'oldest').map((r) => r.rkey)).toEqual(['b', 'c', 'a', 'd']) }) it('breaks ties by name, then by uri', () => { const tied = [ row('z', { name: 'Same' }, '2026-01-01T00:00:00Z'), row('y', { name: 'Same' }, '2026-01-01T00:00:00Z'), ] expect(sortSubscriptions(tied, 'newest').map((r) => r.publication)).toEqual([ tied[1]!.publication, tied[0]!.publication, ]) }) }) describe('subscriptionStats', () => { it('counts distinct hosts and finds the newest dated subscription', () => { const stats = subscriptionStats([ row('a', { name: 'A', host: 'one.example.com' }, '2026-01-01T00:00:00Z'), row('b', { name: 'B', host: 'one.example.com' }, '2026-04-01T00:00:00Z'), row('c', { name: 'C', host: 'two.example.com' }, '2026-02-01T00:00:00Z'), row('d', { name: 'D' }, undefined), ]) expect(stats).toEqual({ total: 4, hosts: 2, newestAt: '2026-04-01T00:00:00Z', }) }) it('reports nothing newest when no record carries a usable date', () => { expect(subscriptionStats([row('a', { name: 'A' }), row('b', undefined, 'nope')])).toEqual({ total: 2, hosts: 0, newestAt: undefined, }) }) }) describe('URL helpers', () => { it('accepts only http(s) URLs', () => { expect(safeHttpUrl('https://example.com/a?b=1#c')).toBe('https://example.com/a?b=1#c') expect(safeHttpUrl('http://example.com')).toBe('http://example.com/') expect(safeHttpUrl('javascript:alert(1)')).toBeUndefined() expect(safeHttpUrl('data:text/html,hi')).toBeUndefined() expect(safeHttpUrl('ftp://example.com')).toBeUndefined() expect(safeHttpUrl(42)).toBeUndefined() }) it('reads the host, and nothing for a bad URL', () => { expect(hostOf('https://blog.example.com/a')).toBe('blog.example.com') expect(hostOf(undefined)).toBeUndefined() expect(hostOf('not a url')).toBeUndefined() }) it('falls back to the publication DID', () => { expect(publicationFallback('at://did:plc:abc/site.standard.publication/self')).toBe( 'did:plc:abc', ) expect(publicationFallback('nonsense')).toBe('nonsense') }) }) describe('formatSubscriptionDate', () => { it('formats a valid date and leaves invalid or absent ones empty', () => { expect(formatSubscriptionDate('2026-02-03T04:05:06Z')).not.toBe('') expect(formatSubscriptionDate('not a date')).toBe('') expect(formatSubscriptionDate(undefined)).toBe('') }) })