gell-dispatch 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +45 -0
- package/.mocharc.json +4 -0
- package/README.md +4 -2
- package/bus.js +169 -0
- package/docs/superpowers/plans/2026-11-25-registry.md +215 -0
- package/docs/superpowers/specs/2026-11-25-registry-design.md +87 -0
- package/kind/action.js +6 -0
- package/kind/event.js +38 -0
- package/kind/snapshot.js +6 -0
- package/kind/type.js +21 -0
- package/middleware/arguments.js +50 -0
- package/middleware/attributes.js +38 -0
- package/middleware/roles.js +81 -0
- package/package.json +29 -4
- package/registry.js +97 -0
- package/stream/sink.js +35 -0
- package/test/bootstrap.js +25 -0
- package/test/kind/test-action.js +81 -0
- package/test/kind/test-snapshot.js +81 -0
- package/test/kind/test-type.js +54 -0
- package/test/middleware/test-arguments.js +565 -0
- package/test/middleware/test-attributes.js +79 -0
- package/test/middleware/test-roles.js +176 -0
- package/test/resolver.js +26 -0
- package/test/stream/test-sink.js +112 -0
- package/test/test-bus.js +383 -0
- package/test/test-registry.js +151 -0
- package/test/util/test-chain.js +164 -0
- package/test/util/test-error.js +33 -0
- package/test/util/test-parameters.js +30 -0
- package/test/util/test-restrict.js +73 -0
- package/util/chain.js +9 -0
- package/util/error.js +15 -0
- package/util/parameters.js +22 -0
- package/util/restrict.js +27 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
|
|
3
|
+
const { default: State } = require('gell-state');
|
|
4
|
+
|
|
5
|
+
const roles = require('../../middleware/roles');
|
|
6
|
+
|
|
7
|
+
describe('roles middleware', function() {
|
|
8
|
+
let middleware, event, resumed;
|
|
9
|
+
|
|
10
|
+
function resume() {
|
|
11
|
+
resumed = true;
|
|
12
|
+
|
|
13
|
+
return 'resumed';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function invocation(triggerName, triggerSpec={}) {
|
|
17
|
+
return {
|
|
18
|
+
route: `unittest.event#${triggerName}`,
|
|
19
|
+
eventName: 'unittest.event',
|
|
20
|
+
triggerName,
|
|
21
|
+
triggerSpec
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function caller(attributes) {
|
|
26
|
+
const caller_ = new State();
|
|
27
|
+
Object.keys(attributes).forEach(k => caller_.set(k, attributes[k]));
|
|
28
|
+
|
|
29
|
+
return caller_;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function rejection(event) {
|
|
33
|
+
try {
|
|
34
|
+
middleware(event, resume);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return e;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
assert.fail('should have thrown exception');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
beforeEach(function() {
|
|
43
|
+
middleware = roles();
|
|
44
|
+
resumed = false;
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
describe('trigger without roles', function() {
|
|
48
|
+
beforeEach(function() {
|
|
49
|
+
event = { __invocationSpec: invocation('action') };
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('allows caller with any role', function() {
|
|
53
|
+
event.caller = caller({ role: 'driver' });
|
|
54
|
+
|
|
55
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
56
|
+
assert(resumed);
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('rejects caller without a role (401)', function() {
|
|
60
|
+
event.caller = caller({});
|
|
61
|
+
|
|
62
|
+
const e = rejection(event);
|
|
63
|
+
|
|
64
|
+
assert.strictEqual(e.name, 'UnauthorizedError');
|
|
65
|
+
assert.strictEqual(e.status, 401);
|
|
66
|
+
assert.strictEqual(e.message, 'call to event trigger (route="unittest.event#action") requires an authorized caller');
|
|
67
|
+
assert(!resumed);
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('rejects missing caller (401)', function() {
|
|
71
|
+
assert.strictEqual(rejection(event).status, 401);
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
describe('trigger with roles', function() {
|
|
76
|
+
beforeEach(function() {
|
|
77
|
+
event = { __invocationSpec: invocation('action', { roles: ['administrator', 'manager'] }) };
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('allows caller with an allowed role', function() {
|
|
81
|
+
event.caller = caller({ role: 'manager' });
|
|
82
|
+
|
|
83
|
+
middleware(event, resume);
|
|
84
|
+
|
|
85
|
+
assert(resumed);
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('rejects caller without an allowed role (403)', function() {
|
|
89
|
+
event.caller = caller({ role: 'driver' });
|
|
90
|
+
|
|
91
|
+
const e = rejection(event);
|
|
92
|
+
|
|
93
|
+
assert.strictEqual(e.name, 'ForbiddenError');
|
|
94
|
+
assert.strictEqual(e.status, 403);
|
|
95
|
+
assert.strictEqual(e.message, 'call to event trigger (route="unittest.event#action") is not allowed for caller (roles="driver")');
|
|
96
|
+
assert(!resumed);
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('supports caller with a list of roles', function() {
|
|
100
|
+
event.caller = caller({ roles: ['driver', 'administrator'] });
|
|
101
|
+
|
|
102
|
+
middleware(event, resume);
|
|
103
|
+
|
|
104
|
+
assert(resumed);
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('supports plain object caller', function() {
|
|
108
|
+
event.caller = { role: 'administrator' };
|
|
109
|
+
|
|
110
|
+
middleware(event, resume);
|
|
111
|
+
|
|
112
|
+
assert(resumed);
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('checks api triggers', function() {
|
|
116
|
+
event.__invocationSpec = invocation('api', { roles: ['administrator'] });
|
|
117
|
+
event.caller = caller({ role: 'driver' });
|
|
118
|
+
|
|
119
|
+
assert.strictEqual(rejection(event).status, 403);
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('public trigger', function() {
|
|
124
|
+
beforeEach(function() {
|
|
125
|
+
event = { __invocationSpec: invocation('action', { public: true }) };
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('allows missing caller', function() {
|
|
129
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
130
|
+
assert(resumed);
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('allows caller without a role', function() {
|
|
134
|
+
event.caller = caller({});
|
|
135
|
+
|
|
136
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
137
|
+
assert(resumed);
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it('ignores roles on a public trigger', function() {
|
|
141
|
+
event = { __invocationSpec: invocation('api', { public: true, roles: ['administrator'] }), caller: caller({ role: 'driver' }) };
|
|
142
|
+
|
|
143
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
describe('unchecked triggers', function() {
|
|
148
|
+
it('does not check effect triggers', function() {
|
|
149
|
+
event = { __invocationSpec: invocation('effect', { roles: ['administrator'] }) };
|
|
150
|
+
|
|
151
|
+
middleware(event, resume);
|
|
152
|
+
|
|
153
|
+
assert(resumed);
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('checks configured triggers only', function() {
|
|
157
|
+
middleware = roles({ triggers: ['job'] });
|
|
158
|
+
event = { __invocationSpec: invocation('action', { roles: ['administrator'] }) };
|
|
159
|
+
|
|
160
|
+
middleware(event, resume);
|
|
161
|
+
|
|
162
|
+
assert(resumed);
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
describe('options', function() {
|
|
167
|
+
it('uses custom rolesOf', function() {
|
|
168
|
+
middleware = roles({ rolesOf: caller => caller.groups });
|
|
169
|
+
event = { caller: { groups: ['administrator'] }, __invocationSpec: invocation('action', { roles: ['administrator'] }) };
|
|
170
|
+
|
|
171
|
+
middleware(event, resume);
|
|
172
|
+
|
|
173
|
+
assert(resumed);
|
|
174
|
+
})
|
|
175
|
+
})
|
|
176
|
+
})
|
package/test/resolver.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const _ = require('lodash');
|
|
2
|
+
|
|
3
|
+
const { default: State } = require('gell-state');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Resolves event routes ("event.name#trigger") from a map of event modules
|
|
7
|
+
* - yields [module, { resource, f }, { resource: eventName, f: triggerName }] as gell-dispatch/bus expects
|
|
8
|
+
*
|
|
9
|
+
* NOTE: test double for gell-session/dispatch/resolver (directory)
|
|
10
|
+
*
|
|
11
|
+
* @param {*} events
|
|
12
|
+
*/
|
|
13
|
+
module.exports = function* (events) {
|
|
14
|
+
let name = yield new State();
|
|
15
|
+
|
|
16
|
+
while (true) {
|
|
17
|
+
if (_.isString(name)) {
|
|
18
|
+
const [resourceName, functionName] = name.split('#');
|
|
19
|
+
const resource = resourceName ? events[resourceName] : undefined;
|
|
20
|
+
const f = resource && functionName ? resource[functionName] : undefined;
|
|
21
|
+
|
|
22
|
+
name = yield [resource, { resource, f }, { resource: resourceName, f: functionName }];
|
|
23
|
+
}
|
|
24
|
+
else name = yield [];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
|
|
3
|
+
const { eventSink } = require('../../stream/sink');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Store whose write transactions record the operations written to them and whether they were committed
|
|
7
|
+
*/
|
|
8
|
+
function fakeStore() {
|
|
9
|
+
const transactions = [];
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
transactions,
|
|
13
|
+
write() {
|
|
14
|
+
const txn = { operations: [], committed: false };
|
|
15
|
+
transactions.push(txn);
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
next(operation) { txn.operations.push(operation); return { value: undefined }; },
|
|
19
|
+
return() { txn.committed = true; return { value: Promise.resolve(), done: true }; },
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('event sink', function() {
|
|
26
|
+
let store, $sink, received;
|
|
27
|
+
|
|
28
|
+
const EVENT = {
|
|
29
|
+
__metadata: { name: 'unittest.event' },
|
|
30
|
+
|
|
31
|
+
effect: async ({ context, deps }) => {
|
|
32
|
+
received.push({ context, deps, committed: store.transactions.map(t => t.committed) });
|
|
33
|
+
|
|
34
|
+
context.$txn.next({ operation: 'put', item: { id: context.id } });
|
|
35
|
+
|
|
36
|
+
return `effect ${context.id}`;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
beforeEach(function() {
|
|
41
|
+
store = fakeStore();
|
|
42
|
+
received = [];
|
|
43
|
+
|
|
44
|
+
this.deps.define('store', store);
|
|
45
|
+
|
|
46
|
+
$sink = eventSink(EVENT, this.deps);
|
|
47
|
+
$sink.next();
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('invokes the event effect with the context and deps', async function() {
|
|
51
|
+
await $sink.next({ context: { id: '100' } }).value;
|
|
52
|
+
|
|
53
|
+
assert.strictEqual(received.length, 1);
|
|
54
|
+
assert.strictEqual(received[0].context.id, '100');
|
|
55
|
+
assert.strictEqual(received[0].deps, this.deps);
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('resolves the result of the effect', async function() {
|
|
59
|
+
assert.strictEqual(await $sink.next({ context: { id: '100' } }).value, 'effect 100');
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('provides a new transaction and the deps in the context', async function() {
|
|
63
|
+
await $sink.next({ context: { id: '100' } }).value;
|
|
64
|
+
|
|
65
|
+
const { context } = received[0];
|
|
66
|
+
|
|
67
|
+
assert(context.$txn);
|
|
68
|
+
assert.strictEqual(context.deps, this.deps);
|
|
69
|
+
assert.deepStrictEqual(store.transactions[0].operations, [{ operation: 'put', item: { id: '100' } }]);
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('commits the transaction after the effect', async function() {
|
|
73
|
+
await $sink.next({ context: { id: '100' } }).value;
|
|
74
|
+
|
|
75
|
+
assert.deepStrictEqual(received[0].committed, [false]);
|
|
76
|
+
assert.strictEqual(store.transactions[0].committed, true);
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('uses a transaction per invocation', async function() {
|
|
80
|
+
await $sink.next({ context: { id: '100' } }).value;
|
|
81
|
+
await $sink.next({ context: { id: '200' } }).value;
|
|
82
|
+
|
|
83
|
+
assert.strictEqual(store.transactions.length, 2);
|
|
84
|
+
assert.deepStrictEqual(store.transactions[1].operations, [{ operation: 'put', item: { id: '200' } }]);
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('invokes the effect without arguments', async function() {
|
|
88
|
+
const event = { ...EVENT, effect: async ({ context }) => Object.keys(context).sort() };
|
|
89
|
+
|
|
90
|
+
$sink = eventSink(event, this.deps);
|
|
91
|
+
$sink.next();
|
|
92
|
+
|
|
93
|
+
assert.deepStrictEqual(await $sink.next().value, ['$txn', 'deps']);
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('does not commit the transaction if the effect fails', async function() {
|
|
97
|
+
const event = { ...EVENT, effect: async () => { throw new Error('failed'); } };
|
|
98
|
+
|
|
99
|
+
$sink = eventSink(event, this.deps);
|
|
100
|
+
$sink.next();
|
|
101
|
+
|
|
102
|
+
await assert.rejects(async z => $sink.next({ context: {} }).value, /failed/);
|
|
103
|
+
|
|
104
|
+
assert.strictEqual(store.transactions[0].committed, false);
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('logs the invocation', async function() {
|
|
108
|
+
await $sink.next({ context: { id: '100' } }).value;
|
|
109
|
+
|
|
110
|
+
assert.deepStrictEqual(this.logger.messages, [{ level: 'info', message: 'invoking event (name=unittest.event)' }]);
|
|
111
|
+
})
|
|
112
|
+
})
|
package/test/test-bus.js
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
|
|
4
|
+
const { default: init } = require('gell-future');
|
|
5
|
+
const { compile } = require('gell-domain/type/compiler');
|
|
6
|
+
|
|
7
|
+
const bus = require('../bus');
|
|
8
|
+
const resolver = require('./resolver');
|
|
9
|
+
|
|
10
|
+
const attributeMiddleware = require('../middleware/attributes');
|
|
11
|
+
const argumentMiddleware = require('../middleware/arguments')(compile());
|
|
12
|
+
|
|
13
|
+
const EVENTS = {
|
|
14
|
+
nometadata: {
|
|
15
|
+
action: z => 'gello!'
|
|
16
|
+
},
|
|
17
|
+
simple: {
|
|
18
|
+
__metadata: {
|
|
19
|
+
triggers: {
|
|
20
|
+
action: {}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
action: async event => 'success'
|
|
25
|
+
},
|
|
26
|
+
paramterized: {
|
|
27
|
+
__metadata: {
|
|
28
|
+
triggers: {
|
|
29
|
+
effect: {
|
|
30
|
+
params: {
|
|
31
|
+
message: 'string'
|
|
32
|
+
},
|
|
33
|
+
context: {
|
|
34
|
+
session$: 'complex'
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
action: {
|
|
38
|
+
params: {
|
|
39
|
+
message: 'string',
|
|
40
|
+
count: {
|
|
41
|
+
type: 'number',
|
|
42
|
+
optional: true
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
context: {
|
|
46
|
+
user$: {
|
|
47
|
+
type: 'complex',
|
|
48
|
+
optional: true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
effect: z => {},
|
|
56
|
+
action: z => {}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('event bus', function() {
|
|
61
|
+
let events, $resolver, $bus;
|
|
62
|
+
|
|
63
|
+
beforeEach(function() {
|
|
64
|
+
events = { ...EVENTS };
|
|
65
|
+
|
|
66
|
+
[$resolver] = init(resolver(events));
|
|
67
|
+
|
|
68
|
+
const middleware = [
|
|
69
|
+
attributeMiddleware,
|
|
70
|
+
argumentMiddleware,
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
[$bus] = init(bus($resolver, middleware, this.deps));
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('can be initialized', function() {
|
|
77
|
+
assert($bus);
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
describe('invalid event modules', function() {
|
|
81
|
+
let invalidEvent;
|
|
82
|
+
|
|
83
|
+
beforeEach(function() {
|
|
84
|
+
invalidEvent = {};
|
|
85
|
+
|
|
86
|
+
events.invalid = invalidEvent;
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('fails if missing event name in route', async function() {
|
|
90
|
+
await assert.rejects(async z => $bus.next({ route: '#action' }).value, { name: 'AssertionError' });
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('fails if missing trigger name in route', async function() {
|
|
94
|
+
await assert.rejects(async z => $bus.next({ route: 'invalid' }).value, { name: 'AssertionError' });
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('fails if no trigger function provided', async function() {
|
|
98
|
+
await assert.rejects(async z => $bus.next({ route: 'invalid#action' }).value, {
|
|
99
|
+
name: 'InvalidEventModule',
|
|
100
|
+
route: 'invalid#action',
|
|
101
|
+
eventName: 'invalid',
|
|
102
|
+
trigger: 'action',
|
|
103
|
+
message: 'route (name="invalid") does not define trigger function (name="action")'
|
|
104
|
+
});
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
describe('no metadata', function() {
|
|
109
|
+
let caller;
|
|
110
|
+
|
|
111
|
+
beforeEach(function() {
|
|
112
|
+
caller = 'user';
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('invokes trigger function', async function() {
|
|
116
|
+
const res = await $bus.next({ route: 'nometadata#action' }).value;
|
|
117
|
+
|
|
118
|
+
assert.strictEqual(res, 'gello!');
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('constructs event correctly', async function() {
|
|
122
|
+
const event = await $bus.next({ route: 'nometadata#action', caller, invoke: false }).value;
|
|
123
|
+
|
|
124
|
+
// NOTE: each invocation runs with its own branch of the bus deps
|
|
125
|
+
assert.notStrictEqual(event.deps, this.deps);
|
|
126
|
+
assert.strictEqual(event.deps._parent, this.deps);
|
|
127
|
+
assert.strictEqual(event.caller, caller);
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('provides invocation spec without metadata', async function() {
|
|
131
|
+
const event = await $bus.next({ route: 'nometadata#action', invoke: false }).value;
|
|
132
|
+
|
|
133
|
+
assert.deepStrictEqual(event.__invocationSpec, {
|
|
134
|
+
route: 'nometadata#action',
|
|
135
|
+
eventName: 'nometadata',
|
|
136
|
+
triggerName: 'action',
|
|
137
|
+
triggerSpec: {},
|
|
138
|
+
metadata: {}
|
|
139
|
+
});
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
describe('minimal metadata', function() {
|
|
144
|
+
let minimal;
|
|
145
|
+
|
|
146
|
+
beforeEach(function() {
|
|
147
|
+
minimal = {
|
|
148
|
+
__metadata: {},
|
|
149
|
+
|
|
150
|
+
action: z => {}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
events.minimal = minimal;
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('does not fail if empty', function() {
|
|
157
|
+
return $bus.next({ route: 'minimal#action' }).value;
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('does not fail if triggers empty', function() {
|
|
161
|
+
minimal.__metadata.triggers = {};
|
|
162
|
+
|
|
163
|
+
return $bus.next({ route: 'minimal#action' }).value;
|
|
164
|
+
})
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
describe('action trigger', function() {
|
|
168
|
+
beforeEach(function() {
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
it('invokes action', async function() {
|
|
172
|
+
const res = await $bus.next({ route: 'simple#action' }).value;
|
|
173
|
+
|
|
174
|
+
assert.strictEqual(res, 'success');
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it('route string only', async function() {
|
|
178
|
+
const res = await $bus.next('simple#action').value;
|
|
179
|
+
|
|
180
|
+
assert.strictEqual(res, 'success');
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
describe('event arguments', function() {
|
|
185
|
+
beforeEach(function() {
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
it('fails if required argment not provided', async function() {
|
|
189
|
+
try {
|
|
190
|
+
const trigger = {
|
|
191
|
+
route: 'paramterized#effect',
|
|
192
|
+
context: {
|
|
193
|
+
session$: {}
|
|
194
|
+
},
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
await $bus.next(trigger).value;
|
|
198
|
+
|
|
199
|
+
assert.fail('should have thrown exception');
|
|
200
|
+
} catch (e) {
|
|
201
|
+
assert(_.isError(e));
|
|
202
|
+
|
|
203
|
+
const { name, message } = e;
|
|
204
|
+
|
|
205
|
+
// console.debug('#####', e);
|
|
206
|
+
|
|
207
|
+
assert.strictEqual(name, 'ParameterValidationError');
|
|
208
|
+
assert.strictEqual(message, 'message is required');
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
it('does not fail if parameter is optional', async function() {
|
|
213
|
+
const trigger = {
|
|
214
|
+
route: 'paramterized#action',
|
|
215
|
+
params: {
|
|
216
|
+
message: 'gello!'
|
|
217
|
+
},
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
await $bus.next(trigger).value;
|
|
221
|
+
})
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
describe('event context', function() {
|
|
225
|
+
beforeEach(function() {
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('fails if required attribute not provided', async function() {
|
|
229
|
+
try {
|
|
230
|
+
const trigger = {
|
|
231
|
+
route: 'paramterized#effect',
|
|
232
|
+
params: {
|
|
233
|
+
message: 'gello!'
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
await $bus.next(trigger).value;
|
|
238
|
+
|
|
239
|
+
assert.fail('should have thrown exception');
|
|
240
|
+
} catch (e) {
|
|
241
|
+
assert(_.isError(e));
|
|
242
|
+
|
|
243
|
+
const { name, message } = e;
|
|
244
|
+
|
|
245
|
+
assert.strictEqual(name, 'RequiredAttributeError');
|
|
246
|
+
assert.strictEqual(message, 'call to event trigger (route="paramterized#effect") does not provide required context attribute (name="session$")');
|
|
247
|
+
}
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
it('does not fail if attribute is optional', async function() {
|
|
251
|
+
const trigger = {
|
|
252
|
+
route: 'paramterized#action',
|
|
253
|
+
params: {
|
|
254
|
+
message: 'gello!'
|
|
255
|
+
},
|
|
256
|
+
context: {
|
|
257
|
+
session$: {}
|
|
258
|
+
},
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
await $bus.next(trigger).value;
|
|
262
|
+
})
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
describe('overridding dependencies', function() {
|
|
266
|
+
beforeEach(function() {
|
|
267
|
+
this.deps.define('message', 'gello');
|
|
268
|
+
|
|
269
|
+
events.override = {
|
|
270
|
+
__metadata: {
|
|
271
|
+
triggers: {
|
|
272
|
+
job: {}
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
|
|
276
|
+
job: event => {
|
|
277
|
+
const { deps } = event;
|
|
278
|
+
|
|
279
|
+
return deps.resolveSingle('message')
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
it('uses overridden dependency', async function() {
|
|
285
|
+
const e = {
|
|
286
|
+
route: 'override#job',
|
|
287
|
+
deps: this.deps.branch({ message: 'world!' })
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const message = await $bus.next(e).value;
|
|
291
|
+
|
|
292
|
+
assert.strictEqual(message, 'world!');
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
it('resolves correctly if not overriden', async function() {
|
|
296
|
+
const e = {
|
|
297
|
+
route: 'override#job',
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const message = await $bus.next(e).value;
|
|
301
|
+
|
|
302
|
+
assert.strictEqual(message, 'gello');
|
|
303
|
+
})
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
describe('invocation dependency container', function() {
|
|
307
|
+
beforeEach(function() {
|
|
308
|
+
this.deps.define('message', 'gello');
|
|
309
|
+
|
|
310
|
+
// defines an invocation scoped dependency, then reads it back
|
|
311
|
+
events.define = {
|
|
312
|
+
__metadata: { triggers: { job: {} } },
|
|
313
|
+
job: event => {
|
|
314
|
+
const { deps, params } = event;
|
|
315
|
+
|
|
316
|
+
deps.define('message', params.message);
|
|
317
|
+
|
|
318
|
+
return deps.resolveSingle('message');
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
events.read = {
|
|
323
|
+
__metadata: { triggers: { job: {} } },
|
|
324
|
+
job: event => event.deps.resolveSingle('message')
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
it('is a branch of the bus deps', async function() {
|
|
329
|
+
const event = await $bus.next({ route: 'read#job', invoke: false }).value;
|
|
330
|
+
|
|
331
|
+
assert.strictEqual(event.deps._parent, this.deps);
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
it('is a branch of the deps passed in', async function() {
|
|
335
|
+
const deps = this.deps.branch({ message: 'world!' });
|
|
336
|
+
|
|
337
|
+
const event = await $bus.next({ route: 'read#job', deps, invoke: false }).value;
|
|
338
|
+
|
|
339
|
+
assert.strictEqual(event.deps._parent, deps);
|
|
340
|
+
assert.strictEqual(event.deps.resolveSingle('message'), 'world!');
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
it('is new for every invocation', async function() {
|
|
344
|
+
const one = await $bus.next({ route: 'read#job', invoke: false }).value;
|
|
345
|
+
const two = await $bus.next({ route: 'read#job', invoke: false }).value;
|
|
346
|
+
|
|
347
|
+
assert.notStrictEqual(one.deps, two.deps);
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
it('dependencies defined during an invocation stay in the invocation', async function() {
|
|
351
|
+
const message = await $bus.next({ route: 'define#job', params: { message: 'scoped' } }).value;
|
|
352
|
+
|
|
353
|
+
assert.strictEqual(message, 'scoped');
|
|
354
|
+
assert.strictEqual(this.deps.resolveSingle('message'), 'gello');
|
|
355
|
+
assert.strictEqual(await $bus.next({ route: 'read#job' }).value, 'gello');
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
it('dependencies defined during an invocation do not change the deps passed in', async function() {
|
|
359
|
+
const deps = this.deps.branch({ message: 'world!' });
|
|
360
|
+
|
|
361
|
+
await $bus.next({ route: 'define#job', params: { message: 'scoped' }, deps }).value;
|
|
362
|
+
|
|
363
|
+
assert.strictEqual(deps.resolveSingle('message'), 'world!');
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
it('a nested invocation inherits the outer invocation dependencies', async function() {
|
|
367
|
+
events.outer = {
|
|
368
|
+
__metadata: { triggers: { job: {} } },
|
|
369
|
+
job: async event => {
|
|
370
|
+
event.deps.define('message', 'outer');
|
|
371
|
+
|
|
372
|
+
// NOTE: the bus is a generator; a nested dispatch before the outer trigger's first await re-enters
|
|
373
|
+
// it ("Generator is already running")
|
|
374
|
+
await null;
|
|
375
|
+
|
|
376
|
+
return $bus.next({ route: 'read#job', deps: event.deps }).value;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
assert.strictEqual(await $bus.next({ route: 'outer#job' }).value, 'outer');
|
|
381
|
+
})
|
|
382
|
+
})
|
|
383
|
+
})
|