Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto
Something went wrong. Try again.
3.3 kB · 90 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091// The feedback helpers' logic that needs no network: the lexicon's limits, the// record a report becomes, and telling a missing-scope refusal apart from any// other failed write.import { describe, expect, it } from 'vitest'import { BOARD_URL, DISCUSSION_COLLECTION, SPACE_URI, buildDiscussion, cleanBody, cleanTags, cleanTitle, discussionUrl,} from './feedback'
const space = { uri: SPACE_URI, cid: 'bafyreigxyqzvvsynbpuhn35uls646smjbrp4vybh6nyuesn4elhkzozgme' }
describe('cleanTitle', () => { it('trims and requires something to be there', () => { expect(cleanTitle(' badge never appears ')).toBe('badge never appears') expect(() => cleanTitle(' ')).toThrow(/title/i) })
it('holds the lexicon caps in both characters and bytes', () => { expect(cleanTitle('a'.repeat(300))).toHaveLength(300) expect(() => cleanTitle('a'.repeat(301))).toThrow(/too long/) // 300 code points but 900 bytes: over the byte cap, under the char cap. expect(() => cleanTitle('é'.repeat(301))).toThrow(/too long/) })})
describe('cleanBody', () => { it('keeps line breaks and allows nothing at all', () => { expect(cleanBody('one\ntwo')).toBe('one\ntwo') expect(cleanBody('')).toBe('') })
it('refuses control characters that are not whitespace', () => { expect(() => cleanBody('a\u0007b')).toThrow(/not allowed/) expect(cleanBody('a\tb')).toBe('a\tb') })
it('holds the lexicon cap', () => { expect(() => cleanBody('a'.repeat(10_001))).toThrow(/too long/) })})
describe('cleanTags', () => { it('drops blanks and repeats, keeping the order they were ticked in', () => { expect(cleanTags(['bug', ' ', 'bug', 'thanks'])).toEqual(['bug', 'thanks']) })
it('holds the lexicon caps', () => { expect(() => cleanTags(['a'.repeat(65)])).toThrow(/tag is too long/) expect(() => cleanTags(['1', '2', '3', '4', '5', '6', '7', '8', '9'])).toThrow(/more tags/) })})
describe('buildDiscussion', () => { it('points at the space with a strongRef carrying the fetched cid', () => { const record = buildDiscussion(space, { title: 'x', body: '', tags: [] }) expect(record.$type).toBe(DISCUSSION_COLLECTION) expect(record.space).toEqual({ uri: SPACE_URI, cid: space.cid }) expect(typeof record.createdAt).toBe('string') })
it('leaves out an empty body and an empty tag list', () => { const record = buildDiscussion(space, { title: 'x', body: ' ', tags: [' '] }) expect(record).not.toHaveProperty('body') expect(record).not.toHaveProperty('tags') })
it('carries what was filled in, validated', () => { const record = buildDiscussion(space, { title: ' x ', body: ' why ', tags: ['bug'] }) expect(record).toMatchObject({ title: 'x', body: 'why', tags: ['bug'] }) })
it('refuses what the lexicon would', () => { expect(() => buildDiscussion(space, { title: '', body: '', tags: [] })).toThrow() })})
describe('board links', () => { it('names the space and one report the way userinput.app routes them', () => { expect(BOARD_URL).toMatch( /^https:\/\/userinput\.app\/s\/did:plc:[a-z2-7]+\/[a-z2-7]+$/, ) expect(discussionUrl('did:plc:abc', '3kxyz')).toBe('https://userinput.app/d/did:plc:abc/3kxyz') })})