Home Manual Reference Source Test

tests/constraints2.test.js

  1. import { describe, it, expect, vi } from "vitest";
  2. import { FIXTURES } from "./api.js";
  3. const { buildChordModel } = FIXTURES;
  4. /** @test {modelChords} */
  5. describe("modelChords", () => {
  6. /** @test {modelChords} */
  7. it("emits a structurally complete MZN with F4, H20 and cost_matrix; falls back to the graph tonic on opposite-mode tonic input", () => {
  8. const out = buildChordModel({ bars: 3 });
  9. expect(out).toContain("int: n_nodes = 8;");
  10. expect(out).toContain("int: path_length = 3;");
  11. expect(out).toContain("constraint path[1] = start_node;");
  12. expect(out).toContain("constraint path[path_length] = end_node;");
  13. expect(out).toContain("solve minimize total_cost;");
  14. expect(buildChordModel({ start_chord: "i", end_chord: "i" })).toContain(
  15. "int: start_node = 1;",
  16. );
  17. expect(
  18. buildChordModel({ mode: "minor", start_chord: "I", end_chord: "I" }),
  19. ).toContain("int: start_node = 1;");
  20. expect(buildChordModel({ bars: 2 })).toContain("solve satisfy;");
  21. });
  22. /** @test {modelChords} */
  23. it("emits cost_matrix when creativity > 0; grows n_nodes when a preset adds chromatic nodes", () => {
  24. vi.spyOn(Math, "random").mockReturnValue(0.5);
  25. const noPreset = buildChordModel({ creativity: 0.5 });
  26. expect(noPreset).toContain("cost_matrix");
  27. expect(noPreset).toContain("int: n_nodes = 8;");
  28. const preset = {
  29. major: { chromatic: ["bII"], edges: { "I->bII": 20 } },
  30. };
  31. const withPreset = buildChordModel({ creativity: 0.5, preset });
  32. expect(withPreset).toContain("int: n_nodes = 9;");
  33. expect(withPreset).toContain("cost_matrix");
  34. Math.random.mockRestore();
  35. });
  36. });