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 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const testAssets = [
|
23 | {
|
24 | type: storage.AssetType.Project,
|
25 | id: '117504922',
|
26 | md5: null
|
27 | },
|
28 | {
|
29 | type: storage.AssetType.Project,
|
30 | id: '117504922.d6ae1ffb76f2bc83421cd3f40fc4fd57',
|
31 | md5: '1225460702e149727de28bff4cfd9e23'
|
32 | },
|
33 | {
|
34 | type: storage.AssetType.ImageVector,
|
35 | id: 'f88bf1935daea28f8ca098462a31dbb0',
|
36 | md5: 'f88bf1935daea28f8ca098462a31dbb0'
|
37 | },
|
38 | {
|
39 | type: storage.AssetType.ImageVector,
|
40 | id: '6e8bd9ae68fdb02b7e1e3df656a75635',
|
41 | md5: '6e8bd9ae68fdb02b7e1e3df656a75635',
|
42 | ext: storage.DataFormat.SVG
|
43 | },
|
44 | {
|
45 | type: storage.AssetType.ImageBitmap,
|
46 | id: '7e24c99c1b853e52f8e7f9004416fa34',
|
47 | md5: '7e24c99c1b853e52f8e7f9004416fa34'
|
48 | },
|
49 | {
|
50 | type: storage.AssetType.ImageBitmap,
|
51 | id: '66895930177178ea01d9e610917f8acf',
|
52 | md5: '66895930177178ea01d9e610917f8acf',
|
53 | ext: storage.DataFormat.PNG
|
54 | },
|
55 | {
|
56 | type: storage.AssetType.ImageBitmap,
|
57 | id: 'fe5e3566965f9de793beeffce377d054',
|
58 | md5: 'fe5e3566965f9de793beeffce377d054',
|
59 | ext: storage.DataFormat.JPG
|
60 | },
|
61 | {
|
62 | type: storage.AssetType.Sound,
|
63 | id: '83c36d806dc92327b9e7049a565c6bff',
|
64 | md5: '83c36d806dc92327b9e7049a565c6bff'
|
65 | }
|
66 | ];
|
67 |
|
68 | test('addWebSource', t => {
|
69 | t.doesNotThrow(() => {
|
70 | storage.addWebSource(
|
71 | [storage.AssetType.Project],
|
72 | asset => {
|
73 | const idParts = asset.assetId.split('.');
|
74 | return idParts[1] ?
|
75 | `https://cdn.projects.scratch.mit.edu/internalapi/project/${idParts[0]}/get/${idParts[1]}` :
|
76 | `https://cdn.projects.scratch.mit.edu/internalapi/project/${idParts[0]}/get/`;
|
77 | });
|
78 | });
|
79 | t.doesNotThrow(() => {
|
80 | storage.addWebSource(
|
81 | [storage.AssetType.ImageVector, storage.AssetType.ImageBitmap, storage.AssetType.Sound],
|
82 | asset => `https://cdn.assets.scratch.mit.edu/internalapi/asset/${asset.assetId}.${asset.dataFormat}/get/`
|
83 | );
|
84 | });
|
85 | t.end();
|
86 | });
|
87 |
|
88 | test('load', t => {
|
89 | const promises = [];
|
90 | const checkAsset = (assetInfo, asset) => {
|
91 | t.type(asset, storage.Asset);
|
92 | t.strictEqual(asset.assetId, assetInfo.id);
|
93 | t.strictEqual(asset.assetType, assetInfo.type);
|
94 | t.ok(asset.data.length);
|
95 |
|
96 |
|
97 | t.true(asset.clean);
|
98 |
|
99 | if (assetInfo.md5) {
|
100 | t.strictEqual(md5(asset.data), assetInfo.md5);
|
101 | }
|
102 | };
|
103 | for (let i = 0; i < testAssets.length; ++i) {
|
104 | const assetInfo = testAssets[i];
|
105 |
|
106 | let promise = storage.load(assetInfo.type, assetInfo.id, assetInfo.ext);
|
107 | t.type(promise, 'Promise');
|
108 |
|
109 | promise = promise.then(asset => checkAsset(assetInfo, asset));
|
110 | promises.push(promise);
|
111 | }
|
112 |
|
113 | return Promise.all(promises);
|
114 | });
|