1 | const test = require('tap').test;
|
2 |
|
3 | const ScratchStorage = require('../../dist/node/scratch-storage');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | class LoggingHelper {
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | constructor (storage, label, shouldSucceed, logContainer) {
|
18 | this.storage = storage;
|
19 | this.label = label;
|
20 | this.shouldSucceed = shouldSucceed;
|
21 | this.logContainer = logContainer;
|
22 | }
|
23 |
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | load (assetType, assetId, dataFormat) {
|
32 | this.logContainer.push(this.label);
|
33 | return this.shouldSucceed ?
|
34 | Promise.resolve(new this.storage.Asset(assetType, assetId, dataFormat, Buffer.from(this.label))) :
|
35 | Promise.reject(new Error(`This is an expected failure from ${this.label}`));
|
36 | }
|
37 | }
|
38 |
|
39 | test('ScratchStorage constructor', t => {
|
40 | const storage = new ScratchStorage();
|
41 | t.type(storage, ScratchStorage);
|
42 | t.end();
|
43 | });
|
44 |
|
45 | test('LoggingHelper constructor', t => {
|
46 | const storage = new ScratchStorage();
|
47 | const loggingHelper = new LoggingHelper(storage, 'constructor test', true, []);
|
48 | t.type(loggingHelper, LoggingHelper);
|
49 | t.end();
|
50 | });
|
51 |
|
52 | test('addHelper', t => {
|
53 | const logContainer = [];
|
54 | const storage = new ScratchStorage();
|
55 |
|
56 | const initialHelperCount = storage._helpers.length;
|
57 |
|
58 |
|
59 |
|
60 | const loggingHelpers = [
|
61 | new LoggingHelper(storage, 'first', false, logContainer),
|
62 | new LoggingHelper(storage, 'second', false, logContainer),
|
63 | new LoggingHelper(storage, 'third', true, logContainer)
|
64 | ];
|
65 |
|
66 |
|
67 | storage.addHelper(loggingHelpers[2], -50);
|
68 | storage.addHelper(loggingHelpers[0], 50);
|
69 | storage.addHelper(loggingHelpers[1], 0);
|
70 |
|
71 |
|
72 | t.equal(storage._helpers.length, initialHelperCount + loggingHelpers.length);
|
73 |
|
74 |
|
75 | t.deepEqual(logContainer, []);
|
76 |
|
77 | return storage.load(storage.AssetType.Project, '0').then(() => {
|
78 |
|
79 | t.deepEqual(logContainer, [
|
80 | 'first',
|
81 | 'second',
|
82 | 'third'
|
83 | ]);
|
84 | });
|
85 | });
|