tests/integration/midi.test.js
- import { describe, it, expect } from "vitest";
- import { generateMidi } from "../../src/controller/tracks.js";
- describe("integration: midi-writer-js", () => {
-
- it("emits a real MIDI binary with the expected header from a realistic solution", () => {
- const part = {
- solution: {
- note_0_0: 60,
- note_0_1: -2,
- note_0_2: 64,
- note_0_3: -1,
- },
- numSubBeats: 4,
- subdivision: 1,
- numerator: 4,
- denominator: 4,
- key: 0,
- mode: "major",
- };
- const step3Params = { voices: 1, voice: [{ instrument: 0 }] };
- const { combinedB64, voicesB64 } = generateMidi([part], step3Params, 120);
- expect(voicesB64).toHaveLength(1);
-
- const bytes = Uint8Array.from(atob(combinedB64), (c) => c.charCodeAt(0));
- const header = String.fromCharCode(...bytes.slice(0, 4));
- expect(header).toBe("MThd");
-
- const ascii = String.fromCharCode(...bytes);
- expect(ascii).toContain("MTrk");
- });
-
- it("produces one MTrk per voice in the combined file when multiple voices are present", () => {
- const part = {
- solution: { note_0_0: 60, note_1_0: 67 },
- numSubBeats: 1,
- subdivision: 1,
- numerator: 1,
- denominator: 4,
- key: 0,
- mode: "major",
- };
- const step3Params = {
- voices: 2,
- voice: [{ instrument: 0 }, { instrument: 42 }],
- };
- const { combinedB64, voicesB64 } = generateMidi([part], step3Params, 120);
- expect(voicesB64).toHaveLength(2);
- const bytes = Uint8Array.from(atob(combinedB64), (c) => c.charCodeAt(0));
- const ascii = String.fromCharCode(...bytes);
-
- const trackCount = ascii.match(/MTrk/g)?.length ?? 0;
- expect(trackCount).toBe(2);
- });
- });