82 lines
2.1 KiB
TypeScript
82 lines
2.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { executeMidiTask } from "../../src/core/midi-task";
|
|
import { encodeMidi, noteSpans, type MidiDocument } from "../../src/core/midi";
|
|
|
|
const document: MidiDocument = {
|
|
name: "worker.mid",
|
|
format: 0,
|
|
division: 480,
|
|
warnings: [],
|
|
tracks: [
|
|
{
|
|
name: "Notes",
|
|
events: [
|
|
{
|
|
kind: "channel",
|
|
tick: 0,
|
|
order: 0,
|
|
status: 9,
|
|
channel: 0,
|
|
data: Uint8Array.of(60, 100),
|
|
},
|
|
{
|
|
kind: "channel",
|
|
tick: 480,
|
|
order: 1,
|
|
status: 8,
|
|
channel: 0,
|
|
data: Uint8Array.of(60, 0),
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
describe("MIDI worker task protocol", () => {
|
|
it("parses, transforms and exports without UI state", () => {
|
|
const encoded = encodeMidi(document);
|
|
const parsed = executeMidiTask({
|
|
kind: "parse",
|
|
name: "worker.mid",
|
|
bytes: Uint8Array.from(encoded).buffer,
|
|
});
|
|
expect(parsed.kind).toBe("document");
|
|
if (parsed.kind !== "document") return;
|
|
const transformed = executeMidiTask({
|
|
kind: "operation",
|
|
document: parsed.document,
|
|
operation: { kind: "transpose", semitones: 12 },
|
|
});
|
|
expect(transformed.kind).toBe("document");
|
|
if (transformed.kind !== "document") return;
|
|
expect(noteSpans(transformed.document)[0]?.note).toBe(72);
|
|
const edited = executeMidiTask({
|
|
kind: "operation",
|
|
document: transformed.document,
|
|
operation: {
|
|
kind: "edit-note",
|
|
onOrder: noteSpans(transformed.document)[0]!.onOrder,
|
|
note: 70,
|
|
velocity: 80,
|
|
startTick: 10,
|
|
endTick: 240,
|
|
},
|
|
});
|
|
expect(edited.kind).toBe("document");
|
|
if (edited.kind !== "document") return;
|
|
expect(noteSpans(edited.document)[0]).toMatchObject({
|
|
note: 70,
|
|
velocity: 80,
|
|
startTick: 10,
|
|
endTick: 240,
|
|
});
|
|
expect(
|
|
executeMidiTask({
|
|
kind: "export",
|
|
document: edited.document,
|
|
format: "csv",
|
|
}).kind,
|
|
).toBe("text");
|
|
});
|
|
});
|