oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

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.
6import * as Y from 'yjs'
7import assert from 'node:assert/strict'
8
9const EXPECTED_TEXT = 'from rust 🦀 café'
10const EXPECTED_DELTA = [
11 { insert: 'from ' },
12 { insert: 'rust', attributes: { bold: true } },
13 { insert: ' 🦀 café' }
14]
15
16const chunks = []
17for await (const chunk of process.stdin) chunks.push(chunk)
18const update = new Uint8Array(Buffer.concat(chunks))
19
20if (update.length === 0) {
21 console.error('verify-rust: empty update on stdin')
22 process.exit(1)
23}
24
25const doc = new Y.Doc()
26Y.applyUpdate(doc, update)
27const text = doc.getText('content')
28const actualText = text.toString()
29const actualDelta = text.toDelta()
30
31try {
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
42console.log(`verify-rust: ok (${update.length} bytes)`)
43