import { describe, expect, it } from 'vitest' import { profileHandleFromUrl } from './profile-url' describe('profileHandleFromUrl', () => { it('reads the handle from a profile URL', () => { expect(profileHandleFromUrl('https://bsky.app/profile/alice.example.com')).toBe( 'alice.example.com', ) }) it('accepts www.bsky.app', () => { expect(profileHandleFromUrl('https://www.bsky.app/profile/alice.example.com')).toBe( 'alice.example.com', ) }) it('reads a DID without decoding its colons', () => { expect(profileHandleFromUrl('https://bsky.app/profile/did:plc:abc123')).toBe( 'did:plc:abc123', ) }) it('still names the profile from a post path', () => { expect( profileHandleFromUrl('https://bsky.app/profile/alice.example.com/post/3kabc'), ).toBe('alice.example.com') }) it('tolerates a trailing slash, query and fragment', () => { expect(profileHandleFromUrl('https://bsky.app/profile/alice.example.com/')).toBe( 'alice.example.com', ) expect( profileHandleFromUrl('https://bsky.app/profile/alice.example.com?tab=posts#top'), ).toBe('alice.example.com') }) it('decodes a percent-encoded handle', () => { expect(profileHandleFromUrl('https://bsky.app/profile/alice%2Eexample.com')).toBe( 'alice.example.com', ) }) it('is undefined on every other host', () => { for (const url of [ 'https://example.com/profile/alice.example.com', 'https://notbsky.app/profile/alice.example.com', 'https://bsky.app.example.com/profile/alice.example.com', 'https://bsky.app:8443/profile/alice.example.com', ]) { expect(profileHandleFromUrl(url), url).toBeUndefined() } }) it('is undefined on other bluesky routes', () => { for (const url of [ 'https://bsky.app/', 'https://bsky.app/settings', 'https://bsky.app/search?q=alice', 'https://bsky.app/profile', 'https://bsky.app/profile/', ]) { expect(profileHandleFromUrl(url), url).toBeUndefined() } }) it('is undefined for reserved profile segments', () => { for (const segment of ['settings', 'search', 'notifications', 'profile', 'terms']) { expect( profileHandleFromUrl(`https://bsky.app/profile/${segment}`), segment, ).toBeUndefined() } }) it('is undefined for malformed input', () => { for (const url of [ '', 'not a url', 'https://bsky.app/profile/%E0%A4%A', 'https://bsky.app/profile/alice%2Fexample.com', 'https://bsky.app/profile/alice%20example.com', 'ftp://bsky.app/profile/alice.example.com', ]) { expect(profileHandleFromUrl(url), url).toBeUndefined() } }) })