Zum Inhalt

tests/stores/presetReactivity.test.ts

// Verifies the live-proxy behaviour of presetState.edited. The engine
// captures `presetState.edited` once and reads from it every frame in
// writeParams() — if the captured reference is a *snapshot*, all slider
// edits silently no-op until selectPreset() forces a new reference.

import { describe, expect, it } from 'vitest';
import { DEFAULT_PRESET } from '~/presets/builtIn';
import {
  presetState,
  selectPreset,
  updateEdited,
  updateMatrixCell,
  updateSpecies,
} from '~/stores/presetStore';

describe('presetState.edited live-proxy reads', () => {
  it('a captured edited reference reflects later top-level mutations', () => {
    selectPreset(DEFAULT_PRESET);
    const captured = presetState.edited;
    const before = captured.decayRate;
    const newValue = before > 0.9 ? 0.85 : 0.95;
    updateEdited('decayRate', newValue);
    expect(captured.decayRate).toBe(newValue);
  });

  it('a captured edited reference reflects later audioReactive subobject replacements', () => {
    selectPreset(DEFAULT_PRESET);
    const captured = presetState.edited;
    updateEdited('audioReactive', {
      ...presetState.edited.audioReactive,
      gain: 2.5,
    });
    expect(captured.audioReactive.gain).toBe(2.5);
  });

  it('a captured edited reference reflects species mutations', () => {
    selectPreset(DEFAULT_PRESET);
    const captured = presetState.edited;
    updateSpecies(0, 'turnSpeed', 2.5);
    expect(captured.species[0].turnSpeed).toBe(2.5);
  });

  it('a captured edited reference reflects matrix-cell mutations', () => {
    selectPreset(DEFAULT_PRESET);
    const captured = presetState.edited;
    updateMatrixCell(1, 2, -0.42);
    expect(captured.interactionMatrix[1][2]).toBe(-0.42);
  });
});