UNPKG

3.49 kBJavaScriptView Raw
1const md5 = require('js-md5');
2const test = require('tap').test;
3
4const ScratchStorage = require('../../dist/node/scratch-storage');
5
6var storage;
7test('constructor', t => {
8 storage = new ScratchStorage();
9 t.type(storage, ScratchStorage);
10 t.end();
11});
12
13/**
14 *
15 * @type {AssetTestInfo[]}
16 * @typedef {object} AssetTestInfo
17 * @property {AssetType} type - The type of the asset.
18 * @property {string} id - The asset's unique ID.
19 * @property {DataFormat} [ext] - Optional: the asset's data format / file extension.
20 */
21const testAssets = [
22 {
23 type: storage.AssetType.Project,
24 id: '117504922',
25 md5: null // don't check MD5 for project without revision ID
26 },
27 {
28 type: storage.AssetType.Project,
29 id: '117504922.d6ae1ffb76f2bc83421cd3f40fc4fd57',
30 md5: '1225460702e149727de28bff4cfd9e23'
31 },
32 {
33 type: storage.AssetType.ImageVector,
34 id: 'f88bf1935daea28f8ca098462a31dbb0', // cat1-a
35 md5: 'f88bf1935daea28f8ca098462a31dbb0'
36 },
37 {
38 type: storage.AssetType.ImageVector,
39 id: '6e8bd9ae68fdb02b7e1e3df656a75635', // cat1-b
40 md5: '6e8bd9ae68fdb02b7e1e3df656a75635',
41 ext: storage.DataFormat.SVG
42 },
43 {
44 type: storage.AssetType.ImageBitmap,
45 id: '7e24c99c1b853e52f8e7f9004416fa34', // squirrel
46 md5: '7e24c99c1b853e52f8e7f9004416fa34'
47 },
48 {
49 type: storage.AssetType.ImageBitmap,
50 id: '66895930177178ea01d9e610917f8acf', // bus
51 md5: '66895930177178ea01d9e610917f8acf',
52 ext: storage.DataFormat.PNG
53 },
54 {
55 type: storage.AssetType.ImageBitmap,
56 id: 'fe5e3566965f9de793beeffce377d054', // building at MIT
57 md5: 'fe5e3566965f9de793beeffce377d054',
58 ext: storage.DataFormat.JPG
59 },
60 {
61 type: storage.AssetType.Sound,
62 id: '83c36d806dc92327b9e7049a565c6bff', // meow
63 md5: '83c36d806dc92327b9e7049a565c6bff' // wat
64 }
65];
66
67test('addWebSource', t => {
68 t.doesNotThrow(() => {
69 storage.addWebSource(
70 [storage.AssetType.Project],
71 asset => {
72 const idParts = asset.assetId.split('.');
73 return idParts[1] ?
74 `https://cdn.projects.scratch.mit.edu/internalapi/project/${idParts[0]}/get/${idParts[1]}` :
75 `https://cdn.projects.scratch.mit.edu/internalapi/project/${idParts[0]}/get/`;
76 });
77 });
78 t.doesNotThrow(() => {
79 storage.addWebSource(
80 [storage.AssetType.ImageVector, storage.AssetType.ImageBitmap, storage.AssetType.Sound],
81 asset => `https://cdn.assets.scratch.mit.edu/internalapi/asset/${asset.assetId}.${asset.dataFormat}/get/`
82 );
83 });
84 t.end();
85});
86
87test('load', t => {
88 const promises = [];
89 const checkAsset = (assetInfo, asset) => {
90 t.type(asset, storage.Asset);
91 t.strictEqual(asset.assetId, assetInfo.id);
92 t.strictEqual(asset.assetType, assetInfo.type);
93 t.ok(asset.data.length);
94
95 if (assetInfo.md5) {
96 t.strictEqual(md5(asset.data), assetInfo.md5);
97 }
98 };
99 for (var i = 0; i < testAssets.length; ++i) {
100 const assetInfo = testAssets[i];
101
102 var promise = storage.load(assetInfo.type, assetInfo.id, assetInfo.ext);
103 t.type(promise, 'Promise');
104
105 promise = promise.then(asset => checkAsset(assetInfo, asset));
106 promises.push(promise);
107 }
108
109 return Promise.all(promises);
110});