Skip to content

Commit 31fd99e

Browse files
committed
Update and add new tests
1 parent d88374d commit 31fd99e

File tree

8 files changed

+122
-33
lines changed

8 files changed

+122
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { readFile } from "node:fs/promises";
2+
import { fromArrayBuffer, toArrayBuffer } from "../../../src";
3+
4+
test("Import and export identical complex songs", async () => {
5+
const originalFile = await readFile("tests/sample/full.nbs");
6+
const originalArray = new Uint8Array(originalFile);
7+
const originalSong = fromArrayBuffer(originalArray.buffer);
8+
9+
const exportedBuffer = toArrayBuffer(originalSong);
10+
const exportedArray = new Uint8Array(exportedBuffer);
11+
12+
expect(Buffer.compare(originalArray, exportedArray)).toBe(0);
13+
});
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { readFile } from "node:fs/promises";
2+
import { fromArrayBuffer, toArrayBuffer } from "../../../src";
3+
4+
test("Import and export identical songs", async () => {
5+
const originalFile = await readFile("tests/sample/simple.nbs");
6+
const originalArray = new Uint8Array(originalFile);
7+
const originalSong = fromArrayBuffer(originalArray.buffer);
8+
9+
const exportedBuffer = toArrayBuffer(originalSong);
10+
const exportedArray = new Uint8Array(exportedBuffer);
11+
12+
expect(Buffer.compare(originalArray, exportedArray)).toBe(0);
13+
});

tests/consisdency/transform.test.ts

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { readFile, writeFile } from "node:fs/promises";
2+
import { Layer, fromArrayBuffer, toArrayBuffer } from "../../src";
3+
4+
test("Transform a file to an expected result", async () => {
5+
const originalFile = await readFile("tests/sample/simple.nbs");
6+
const originalArray = new Uint8Array(originalFile);
7+
const originalSong = fromArrayBuffer(originalArray.buffer);
8+
9+
const expectedFile = await readFile("tests/sample/simpleAdded.nbs");
10+
const expectedArray = new Uint8Array(expectedFile);
11+
12+
const harp = originalSong.layers[0];
13+
14+
const topPling = new Layer(5);
15+
topPling.volume = 80;
16+
17+
const middlePling = new Layer(6);
18+
middlePling.volume = 30;
19+
middlePling.stereo = -40;
20+
21+
const bottomPling = new Layer(7);
22+
bottomPling.volume = 30;
23+
bottomPling.stereo = 40;
24+
25+
originalSong.addNote(harp, 64, 0, {
26+
"key": 46,
27+
"velocity": 100,
28+
"panning": 0,
29+
"pitch": 0
30+
});
31+
topPling.addNote(64, 8, {
32+
"key": 46,
33+
"velocity": 100,
34+
"panning": 0,
35+
"pitch": 0
36+
});
37+
middlePling.addNote(64, 8, {
38+
"key": 41,
39+
"velocity": 100,
40+
"panning": 0,
41+
"pitch": 0
42+
});
43+
bottomPling.addNote(64, 8, {
44+
"key": 38,
45+
"velocity": 100,
46+
"panning": 0,
47+
"pitch": 0
48+
});
49+
50+
originalSong.addNote(harp, 72, 0, {
51+
"key": 45,
52+
"velocity": 100,
53+
"panning": 0,
54+
"pitch": 0
55+
});
56+
topPling.addNote(72, 8, {
57+
"key": 45,
58+
"velocity": 100,
59+
"panning": 0,
60+
"pitch": 0
61+
});
62+
middlePling.addNote(72, 8, {
63+
"key": 38,
64+
"velocity": 100,
65+
"panning": 0,
66+
"pitch": 0
67+
});
68+
bottomPling.addNote(72, 8, {
69+
"key": 41,
70+
"velocity": 100,
71+
"panning": 0,
72+
"pitch": 0
73+
});
74+
75+
originalSong.addLayer(topPling);
76+
originalSong.addLayer(middlePling);
77+
originalSong.addLayer(bottomPling);
78+
79+
originalSong.meta.description = "A simple melody used for testing NBS.js. Adds two leading notes at the end of the initial melody.";
80+
originalSong.stats.minutesSpent = 9;
81+
originalSong.stats.leftClicks = 324;
82+
originalSong.stats.rightClicks = 4;
83+
originalSong.stats.blocksAdded = 40;
84+
85+
const exportedBuffer = toArrayBuffer(originalSong);
86+
const exportedArray = new Uint8Array(exportedBuffer);
87+
88+
expect(Buffer.compare(exportedArray, expectedArray)).toBe(0);
89+
});

tests/fromArrayBuffer/fromArrayBuffer.test.ts tests/read/basicMetadata.test.ts

+7-33
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,18 @@
11
import { readFile } from "node:fs/promises";
22
import { fromArrayBuffer } from "../../src";
33

4-
/**
5-
* Options for the test results.
6-
*/
74
interface TestOptions {
8-
/**
9-
* The version of the song.
10-
*/
115
"version": number;
12-
13-
/**
14-
* The length of the song.
15-
*/
166
"length": number;
17-
18-
/**
19-
* The amount of instruments in the song.
20-
*/
217
"instruments": number;
22-
23-
/**
24-
* The amount of layers in the song.
25-
*/
268
"layers": number;
27-
28-
/**
29-
* The tempo of the song.
30-
*/
319
"tempo": number;
32-
33-
/**
34-
* The last measure of the song.
35-
*/
3610
"lastMeasure": number | undefined;
3711
}
3812

39-
test("Read song from file", async () => {
40-
const songFile = await readFile("tests/sample/test.nbs");
41-
const buffer = new Uint8Array(songFile).buffer;
13+
test("Read song from file, compare metadata", async () => {
14+
const file = await readFile("tests/sample/simple.nbs");
15+
const buffer = new Uint8Array(file).buffer;
4216
const song = fromArrayBuffer(buffer, {
4317
"ignoreEmptyLayers": true
4418
});
@@ -54,11 +28,11 @@ test("Read song from file", async () => {
5428

5529
const target: TestOptions = {
5630
"version": 5,
57-
"length": 12,
31+
"length": 62,
5832
"instruments": 16,
59-
"layers": 5,
60-
"tempo": 7.5,
61-
"lastMeasure": 12
33+
"layers": 4,
34+
"tempo": 10,
35+
"lastMeasure": 64
6236
};
6337

6438
expect(input).toStrictEqual(target);

tests/sample/full.nbs

29.8 KB
Binary file not shown.

tests/sample/simple.nbs

813 Bytes
Binary file not shown.

tests/sample/simpleAdded.nbs

942 Bytes
Binary file not shown.

tests/sample/test.nbs

-505 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)