hash_file_data.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const HashFileData = require('../lib/file_data/hash_file_data')
  2. const { expect } = require('chai')
  3. const StringFileData = require('../lib/file_data/string_file_data')
  4. const sinon = require('sinon')
  5. const Blob = require('../lib/blob')
  6. describe('HashFileData', function () {
  7. beforeEach(function () {
  8. this.fileHash = 'a5675307b61ec2517330622a6e649b4ca1ee5612'
  9. this.rangesHash = '380de212d09bf8498065833dbf242aaf11184316'
  10. this.blobStore = {
  11. getString: sinon.stub(),
  12. getObject: sinon.stub(),
  13. getBlob: sinon.stub(),
  14. }
  15. })
  16. describe('constructor', function () {
  17. it('should create a new instance of HashFileData from content hash and ranges hash', function () {
  18. const fileData = new HashFileData(this.fileHash, this.rangesHash)
  19. expect(fileData).to.be.instanceOf(HashFileData)
  20. expect(fileData.getHash()).to.equal(this.fileHash)
  21. expect(fileData.getRangesHash()).to.equal(this.rangesHash)
  22. })
  23. it('should create a new instance of HashFileData with no ranges hash', function () {
  24. const fileData = new HashFileData(this.fileHash)
  25. expect(fileData).to.be.instanceOf(HashFileData)
  26. expect(fileData.getHash()).to.equal(this.fileHash)
  27. expect(fileData.getRangesHash()).to.be.undefined
  28. })
  29. })
  30. describe('fromRaw', function () {
  31. it('should create a new instance of HashFileData from raw data', function () {
  32. const raw = { hash: this.fileHash, rangesHash: this.rangesHash }
  33. const fileData = HashFileData.fromRaw(raw)
  34. expect(fileData).to.be.instanceOf(HashFileData)
  35. expect(fileData.getHash()).to.equal(raw.hash)
  36. expect(fileData.getRangesHash()).to.equal(raw.rangesHash)
  37. })
  38. it('should create a new instance of HashFileData from raw data without ranges hash', function () {
  39. const raw = { hash: this.fileHash }
  40. const fileData = HashFileData.fromRaw(raw)
  41. expect(fileData).to.be.instanceOf(HashFileData)
  42. expect(fileData.getHash()).to.equal(raw.hash)
  43. expect(fileData.getRangesHash()).to.equal(undefined)
  44. })
  45. })
  46. describe('toRaw', function () {
  47. it('should include ranges hash when present', function () {
  48. const fileData = new HashFileData(this.fileHash, this.rangesHash)
  49. const raw = fileData.toRaw()
  50. expect(raw).to.deep.equal({
  51. hash: this.fileHash,
  52. rangesHash: this.rangesHash,
  53. })
  54. })
  55. it('should omit ranges hash when not present', function () {
  56. const fileData = new HashFileData(this.fileHash)
  57. const raw = fileData.toRaw()
  58. expect(raw).to.deep.equal({
  59. hash: this.fileHash,
  60. })
  61. })
  62. })
  63. describe('toEager', function () {
  64. it('should convert HashFileData to StringFileData including ranges', async function () {
  65. const trackedChanges = [
  66. {
  67. range: { pos: 5, length: 10 },
  68. tracking: {
  69. userId: 'foo',
  70. type: 'insert',
  71. ts: '2024-01-01T00:00:00.000Z',
  72. },
  73. },
  74. ]
  75. const comments = [
  76. {
  77. id: 'comment-1',
  78. ranges: [{ pos: 1, length: 4 }],
  79. },
  80. ]
  81. const fileData = new HashFileData(this.fileHash, this.rangesHash)
  82. this.blobStore.getString.withArgs(this.fileHash).resolves('content')
  83. this.blobStore.getObject.withArgs(this.rangesHash).resolves({
  84. trackedChanges,
  85. comments,
  86. })
  87. this.blobStore.getBlob
  88. .withArgs(this.rangesHash)
  89. .resolves(new Blob(this.rangesHash, 20, 20))
  90. this.blobStore.getBlob
  91. .withArgs(this.fileHash)
  92. .resolves(new Blob(this.fileHash, 20, 20))
  93. const eagerFileData = await fileData.toEager(this.blobStore)
  94. expect(eagerFileData).to.be.instanceOf(StringFileData)
  95. expect(eagerFileData.getContent()).to.equal('content')
  96. expect(eagerFileData.trackedChanges.toRaw()).to.deep.equal(trackedChanges)
  97. expect(eagerFileData.getComments().toRaw()).to.deep.equal(comments)
  98. })
  99. it('should convert HashFileData to StringFileData without ranges', async function () {
  100. const fileData = new HashFileData(this.fileHash, undefined)
  101. this.blobStore.getString.withArgs(this.fileHash).resolves('content')
  102. this.blobStore.getBlob
  103. .withArgs(this.fileHash)
  104. .resolves(new Blob(this.fileHash, 20, 20))
  105. const eagerFileData = await fileData.toEager(this.blobStore)
  106. expect(eagerFileData).to.be.instanceOf(StringFileData)
  107. expect(eagerFileData.getContent()).to.equal('content')
  108. expect(eagerFileData.trackedChanges.toRaw()).to.deep.equal([])
  109. expect(eagerFileData.getComments().toRaw()).to.deep.equal([])
  110. })
  111. })
  112. })