From 08e892de31c5f6cee927ecaa6fb03053ab7b9d1c Mon Sep 17 00:00:00 2001 From: monkey-w1n5t0n Date: Tue, 7 Apr 2026 21:07:45 +0200 Subject: [PATCH] feat: add output mode switching with MLP resize, confirmation dialog, warm-start, and pill toggle UI (f13) - Add ModeDrawer component with pill toggle (visual/synth/midi-cc/audio-canvas) - Add confirmation dialog when switching modes with existing training data - Skip unnecessary MLP resize when output count unchanged - Hidden layer weights preserved via warm-start on mode switch - 18 new Playwright tests covering VAL-MODE-001 through VAL-MODE-007 - All 183 tests passing, typecheck clean Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- src/App.tsx | 6 + src/components/layout/ModeDrawer.tsx | 67 +++ src/components/layout/drawer.css | 35 ++ src/stores/ml-store.ts | 20 +- tests/solidjs/output-mode-switching.spec.ts | 475 ++++++++++++++++++++ 5 files changed, 601 insertions(+), 2 deletions(-) create mode 100644 src/components/layout/ModeDrawer.tsx create mode 100644 tests/solidjs/output-mode-switching.spec.ts diff --git a/src/App.tsx b/src/App.tsx index 51245ff..ebb3c02 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,6 +7,7 @@ import Joystick from './components/input/Joystick'; import FlowField from './components/visual/FlowField'; import Dock from './components/layout/Dock'; import TrainingDrawer from './components/layout/TrainingDrawer'; +import ModeDrawer from './components/layout/ModeDrawer'; import StatusLine from './components/layout/StatusLine'; import RLButtons from './components/rl/RLButtons'; @@ -143,6 +144,11 @@ export default function App() { visible={openDrawers().has('training')} onClose={() => closeDrawer('training')} /> + closeDrawer('mode')} + /> diff --git a/src/components/layout/ModeDrawer.tsx b/src/components/layout/ModeDrawer.tsx new file mode 100644 index 0000000..bf052dd --- /dev/null +++ b/src/components/layout/ModeDrawer.tsx @@ -0,0 +1,67 @@ +/** + * ModeDrawer — Drawer panel for output mode switching. + * + * Contains a pill toggle for switching between: + * visual | synth | midi-cc | audio-canvas + * + * Switching modes triggers MLP recreation with warm-start weight transfer. + * A confirmation dialog appears if existing training data would be lost. + */ + +import { For } from 'solid-js'; +import type { MLStore, OutputMode } from '../../stores/ml-store'; +import './drawer.css'; + +const MODE_OPTIONS: Array<{ mode: OutputMode; label: string }> = [ + { mode: 'visual', label: 'Visual' }, + { mode: 'synth', label: 'Synth' }, + { mode: 'midi-cc', label: 'MIDI CC' }, + { mode: 'audio-canvas', label: 'Audio' }, +]; + +export interface ModeDrawerProps { + mlStore: MLStore; + visible: boolean; + onClose: () => void; +} + +export default function ModeDrawer(props: ModeDrawerProps) { + function onModeSelect(mode: OutputMode): void { + if (mode === props.mlStore.state.outputMode) return; + props.mlStore.setOutputMode(mode); + } + + return ( +
+
+ Output Mode + +
+
+
+ + {(opt) => ( + + )} + +
+
+
Outputs: {props.mlStore.outputCount()}
+
Mode: {props.mlStore.state.outputMode}
+
+
+
+ ); +} diff --git a/src/components/layout/drawer.css b/src/components/layout/drawer.css index adcc71b..c20b0ee 100644 --- a/src/components/layout/drawer.css +++ b/src/components/layout/drawer.css @@ -205,6 +205,41 @@ pointer-events: none; } +/* Pill toggle for mode switching */ +.pill-toggle { + display: flex; + gap: 4px; + background: rgba(0, 0, 0, 0.3); + border-radius: 8px; + padding: 3px; +} + +.pill-opt { + flex: 1; + padding: 5px 8px; + border: none; + border-radius: 6px; + background: transparent; + color: #888; + font-size: 10px; + font-family: var(--font-mono, monospace); + cursor: pointer; + transition: background 0.2s, color 0.2s; + white-space: nowrap; + text-align: center; +} + +.pill-opt:hover { + background: rgba(255, 255, 255, 0.08); + color: #ccc; +} + +.pill-opt.active { + background: rgba(0, 212, 255, 0.15); + color: var(--accent, #00d4ff); + font-weight: 600; +} + /* Drawer slide-in animation */ @keyframes drawerSlideIn { from { diff --git a/src/stores/ml-store.ts b/src/stores/ml-store.ts index 6ec52cb..a6c4ad4 100644 --- a/src/stores/ml-store.ts +++ b/src/stores/ml-store.ts @@ -311,11 +311,27 @@ export async function createMLStore(bus: SignalBus): Promise { getImlHand: () => imlHand, setOutputMode: async (mode: OutputMode) => { + const targetCount = outputCountForMode(mode, state.midiCCCount); + const needsResize = targetCount !== outputCount(); + + // Warn about training data loss if resizing with existing examples + if (needsResize && activeIml && activeIml.exampleCount > 0) { + const confirmed = window.confirm( + `Switching to ${mode} mode requires ${targetCount} outputs (currently ${outputCount()}). ` + + `This will reset training examples. Network weights will be partially preserved. Continue?` + ); + if (!confirmed) { + // Abort — mode stays unchanged + return; + } + } + setState('outputMode', mode); modeTopic.emit(mode); - const targetCount = outputCountForMode(mode, state.midiCCCount); - await resizeMLP(targetCount); + if (needsResize) { + await resizeMLP(targetCount); + } }, setMidiCCCount: async (count: number) => { diff --git a/tests/solidjs/output-mode-switching.spec.ts b/tests/solidjs/output-mode-switching.spec.ts new file mode 100644 index 0000000..a456595 --- /dev/null +++ b/tests/solidjs/output-mode-switching.spec.ts @@ -0,0 +1,475 @@ +/** + * e2e tests for f13-output-mode-switching. + * Validates: + * VAL-MODE-001: Default mode is visual + * VAL-MODE-002: Switch to synth mode + * VAL-MODE-003: Switch to MIDI CC mode + * VAL-MODE-004: Switch to Audio Canvas mode + * VAL-MODE-005: Mode switch triggers MLP resize + * VAL-MODE-006: Confirmation dialog on destructive resize + * VAL-MODE-007: MLP warm-start preserves hidden weights + */ +import { test, expect } from '@playwright/test'; +import { loadSolidApp } from './helpers'; + +test.describe('Output Mode Switching', () => { + + // ─── VAL-MODE-001: Default mode is visual ─── + + test('VAL-MODE-001a: on fresh load, output mode is visual', async ({ page }) => { + await loadSolidApp(page); + + const mode = await page.evaluate(() => { + const store = (window as any).__nispsStore; + return store.state.outputMode; + }); + + expect(mode).toBe('visual'); + }); + + test('VAL-MODE-001b: fresh load shows flow field (20 outputs)', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const probe = (window as any).__nisps; + return { + outputCount: probe.getOutputs().length, + outputMode: (window as any).__nispsStore.state.outputMode, + }; + }); + + expect(result.outputMode).toBe('visual'); + expect(result.outputCount).toBe(20); + }); + + test('VAL-MODE-001c: flowfield canvas is visible in visual mode', async ({ page }) => { + await loadSolidApp(page); + + const canvasVisible = await page.evaluate(() => { + const canvas = document.getElementById('flowfield-canvas'); + if (!canvas) return false; + const rect = canvas.getBoundingClientRect(); + return rect.width > 0 && rect.height > 0; + }); + + expect(canvasVisible).toBe(true); + }); + + // ─── VAL-MODE-002: Switch to synth mode ─── + + test('VAL-MODE-002a: switching to synth mode changes outputCount to 126', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + await store.setOutputMode('synth'); + + return { + outputMode: store.state.outputMode, + outputCount: probe.getOutputs().length, + allBounded: probe.getOutputs().every((v: number) => v >= 0 && v <= 1), + }; + }); + + expect(result.outputMode).toBe('synth'); + expect(result.outputCount).toBe(126); + expect(result.allBounded).toBe(true); + }); + + // ─── VAL-MODE-003: Switch to MIDI CC mode ─── + + test('VAL-MODE-003a: switching to midi-cc mode changes outputCount to default 8', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + await store.setOutputMode('midi-cc'); + + return { + outputMode: store.state.outputMode, + outputCount: probe.getOutputs().length, + allBounded: probe.getOutputs().every((v: number) => v >= 0 && v <= 1), + }; + }); + + expect(result.outputMode).toBe('midi-cc'); + expect(result.outputCount).toBe(8); + expect(result.allBounded).toBe(true); + }); + + // ─── VAL-MODE-004: Switch to Audio Canvas mode ─── + + test('VAL-MODE-004a: switching to audio-canvas mode changes outputCount to 36', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + await store.setOutputMode('audio-canvas'); + + return { + outputMode: store.state.outputMode, + outputCount: probe.getOutputs().length, + allBounded: probe.getOutputs().every((v: number) => v >= 0 && v <= 1), + }; + }); + + expect(result.outputMode).toBe('audio-canvas'); + expect(result.outputCount).toBe(36); + expect(result.allBounded).toBe(true); + }); + + // ─── VAL-MODE-005: Mode switch triggers MLP resize ─── + + test('VAL-MODE-005a: switching from visual (20) to synth (126) changes output count', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + const beforeCount = probe.getOutputs().length; + + await store.setOutputMode('synth'); + + return { + beforeCount, + afterCount: probe.getOutputs().length, + }; + }); + + expect(result.beforeCount).toBe(20); + expect(result.afterCount).toBe(126); + }); + + test('VAL-MODE-005b: switching back to visual produces 20 outputs', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + await store.setOutputMode('synth'); + const synthCount = probe.getOutputs().length; + + await store.setOutputMode('visual'); + + return { + synthCount, + visualCount: probe.getOutputs().length, + }; + }); + + expect(result.synthCount).toBe(126); + expect(result.visualCount).toBe(20); + }); + + test('VAL-MODE-005c: MLP IML instances have correct nOutputs after resize', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + await store.setOutputMode('synth'); + + const imlJoy = store.getImlJoy(); + const imlHand = store.getImlHand(); + + return { + joyOutputs: imlJoy?.nOutputs, + handOutputs: imlHand?.nOutputs, + probeCount: probe.getOutputs().length, + }; + }); + + expect(result.joyOutputs).toBe(126); + expect(result.handOutputs).toBe(126); + expect(result.probeCount).toBe(126); + }); + + // ─── VAL-MODE-006: Confirmation dialog on destructive resize ─── + + test('VAL-MODE-006a: confirmation dialog appears when switching modes with training data', async ({ page }) => { + await loadSolidApp(page); + + // Set up a listener for the confirmation dialog before the mode switch + let dialogTriggered = false; + page.on('dialog', async dialog => { + dialogTriggered = true; + expect(dialog.type()).toBe('confirm'); + expect(dialog.message()).toContain('reset'); + await dialog.accept(); + }); + + await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + // Add some training examples + probe.thumbsUp(); + probe.thumbsUp(); + probe.thumbsUp(); + + const count = probe.getExampleCount(); + + // Now switch mode - should trigger confirmation + await store.setOutputMode('synth'); + }); + + expect(dialogTriggered).toBe(true); + }); + + test('VAL-MODE-006b: cancelling confirmation prevents mode switch', async ({ page }) => { + await loadSolidApp(page); + + // Set up a listener to dismiss the dialog + page.on('dialog', async dialog => { + await dialog.dismiss(); + }); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + // Add some training examples + probe.thumbsUp(); + probe.thumbsUp(); + + // Try to switch mode - will be cancelled + await store.setOutputMode('synth'); + + return { + mode: store.state.outputMode, + outputCount: probe.getOutputs().length, + exampleCount: probe.getExampleCount(), + }; + }); + + // Mode should remain unchanged after cancel + expect(result.mode).toBe('visual'); + expect(result.outputCount).toBe(20); + expect(result.exampleCount).toBeGreaterThanOrEqual(2); + }); + + test('VAL-MODE-006c: no confirmation when switching with no training data', async ({ page }) => { + await loadSolidApp(page); + + let dialogTriggered = false; + page.on('dialog', async dialog => { + dialogTriggered = true; + await dialog.accept(); + }); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + // No examples added - fresh state + const count = probe.getExampleCount(); + + // Switch mode - should NOT trigger confirmation + await store.setOutputMode('synth'); + + return { + exampleCountBefore: count, + mode: store.state.outputMode, + outputCount: probe.getOutputs().length, + }; + }); + + expect(dialogTriggered).toBe(false); + expect(result.mode).toBe('synth'); + expect(result.outputCount).toBe(126); + }); + + // ─── VAL-MODE-007: MLP warm-start preserves hidden weights ─── + + test('VAL-MODE-007a: hidden layer weights preserved on mode switch', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + // Randomize weights so they have distinct values + probe.randomise(); + + // Get weights before mode switch + const weightsBefore = probe.getWeights(); + const beforeCount = weightsBefore.length; + + // Compute hidden layer weight count (same architecture hidden layers) + // Architecture: [3, 32, 48, 64, N_outputs] + // Hidden layer 1: 32 * (3+1) = 128 weights + // Hidden layer 2: 48 * (32+1) = 1584 weights + // Hidden layer 3: 64 * (48+1) = 3136 weights + // Total hidden: 128 + 1584 + 3136 = 4848 + const hiddenWeightCount = 3 * (32 + 1) + 32 * (48 + 1) + 48 * (64 + 1); + + // Switch mode (20 → 126 outputs) + await store.setOutputMode('synth'); + + const weightsAfter = probe.getWeights(); + + return { + beforeCount, + afterCount: weightsAfter.length, + hiddenWeightCount, + // Check that hidden weights are preserved (first hiddenWeightCount values) + hiddenWeightsMatch: weightsBefore.slice(0, hiddenWeightCount).every( + (w: number, i: number) => Math.abs(w - weightsAfter[i]) < 1e-6 + ), + // Output layer weights should be different (resized) + outputWeightsChanged: beforeCount !== weightsAfter.length, + }; + }); + + expect(result.hiddenWeightsMatch).toBe(true); + expect(result.outputWeightsChanged).toBe(true); + }); + + test('VAL-MODE-007b: mode switch to smaller output count also preserves hidden weights', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + // Start in synth mode (126 outputs) + await store.setOutputMode('synth'); + probe.randomise(); + + const weightsBefore = probe.getWeights(); + + // Switch to visual (20 outputs) — shrink + await store.setOutputMode('visual'); + + const weightsAfter = probe.getWeights(); + + const hiddenWeightCount = 3 * (32 + 1) + 32 * (48 + 1) + 48 * (64 + 1); + + return { + hiddenWeightsMatch: weightsBefore.slice(0, hiddenWeightCount).every( + (w: number, i: number) => Math.abs(w - weightsAfter[i]) < 1e-6 + ), + }; + }); + + expect(result.hiddenWeightsMatch).toBe(true); + }); + + // ─── Mode switching via UI (pill toggle) ─── + + test('mode pill toggle is visible in mode drawer', async ({ page }) => { + await loadSolidApp(page); + + // Open the mode drawer + await page.click('[data-drawer="mode"]'); + + // Check that pill toggle exists + const pillToggle = page.locator('#output-mode-toggle'); + await expect(pillToggle).toBeVisible(); + + // Check all mode options exist + const pills = page.locator('#output-mode-toggle .pill-opt'); + await expect(pills).toHaveCount(4); + }); + + test('clicking synth pill switches to synth mode', async ({ page }) => { + await loadSolidApp(page); + + // Open mode drawer + await page.click('[data-drawer="mode"]'); + + // Click synth pill + await page.click('#output-mode-toggle .pill-opt[data-mode="synth"]'); + + // Wait for async mode switch to complete + await page.waitForFunction(() => { + const store = (window as any).__nispsStore; + return store.state.outputMode === 'synth' && (window as any).__nisps.getOutputs().length === 126; + }, { timeout: 10_000 }); + + const result = await page.evaluate(() => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + return { + mode: store.state.outputMode, + outputCount: probe.getOutputs().length, + }; + }); + + expect(result.mode).toBe('synth'); + expect(result.outputCount).toBe(126); + }); + + test('active pill reflects current mode', async ({ page }) => { + await loadSolidApp(page); + + // Open mode drawer + await page.click('[data-drawer="mode"]'); + + // Visual should be active by default + const visualPill = page.locator('#output-mode-toggle .pill-opt[data-mode="visual"]'); + await expect(visualPill).toHaveClass(/active/); + + // Click synth + await page.click('#output-mode-toggle .pill-opt[data-mode="synth"]'); + + // Visual should no longer be active, synth should be + const synthPill = page.locator('#output-mode-toggle .pill-opt[data-mode="synth"]'); + await expect(synthPill).toHaveClass(/active/); + }); + + // ─── Inference works after mode switch ─── + + test('inference works correctly after multiple mode switches', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(async () => { + const store = (window as any).__nispsStore; + const probe = (window as any).__nisps; + + // visual → synth → midi-cc → visual + await store.setOutputMode('synth'); + probe.setInputs(0.3, 0.7); + const synthOutputs = probe.getOutputs(); + + await store.setOutputMode('midi-cc'); + probe.setInputs(0.3, 0.7); + const midiOutputs = probe.getOutputs(); + + await store.setOutputMode('visual'); + probe.setInputs(0.3, 0.7); + const visualOutputs = probe.getOutputs(); + + return { + synthCount: synthOutputs.length, + midiCount: midiOutputs.length, + visualCount: visualOutputs.length, + synthBounded: synthOutputs.every((v: number) => v >= 0 && v <= 1), + midiBounded: midiOutputs.every((v: number) => v >= 0 && v <= 1), + visualBounded: visualOutputs.every((v: number) => v >= 0 && v <= 1), + synthFinite: synthOutputs.every((v: number) => Number.isFinite(v)), + midiFinite: midiOutputs.every((v: number) => Number.isFinite(v)), + visualFinite: visualOutputs.every((v: number) => Number.isFinite(v)), + }; + }); + + expect(result.synthCount).toBe(126); + expect(result.midiCount).toBe(8); + expect(result.visualCount).toBe(20); + expect(result.synthBounded).toBe(true); + expect(result.midiBounded).toBe(true); + expect(result.visualBounded).toBe(true); + expect(result.synthFinite).toBe(true); + expect(result.midiFinite).toBe(true); + expect(result.visualFinite).toBe(true); + }); +});