<commit_before>const {assert} = require('chai')
const temp = require('temp').track()
const {TextBuffer} = require('../..')
const fs = require('fs')

describe('TextBuffer', () => {
  describe('.load', () => {
    if (!TextBuffer.prototype.load) return;

    it('loads the contents of the given filePath in the background', () => {
      const buffer = new TextBuffer()

      const {path: filePath} = temp.openSync()
      const content = 'a\nb\nc\n'.repeat(10 * 1024)
      fs.writeFileSync(filePath, content)

      const progressValues = []

      return buffer.load(filePath, (i) => progressValues.push(i))
        .then(() => {
          assert.equal(buffer.getText(), content)

          assert.deepEqual(progressValues, progressValues.map(Number).sort())
          assert(progressValues[0] >= 0)
          assert(progressValues[progressValues.length - 1] <= content.length)
        })
    })

    it('can load a file in a non-UTF8 encoding', () => {
      const buffer = new TextBuffer()

      const {path: filePath} = temp.openSync()
      const content = 'a\nb\nc\n'.repeat(10)
      fs.writeFileSync(filePath, content, 'utf16le')

      return buffer.load(filePath, 'UTF-16LE')
        .then(() => assert.equal(buffer.getText(), content))
    })

    it('rejects its promise if an invalid encoding is given', () => {
      const buffer = new TextBuffer()

      const {path: filePath} = temp.openSync()
      const content = 'a\nb\nc\n'.repeat(10)
      fs.writeFileSync(filePath, content, 'utf16le')

      let rejection = null
      return buffer.load(filePath, 'GARBAGE-16')
        .catch(error => rejection = error)
        .then(() => assert.equal(rejection.message, 'Invalid encoding name: GARBAGE-16'))
    })

    it('resolves with a Patch representing the difference between the old and new text', () => {
      const buffer = new TextBuffer('cat\ndog\nelephant\nfox')
      const {path: filePath} = temp.openSync()
      fs.writeFileSync(filePath, 'bug\ncat\ndog\nelephant\nfox\ngoat')

      return buffer.load(filePath).then(patch => {
        assert.deepEqual(JSON.parse(JSON.stringify(patch.getChanges())), [
          {
            oldStart: {row: 0, column: 0}, oldEnd: {row: 0, column: 0},
            newStart: {row: 0, column: 0}, newEnd: {row: 1, column: 0}
          },
          {
            oldStart: {row: 3, column: 3}, oldEnd: {row: 3, column: 3},
            newStart: {row: 4, column: 3}, newEnd: {row: 5, column: 4}
          }
        ])
      })
    })
  })

  describe('.loadSync', () => {
    it('returns a Patch representing the difference between the old and new text', () => {
      const buffer = new TextBuffer('cat\ndog\nelephant\nfox')
      const {path: filePath} = temp.openSync()
      fs.writeFileSync(filePath, 'bug\ncat\ndog\nelephant\nfox\ngoat')

      const patch = buffer.loadSync(filePath, 'UTF-8')

      assert.deepEqual(JSON.parse(JSON.stringify(patch.getChanges())), [
        {
          oldStart: {row: 0, column: 0}, oldEnd: {row: 0, column: 0},
          newStart: {row: 0, column: 0}, newEnd: {row: 1, column: 0}
        },
        {
          oldStart: {row: 3, column: 3}, oldEnd: {row: 3, column: 3},
          newStart: {row: 4, column: 3}, newEnd: {row: 5, column: 4}
        }
      ])
    })
  })

  describe('.save', () => {
    if (!TextBuffer.prototype.save) return;

    it('writes the buffer\'s content to the given file', () => {
      const buffer = new TextBuffer('abcdefghijklmnopqrstuvwxyz')

      // Perform some edits just to exercise the saving logic
      buffer.setTextInRange(Range(Point(0, 3), Point(0, 3)), '123')
      buffer.setTextInRange(Range(Point(0, 7), Point(0, 7)), '456')
      assert.equal(buffer.getText(), 'abc123d456efghijklmnopqrstuvwxyz')

      const {path: filePath} = temp.openSync()
      const savePromise = buffer.save(filePath)

      buffer.setTextInRange(Range(Point(0, 11), Point(0, 11)), '789')
      assert.equal(buffer.getText(), 'abc123d456e789fghijklmnopqrstuvwxyz')

      return savePromise.then(() => {
        assert.equal(fs.readFileSync(filePath, 'utf8'), 'abc123d456efghijklmnopqrstuvwxyz')
        assert.equal(buffer.getText(), 'abc123d456e789fghijklmnopqrstuvwxyz')
      })
    })

    it('handles concurrent saves', () => {<commit_msg>Update JS tests for TextBuffer.load to include the old and new text<commit_after>const {assert} = require('chai')
const temp = require('temp').track()
const {TextBuffer} = require('../..')
const fs = require('fs')

describe('TextBuffer', () => {
  describe('.load', () => {
    if (!TextBuffer.prototype.load) return;

    it('loads the contents of the given filePath in the background', () => {
      const buffer = new TextBuffer()

      const {path: filePath} = temp.openSync()
      const content = 'a\nb\nc\n'.repeat(10 * 1024)
      fs.writeFileSync(filePath, content)

      const progressValues = []

      return buffer.load(filePath, (i) => progressValues.push(i))
        .then(() => {
          assert.equal(buffer.getText(), content)

          assert.deepEqual(progressValues, progressValues.map(Number).sort())
          assert(progressValues[0] >= 0)
          assert(progressValues[progressValues.length - 1] <= content.length)
        })
    })

    it('can load a file in a non-UTF8 encoding', () => {
      const buffer = new TextBuffer()

      const {path: filePath} = temp.openSync()
      const content = 'a\nb\nc\n'.repeat(10)
      fs.writeFileSync(filePath, content, 'utf16le')

      return buffer.load(filePath, 'UTF-16LE')
        .then(() => assert.equal(buffer.getText(), content))
    })

    it('rejects its promise if an invalid encoding is given', () => {
      const buffer = new TextBuffer()

      const {path: filePath} = temp.openSync()
      const content = 'a\nb\nc\n'.repeat(10)
      fs.writeFileSync(filePath, content, 'utf16le')

      let rejection = null
      return buffer.load(filePath, 'GARBAGE-16')
        .catch(error => rejection = error)
        .then(() => assert.equal(rejection.message, 'Invalid encoding name: GARBAGE-16'))
    })

    it('resolves with a Patch representing the difference between the old and new text', () => {
      const buffer = new TextBuffer('cat\ndog\nelephant\nfox')
      const {path: filePath} = temp.openSync()
      fs.writeFileSync(filePath, 'bug\ncat\ndog\nelephant\nfox\ngoat')

      return buffer.load(filePath).then(patch => {
        assert.deepEqual(JSON.parse(JSON.stringify(patch.getChanges())), [
          {
            oldStart: {row: 0, column: 0}, oldEnd: {row: 0, column: 0},
            newStart: {row: 0, column: 0}, newEnd: {row: 1, column: 0},
            oldText: '',
            newText: 'bug\n'
          },
          {
            oldStart: {row: 3, column: 3}, oldEnd: {row: 3, column: 3},
            newStart: {row: 4, column: 3}, newEnd: {row: 5, column: 4},
            oldText: '',
            newText: '\ngoat'
          }
        ])
      })
    })
  })

  describe('.loadSync', () => {
    it('returns a Patch representing the difference between the old and new text', () => {
      const buffer = new TextBuffer('cat\ndog\nelephant\nfox')
      const {path: filePath} = temp.openSync()
      fs.writeFileSync(filePath, 'bug\ncat\ndog\nelephant\nfox\ngoat')

      const patch = buffer.loadSync(filePath, 'UTF-8')

      assert.deepEqual(JSON.parse(JSON.stringify(patch.getChanges())), [
        {
          oldStart: {row: 0, column: 0}, oldEnd: {row: 0, column: 0},
          newStart: {row: 0, column: 0}, newEnd: {row: 1, column: 0},
          oldText: '',
          newText: 'bug\n'
        },
        {
          oldStart: {row: 3, column: 3}, oldEnd: {row: 3, column: 3},
          newStart: {row: 4, column: 3}, newEnd: {row: 5, column: 4},
          oldText: '',
          newText: '\ngoat'
        }
      ])
    })
  })

  describe('.save', () => {
    if (!TextBuffer.prototype.save) return;

    it('writes the buffer\'s content to the given file', () => {
      const buffer = new TextBuffer('abcdefghijklmnopqrstuvwxyz')

      // Perform some edits just to exercise the saving logic
      buffer.setTextInRange(Range(Point(0, 3), Point(0, 3)), '123')
      buffer.setTextInRange(Range(Point(0, 7), Point(0, 7)), '456')
      assert.equal(buffer.getText(), 'abc123d456efghijklmnopqrstuvwxyz')

      const {path: filePath} = temp.openSync()
      const savePromise = buffer.save(filePath)

      buffer.setTextInRange(Range(Point(0, 11), Point(0, 11)), '789')
      assert.equal(buffer.getText(), 'abc123d456e789fghijklmnopqrstuvwxyz')

      return savePromise.then(() => {
        assert.equal(fs.readFileSync(filePath, 'utf8'), 'abc123d456efghijklmnopqrstuvwxyz')
        assert.equal(buffer.getText(), 'abc123d456e789fghijklmnopqrstuvwxyz')
      })
    })

    it('handles concurrent saves', () => {
{'fraction_non_alphanumeric': 0.15816326530612246, 'fraction_numerical': 0.025170068027210883, 'mean_word_length': 2.7456475583864117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '896634', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2802, 'n_words': 659}