|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE) |
| 4 | + */ |
| 5 | +/* global describe, it, before, after, expect, pryv, testData */ |
| 6 | + |
| 7 | +const { createId: cuid } = require('@paralleldrive/cuid2'); |
| 8 | + |
| 9 | +let conn = null; |
| 10 | +const testStreamId = 'str-' + cuid().slice(0, 8); |
| 11 | +const childStreamId = 'str-child-' + cuid().slice(0, 8); |
| 12 | + |
| 13 | +describe('[STRX] Streams', () => { |
| 14 | + before(async function () { |
| 15 | + this.timeout(15000); |
| 16 | + await testData.prepare(); |
| 17 | + conn = new pryv.Connection(testData.apiEndpointWithToken); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('[SCRX] streams.create', function () { |
| 21 | + it('[SCRA] create a root stream', async () => { |
| 22 | + const res = await conn.api([{ |
| 23 | + method: 'streams.create', |
| 24 | + params: { id: testStreamId, name: 'Test Stream' } |
| 25 | + }]); |
| 26 | + expect(res[0]).to.exist; |
| 27 | + expect(res[0].stream).to.exist; |
| 28 | + expect(res[0].stream.id).to.equal(testStreamId); |
| 29 | + expect(res[0].stream.name).to.equal('Test Stream'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('[SCRB] create a child stream', async () => { |
| 33 | + const res = await conn.api([{ |
| 34 | + method: 'streams.create', |
| 35 | + params: { id: childStreamId, name: 'Child Stream', parentId: testStreamId } |
| 36 | + }]); |
| 37 | + expect(res[0]).to.exist; |
| 38 | + expect(res[0].stream).to.exist; |
| 39 | + expect(res[0].stream.parentId).to.equal(testStreamId); |
| 40 | + }); |
| 41 | + |
| 42 | + it('[SCRC] reject duplicate stream id', async () => { |
| 43 | + const res = await conn.api([{ |
| 44 | + method: 'streams.create', |
| 45 | + params: { id: testStreamId, name: 'Duplicate' } |
| 46 | + }]); |
| 47 | + expect(res[0]).to.exist; |
| 48 | + expect(res[0].error).to.exist; |
| 49 | + expect(res[0].error.id).to.equal('item-already-exists'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('[SGTX] streams.get', function () { |
| 54 | + it('[SGTA] get all streams', async () => { |
| 55 | + const res = await conn.api([{ |
| 56 | + method: 'streams.get', |
| 57 | + params: {} |
| 58 | + }]); |
| 59 | + expect(res[0]).to.exist; |
| 60 | + expect(res[0].streams).to.exist; |
| 61 | + expect(Array.isArray(res[0].streams)).to.equal(true); |
| 62 | + expect(res[0].streams.length).to.be.gt(0); |
| 63 | + }); |
| 64 | + |
| 65 | + it('[SGTB] get stream tree includes parent and child', async () => { |
| 66 | + const res = await conn.api([{ |
| 67 | + method: 'streams.get', |
| 68 | + params: {} |
| 69 | + }]); |
| 70 | + const streams = res[0].streams; |
| 71 | + const parent = findStream(streams, testStreamId); |
| 72 | + expect(parent).to.exist; |
| 73 | + expect(parent.children).to.exist; |
| 74 | + const child = parent.children.find(s => s.id === childStreamId); |
| 75 | + expect(child).to.exist; |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('[SUPX] streams.update', function () { |
| 80 | + it('[SUPA] rename a stream', async () => { |
| 81 | + const res = await conn.api([{ |
| 82 | + method: 'streams.update', |
| 83 | + params: { id: childStreamId, update: { name: 'Renamed Child' } } |
| 84 | + }]); |
| 85 | + expect(res[0]).to.exist; |
| 86 | + expect(res[0].stream).to.exist; |
| 87 | + expect(res[0].stream.name).to.equal('Renamed Child'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('[SDLX] streams.delete', function () { |
| 92 | + it('[SDLA] trash a stream (first delete)', async () => { |
| 93 | + const res = await conn.api([{ |
| 94 | + method: 'streams.delete', |
| 95 | + params: { id: childStreamId } |
| 96 | + }]); |
| 97 | + expect(res[0]).to.exist; |
| 98 | + expect(res[0].stream).to.exist; |
| 99 | + expect(res[0].stream.trashed).to.equal(true); |
| 100 | + }); |
| 101 | + |
| 102 | + it('[SDLB] delete a trashed stream (second delete)', async () => { |
| 103 | + const res = await conn.api([{ |
| 104 | + method: 'streams.delete', |
| 105 | + params: { id: childStreamId } |
| 106 | + }]); |
| 107 | + expect(res[0]).to.exist; |
| 108 | + // After second delete, stream is gone |
| 109 | + expect(res[0].streamDeletion).to.exist; |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + // Cleanup parent stream |
| 114 | + after(async () => { |
| 115 | + if (!conn) return; |
| 116 | + await conn.api([ |
| 117 | + { method: 'streams.delete', params: { id: testStreamId } }, |
| 118 | + { method: 'streams.delete', params: { id: testStreamId } } |
| 119 | + ]); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +function findStream (streams, id) { |
| 124 | + for (const s of streams) { |
| 125 | + if (s.id === id) return s; |
| 126 | + if (s.children) { |
| 127 | + const found = findStream(s.children, id); |
| 128 | + if (found) return found; |
| 129 | + } |
| 130 | + } |
| 131 | + return null; |
| 132 | +} |
0 commit comments