Something went wrong. Try again.
WIP: A simple cli for daily tangled use cases and AI integration. This is for my personal use right now, but happy if others get mileage fro
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { resolveHandleToDid, resolvePdsFromIdentifier } from '../../src/utils/pds-resolver.js';
const PLC_DID = 'did:plc:abc123';const WEB_DID = 'did:web:example.com';const PDS_URL = 'https://pds.example.com';
const mockDidDoc = { id: PLC_DID, service: [ { id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: PDS_URL, }, ],};
vi.mock('@atproto/identity', () => { const mockResolveHandle = vi.fn(); const mockResolveNoCheck = vi.fn();
return { IdResolver: vi.fn().mockImplementation(() => ({ handle: { resolve: mockResolveHandle }, did: { resolveNoCheck: mockResolveNoCheck }, })), ensureAtpDocument: vi .fn() .mockImplementation((doc: { service?: Array<{ id: string; serviceEndpoint: string }> }) => { const pds = doc?.service?.find((s) => s.id === '#atproto_pds')?.serviceEndpoint; if (!pds) throw new Error('No PDS in document'); return { pds }; }), _mockResolveHandle: mockResolveHandle, _mockResolveNoCheck: mockResolveNoCheck, };});
async function getMocks() { const mod = await import('@atproto/identity'); const m = mod as unknown as { _mockResolveHandle: ReturnType<typeof vi.fn>; _mockResolveNoCheck: ReturnType<typeof vi.fn>; }; return { resolveHandle: m._mockResolveHandle, resolveNoCheck: m._mockResolveNoCheck };}
describe('resolvePdsFromIdentifier', () => { beforeEach(() => { vi.clearAllMocks(); });
afterEach(() => { vi.clearAllMocks(); });
describe('with a handle', () => { it('resolves handle to DID then returns PDS from DID document', async () => { const { resolveHandle, resolveNoCheck } = await getMocks(); resolveHandle.mockResolvedValue(PLC_DID); resolveNoCheck.mockResolvedValue(mockDidDoc);
const result = await resolvePdsFromIdentifier('user.tngl.sh');
expect(resolveHandle).toHaveBeenCalledWith('user.tngl.sh'); expect(resolveNoCheck).toHaveBeenCalledWith(PLC_DID); expect(result).toBe(PDS_URL); });
it('throws when handle cannot be resolved', async () => { const { resolveHandle } = await getMocks(); resolveHandle.mockResolvedValue(undefined);
await expect(resolvePdsFromIdentifier('unknown.tngl.sh')).rejects.toThrow( /Could not resolve handle/ ); });
it('throws when handle resolver throws', async () => { const { resolveHandle } = await getMocks(); resolveHandle.mockRejectedValue(new Error('DNS timeout'));
await expect(resolvePdsFromIdentifier('user.tngl.sh')).rejects.toThrow('DNS timeout'); }); });
describe('with a did:plc DID', () => { it('skips handle resolution and returns PDS from DID document', async () => { const { resolveHandle, resolveNoCheck } = await getMocks(); resolveNoCheck.mockResolvedValue(mockDidDoc);
const result = await resolvePdsFromIdentifier(PLC_DID);
expect(resolveHandle).not.toHaveBeenCalled(); expect(resolveNoCheck).toHaveBeenCalledWith(PLC_DID); expect(result).toBe(PDS_URL); });
it('throws when DID document has no PDS service entry', async () => { const { resolveNoCheck } = await getMocks(); resolveNoCheck.mockResolvedValue({ id: PLC_DID, service: [] });
await expect(resolvePdsFromIdentifier(PLC_DID)).rejects.toThrow(/No PDS/); });
it('throws when DID document resolver throws', async () => { const { resolveNoCheck } = await getMocks(); resolveNoCheck.mockRejectedValue(new Error('plc.directory unavailable'));
await expect(resolvePdsFromIdentifier(PLC_DID)).rejects.toThrow('plc.directory unavailable'); }); });
describe('with a did:web DID', () => { it('skips handle resolution and returns PDS from DID document', async () => { const { resolveHandle, resolveNoCheck } = await getMocks(); const webDidDoc = { ...mockDidDoc, id: WEB_DID }; resolveNoCheck.mockResolvedValue(webDidDoc);
const result = await resolvePdsFromIdentifier(WEB_DID);
expect(resolveHandle).not.toHaveBeenCalled(); expect(result).toBe(PDS_URL); }); });});
describe('resolveHandleToDid', () => { beforeEach(() => { vi.clearAllMocks(); });
afterEach(() => { vi.clearAllMocks(); });
it('should resolve handle to DID', async () => { const { resolveHandle } = await getMocks(); resolveHandle.mockResolvedValue(PLC_DID);
const result = await resolveHandleToDid('mark.bsky.social');
expect(result).toBe(PLC_DID); expect(resolveHandle).toHaveBeenCalledWith('mark.bsky.social'); });
it('should strip leading @ from handle', async () => { const { resolveHandle } = await getMocks(); resolveHandle.mockResolvedValue(PLC_DID);
await resolveHandleToDid('@mark.bsky.social');
expect(resolveHandle).toHaveBeenCalledWith('mark.bsky.social'); });
it('should throw error when handle resolves to nothing', async () => { const { resolveHandle } = await getMocks(); resolveHandle.mockResolvedValue(undefined);
await expect(resolveHandleToDid('nonexistent.bsky.social')).rejects.toThrow( 'No DID found for handle: nonexistent.bsky.social' ); });
it('should throw error on resolution failure', async () => { const { resolveHandle } = await getMocks(); resolveHandle.mockRejectedValue(new Error('Network error'));
await expect(resolveHandleToDid('mark.bsky.social')).rejects.toThrow( "Failed to resolve handle 'mark.bsky.social': Network error" ); });});