2016-12-08 16:38:38 -08:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const test = require('tap').test;
|
|
|
|
|
2017-09-08 15:08:26 +08:00
|
|
|
const ScratchStorage = require('../../dist/node_commonjs2/scratch-storage');
|
2016-12-08 16:38:38 -08:00
|
|
|
|
2017-03-17 15:13:53 -07:00
|
|
|
var storage;
|
2016-12-08 16:38:38 -08:00
|
|
|
test('constructor', t => {
|
|
|
|
storage = new ScratchStorage();
|
|
|
|
t.type(storage, ScratchStorage);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 16:13:40 -07:00
|
|
|
const defaultAssetTypes = [storage.AssetType.ImageBitmap, storage.AssetType.ImageVector, storage.AssetType.Sound];
|
|
|
|
const defaultIds = {};
|
|
|
|
|
2016-12-08 16:38:38 -08:00
|
|
|
test('getDefaultAssetId', t => {
|
2017-03-17 15:13:53 -07:00
|
|
|
for (var i = 0; i < defaultAssetTypes.length; ++i) {
|
2016-12-08 16:38:38 -08:00
|
|
|
const assetType = defaultAssetTypes[i];
|
|
|
|
const id = storage.getDefaultAssetId(assetType);
|
|
|
|
t.type(id, 'string');
|
|
|
|
defaultIds[assetType.name] = id;
|
|
|
|
}
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('load', t => {
|
|
|
|
const promises = [];
|
2017-04-21 09:25:51 -07:00
|
|
|
const checkAsset = (assetType, id, asset) => {
|
|
|
|
t.type(asset, storage.Asset);
|
|
|
|
t.strictEqual(asset.assetId, id);
|
|
|
|
t.strictEqual(asset.assetType, assetType);
|
|
|
|
t.ok(asset.data.length);
|
|
|
|
|
|
|
|
const hash = crypto.createHash('md5');
|
|
|
|
hash.update(asset.data);
|
|
|
|
t.strictEqual(hash.digest('hex'), id);
|
|
|
|
};
|
2017-03-17 15:13:53 -07:00
|
|
|
for (var i = 0; i < defaultAssetTypes.length; ++i) {
|
2016-12-08 16:38:38 -08:00
|
|
|
const assetType = defaultAssetTypes[i];
|
|
|
|
const id = defaultIds[assetType.name];
|
|
|
|
|
|
|
|
const promise = storage.load(assetType, id);
|
|
|
|
t.type(promise, 'Promise');
|
|
|
|
|
|
|
|
promises.push(promise);
|
|
|
|
|
2017-04-21 09:25:51 -07:00
|
|
|
promise.then(asset => checkAsset(assetType, id, asset));
|
2016-12-08 16:38:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises);
|
|
|
|
});
|