-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.test.js
More file actions
130 lines (115 loc) · 3.3 KB
/
Copy pathplugin.test.js
File metadata and controls
130 lines (115 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { SitespeedioPlugin } from '../plugin.js';
function makeContext() {
return {
messageMaker(name) {
return {
make(type, data, extras) {
return { type, data, extras, source: name };
}
};
},
getLogger(channel) {
return { channel, info() {}, warn() {}, error() {} };
},
filterRegistry: { id: 'registry' },
storageManager: { id: 'storage' }
};
}
function makeQueue() {
const posted = [];
return {
posted,
postMessage(message) {
posted.push(message);
return message;
}
};
}
class TestPlugin extends SitespeedioPlugin {
async processMessage() {}
}
class NoProcessPlugin extends SitespeedioPlugin {}
test('abstract base class cannot be instantiated directly', () => {
assert.throws(
() =>
new SitespeedioPlugin({
name: 'x',
context: makeContext(),
queue: makeQueue()
}),
/Abstract plugin can't be instantiated/
);
});
test('rejects plugin names containing a dot', () => {
assert.throws(
() =>
new TestPlugin({
name: 'bad.name',
context: makeContext(),
queue: makeQueue()
}),
/can't contain dots/
);
});
test('rejects config missing required fields', () => {
assert.throws(() => new TestPlugin(), /requires a config object/);
assert.throws(
() => new TestPlugin({ context: makeContext(), queue: makeQueue() }),
/requires a config object/
);
assert.throws(
() => new TestPlugin({ name: 'p', queue: makeQueue() }),
/requires a config object/
);
assert.throws(
() => new TestPlugin({ name: 'p', context: makeContext() }),
/requires a config object/
);
});
test('getters return the values passed in via config', () => {
const context = makeContext();
const queue = makeQueue();
const options = { some: 'option' };
const plugin = new TestPlugin({ name: 'myplugin', options, context, queue });
assert.equal(plugin.getName(), 'myplugin');
assert.equal(plugin.getOptions(), options);
assert.equal(plugin.getContext(), context);
assert.equal(plugin.getStorageManager(), context.storageManager);
assert.equal(plugin.getFilterRegistry(), context.filterRegistry);
assert.equal(plugin.getLog().channel, 'sitespeed.io.plugin.myplugin');
});
test('sendMessage posts a made message to the queue', async () => {
const context = makeContext();
const queue = makeQueue();
const plugin = new TestPlugin({ name: 'myplugin', context, queue });
await plugin.sendMessage('myplugin.data', { hello: 'world' }, { extra: 1 });
assert.equal(queue.posted.length, 1);
assert.deepEqual(queue.posted[0], {
type: 'myplugin.data',
data: { hello: 'world' },
extras: { extra: 1 },
source: 'myplugin'
});
});
test('default processMessage throws when not overridden', async () => {
const plugin = new NoProcessPlugin({
name: 'noproc',
context: makeContext(),
queue: makeQueue()
});
await assert.rejects(
() => plugin.processMessage({ type: 'anything' }),
/must be implemented/
);
});
test('default open and close resolve without error', async () => {
const plugin = new TestPlugin({
name: 'myplugin',
context: makeContext(),
queue: makeQueue()
});
await plugin.open();
await plugin.close();
});