snapshot.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict'
  2. const { expect } = require('chai')
  3. const {
  4. File,
  5. Snapshot,
  6. TextOperation,
  7. Change,
  8. EditFileOperation,
  9. } = require('../..')
  10. describe('Snapshot', function () {
  11. describe('findBlobHashes', function () {
  12. it('finds blob hashes from files', function () {
  13. const snapshot = new Snapshot()
  14. const blobHashes = new Set()
  15. snapshot.findBlobHashes(blobHashes)
  16. expect(blobHashes.size).to.equal(0)
  17. // Add a file without a hash.
  18. snapshot.addFile('foo', File.fromString(''))
  19. snapshot.findBlobHashes(blobHashes)
  20. expect(blobHashes.size).to.equal(0)
  21. // Add a file with a hash.
  22. snapshot.addFile('bar', File.fromHash(File.EMPTY_FILE_HASH))
  23. snapshot.findBlobHashes(blobHashes)
  24. expect(Array.from(blobHashes)).to.have.members([File.EMPTY_FILE_HASH])
  25. })
  26. })
  27. describe('editFile', function () {
  28. let snapshot
  29. let operation
  30. beforeEach(function () {
  31. snapshot = new Snapshot()
  32. snapshot.addFile('hello.txt', File.fromString('hello'))
  33. operation = new TextOperation()
  34. operation.retain(5)
  35. operation.insert(' world!')
  36. })
  37. it('applies text operations to the file', function () {
  38. snapshot.editFile('hello.txt', operation)
  39. const file = snapshot.getFile('hello.txt')
  40. expect(file.getContent()).to.equal('hello world!')
  41. })
  42. it('rejects text operations for nonexistent file', function () {
  43. expect(() => {
  44. snapshot.editFile('does-not-exist.txt', operation)
  45. }).to.throw(Snapshot.EditMissingFileError)
  46. })
  47. })
  48. describe('applyAll', function () {
  49. let snapshot
  50. let change
  51. beforeEach(function () {
  52. snapshot = new Snapshot()
  53. snapshot.addFile('empty.txt', File.fromString(''))
  54. const badTextOp = new TextOperation()
  55. badTextOp.insert('FAIL!')
  56. const goodTextOp = new TextOperation()
  57. goodTextOp.insert('SUCCESS!')
  58. change = new Change(
  59. [
  60. new EditFileOperation('missing.txt', badTextOp),
  61. new EditFileOperation('empty.txt', goodTextOp),
  62. ],
  63. new Date()
  64. )
  65. })
  66. it('ignores recoverable errors', function () {
  67. snapshot.applyAll([change])
  68. const file = snapshot.getFile('empty.txt')
  69. expect(file.getContent()).to.equal('SUCCESS!')
  70. })
  71. it('stops on recoverable errors in strict mode', function () {
  72. expect(() => {
  73. snapshot.applyAll([change], { strict: true })
  74. }).to.throw(Snapshot.EditMissingFileError)
  75. const file = snapshot.getFile('empty.txt')
  76. expect(file.getContent()).to.equal('')
  77. })
  78. })
  79. })