import { 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): 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) })