change.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict'
  2. const { expect } = require('chai')
  3. const {
  4. Change,
  5. File,
  6. Operation,
  7. AddFileOperation,
  8. Snapshot,
  9. Origin,
  10. RestoreFileOrigin,
  11. } = require('../..')
  12. describe('Change', function () {
  13. describe('findBlobHashes', function () {
  14. it('finds blob hashes from operations', function () {
  15. const blobHashes = new Set()
  16. const change = Change.fromRaw({
  17. operations: [],
  18. timestamp: '2015-03-05T12:03:53.035Z',
  19. authors: [null],
  20. })
  21. change.findBlobHashes(blobHashes)
  22. expect(blobHashes.size).to.equal(0)
  23. // AddFile with content doesn't have a hash.
  24. change.pushOperation(Operation.addFile('a.txt', File.fromString('a')))
  25. change.findBlobHashes(blobHashes)
  26. expect(blobHashes.size).to.equal(0)
  27. // AddFile with hash should give us a hash.
  28. change.pushOperation(
  29. Operation.addFile('b.txt', File.fromHash(File.EMPTY_FILE_HASH))
  30. )
  31. change.findBlobHashes(blobHashes)
  32. expect(blobHashes.size).to.equal(1)
  33. expect(blobHashes.has(File.EMPTY_FILE_HASH)).to.be.true
  34. })
  35. })
  36. describe('RestoreFileOrigin', function () {
  37. it('should convert to and from raw', function () {
  38. const origin = new RestoreFileOrigin(1, 'path', new Date())
  39. const raw = origin.toRaw()
  40. const newOrigin = Origin.fromRaw(raw)
  41. expect(newOrigin).to.eql(origin)
  42. })
  43. it('change should have a correct origin class', function () {
  44. const change = Change.fromRaw({
  45. operations: [],
  46. timestamp: '2015-03-05T12:03:53.035Z',
  47. authors: [null],
  48. origin: {
  49. kind: 'file-restore',
  50. version: 1,
  51. path: 'path',
  52. timestamp: '2015-03-05T12:03:53.035Z',
  53. },
  54. })
  55. expect(change.getOrigin()).to.be.an.instanceof(RestoreFileOrigin)
  56. })
  57. })
  58. describe('applyTo', function () {
  59. it('sets the timestamp on the snapshot', function () {
  60. const snapshot = new Snapshot()
  61. snapshot.addFile('main.tex', File.fromString(''))
  62. const operation = new AddFileOperation('main.tex', File.fromString(''))
  63. const now = new Date()
  64. const change = new Change([operation], now)
  65. change.applyTo(snapshot)
  66. expect(snapshot.getTimestamp().toISOString()).to.equal(now.toISOString())
  67. })
  68. })
  69. })