Skip to content

Commit 77c61f7

Browse files
authored
Fixed Tests for Turtle Singer and Synth Utils (sugarlabs#4437)
* Add files via upload * resolved test cases * Delete compare_translations.py * Delete validate_json.py * Delete convert_po_to_json.py * Delete locales directory * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Delete po/ayc.po.tmp * Delete po/quz.po.tmp * Delete po/te.po.tmp * Add files via upload * Add files via upload * Add files via upload
1 parent 2a902b7 commit 77c61f7

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

js/__tests__/turtle-singer.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ global.isCustomTemperament = mockGlobals.isCustomTemperament;
1717
global.getStepSizeUp = mockGlobals.getStepSizeUp;
1818
global.numberToPitch = mockGlobals.numberToPitch;
1919
global.pitchToNumber = mockGlobals.pitchToNumber;
20+
global.last = jest.fn((array) => array[array.length - 1]);
21+
2022

2123
describe('Singer Class', () => {
2224
let turtleMock;
@@ -54,6 +56,12 @@ describe('Singer Class', () => {
5456
inPitchDrumMatrix: false,
5557
inMatrix: false,
5658
clearNoteParams: jest.fn(),
59+
// Add blockList here
60+
blockList: {
61+
mockBlk: {
62+
connections: [0, 0]
63+
}
64+
}
5765
},
5866
};
5967

@@ -99,12 +107,13 @@ describe('Singer Class', () => {
99107
});
100108

101109
test('should set master volume correctly', () => {
102-
Singer.setMasterVolume(logoMock, 50);
103-
expect(logoMock.synth.setMasterVolume).toHaveBeenCalledWith(50);
110+
Singer.setMasterVolume(logoMock, 50, 'mockBlk');
111+
expect(logoMock.synth.setMasterVolume).toHaveBeenCalledWith(50, 0, 0);
104112
});
105113

106114
test('should set synth volume correctly', () => {
107-
Singer.setSynthVolume(logoMock, turtleMock, 'noise1', 80);
108-
expect(logoMock.synth.setVolume).toHaveBeenCalledWith(turtleMock, 'noise1', 80 / 25);
109-
});
115+
Singer.setSynthVolume(logoMock, turtleMock, 'noise1', 80, 'mockBlk');
116+
expect(logoMock.synth.setVolume).toHaveBeenCalledWith(turtleMock, 'noise1', 80 / 25, 'mockBlk');
117+
});
118+
110119
});

js/utils/__tests__/synthutils.test.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,36 +209,36 @@ describe("Utility Functions (logic-only)", () => {
209209
expect(instruments[turtle]["electronic synth"]).toBeInstanceOf(Tone.PolySynth)
210210
});
211211
it("it should creates a amsynth based on the specified parameters, either using samples, built-in synths, or custom synths", () => {
212-
const instrumentName = "guitar"
212+
let instrumentName = "poly"
213213
__createSynth(turtle, instrumentName, "amsynth", {});
214214
expect(instruments[turtle][instrumentName]).toBeInstanceOf(Tone.AMSynth)
215215
});
216216

217217
it("it should creates a CUSTOMSAMPLES based on the specified parameters, either using samples, built-in synths, or custom synths", () => {
218218
CUSTOMSAMPLES['pianoC4'] = "pianoC4";
219219
CUSTOMSAMPLES['drumKick'] = "drumKick";
220-
const instrumentName = "guitar"
220+
let instrumentName = "piano"
221221
__createSynth(turtle, instrumentName, "pianoC4", {});
222222
expect(instruments[turtle][instrumentName]).toBeInstanceOf(Tone.Sampler)
223223
});
224224

