Something went wrong. Try again.
message board on atproto atchan.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186import { test } from 'node:test'import assert from 'node:assert/strict'import { parse_composer_input, parse_edit_input, parse_mod_subject} from './post-forms.ts'
const ALICE = 'did:plc:alice'const BOB = 'did:plc:bob'
function form(fields: Record<string, string>): FormData { const data = new FormData() for (const [k, v] of Object.entries(fields)) data.append(k, v) return data}
test('parse_edit_input requires a uri', () => { const result = parse_edit_input(form({}), ALICE) assert.deepEqual(result, { ok: false, status: 400, message: 'uri required' })})
test('parse_edit_input rejects a name that starts with @ (impersonation)', () => { const result = parse_edit_input( form({ uri: `at://${ALICE}/net.atchan.reply/x`, name: '@admin' }), ALICE ) assert.deepEqual(result, { ok: false, status: 400, message: 'name may not start with @' })})
test('parse_edit_input rejects an unparseable uri', () => { const result = parse_edit_input(form({ uri: 'not-a-uri' }), ALICE) assert.deepEqual(result, { ok: false, status: 400, message: 'invalid uri' })})
test("parse_edit_input refuses to edit a post that is not the caller's own", () => { const result = parse_edit_input( form({ uri: `at://${BOB}/net.atchan.reply/x` }), ALICE ) assert.deepEqual(result, { ok: false, status: 403, message: 'not your post' })})
test("parse_edit_input accepts a valid edit of the caller's own post", () => { const uri = `at://${ALICE}/net.atchan.reply/x` const result = parse_edit_input( form({ uri, name: ' anon ', body: 'hello' }), ALICE ) assert.equal(result.ok, true) assert.ok(result.ok && result.target.toString() === uri) // trimmed, and empty-after-trim would have been undefined assert.equal(result.ok && result.name, 'anon') assert.equal(result.ok && result.body, 'hello')})
test('parse_edit_input treats an absent name as undefined, not empty string', () => { const uri = `at://${ALICE}/net.atchan.reply/x` const result = parse_edit_input(form({ uri, name: ' ' }), ALICE) assert.equal(result.ok && result.name, undefined)})
test('parse_mod_subject prefers an account subject (did) when present', () => { const result = parse_mod_subject( form({ did: BOB, uri: 'at://x/y/z', cid: 'cid' }) ) assert.deepEqual(result, { ok: true, subject: { did: BOB } })})
test('parse_mod_subject falls back to a post subject (uri + cid)', () => { const result = parse_mod_subject( form({ uri: 'at://x/net.atchan.reply/y', cid: 'bafycid' }) ) assert.deepEqual(result, { ok: true, subject: { uri: 'at://x/net.atchan.reply/y', cid: 'bafycid' } })})
test('parse_mod_subject requires both uri and cid for a post subject', () => { assert.deepEqual(parse_mod_subject(form({ uri: 'at://x/y/z' })), { ok: false, status: 400, message: 'uri and cid required' }) assert.deepEqual(parse_mod_subject(form({ cid: 'bafycid' })), { ok: false, status: 400, message: 'uri and cid required' }) assert.deepEqual(parse_mod_subject(form({})), { ok: false, status: 400, message: 'uri and cid required' })})
const LIMITS = { name: 5, body: 10, subject: 8 }
test('parse_composer_input reports the field that is over its grapheme limit', () => { assert.deepEqual(parse_composer_input(form({ name: 'abcdef' }), LIMITS), { ok: false, status: 400, message: 'name must be at most 5 characters (you have 6)' }) assert.deepEqual( parse_composer_input(form({ body: 'abcdefghijk' }), LIMITS), { ok: false, status: 400, message: 'body must be at most 10 characters (you have 11)' } )})
test('parse_composer_input counts graphemes, not code units', () => { // one family emoji is many code units but one character to a reader const result = parse_composer_input(form({ name: '👨👩👧👦' }), LIMITS) assert.equal(result.ok, true)})
test('parse_composer_input rejects a name that starts with @ (impersonation)', () => { assert.deepEqual(parse_composer_input(form({ name: '@mod' }), LIMITS), { ok: false, status: 400, message: 'name may not start with @' })})
test('parse_composer_input reads a subject only when a limit is declared', () => { const with_subject = parse_composer_input(form({ subject: 'hi' }), LIMITS) assert.equal(with_subject.ok && with_subject.subject, 'hi')
// a reply form declares no subject limit, so a stray field is ignored rather // than smuggled onto the record const reply = parse_composer_input(form({ subject: 'hi' }), { name: 5, body: 10 }) assert.equal(reply.ok && reply.subject, undefined)})
test('parse_composer_input parses markup out of the body into facets', () => { const result = parse_composer_input(form({ body: '**bold**' }), { name: 5, body: 100 }) assert.equal(result.ok, true) if (!result.ok) return assert.equal(result.body, 'bold') assert.equal(result.facets?.length, 1)})
test('parse_composer_input leaves absent optional fields undefined', () => { const result = parse_composer_input(form({}), LIMITS) assert.equal(result.ok, true) if (!result.ok) return assert.equal(result.name, undefined) assert.equal(result.body, undefined) assert.equal(result.facets, undefined) assert.equal(result.file, undefined) assert.equal(result.spoiler, false) assert.equal(result.sage, false)})
test('parse_composer_input reads the checkbox flags as present-or-absent', () => { const result = parse_composer_input( form({ spoiler: 'on', sage: 'on' }), LIMITS ) assert.equal(result.ok && result.spoiler, true) assert.equal(result.ok && result.sage, true)})