audios.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const { eq, ok, deepEq } = require('../../../helpers/assert');
  2. const { makeNetwork, makePeer } = require('../../../helpers/setup');
  3. const BLOB = '[file](&audio0000000000000000000000000000000000000000000.sha256)';
  4. describe('audios: publish + list', (t) => {
  5. t('A creates an audio, lists it', async () => {
  6. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  7. const r = await A.use('audios').createAudio(BLOB, ['music', 'electronic'], 'Track 1', 'description', '');
  8. ok(r && r.key);
  9. const list = await A.use('audios').listAll('all');
  10. eq(list.length, 1);
  11. eq(list[0].title, 'Track 1');
  12. deepEq(list[0].tags, ['music', 'electronic']);
  13. });
  14. t('A casts opinion on own audio (interesting)', async () => {
  15. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  16. const r = await A.use('audios').createAudio(BLOB, [], 'T', '', '');
  17. await A.use('audios').createOpinion(r.key, 'interesting');
  18. const list = await A.use('audios').listAll('all');
  19. ok(list[0].opinions.interesting >= 1);
  20. ok(list[0].opinions_inhabitants.includes(A.keypair.id));
  21. });
  22. t('A updates an audio (no opinions yet)', async () => {
  23. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  24. const r = await A.use('audios').createAudio(BLOB, [], 'Old', '', '');
  25. await A.use('audios').updateAudioById(r.key, BLOB, ['x'], 'New', 'newdesc', '');
  26. const list = await A.use('audios').listAll('all');
  27. eq(list[0].title, 'New');
  28. });
  29. t('A deletes audio (tombstone)', async () => {
  30. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  31. const r = await A.use('audios').createAudio(BLOB, [], 'T', '', '');
  32. await A.use('audios').deleteAudioById(r.key);
  33. const list = await A.use('audios').listAll('all');
  34. eq(list.length, 0);
  35. });
  36. t('B sees A audio (public content)', async () => {
  37. const net = makeNetwork(); const A = makePeer(net); const B = makePeer(net);
  38. A.setActor();
  39. await A.use('audios').createAudio(BLOB, [], 'Public Track', '', '');
  40. B.setActor();
  41. const list = await B.use('audios').listAll('all');
  42. eq(list.length, 1);
  43. eq(list[0].title, 'Public Track');
  44. });
  45. });
  46. const BCS_BLOB_ID = '&bcs00000000000000000000000000000000000000000000.sha256';
  47. describe('audios: BCS (Blockchain Sounds)', (t) => {
  48. t('createBcsAudio publishes an audio tagged "bcs" with composition', async () => {
  49. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  50. const composition = [
  51. { type: 'post', name: 'C4', durMs: 250, id: '%blk1.sha256' },
  52. { type: 'vote', name: 'D4', durMs: 220, id: '%blk2.sha256' },
  53. { type: 'audio', name: 'E4', durMs: 280, id: '%blk3.sha256' }
  54. ];
  55. const r = await A.use('audios').createBcsAudio(BCS_BLOB_ID, 'Title', 'Desc', composition);
  56. ok(r && r.key);
  57. const list = await A.use('audios').listAll('all');
  58. eq(list.length, 1);
  59. const a = list[0];
  60. eq(a.title, 'Title');
  61. eq(a.description, 'Desc');
  62. eq(a.url, BCS_BLOB_ID);
  63. eq(a.isBcs, true);
  64. deepEq(a.tags, ['bcs']);
  65. ok(Array.isArray(a.bcsComposition));
  66. eq(a.bcsComposition.length, 3);
  67. eq(a.bcsComposition[0].t, 'post');
  68. eq(a.bcsComposition[0].n, 'C4');
  69. eq(a.bcsComposition[0].d, 250);
  70. eq(a.bcsComposition[0].id, '%blk1.sha256');
  71. });
  72. t('BCS filter returns only audios with composition / "bcs" tag', async () => {
  73. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  74. await A.use('audios').createAudio(BLOB, ['music'], 'Plain Audio', '', '');
  75. await A.use('audios').createBcsAudio(BCS_BLOB_ID, 'BCS One', '', [
  76. { type: 'post', name: 'C4', durMs: 250 }
  77. ]);
  78. const all = await A.use('audios').listAll('all');
  79. eq(all.length, 2);
  80. const bcsOnly = await A.use('audios').listAll('bcs');
  81. eq(bcsOnly.length, 1);
  82. eq(bcsOnly[0].title, 'BCS One');
  83. eq(bcsOnly[0].isBcs, true);
  84. });
  85. t('non-BCS audio has isBcs=false and null bcsComposition', async () => {
  86. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  87. const r = await A.use('audios').createAudio(BLOB, ['music'], 'Plain', '', '');
  88. const a = await A.use('audios').getAudioById(r.key);
  89. eq(a.isBcs, false);
  90. eq(a.bcsComposition, null);
  91. });
  92. t('B sees A BCS audio with composition preserved across feed boundary', async () => {
  93. const net = makeNetwork(); const A = makePeer(net); const B = makePeer(net);
  94. A.setActor();
  95. await A.use('audios').createBcsAudio(BCS_BLOB_ID, 'Shared BCS', 'd', [
  96. { type: 'post', name: 'C4', durMs: 250, id: '%blk1.sha256' },
  97. { type: 'vote', name: 'G4', durMs: 300, id: '%blk2.sha256' }
  98. ]);
  99. B.setActor();
  100. const list = await B.use('audios').listAll('bcs');
  101. eq(list.length, 1);
  102. eq(list[0].author, A.keypair.id);
  103. eq(list[0].isBcs, true);
  104. eq(list[0].bcsComposition.length, 2);
  105. });
  106. t('createBcsAudio drops malformed notes and caps to 512 entries', async () => {
  107. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  108. const noisy = [
  109. { type: 'post', name: 'C4', durMs: 200, id: '%a.sha256' },
  110. { type: '', name: 'D4', durMs: 200 },
  111. { type: 'vote', name: '', durMs: 200 },
  112. { name: 'F4' },
  113. { type: 'audio', name: 'A4', durMs: 200, id: '%b.sha256' }
  114. ];
  115. const r = await A.use('audios').createBcsAudio(BCS_BLOB_ID, 'T', '', noisy);
  116. const a = await A.use('audios').getAudioById(r.key);
  117. eq(a.bcsComposition.length, 2);
  118. eq(a.bcsComposition[0].t, 'post');
  119. eq(a.bcsComposition[1].t, 'audio');
  120. });
  121. });