|
| 1 | +const test = require('tap').test; |
| 2 | + |
| 3 | +const ScratchStorage = require('../../dist/node/scratch-storage'); |
| 4 | + |
| 5 | +let storage; |
| 6 | +test('constructor', t => { |
| 7 | + storage = new ScratchStorage(); |
| 8 | + t.type(storage, ScratchStorage); |
| 9 | + t.end(); |
| 10 | +}); |
| 11 | + |
| 12 | +const logEntries = []; |
| 13 | +class FakeHelper { |
| 14 | + constructor (label, shouldSucceed) { |
| 15 | + this.label = label; |
| 16 | + this.shouldSucceed = shouldSucceed; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Fetch an asset but don't process dependencies. |
| 21 | + * @param {AssetType} assetType - The type of asset to fetch. |
| 22 | + * @param {string} assetId - The ID of the asset to fetch: a project ID, MD5, etc. |
| 23 | + * @param {DataFormat} dataFormat - The file format / file extension of the asset to fetch: PNG, JPG, etc. |
| 24 | + * @return {Promise.<Asset>} A promise for the contents of the asset. |
| 25 | + */ |
| 26 | + load (assetType, assetId, dataFormat) { |
| 27 | + logEntries.push(this.label); |
| 28 | + return this.shouldSucceed ? |
| 29 | + Promise.resolve(new storage.Asset(assetType, assetId, dataFormat, Buffer.from(this.label))) : |
| 30 | + Promise.reject(`This is an expected failure from ${this.label}`); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +test('addHelper', t => { |
| 35 | + storage.addHelper(new FakeHelper('third', true), -50); |
| 36 | + storage.addHelper(new FakeHelper('first', false), 50); |
| 37 | + storage.addHelper(new FakeHelper('second', false), 0); |
| 38 | + t.end(); |
| 39 | +}); |
| 40 | + |
| 41 | +test('priorities', t => { |
| 42 | + t.deepEqual(logEntries, []); |
| 43 | + return storage.load('fakeAssetType', 'fakeAssetId', 'fakeDataFormat').then(() => { |
| 44 | + t.deepEqual(logEntries, [ |
| 45 | + 'first', |
| 46 | + 'second', |
| 47 | + 'third' |
| 48 | + ]); |
| 49 | + }); |
| 50 | +}); |
0 commit comments