Barazo AppView backend barazo.forum
at main 134 lines 4.7 kB view raw
1import { describe, it, expect } from 'vitest' 2import { validateRecord } from '../../../src/firehose/validation.js' 3 4describe('validateRecord', () => { 5 describe('topic post validation', () => { 6 const validTopic = { 7 title: 'Test Topic', 8 content: { $type: 'forum.barazo.richtext#markdown' as const, value: 'Some content here' }, 9 community: 'did:plc:abc123', 10 category: 'general', 11 tags: ['test'], 12 publishedAt: '2026-01-01T00:00:00.000Z', 13 } 14 15 it('accepts a valid topic post', () => { 16 const result = validateRecord('forum.barazo.topic.post', validTopic) 17 expect(result.success).toBe(true) 18 }) 19 20 it('rejects a topic post with missing title', () => { 21 const { title: _, ...invalid } = validTopic 22 const result = validateRecord('forum.barazo.topic.post', invalid) 23 expect(result.success).toBe(false) 24 }) 25 26 it('rejects a topic post with empty content', () => { 27 const result = validateRecord('forum.barazo.topic.post', { 28 ...validTopic, 29 content: { $type: 'forum.barazo.richtext#markdown' as const, value: '' }, 30 }) 31 expect(result.success).toBe(false) 32 }) 33 }) 34 35 describe('topic reply validation', () => { 36 const validReply = { 37 content: { $type: 'forum.barazo.richtext#markdown' as const, value: 'A reply' }, 38 root: { uri: 'at://did:plc:abc/forum.barazo.topic.post/123', cid: 'bafyabc' }, 39 parent: { uri: 'at://did:plc:abc/forum.barazo.topic.post/123', cid: 'bafyabc' }, 40 community: 'did:plc:abc123', 41 createdAt: '2026-01-01T00:00:00.000Z', 42 } 43 44 it('accepts a valid topic reply', () => { 45 const result = validateRecord('forum.barazo.topic.reply', validReply) 46 expect(result.success).toBe(true) 47 }) 48 49 it('rejects a reply with missing root ref', () => { 50 const { root: _, ...invalid } = validReply 51 const result = validateRecord('forum.barazo.topic.reply', invalid) 52 expect(result.success).toBe(false) 53 }) 54 }) 55 56 describe('reaction validation', () => { 57 const validReaction = { 58 subject: { uri: 'at://did:plc:abc/forum.barazo.topic.post/123', cid: 'bafyabc' }, 59 type: 'like', 60 community: 'did:plc:abc123', 61 createdAt: '2026-01-01T00:00:00.000Z', 62 } 63 64 it('accepts a valid reaction', () => { 65 const result = validateRecord('forum.barazo.interaction.reaction', validReaction) 66 expect(result.success).toBe(true) 67 }) 68 69 it('rejects a reaction with missing type', () => { 70 const { type: _, ...invalid } = validReaction 71 const result = validateRecord('forum.barazo.interaction.reaction', invalid) 72 expect(result.success).toBe(false) 73 }) 74 }) 75 76 describe('vote validation', () => { 77 const validVote = { 78 subject: { uri: 'at://did:plc:abc/forum.barazo.topic.post/123', cid: 'bafyabc' }, 79 direction: 'up', 80 community: 'did:plc:abc123', 81 createdAt: '2026-01-01T00:00:00.000Z', 82 } 83 84 it('accepts a valid vote', () => { 85 const result = validateRecord('forum.barazo.interaction.vote', validVote) 86 expect(result.success).toBe(true) 87 }) 88 89 it('rejects a vote with missing direction', () => { 90 const { direction: _, ...invalid } = validVote 91 const result = validateRecord('forum.barazo.interaction.vote', invalid) 92 expect(result.success).toBe(false) 93 }) 94 95 it('rejects a vote with missing subject', () => { 96 const { subject: _, ...invalid } = validVote 97 const result = validateRecord('forum.barazo.interaction.vote', invalid) 98 expect(result.success).toBe(false) 99 }) 100 101 it('rejects a vote with missing community', () => { 102 const { community: _, ...invalid } = validVote 103 const result = validateRecord('forum.barazo.interaction.vote', invalid) 104 expect(result.success).toBe(false) 105 }) 106 }) 107 108 describe('unknown collection', () => { 109 it('rejects an unknown collection', () => { 110 const result = validateRecord('com.example.unknown', { foo: 'bar' }) 111 expect(result.success).toBe(false) 112 if (!result.success) { 113 expect(result.error).toContain('Unsupported collection') 114 } 115 }) 116 }) 117 118 describe('size limit', () => { 119 it('rejects records exceeding 64KB', () => { 120 const oversized = { 121 title: 'Test', 122 content: { $type: 'forum.barazo.richtext#markdown' as const, value: 'x'.repeat(65_537) }, 123 community: 'did:plc:abc123', 124 category: 'general', 125 publishedAt: '2026-01-01T00:00:00.000Z', 126 } 127 const result = validateRecord('forum.barazo.topic.post', oversized) 128 expect(result.success).toBe(false) 129 if (!result.success) { 130 expect(result.error).toContain('exceeds maximum size') 131 } 132 }) 133 }) 134})