Sifa professional network API (Fastify, AT Protocol, Jetstream) sifa.id/
at main 79 lines 2.6 kB view raw
1import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; 2import { verifyRelMe, isVerifiablePlatform } from '../../src/services/verification.js'; 3 4const mockFetch = vi.fn(); 5 6beforeEach(() => { 7 vi.stubGlobal('fetch', mockFetch); 8}); 9 10afterEach(() => { 11 vi.unstubAllGlobals(); 12}); 13 14describe('isVerifiablePlatform', () => { 15 it('returns true for verifiable platforms', () => { 16 expect(isVerifiablePlatform('website')).toBe(true); 17 expect(isVerifiablePlatform('fediverse')).toBe(true); 18 expect(isVerifiablePlatform('rss')).toBe(true); 19 expect(isVerifiablePlatform('github')).toBe(true); 20 }); 21 22 it('returns false for non-verifiable platforms', () => { 23 expect(isVerifiablePlatform('instagram')).toBe(false); 24 expect(isVerifiablePlatform('twitter')).toBe(false); 25 expect(isVerifiablePlatform('other')).toBe(false); 26 }); 27}); 28 29describe('verifyRelMe', () => { 30 it('returns true when rel=me link points to Sifa profile', async () => { 31 mockFetch.mockResolvedValue({ 32 ok: true, 33 text: () => 34 Promise.resolve( 35 '<html><body><a href="https://sifa.id/p/alice" rel="me">Sifa</a></body></html>', 36 ), 37 }); 38 39 const result = await verifyRelMe('https://example.com', 'sifa.id/p/alice', 'did:plc:alice'); 40 expect(result).toBe(true); 41 }); 42 43 it('returns true when rel=me link points to DID', async () => { 44 mockFetch.mockResolvedValue({ 45 ok: true, 46 text: () => 47 Promise.resolve( 48 '<html><head><link rel="me" href="https://did:plc:alice123" /></head></html>', 49 ), 50 }); 51 52 const result = await verifyRelMe('https://example.com', 'sifa.id/p/alice', 'did:plc:alice123'); 53 expect(result).toBe(true); 54 }); 55 56 it('returns false when no rel=me link found', async () => { 57 mockFetch.mockResolvedValue({ 58 ok: true, 59 text: () => Promise.resolve('<html><body><a href="https://other.com">Link</a></body></html>'), 60 }); 61 62 const result = await verifyRelMe('https://example.com', 'sifa.id/p/alice', 'did:plc:alice'); 63 expect(result).toBe(false); 64 }); 65 66 it('returns false on fetch error', async () => { 67 mockFetch.mockRejectedValue(new Error('Network error')); 68 69 const result = await verifyRelMe('https://example.com', 'sifa.id/p/alice', 'did:plc:alice'); 70 expect(result).toBe(false); 71 }); 72 73 it('returns false on non-ok response', async () => { 74 mockFetch.mockResolvedValue({ ok: false }); 75 76 const result = await verifyRelMe('https://example.com', 'sifa.id/p/alice', 'did:plc:alice'); 77 expect(result).toBe(false); 78 }); 79});