<commit_before>/* eslint-env mocha */
const assert = require('assert')
require('./browserenv')
const CodeMirror = require('codemirror')
const Automerge = require('automerge')
const AutomergeCodeMirror = require('..')
const { DocSetWatchableDoc, docSetDoc } = require('automerge-utils')

let docId = 'doc-id'

describe('AutomergeCodeMirror', () => {
  let codeMirror, acm, doc, docSet

  beforeEach(() => {
    doc = Automerge.change(Automerge.init(), doc => {
      doc.text = new Automerge.Text()
    })
    docSet = new Automerge.DocSet()
    docSet.setDoc(docId, doc)
    const watchableDoc = new DocSetWatchableDoc(docSet, docId)

    const getDocText = doc => doc.text

    codeMirror = CodeMirror(document.getElementById('editor'))

    const updateDoc = watchableDoc.set.bind(watchableDoc)
    const registerHandler = watchableDoc.registerHandler.bind(watchableDoc)
    const unregisterHandler = watchableDoc.unregisterHandler.bind(watchableDoc)

    acm = new AutomergeCodeMirror({
      codeMirror,
      getDocText,
      doc,
      updateDoc,
      registerHandler,
      unregisterHandler,
    })
    acm.start()
  })

  describe('Automerge -> CodeMirror', () => {
    it('adds text', () => {
      doc = Automerge.change(doc, editableDoc => {
        editableDoc.text.insertAt(0, ...'HELLO'.split(''))
      })
      docSet.setDoc(docId, doc)

      assert.strictEqual(codeMirror.getValue(), doc.text.join(''))
    })

    it('removes text', () => {
      doc = Automerge.change(doc, editableDoc => {
        editableDoc.text.splice(0, 0, ...'HELLO'.split(''))
      })
      doc = Automerge.change(doc, editableDoc => {
        editableDoc.text.splice(0, 5)
      })

      docSet.setDoc(docId, doc)

      assert.strictEqual(codeMirror.getValue(), doc.text.join(''))
    })

    it('replaces a couple of lines', () => {
      doc = Automerge.change(doc, editableDoc => {
        const text = 'three\nblind\nmice\nsee\nhow\nthey\nrun\n'
        editableDoc.text.splice(0, 0, ...text.split(''))
      })

      doc = Automerge.change(doc, 'Replace', doc => {
        const replacement = 'evil\nrats\n'
        doc.text.splice(6, 11, replacement)
      })

      docSet.setDoc(docId, doc)

      assert.strictEqual(
        codeMirror.getValue(),
        'three\nevil\nrats\nsee\nhow\nthey\nrun\n'
      )
    })

    for (let n = 0; n < 10; n++) {
      it(`works with random edits (fuzz test ${n})`, () => {
        doc = Automerge.change(doc, editableDoc => {
          editableDoc.text.splice(0, 0, ...randomString(20).split(''))
        })
        for (let t = 0; t < 10; t++) {
          doc = monkeyModify(doc)
        }

        docSet.setDoc(docId, doc)
        assert.strictEqual(doc.text.join(''), codeMirror.getValue())
      })
    }
  })

  describe('CodeMirror -> Automerge', () => {
    it('adds text', () => {
      const text = 'HELLO'
      codeMirror.setValue(text)

      doc = docSet.getDoc(docId)
      assert.strictEqual(doc.text.join(''), text)
    })

    it('removes text', () => {
      codeMirror.setValue('')
      codeMirror.replaceRange('HELLO', { line: 0, ch: 0 })
      codeMirror.replaceRange('', { line: 0, ch: 0 }, { line: 0, ch: 5 })

      doc = docSet.getDoc(docId)
      assert.strictEqual(doc.text.join(''), codeMirror.getValue())
    })

    it('replaces a couple of lines', () => {
      const text = 'three\nblind\nmice\nsee\nhow\nthey\nrun\n'
      codeMirror.setValue(text)

      codeMirror.replaceRange(
        'evil\nrats\n',
        { line: 1, ch: 0 },
        { line: 3, ch: 0 }
      )

      doc = docSet.getDoc(docId)
      assert.strictEqual(
        doc.text.join(''),
        'three\nevil\nrats\nsee\nhow\nthey\nrun\n'
      )
    })

    for (let n = 0; n < 10; n++) {
      it(`works with random edits (fuzz test ${n})`, () => {
        codeMirror.setValue(randomString(20))
        for (let t = 0; t < 10; t++) {
          monkeyType(codeMirror)
        }

        doc = docSet.getDoc(docId)
        assert.strictEqual(doc.text.join(''), codeMirror.getValue())
      })
    }<commit_msg>Don't use docSet or watchableDoc<commit_after>/* eslint-env mocha */
const assert = require('assert')
require('./browserenv')
const CodeMirror = require('codemirror')
const Automerge = require('automerge')
const AutomergeCodeMirror = require('..')

describe('AutomergeCodeMirror', () => {
  let codeMirror, acm, doc, updateDoc

  beforeEach(() => {
    doc = Automerge.change(Automerge.init(), doc => {
      doc.text = new Automerge.Text()
    })
    const getDocText = doc => doc.text

    codeMirror = CodeMirror(document.getElementById('editor'))

    const handlers = new Set()
    updateDoc = d => {
      doc = d
      handlers.forEach(h => h(doc))
    }
    const registerHandler = handler => handlers.add(handler)
    const unregisterHandler = handler => handlers.remove(handler)

    acm = new AutomergeCodeMirror({
      codeMirror,
      getDocText,
      doc,
      updateDoc,
      registerHandler,
      unregisterHandler,
    })
    acm.start()
  })

  describe('Automerge -> CodeMirror', () => {
    it('adds text', () => {
      doc = Automerge.change(doc, editableDoc => {
        editableDoc.text.insertAt(0, ...'HELLO'.split(''))
      })
      updateDoc(doc)

      assert.strictEqual(codeMirror.getValue(), doc.text.join(''))
    })

    it('removes text', () => {
      doc = Automerge.change(doc, editableDoc => {
        editableDoc.text.splice(0, 0, ...'HELLO'.split(''))
      })
      doc = Automerge.change(doc, editableDoc => {
        editableDoc.text.splice(0, 5)
      })
      updateDoc(doc)

      assert.strictEqual(codeMirror.getValue(), doc.text.join(''))
    })

    it('replaces a couple of lines', () => {
      doc = Automerge.change(doc, editableDoc => {
        const text = 'three\nblind\nmice\nsee\nhow\nthey\nrun\n'
        editableDoc.text.splice(0, 0, ...text.split(''))
      })

      doc = Automerge.change(doc, 'Replace', doc => {
        const replacement = 'evil\nrats\n'
        doc.text.splice(6, 11, replacement)
      })

      updateDoc(doc)
      assert.strictEqual(
        codeMirror.getValue(),
        'three\nevil\nrats\nsee\nhow\nthey\nrun\n'
      )
    })

    for (let n = 0; n < 10; n++) {
      it(`works with random edits (fuzz test ${n})`, () => {
        doc = Automerge.change(doc, editableDoc => {
          editableDoc.text.splice(0, 0, ...randomString(20).split(''))
        })
        for (let t = 0; t < 10; t++) {
          doc = monkeyModify(doc)
        }

        updateDoc(doc)
        assert.strictEqual(doc.text.join(''), codeMirror.getValue())
      })
    }
  })

  describe('CodeMirror -> Automerge', () => {
    it('adds text', () => {
      const text = 'HELLO'
      codeMirror.setValue(text)

      updateDoc(doc)
      assert.strictEqual(doc.text.join(''), text)
    })

    it('removes text', () => {
      codeMirror.setValue('')
      codeMirror.replaceRange('HELLO', { line: 0, ch: 0 })
      codeMirror.replaceRange('', { line: 0, ch: 0 }, { line: 0, ch: 5 })

      assert.strictEqual(doc.text.join(''), codeMirror.getValue())
    })

    it('replaces a couple of lines', () => {
      const text = 'three\nblind\nmice\nsee\nhow\nthey\nrun\n'
      codeMirror.setValue(text)

      codeMirror.replaceRange(
        'evil\nrats\n',
        { line: 1, ch: 0 },
        { line: 3, ch: 0 }
      )

      assert.strictEqual(
        doc.text.join(''),
        'three\nevil\nrats\nsee\nhow\nthey\nrun\n'
      )
    })

    for (let n = 0; n < 10; n++) {
      it(`works with random edits (fuzz test ${n})`, () => {
        codeMirror.setValue(randomString(20))
        for (let t = 0; t < 10; t++) {
          monkeyType(codeMirror)
        }

        assert.strictEqual(doc.text.join(''), codeMirror.getValue())
      })
    }
{'fraction_non_alphanumeric': 0.16733016566071657, 'fraction_numerical': 0.009759856170540645, 'mean_word_length': 2.4172882843352346, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2991496', 'n_tokens_mistral': 2783, 'n_tokens_neox': 2609, 'n_words': 479}