Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import { beforeEach, describe, expect, it, vi } from 'vitest'
// A plain function rather than vi.fn: Vitest 4 reports a throw from a vi.fn// mock exported by a mocked module as a test failure even when the caller// catches it, which is exactly the path under test here.const state = vi.hoisted(() => ({ calls: [] as string[], impl: (_handle: string): Promise<string> => Promise.reject(new Error('no')),}))vi.mock('./atproto', () => ({ resolveHandleToDid: (handle: string) => { state.calls.push(handle) return state.impl(handle) },}))
const { handleCandidate, resolveSiteAccount, showsAuthorRole, showsSiteProfile } = await import('./site-handle')
beforeEach(() => { state.calls = [] state.impl = () => Promise.reject(new Error('no'))})
describe('handleCandidate', () => { it('accepts a DNS hostname, normalized', () => { expect(handleCandidate('ATPROTO.COM.')).toBe('atproto.com') })
it('rejects things that cannot be handles without asking the network', () => { for (const host of [ 'localhost', 'machine', '127.0.0.1', '[::1]', 'example.local', 'example.invalid', '', ]) { expect(handleCandidate(host), host).toBeUndefined() } })})
describe('resolveSiteAccount', () => { it('resolves the host itself first', async () => { state.impl = async () => 'did:plc:atproto' await expect(resolveSiteAccount('https://atproto.com/some/page?q=1')).resolves.toEqual({ handle: 'atproto.com', did: 'did:plc:atproto', }) expect(state.calls).toEqual(['atproto.com']) })
// `www.` is usually a CNAME, not the account's handle. The apex is the // fallback, and the answer names the handle that actually resolved. it('falls back to the apex behind a www host', async () => { state.impl = async (handle) => { if (handle.startsWith('www.')) throw new Error('not a handle') return 'did:plc:apex' } await expect(resolveSiteAccount('https://www.permadeath.com/')).resolves.toEqual({ handle: 'permadeath.com', did: 'did:plc:apex', }) expect(state.calls).toEqual(['www.permadeath.com', 'permadeath.com']) })
it('is null for an ordinary site and for a failed lookup', async () => { await expect(resolveSiteAccount('https://example.com/')).resolves.toBeNull() })
it('does not ask about a host that cannot be a handle', async () => { await expect(resolveSiteAccount('http://127.0.0.1:8080/')).resolves.toBeNull() expect(state.calls).toEqual([]) })
it('is null for a malformed url', async () => { await expect(resolveSiteAccount('not a url')).resolves.toBeNull() expect(state.calls).toEqual([]) })})
describe('profile roles', () => { const SITE = { handle: 'reader.example.com', did: 'did:plc:reader' }
it('shows the site card when there is no publication', () => { expect(showsSiteProfile(SITE, undefined)).toBe(true) expect(showsAuthorRole(SITE, undefined)).toBe(false) })
// A reader site showing somebody else's publication: the two accounts are // both on screen, so each needs a role label. it('shows and distinguishes both accounts when they differ', () => { expect(showsSiteProfile(SITE, 'did:plc:author')).toBe(true) expect(showsAuthorRole(SITE, 'did:plc:author')).toBe(true) })
// The publication's own home is one account; the owner card already draws // it, so a second card and an "Author" label would only repeat it. it('collapses to the owner card when the page is the publisher', () => { expect(showsSiteProfile(SITE, SITE.did)).toBe(false) expect(showsAuthorRole(SITE, SITE.did)).toBe(false) })
it('shows neither role when the host is not an account', () => { expect(showsSiteProfile(undefined, undefined)).toBe(false) expect(showsAuthorRole(undefined, 'did:plc:author')).toBe(false) })})