1 | const md5 = require('js-md5');
|
2 | const test = require('tap').test;
|
3 |
|
4 | const ScratchStorage = require('../../dist/node/scratch-storage');
|
5 |
|
6 | let storage;
|
7 | test('constructor', t => {
|
8 | storage = new ScratchStorage();
|
9 | t.type(storage, ScratchStorage);
|
10 | t.end();
|
11 | });
|
12 |
|
13 | const defaultAssetTypes = [storage.AssetType.ImageBitmap, storage.AssetType.ImageVector, storage.AssetType.Sound];
|
14 | const defaultIds = {};
|
15 |
|
16 | test('getDefaultAssetId', t => {
|
17 | for (let i = 0; i < defaultAssetTypes.length; ++i) {
|
18 | const assetType = defaultAssetTypes[i];
|
19 | const id = storage.getDefaultAssetId(assetType);
|
20 | t.type(id, 'string');
|
21 | defaultIds[assetType.name] = id;
|
22 | }
|
23 | t.end();
|
24 | });
|
25 |
|
26 | test('load', t => {
|
27 | const promises = [];
|
28 | const checkAsset = (assetType, id, asset) => {
|
29 | t.type(asset, storage.Asset);
|
30 | t.strictEqual(asset.assetId, id);
|
31 | t.strictEqual(asset.assetType, assetType);
|
32 | t.ok(asset.data.length);
|
33 | t.strictEqual(md5(asset.data), id);
|
34 | };
|
35 | for (let i = 0; i < defaultAssetTypes.length; ++i) {
|
36 | const assetType = defaultAssetTypes[i];
|
37 | const id = defaultIds[assetType.name];
|
38 |
|
39 | const promise = storage.load(assetType, id);
|
40 | t.type(promise, 'Promise');
|
41 |
|
42 | promises.push(promise);
|
43 |
|
44 | promise.then(asset => checkAsset(assetType, id, asset));
|
45 | }
|
46 |
|
47 | return Promise.all(promises);
|
48 | });
|