|
| 1 | +// A brace in a chat line is a character someone typed, not structure (#2447): `parseRoomShare` |
| 2 | +// finds the end of the JSON envelope by counting braces, and it must not count the ones inside the |
| 3 | +// strings `encodeRoomShare` writes — otherwise a single unbalanced brace drops the whole line. |
| 4 | +import assert from 'node:assert/strict' |
| 5 | +import { test } from 'node:test' |
| 6 | +import { encodeRoomShare, parseRoomShare } from '../src/shared/room-share.js' |
| 7 | + |
| 8 | +const line = (text) => encodeRoomShare({ op: 'line', roomId: 'r1', seq: 7, who: 'Alice', text }) |
| 9 | + |
| 10 | +test('an open brace in a line does not end the envelope early', () => { |
| 11 | + const text = 'if (x) { y()' |
| 12 | + const parsed = parseRoomShare(line(text)) |
| 13 | + assert.ok(parsed, 'the envelope was dropped') |
| 14 | + assert.equal(parsed.text, text) |
| 15 | +}) |
| 16 | + |
| 17 | +test('a close brace in a line does not end the envelope early', () => { |
| 18 | + const text = 'so the closing brace is }' |
| 19 | + const parsed = parseRoomShare(line(text)) |
| 20 | + assert.ok(parsed, 'the envelope was dropped') |
| 21 | + assert.equal(parsed.text, text) |
| 22 | +}) |
| 23 | + |
| 24 | +test('a brace inside a quoted word, and an escaped quote, both stay text', () => { |
| 25 | + const text = 'he said "{" and then "}" and \\ back' |
| 26 | + const parsed = parseRoomShare(line(text)) |
| 27 | + assert.ok(parsed, 'the envelope was dropped') |
| 28 | + assert.equal(parsed.text, text) |
| 29 | +}) |
| 30 | + |
| 31 | +test('an envelope that really is cut short is still refused (control)', () => { |
| 32 | + const text = line('hello there') |
| 33 | + assert.equal(parseRoomShare(text.slice(0, text.length - 3)), undefined) |
| 34 | +}) |
0 commit comments