file.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, undefined, metadata)
  22. expect(file.toRaw()).to.eql({
  23. hash: File.EMPTY_FILE_HASH,
  24. 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, undefined, 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', async function () {
  41. const file = File.fromHash(File.EMPTY_FILE_HASH)
  42. const fakeBlobStore = new FakeBlobStore()
  43. const raw = await file.store(fakeBlobStore)
  44. expect(raw).to.eql({ hash: File.EMPTY_FILE_HASH })
  45. })
  46. it('returns non-empty metadata', async function () {
  47. const metadata = { main: true }
  48. const file = File.fromHash(File.EMPTY_FILE_HASH, undefined, metadata)
  49. const fakeBlobStore = new FakeBlobStore()
  50. const raw = await file.store(fakeBlobStore)
  51. expect(raw).to.eql({
  52. hash: File.EMPTY_FILE_HASH,
  53. metadata,
  54. })
  55. })
  56. it('returns a deep clone of metadata', async function () {
  57. const metadata = { externalFile: { id: 123 } }
  58. const file = File.fromHash(File.EMPTY_FILE_HASH, undefined, metadata)
  59. const fakeBlobStore = new FakeBlobStore()
  60. const raw = await file.store(fakeBlobStore)
  61. raw.metadata.externalFile.id = 456
  62. expect(file.getMetadata().externalFile.id).to.equal(123)
  63. })
  64. })
  65. describe('with string data', function () {
  66. it('can be created from a string', function () {
  67. const file = File.fromString('foo')
  68. expect(file.getContent()).to.equal('foo')
  69. })
  70. })
  71. describe('with hollow string data', function () {
  72. it('can be cloned', function () {
  73. const file = File.createHollow(null, 0)
  74. expect(file.getStringLength()).to.equal(0)
  75. const clone = file.clone()
  76. expect(clone.getStringLength()).to.equal(0)
  77. })
  78. })
  79. it('getComments() returns an empty comment list', function () {
  80. const file = File.fromString('foo')
  81. expect(file.getComments().toRaw()).to.eql([])
  82. })
  83. })