file.test.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict'
  2. const { expect } = require('chai')
  3. const FakeBlobStore = require('./support/fake_blob_store')
  4. const ot = require('..')
  5. const File = ot.File
  6. describe('File', function () {
  7. it('can have attached metadata', function () {
  8. // no metadata
  9. let file = File.fromString('foo')
  10. expect(file.getMetadata()).to.eql({})
  11. // metadata passed in at construction time
  12. file = File.fromString('foo', { main: true })
  13. expect(file.getMetadata()).to.eql({ main: true })
  14. // metadata set at runtime
  15. file.setMetadata({ main: false })
  16. expect(file.getMetadata()).to.eql({ main: false })
  17. })
  18. describe('toRaw', function () {
  19. it('returns non-empty metadata', function () {
  20. const metadata = { main: true }
  21. const file = File.fromHash(File.EMPTY_FILE_HASH, metadata)
  22. expect(file.toRaw()).to.eql({
  23. hash: File.EMPTY_FILE_HASH,
  24. metadata: metadata,
  25. })
  26. delete file.getMetadata().main
  27. expect(file.toRaw()).to.eql({ hash: File.EMPTY_FILE_HASH })
  28. })
  29. it('returns a deep clone of metadata', function () {
  30. const metadata = { externalFile: { id: 123 } }
  31. const file = File.fromHash(File.EMPTY_FILE_HASH, metadata)
  32. const raw = file.toRaw()
  33. const fileMetadata = file.getMetadata()
  34. const rawMetadata = raw.metadata
  35. expect(rawMetadata).not.to.equal(fileMetadata)
  36. expect(rawMetadata).to.deep.equal(fileMetadata)
  37. })
  38. })
  39. describe('store', function () {
  40. it('does not return empty metadata', function () {
  41. const file = File.fromHash(File.EMPTY_FILE_HASH)
  42. const fakeBlobStore = new FakeBlobStore()
  43. return file.store(fakeBlobStore).then(raw => {
  44. expect(raw).to.eql({ hash: File.EMPTY_FILE_HASH })
  45. })
  46. })
  47. it('returns non-empty metadata', function () {
  48. const metadata = { main: true }
  49. const file = File.fromHash(File.EMPTY_FILE_HASH, metadata)
  50. const fakeBlobStore = new FakeBlobStore()
  51. return file.store(fakeBlobStore).then(raw => {
  52. expect(raw).to.eql({
  53. hash: File.EMPTY_FILE_HASH,
  54. metadata: metadata,
  55. })
  56. })
  57. })
  58. it('returns a deep clone of metadata', function () {
  59. const metadata = { externalFile: { id: 123 } }
  60. const file = File.fromHash(File.EMPTY_FILE_HASH, metadata)
  61. const fakeBlobStore = new FakeBlobStore()
  62. return file.store(fakeBlobStore).then(raw => {
  63. raw.metadata.externalFile.id = 456
  64. expect(file.getMetadata().externalFile.id).to.equal(123)
  65. })
  66. })
  67. })
  68. describe('with string data', function () {
  69. it('can be created from a string', function () {
  70. const file = File.fromString('foo')
  71. expect(file.getContent()).to.equal('foo')
  72. })
  73. })
  74. describe('with hollow string data', function () {
  75. it('can be cloned', function () {
  76. const file = File.createHollow(null, 0)
  77. expect(file.getStringLength()).to.equal(0)
  78. const clone = file.clone()
  79. expect(clone.getStringLength()).to.equal(0)
  80. })
  81. })
  82. })