Home Reference Source Test

test/atomic/timetag.spec.js

  1. import { expect } from 'chai'
  2.  
  3. import AtomicTimetag, {
  4. Timetag,
  5. SECONDS_70_YEARS,
  6. } from '../../src/atomic/timetag'
  7.  
  8. /** @test {Timetag} */
  9. describe('Timetag', () => {
  10. let timetag
  11. let anotherTimetag
  12.  
  13. before(() => {
  14. timetag = new Timetag(SECONDS_70_YEARS + 1234, 0)
  15. anotherTimetag = new Timetag(3718482449, 131799040)
  16. })
  17.  
  18. it('sets the values correctly on initialization', () => {
  19. expect(timetag.seconds).to.be.equals(SECONDS_70_YEARS + 1234)
  20. expect(timetag.fractions).to.be.equals(0)
  21. })
  22.  
  23. /** @test {Timetag#timestamp} */
  24. describe('timestamp', () => {
  25. it('converts correctly to js timestamps', () => {
  26. expect(timetag.timestamp()).to.be.equals(1234 * 1000)
  27. expect(anotherTimetag.timestamp()).to.be.equals(1509493649000)
  28. })
  29.  
  30. it('converts correctly to NTP timestamps', () => {
  31. timetag.timestamp(1)
  32.  
  33. expect(timetag.seconds).to.be.equals(SECONDS_70_YEARS)
  34. expect(timetag.fractions).to.be.equals(4294967)
  35. })
  36. })
  37. })
  38.  
  39. /** @test {AtomicTimetag} */
  40. describe('AtomicTimetag', () => {
  41. const bitArray = {
  42. 0: 0, 1: 1, 2: 248, 3: 99, 4: 0, 5: 4, 6: 84, 7: 63,
  43. }
  44.  
  45. let atomic
  46.  
  47. before(() => {
  48. atomic = new AtomicTimetag(new Timetag(129123, 283711))
  49. })
  50.  
  51. /** @test {AtomicTimetag#pack} */
  52. describe('pack', () => {
  53. let result
  54.  
  55. before(() => {
  56. result = atomic.pack()
  57. })
  58.  
  59. it('returns correct bits', () => {
  60. expect(JSON.stringify(result)).to.equal(JSON.stringify(bitArray))
  61. })
  62.  
  63. it('consists of 64 bits', () => {
  64. expect(result.byteLength * 8).to.equal(64)
  65. })
  66. })
  67.  
  68. /** @test {AtomicTimetag#unpack} */
  69. describe('unpack', () => {
  70. let returnValue
  71.  
  72. before(() => {
  73. const data = new Uint8Array([1, 1, 1, 0, 0, 0, 1, 0])
  74. const dataView = new DataView(data.buffer)
  75.  
  76. returnValue = atomic.unpack(dataView, 0)
  77. })
  78.  
  79. it('returns a number', () => {
  80. expect(returnValue).to.be.a('number')
  81. })
  82.  
  83. it('sets the offset to 8', () => {
  84. expect(atomic.offset).to.equal(8)
  85. })
  86.  
  87. it('sets the correct NTP values', () => {
  88. expect(atomic.value.seconds).to.equal(16843008)
  89. expect(atomic.value.fractions).to.equal(256)
  90. })
  91. })
  92.  
  93. describe('constructor', () => {
  94. it('with an integer timestamp', () => {
  95. atomic = new AtomicTimetag(5000)
  96. expect(atomic.value.seconds).to.equal(2208988805)
  97. })
  98.  
  99. it('with a Date instance', () => {
  100. const date = new Date(2015, 2, 21, 5, 0, 21)
  101. date.setUTCHours(4)
  102. atomic = new AtomicTimetag(date)
  103. expect(atomic.value.seconds).to.equal(3635899221)
  104. })
  105. })
  106. })