Skip to content

Commit

Permalink
Tests added for turtleactions (#4234)
Browse files Browse the repository at this point in the history
* Tests added for turtleactions

Signed-off-by: Justin Charles <[email protected]>

* Update MeterActions.test.js

---------

Signed-off-by: Justin Charles <[email protected]>
  • Loading branch information
justin212407 authored Jan 7, 2025
1 parent a603743 commit d6f5975
Show file tree
Hide file tree
Showing 10 changed files with 787 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/turtleactions/DrumActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,4 @@ function setupDrumActions(activity) {
}
};
}
module.exports = setupDrumActions;
1 change: 1 addition & 0 deletions js/turtleactions/MeterActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,4 @@ function setupMeterActions(activity) {
}
};
}
module.exports = setupMeterActions;
1 change: 1 addition & 0 deletions js/turtleactions/PitchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,4 @@ function setupPitchActions(activity) {
}
};
}
module.exports = setupPitchActions;
1 change: 1 addition & 0 deletions js/turtleactions/ToneActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,4 @@ function setupToneActions(activity) {
}
};
}
module.exports = setupToneActions;
1 change: 1 addition & 0 deletions js/turtleactions/VolumeActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,4 @@ function setupVolumeActions(activity) {
}
};
}
module.exports = setupVolumeActions;
117 changes: 117 additions & 0 deletions js/turtleactions/__tests__/DrumActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const setupDrumActions = require('../DrumActions');

describe('setupDrumActions', () => {
let activity;
let turtle;
let targetTurtle;

beforeAll(() => {
global.Singer = {
DrumActions: {},
processNote: jest.fn(),
};
global.DEFAULTDRUM = 'defaultDrum';
global.DRUMNAMES = { drum1: ['d1', 'drum1'], drum2: ['d2', 'drum2'] };
global.NOISENAMES = { noise1: ['n1', 'noise1'] };
global.DEFAULTVOLUME = 100;
global.last = jest.fn(array => array[array.length - 1]);
global._ = jest.fn(msg => msg);
});

beforeEach(() => {
activity = {
turtles: {
ithTurtle: jest.fn(),
},
blocks: {
blockList: { 1: {} },
},
logo: {
setDispatchBlock: jest.fn(),
setTurtleListener: jest.fn(),
clearNoteParams: jest.fn(),
inRhythmRuler: false,
rhythmRuler: { Drums: [], Rulers: [] },
},
errorMsg: jest.fn(),
};

targetTurtle = {
singer: {
drumStyle: [],
inNoteBlock: [],
noteDrums: {},
synthVolume: {},
crescendoInitialVolume: {},
noteBeatValues: {},
beatFactor: 1,
pushedNote: false,
},
};

activity.turtles.ithTurtle.mockReturnValue(targetTurtle);
setupDrumActions(activity);
});

it('should return the correct drum name', () => {
const result = Singer.DrumActions.GetDrumname('d1');
expect(result).toBe('drum1');

const result2 = Singer.DrumActions.GetDrumname('unknown');
expect(result2).toBe(DEFAULTDRUM);
});

it('should play a standalone drum sound', () => {
if (!targetTurtle.singer.noteDrums[1]) targetTurtle.singer.noteDrums[1] = [];
Singer.DrumActions.playDrum('d1', 0, 1);
expect(Singer.processNote).toHaveBeenCalledWith(
activity,
expect.any(Number),
false,
expect.anything(),
0,
expect.any(Function)
);
expect(activity.logo.clearNoteParams).toHaveBeenCalledWith(targetTurtle, 1, []);
expect(targetTurtle.singer.inNoteBlock).toContain(1);
expect(targetTurtle.singer.noteDrums[1]).toContain('drum1');
expect(targetTurtle.singer.pushedNote).toBe(true);
});


it('should add a drum to an existing note block', () => {
targetTurtle.singer.inNoteBlock.push(2);
targetTurtle.singer.noteDrums[2] = [];
Singer.DrumActions.playDrum('d1', 0, 1);
expect(targetTurtle.singer.noteDrums[2]).toContain('drum1');
});

it('should set the drum style and add a listener', () => {
Singer.DrumActions.setDrum('d1', 0, 1);
expect(targetTurtle.singer.drumStyle).toContain('drum1');
expect(activity.logo.setDispatchBlock).toHaveBeenCalledWith(1, 0, '_setdrum_0');
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(0, '_setdrum_0', expect.any(Function));
});

it('should map pitch to drum', () => {
Singer.DrumActions.mapPitchToDrum('d1', 0, 1);
expect(targetTurtle.singer.drumStyle).toContain('drum1');
expect(activity.logo.setDispatchBlock).toHaveBeenCalledWith(1, 0, '_mapdrum_0');
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(0, '_mapdrum_0', expect.any(Function));
});

it('should play noise in a note block', () => {
targetTurtle.singer.inNoteBlock.push(2);
targetTurtle.singer.noteDrums[2] = [];
targetTurtle.singer.noteBeatValues[2] = [];
Singer.DrumActions.playNoise('n1', 0, 1);
expect(targetTurtle.singer.noteDrums[2]).toContain('noise1');
expect(targetTurtle.singer.noteBeatValues[2]).toContain(1);
expect(targetTurtle.singer.pushedNote).toBe(true);
});

it('should throw an error for standalone noise block', () => {
Singer.DrumActions.playNoise('n1', 0, 1);
expect(activity.errorMsg).toHaveBeenCalledWith('Noise Block: Did you mean to use a Note block?', 1);
});
});
155 changes: 155 additions & 0 deletions js/turtleactions/__tests__/MeterActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
const setupMeterActions = require('../MeterActions');