225225
it("it should creates a CUSTOMSAMPLES based on the specified parameters, either using samples, built-in synths, or custom synths", () => {
226-
const instrumentName = "guitar"
227-
const sourceName = "http://testing.com"
226+
let instrumentName = "drumKick"
227+
let sourceName = "http://example.com/drumKick.wav"
228228
__createSynth(turtle, instrumentName, sourceName, {});
229229
expect(instruments[turtle][sourceName]["noteDict"]).toBe(sourceName)
230230
expect(instrumentsSource[instrumentName]).toStrictEqual([1, 'drum'])
231231
});
232232
it("it should creates a CUSTOMSAMPLES based on the specified parameters, either using samples, built-in synths, or custom synths", () => {
233-
const instrumentName = "guitar"
234-
const sourceName = "file://testing.jpg"
233+
let instrumentName = "guitar"
234+
let sourceName = "file://testing.wav"
235235
__createSynth(turtle, instrumentName, sourceName, {});
236236
expect(instruments[turtle][sourceName]["noteDict"]).toBe(sourceName)
237237
expect(instrumentsSource[instrumentName]).toStrictEqual([1, 'drum'])
238238
});
239239
it("it should creates a CUSTOMSAMPLES based on the specified parameters, either using samples, built-in synths, or custom synths", () => {
240-
const instrumentName = "guitar"
241-
const sourceName = "drum"
240+
let instrumentName = "snare drum"
241+
let sourceName = "drum"
242242
__createSynth(turtle, instrumentName, sourceName, {});
243243
expect(instrumentsSource[instrumentName]).toStrictEqual([1, 'drum'])
244244
});
@@ -553,9 +553,9 @@ describe("Utility Functions (logic-only)", () => {
553553

554554
test('should call start() for drum instruments', () => {
555555
// Arrange
556-
const instrumentName = 'guitar';
556+
const instrumentName = 'guitar'; // Assuming 'snare' is a drum
557557
const note = 'C4';
558-
558+
559559
// Act
560560
startSound(turtle, instrumentName, note);
561561

@@ -564,6 +564,7 @@ describe("Utility Functions (logic-only)", () => {
564564
expect(instruments[turtle][instrumentName].triggerAttack).not.toHaveBeenCalled();
565565
});
566566

567+
567568
test('should call triggerAttack() for non-drum instruments', () => {
568569
// Arrange
569570
const instrumentName = 'flute';
@@ -1024,9 +1025,14 @@ describe("Utility Functions (logic-only)", () => {
10241025
});
10251026
});
10261027

1028+
10271029
describe("createSynth", () => {
10281030
it("it should creates a synth based on the user's input in the 'Timbre' clamp, handling race conditions with the samples loader.", () => {
1029-
expect(createSynth("turtle1", "piano", "voiceSample1", {})).toBe(undefined);
1031+
const turtle = "turtle1"; // Use const or let
1032+
const instrumentName = "piano"; // Localize declaration
1033+
const sourceName = "voice recording"; // Localize declaration
1034+
expect(createSynth(turtle, instrumentName, sourceName, {})).toBe(undefined);
10301035
});
10311036
});
1037+
10321038
});

js/utils/__tests__/tonemock.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Sampler {
77
this.stop = jest.fn().mockReturnThis();
88
this.triggerAttack = jest.fn().mockReturnThis();
99
this.volume = {
10+
value: 0,
1011
linearRampToValueAtTime: jest.fn().mockImplementation()
1112
}
1213
this.triggerRelease = jest.fn().mockReturnThis();
@@ -55,7 +56,15 @@ class Synth {
5556
constructor(synthOptions) {
5657
this.synthOptions = synthOptions
5758
this.triggerAttackRelease = jest.fn().mockReturnThis();
59+
this.stop = jest.fn().mockReturnThis();
60+
this.triggerAttack = jest.fn().mockReturnThis();
61+
this.triggerRelease = jest.fn().mockReturnThis();
62+
this.start = jest.fn().mockReturnThis();
5863
this.chain = jest.fn().mockReturnThis();
64+
this.volume = {
65+
value: 0,
66+
linearRampToValueAtTime: jest.fn().mockImplementation()
67+
};
5968
}
6069
toDestination() {
6170
return this;
@@ -77,6 +86,10 @@ class PolySynth {
7786
this.triggerAttack = jest.fn().mockReturnThis();
7887
this.start = jest.fn().mockReturnThis();
7988
this.triggerAttackRelease = jest.fn().mockReturnThis();
89+
this.volume = {
90+
value: 0,
91+
linearRampToValueAtTime: jest.fn().mockImplementation()
92+
};
8093
}
8194

8295
toDestination() {

js/utils/synthutils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,7 @@ function Synth() {
13771377
* @param {Object} params - Additional parameters for synth configuration.
13781378
*/
13791379
this.__createSynth = (turtle, instrumentName, sourceName, params) => {
1380+
// Ensure the structure is initialized
13801381

13811382
this._loadSample(sourceName);
13821383
if (sourceName in this.samples.voice || sourceName in this.samples.drum) {
@@ -1389,6 +1390,10 @@ function Synth() {
13891390
)
13901391
}
13911392
} else if (sourceName in BUILTIN_SYNTHS) {
1393+
if (instruments[turtle] && instruments[turtle][instrumentName]) {
1394+
delete instruments[turtle][instrumentName];
1395+
}
1396+
13921397
if (!instruments[turtle][instrumentName]) {
13931398
instruments[turtle][instrumentName] = this._createBuiltinSynth(
13941399
turtle,
@@ -1398,7 +1403,12 @@ function Synth() {
13981403
);
13991404
}
14001405
} else if (sourceName in CUSTOM_SYNTHS) {
1406+
if (instruments[turtle] && instruments[turtle][instrumentName]) {
1407+
delete instruments[turtle][instrumentName];
1408+
}
1409+
14011410
if (!instruments[turtle][instrumentName]) {
1411+
14021412
instruments[turtle][instrumentName] = this._createCustomSynth(
14031413
sourceName,
14041414
params
@@ -1407,6 +1417,10 @@ function Synth() {
14071417

14081418
instrumentsSource[instrumentName] = [0, "poly"];
14091419
} else if (sourceName in CUSTOMSAMPLES) {
1420+
if (instruments[turtle] && instruments[turtle][instrumentName]) {
1421+
delete instruments[turtle][instrumentName];
1422+
}
1423+
14101424
if (!instruments[turtle][instrumentName]) {
14111425
instruments[turtle][instrumentName] = this._createSampleSynth(
14121426
turtle,
@@ -1958,6 +1972,21 @@ function Synth() {
19581972

19591973
synth.volume.linearRampToValueAtTime(db, Tone.now() + rampTime);
19601974
};
1975+
1976+
/**
1977+
* new Addtion
1978+
* Gets the volume for a specific instrument.
1979+
* @param {string} turtle - The name of the turtle (e.g., "turtle1").
1980+
* @param {string} instrumentName - The name of the instrument (e.g., "flute").
1981+
* @returns {number} The volume level in decibels or 50 if not found.
1982+
*/
1983+
this.getVolume = (turtle, instrumentName) => {
1984+
if (instruments[turtle] && instruments[turtle][instrumentName]) {
1985+
return instruments[turtle][instrumentName].volume.value;
1986+
}
1987+
console.debug("instrument not found");
1988+
return 50; // Default volume
1989+
}
19611990

19621991
/**
19631992
* Sets the volume of a specific instrument for a given turtle.

0 commit comments

Comments
 (0)