Skip to content

Commit

Permalink
add test for delay-action
Browse files Browse the repository at this point in the history
  • Loading branch information
jwetzell committed Jun 15, 2024
1 parent 27aa6e1 commit 4b01c03
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions lib/tests/actions/delay-action.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import assert from 'node:assert';
import { describe, test } from 'node:test';
import { DelayAction } from '../../src/actions/index.js';
import MIDIMessage from '../../src/messages/midi-message.js';

describe('DelayAction', () => {
test('create', () => {
const action = new DelayAction({
type: 'delay',
params: {
duration: 100,
actions: [
{
type: 'log',
enabled: true,
},
],
},
transforms: [],
enabled: true,
});

assert.notEqual(action, undefined);
});

test('delay accurate within reason', async () => {
const action = new DelayAction({
type: 'delay',
params: {
duration: 250,
actions: [
{
type: 'log',
enabled: true,
},
],
},
transforms: [],
enabled: true,
});

assert.notEqual(action, undefined);
const vars = {};
const runTime = Date.now();

action.on('action', (actionPath, fired) => {
const timeToRun = Math.abs(Date.now() - runTime - action.params.duration);
assert.equal(actionPath, 'actions/0');
assert.equal(fired, true);
assert.equal(timeToRun < 10, true);
});

action.run(new MIDIMessage([0x80, 60, 127], 'test'), vars);
});

test('delay with sub delay', async () => {
const action = new DelayAction({
type: 'delay',
params: {
duration: 250,
actions: [
{
type: 'delay',
params: {
duration: 250,
actions: [
{
type: 'log',
enabled: true,
},
],
},
enabled: true,
},
],
},
transforms: [],
enabled: true,
});

assert.notEqual(action, undefined);
const vars = {};
const runTime = Date.now();

let actionCount = 1;
action.on('action', (actionPath, fired) => {
const timeToRun = Math.abs(Date.now() - runTime - action.params.duration * actionCount);
if (actionCount === 1) {
assert.equal(actionPath, 'actions/0');
} else {
assert.equal(actionPath, 'actions/0/actions/0');
}
assert.equal(fired, true);
assert.equal(timeToRun < 10, true);
actionCount += 1;
});

action.run(new MIDIMessage([0x80, 60, 127], 'test'), vars);
});

test('delay emits transforms', async () => {
const action = new DelayAction({
type: 'delay',
params: {
duration: 500,
actions: [
{
type: 'log',
enabled: true,
},
],
},
transforms: [
{
type: 'scale',
params: {
property: 'note',
inRange: [0, 127],
outRange: [0, 1],
},
enabled: true,
},
],
enabled: true,
});

assert.notEqual(action, undefined);
const vars = {};

action.on('transform', (transformPath, fired) => {
assert.equal(transformPath, 'transforms/0');
assert.equal(fired, true);
});

action.run(new MIDIMessage([0x80, 60, 127], 'test'), vars);
});
});

0 comments on commit 4b01c03

Please sign in to comment.