describe('setupMeterActions', () => {
let activity;
let targetTurtle;

beforeAll(() => {
global.Singer = {
MeterActions: {},
RhythmActions: {
getNoteValue: jest.fn(() => 1),
},
};
global.TONEBPM = 240;
global.Queue = jest.fn((action, duration, blk) => ({ action, duration, blk }));
global.last = jest.fn((array) => array[array.length - 1]);
global._ = jest.fn((msg) => msg);
global.rationalToFraction = jest.fn((value) => [value * 4, 4]);
});


beforeEach(() => {
activity = {
turtles: {
ithTurtle: jest.fn(),
turtleList: [],
addTurtle: jest.fn(),
},
blocks: {
blockList: { 1: {} },
},
logo: {
actions: {},
setDispatchBlock: jest.fn(),
setTurtleListener: jest.fn(),
initTurtle: jest.fn(),
prepSynths: jest.fn(),
runFromBlockNow: jest.fn(),
notation: {
notationMeter: jest.fn(),
notationPickup: jest.fn(),
},
},
errorMsg: jest.fn(),
stage: {
dispatchEvent: jest.fn(),
},
};

targetTurtle = {
singer: {
beatsPerMeasure: 0,
noteValuePerBeat: 0,
beatList: [],
defaultStrongBeats: false,
bpm: [],
notesPlayed: [0, 1],
pickup: 0,
drift: 0,
},
queue: [],
parentFlowQueue: [],
unhighlightQueue: [],
parameterQueue: [],
};

activity.turtles.ithTurtle.mockReturnValue(targetTurtle);

setupMeterActions(activity);
});

it('should set the meter correctly', () => {
Singer.MeterActions.setMeter(4, 4, 0);
expect(targetTurtle.singer.beatsPerMeasure).toBe(4);
expect(targetTurtle.singer.noteValuePerBeat).toBe(1 / 4);
expect(activity.logo.notation.notationMeter).toHaveBeenCalledWith(0, 4, 1 / 4);
});

it('should set the pickup value correctly', () => {
Singer.MeterActions.setPickup(2, 0);
expect(targetTurtle.singer.pickup).toBe(2);
expect(activity.logo.notation.notationPickup).toHaveBeenCalledWith(0, 2);
});

it('should set BPM within range', () => {
Singer.MeterActions.setBPM(120, 0.25, 0, 1);
expect(targetTurtle.singer.bpm).toContain(120);
});

it('should handle BPM below range', () => {
Singer.MeterActions.setBPM(10, 0.25, 0, 1);
expect(activity.errorMsg).toHaveBeenCalledWith('1/4 beats per minute must be greater than 30', 1);
expect(targetTurtle.singer.bpm).toContain(30);
});

it('should set the master BPM correctly', () => {
Singer.MeterActions.setMasterBPM(100, 0.25, 1);
expect(Singer.masterBPM).toBe(100);
expect(Singer.defaultBPMFactor).toBe(TONEBPM / 100);
});

it('should set a listener for every beat', () => {
activity.turtles.turtleList = [{ companionTurtle: null }];
activity.turtles.addTurtle = jest.fn();
activity.logo.prepSynths = jest.fn();
targetTurtle.id = 0;

Singer.MeterActions.onEveryBeatDo('testAction', false, null, 0, 1);

expect(activity.turtles.addTurtle).toHaveBeenCalled();
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(
1,
'__everybeat_1__',
expect.any(Function)
);
});

it('should set a listener for every note', () => {
Singer.MeterActions.onEveryNoteDo('testAction', false, null, 0, 1);
expect(activity.logo.setTurtleListener).toHaveBeenCalled();
});

it('should calculate the current meter correctly', () => {
targetTurtle.singer.beatsPerMeasure = 4;
targetTurtle.singer.noteValuePerBeat = 0.25;
const meter = Singer.MeterActions.getCurrentMeter(0);
expect(meter).toBe('4:0.25');
});

it('should calculate the BPM correctly', () => {
targetTurtle.singer.bpm.push(120);
const bpm = Singer.MeterActions.getBPM(0);
expect(bpm).toBe(120);
});

it('should get the beat factor correctly', () => {
targetTurtle.singer.noteValuePerBeat = 0.25;
const factor = Singer.MeterActions.getBeatFactor(0);
expect(factor).toBe(0.25);
});

it('should calculate whole notes played correctly', () => {
targetTurtle.singer.notesPlayed = [4, 1];
const wholeNotes = Singer.MeterActions.getWholeNotesPlayed(0);
expect(wholeNotes).toBe(4);
});

it('should return the correct meter format', () => {
targetTurtle.singer.beatsPerMeasure = 4;
targetTurtle.singer.noteValuePerBeat = 1;

const meter = Singer.MeterActions.getCurrentMeter(0);
expect(meter).toBe('4:1');
});
});
Loading

0 comments on commit d6f5975

Please sign in to comment.