test: more

This commit is contained in:
cha0s 2024-02-04 13:56:15 -06:00
parent 8a02c0232d
commit 24ba9cc82d
3 changed files with 47 additions and 79 deletions

View File

@ -2,7 +2,40 @@ import {expect} from 'chai';
import {Flecks} from '@flecks/core/build/flecks'; import {Flecks} from '@flecks/core/build/flecks';
it('detects suspicious hook ordering', async () => { it('includes all by default', async () => {
const flecks = await Flecks.from({
config: {
one: {test: ['one', 'two']},
},
flecks: {
one: {hooks: {'one.test': () => {}}},
two: {hooks: {'one.test': () => {}}},
},
});
expect(flecks.expandedFlecks('one.test'))
.to.deep.equal(['one', 'two']);
});
it('respects elision', async () => {
const flecks = await Flecks.from({
config: {
one: {test: ['two', '...', 'three']},
},
flecks: {
one: {hooks: {'one.test': () => {}}},
two: {hooks: {'one.test': () => {}}},
three: {hooks: {'one.test': () => {}}},
four: {hooks: {'one.test': () => {}}},
},
});
const expanded = flecks.expandedFlecks('one.test');
expect(expanded.shift())
.to.equal('two');
expect(expanded.pop())
.to.equal('three');
});
it('detects yet allows suspicious hook ordering', async () => {
const flecks = await Flecks.from({ const flecks = await Flecks.from({
config: { config: {
one: {test: ['one', 'two']}, one: {test: ['one', 'two']},
@ -16,7 +49,19 @@ it('detects suspicious hook ordering', async () => {
Flecks.debug = (message) => { Flecks.debug = (message) => {
suspected = message.includes('Suspicious ordering specification'); suspected = message.includes('Suspicious ordering specification');
}; };
flecks.expandedFlecks('one.test'); expect(flecks.expandedFlecks('one.test'))
.to.deep.equal(['one', 'two']);
expect(suspected) expect(suspected)
.to.be.true; .to.be.true;
}); });
it('throws on cyclic dependency', async () => {
const flecks = await Flecks.from({
flecks: {
one: {hooks: {'one.test': Flecks.priority(() => {}, {before: 'two'})}},
two: {hooks: {'one.test': Flecks.priority(() => {}, {before: 'one'})}},
},
});
expect(() => flecks.expandedFlecks('one.test'))
.to.throw(/Illegal ordering specification/);
});

View File

@ -4,7 +4,6 @@ import {Flecks} from '@flecks/core';
const testOne = require('./packages/one'); const testOne = require('./packages/one');
const testTwo = require('./packages/two'); const testTwo = require('./packages/two');
const testThree = require('./packages/three');
it('can make middleware', async () => { it('can make middleware', async () => {
const flecks = await Flecks.from({ const flecks = await Flecks.from({
@ -30,71 +29,3 @@ it('can make middleware', async () => {
}); });
}); });
}); });
it('respects explicit middleware configuration', async () => {
const flecks = await Flecks.from({
config: {
'@flecks/core/test': {
middleware: [
'@flecks/core/two',
'@flecks/core/one',
],
},
},
flecks: {
'@flecks/core/one': testOne,
'@flecks/core/two': testTwo,
},
});
const foo = {bar: 1};
const mw = flecks.makeMiddleware('@flecks/core/test.middleware');
await new Promise((resolve) => {
mw(foo, () => {
expect(foo.bar).to.equal(3);
resolve();
});
});
});
it('respects middleware elision', async () => {
const flecks = await Flecks.from({
config: {
'@flecks/core/test': {
middleware: [
'...',
],
},
},
flecks: {
'@flecks/core/one': testOne,
'@flecks/core/two': testTwo,
},
});
const foo = {bar: 1};
const mw = flecks.makeMiddleware('@flecks/core/test.middleware');
await new Promise((resolve) => {
mw(foo, () => {
expect(foo.bar).to.equal(3);
resolve();
});
});
});
it('throws on elision graph cycle', async () => {
const flecks = await Flecks.from({
config: {
'@flecks/core/test': {
middleware: [
'...',
],
},
},
flecks: {
'@flecks/core/one': testOne,
'@flecks/core/two': testTwo,
'@flecks/core/three': testThree,
},
});
const tryMaking = () => flecks.makeMiddleware('@flecks/core/test.middleware');
expect(tryMaking).to.throw();
});

View File

@ -1,8 +0,0 @@
import {Flecks} from '@flecks/core';
export const hooks = {
'@flecks/core/test.middleware': Flecks.priority(
() => () => {},
{after: '@flecks/core/one', before: '@flecks/core/two'},
),
};