UtilsTests.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // @ts-check
  2. const { createHash } = require('node:crypto')
  3. const { expect } = require('chai')
  4. const Utils = require('../../../app/js/Utils')
  5. describe('Utils', function () {
  6. describe('addTrackedDeletesToContent', function () {
  7. it("doesn't modify text without tracked deletes", function () {
  8. const content = 'the quick brown fox'
  9. const trackedChanges = []
  10. const result = Utils.addTrackedDeletesToContent(content, trackedChanges)
  11. expect(result).to.equal(content)
  12. })
  13. it('adds tracked deletes to text but skips tracked inserts', function () {
  14. const content = 'the brown fox jumps over the dog'
  15. const metadata = { user_id: 'user1', ts: new Date().toString() }
  16. const trackedChanges = [
  17. { id: 'tc1', op: { d: 'quick ', p: 4 }, metadata },
  18. { id: 'tc2', op: { i: 'brown ', p: 5 }, metadata },
  19. { id: 'tc3', op: { d: 'lazy ', p: 29 }, metadata },
  20. ]
  21. const result = Utils.addTrackedDeletesToContent(content, trackedChanges)
  22. expect(result).to.equal('the quick brown fox jumps over the lazy dog')
  23. })
  24. })
  25. describe('computeDocHash', function () {
  26. it('computes the hash for an empty doc', function () {
  27. const actual = Utils.computeDocHash([])
  28. const expected = stringHash('')
  29. expect(actual).to.equal(expected)
  30. })
  31. it('computes the hash for a single-line doc', function () {
  32. const actual = Utils.computeDocHash(['hello'])
  33. const expected = stringHash('hello')
  34. expect(actual).to.equal(expected)
  35. })
  36. it('computes the hash for a multiline doc', function () {
  37. const actual = Utils.computeDocHash(['hello', 'there', 'world'])
  38. const expected = stringHash('hello\nthere\nworld')
  39. expect(actual).to.equal(expected)
  40. })
  41. })
  42. })
  43. function stringHash(s) {
  44. const hash = createHash('sha1')
  45. hash.update(s)
  46. return hash.digest('hex')
  47. }