Barazo AppView backend barazo.forum
at main 139 lines 4.1 kB view raw
1import { describe, it, expect } from 'vitest' 2import { 3 resolveProfile, 4 type SourceProfile, 5 type CommunityOverride, 6} from '../../../src/lib/resolve-profile.js' 7 8// --------------------------------------------------------------------------- 9// resolveProfile 10// --------------------------------------------------------------------------- 11 12const baseSource: SourceProfile = { 13 did: 'did:plc:abc123', 14 handle: 'jay.bsky.team', 15 displayName: 'Jay', 16 avatarUrl: 'https://cdn.example.com/avatar.jpg', 17 bannerUrl: 'https://cdn.example.com/banner.jpg', 18 bio: 'Hello from the AT Protocol', 19} 20 21describe('resolveProfile', () => { 22 it('returns source profile values when override is null', () => { 23 const result = resolveProfile(baseSource, null) 24 25 expect(result).toEqual({ 26 did: 'did:plc:abc123', 27 handle: 'jay.bsky.team', 28 displayName: 'Jay', 29 avatarUrl: 'https://cdn.example.com/avatar.jpg', 30 bannerUrl: 'https://cdn.example.com/banner.jpg', 31 bio: 'Hello from the AT Protocol', 32 }) 33 }) 34 35 it('uses all override values when every field is set', () => { 36 const override: CommunityOverride = { 37 displayName: 'Jay in Wonderland', 38 avatarUrl: 'https://cdn.example.com/community-avatar.jpg', 39 bannerUrl: 'https://cdn.example.com/community-banner.jpg', 40 bio: 'Community-specific bio', 41 } 42 43 const result = resolveProfile(baseSource, override) 44 45 expect(result).toEqual({ 46 did: 'did:plc:abc123', 47 handle: 'jay.bsky.team', 48 displayName: 'Jay in Wonderland', 49 avatarUrl: 'https://cdn.example.com/community-avatar.jpg', 50 bannerUrl: 'https://cdn.example.com/community-banner.jpg', 51 bio: 'Community-specific bio', 52 }) 53 }) 54 55 it('falls back to source for null override fields', () => { 56 const override: CommunityOverride = { 57 displayName: 'Jay in the Community', 58 avatarUrl: null, 59 bannerUrl: null, 60 bio: 'Override bio only', 61 } 62 63 const result = resolveProfile(baseSource, override) 64 65 expect(result).toEqual({ 66 did: 'did:plc:abc123', 67 handle: 'jay.bsky.team', 68 displayName: 'Jay in the Community', 69 avatarUrl: 'https://cdn.example.com/avatar.jpg', 70 bannerUrl: 'https://cdn.example.com/banner.jpg', 71 bio: 'Override bio only', 72 }) 73 }) 74 75 it('returns all nulls for nullable fields when source is all null and no override', () => { 76 const nullSource: SourceProfile = { 77 did: 'did:plc:empty', 78 handle: 'empty.bsky.social', 79 displayName: null, 80 avatarUrl: null, 81 bannerUrl: null, 82 bio: null, 83 } 84 85 const result = resolveProfile(nullSource, null) 86 87 expect(result).toEqual({ 88 did: 'did:plc:empty', 89 handle: 'empty.bsky.social', 90 displayName: null, 91 avatarUrl: null, 92 bannerUrl: null, 93 bio: null, 94 }) 95 }) 96 97 it('uses override values when source nullable fields are all null', () => { 98 const nullSource: SourceProfile = { 99 did: 'did:plc:empty', 100 handle: 'empty.bsky.social', 101 displayName: null, 102 avatarUrl: null, 103 bannerUrl: null, 104 bio: null, 105 } 106 107 const override: CommunityOverride = { 108 displayName: 'Community Name', 109 avatarUrl: 'https://cdn.example.com/override-avatar.jpg', 110 bannerUrl: 'https://cdn.example.com/override-banner.jpg', 111 bio: 'Override bio', 112 } 113 114 const result = resolveProfile(nullSource, override) 115 116 expect(result).toEqual({ 117 did: 'did:plc:empty', 118 handle: 'empty.bsky.social', 119 displayName: 'Community Name', 120 avatarUrl: 'https://cdn.example.com/override-avatar.jpg', 121 bannerUrl: 'https://cdn.example.com/override-banner.jpg', 122 bio: 'Override bio', 123 }) 124 }) 125 126 it('always takes did and handle from source, never from override', () => { 127 const override: CommunityOverride = { 128 displayName: 'Override Name', 129 avatarUrl: null, 130 bannerUrl: null, 131 bio: null, 132 } 133 134 const result = resolveProfile(baseSource, override) 135 136 expect(result.did).toBe(baseSource.did) 137 expect(result.handle).toBe(baseSource.handle) 138 }) 139})