| 1 | // Reads a Rust-produced v1 update on stdin and asserts JS yjs decodes it to |
| 2 | // the expected text AND the expected formatting. This is the direction |
| 3 | // Task 3 cannot test. Plain text alone cannot distinguish a correct |
| 4 | // implementation from one that dropped formatting, so this also checks |
| 5 | // toDelta(), the way fixtures/generate.mjs's expected.json does. |
| 6 | import * as Y from 'yjs' |
| 7 | import assert from 'node:assert/strict' |
| 8 | |
| 9 | const EXPECTED_TEXT = 'from rust 🦀 café' |
| 10 | const EXPECTED_DELTA = [ |
| 11 | { insert: 'from ' }, |
| 12 | { insert: 'rust', attributes: { bold: true } }, |
| 13 | { insert: ' 🦀 café' } |
| 14 | ] |
| 15 | |
| 16 | const chunks = [] |
| 17 | for await (const chunk of process.stdin) chunks.push(chunk) |
| 18 | const update = new Uint8Array(Buffer.concat(chunks)) |
| 19 | |
| 20 | if (update.length === 0) { |
| 21 | console.error('verify-rust: empty update on stdin') |
| 22 | process.exit(1) |
| 23 | } |
| 24 | |
| 25 | const doc = new Y.Doc() |
| 26 | Y.applyUpdate(doc, update) |
| 27 | const text = doc.getText('content') |
| 28 | const actualText = text.toString() |
| 29 | const actualDelta = text.toDelta() |
| 30 | |
| 31 | try { |
| 32 | assert.strictEqual(actualText, EXPECTED_TEXT) |
| 33 | assert.deepStrictEqual(actualDelta, EXPECTED_DELTA) |
| 34 | } catch (err) { |
| 35 | console.error('verify-rust: mismatch') |
| 36 | console.error(` text: expected ${JSON.stringify(EXPECTED_TEXT)}, got ${JSON.stringify(actualText)}`) |
| 37 | console.error(` delta: expected ${JSON.stringify(EXPECTED_DELTA)}, got ${JSON.stringify(actualDelta)}`) |
| 38 | console.error(err.message) |
| 39 | process.exit(1) |
| 40 | } |
| 41 | |
| 42 | console.log(`verify-rust: ok (${update.length} bytes)`) |
| 43 | |