From 4e46036191324d0f83b5616001603756f350c0ee Mon Sep 17 00:00:00 2001 From: monkey-w1n5t0n Date: Tue, 7 Apr 2026 20:28:17 +0200 Subject: [PATCH] feat: add visual preset chips (calm-to-chaotic, rainbow, vortex, spiral, embers) that load predefined examples and trigger training (f12) Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- src/components/layout/TrainingDrawer.tsx | 24 +++ src/components/layout/drawer.css | 43 +++++ src/stores/ml-store.ts | 95 +++++++++++ tests/solidjs/visual-presets.spec.ts | 208 +++++++++++++++++++++++ 4 files changed, 370 insertions(+) create mode 100644 tests/solidjs/visual-presets.spec.ts diff --git a/src/components/layout/TrainingDrawer.tsx b/src/components/layout/TrainingDrawer.tsx index 961467a..601e48e 100644 --- a/src/components/layout/TrainingDrawer.tsx +++ b/src/components/layout/TrainingDrawer.tsx @@ -7,6 +7,7 @@ * - Clear Ex button (clears examples only) * - Clear All button (clears everything including loss history) * - Randomize button (randomizes weights) + * - Visual preset chips (load predefined examples + train) * - Loss plot canvas (shows loss history) * * Status line is rendered separately at the bottom of the screen. @@ -15,8 +16,10 @@ import { createSignal, onCleanup, + For, } from 'solid-js'; import type { MLStore } from '../../stores/ml-store'; +import { VISUAL_PRESET_NAMES, VISUAL_PRESET_LABELS } from '../../stores/ml-store'; import './drawer.css'; export interface TrainingDrawerProps { @@ -58,6 +61,11 @@ export default function TrainingDrawer(props: TrainingDrawerProps) { props.mlStore.randomise(); } + function onLoadPreset(name: string): void { + props.mlStore.loadVisualPreset(name); + drawLossPlot(); + } + // ─── Button flash effect ─── function flashButton(id: string): void { @@ -131,6 +139,22 @@ export default function TrainingDrawer(props: TrainingDrawerProps) { Randomize +
+ +
+ + {(name) => ( + + )} + +
+
= { + 'calm-to-chaotic': [ + { input: [0.1, 0.9], output: [0.25, 0.3, 0.1, 0.55, 0.2, 0.3, 0.02, 0.05, 0.9, 0.45, 0.25, 0.2, 0.9, 0.0, 0.0, 0.2, 0.05, 0.0, 0.0, 0.2] }, + { input: [0.9, 0.1], output: [0.75, 0.7, 0.9, 0.05, 0.8, 0.7, 0.9, 0.95, 0.3, 0.2, 0.85, 0.7, 0.25, 0.55, 1.0, 0.92, 0.02, 0.95, 0.8, 0.85] }, + { input: [0.5, 0.5], output: [0.5, 0.5, 0.5, 0.3, 0.5, 0.5, 0.4, 0.5, 0.7, 0.6, 0.5, 0.45, 0.55, 0.9, 0.5, 0.65, 0.08, 0.45, 0.4, 0.5] }, + ], + 'rainbow-sweep': [ + { input: [0.0, 0.5], output: [0.5, 0.5, 0.4, 0.0, 0.3, 0.4, 0.05, 0.3, 0.8, 0.55, 0.4, 0.3, 0.8, 0.0, 0.0, 0.45, 0.08, 0.2, 0.25, 0.45] }, + { input: [0.5, 0.5], output: [0.5, 0.5, 0.4, 0.5, 0.3, 0.4, 0.05, 0.3, 0.8, 0.55, 0.55, 0.35, 0.7, 0.0, 0.4, 0.45, 0.08, 0.45, 0.4, 0.6] }, + { input: [1.0, 0.5], output: [0.5, 0.5, 0.4, 1.0, 0.3, 0.4, 0.05, 0.3, 0.8, 0.55, 0.75, 0.45, 0.6, 0.0, 0.8, 0.45, 0.08, 0.7, 0.55, 0.75] }, + ], + 'vortex': [ + { input: [0.5, 0.5], output: [0.0, 0.8, 0.8, 0.6, 0.1, 0.15, 0.02, 1.0, 1.0, 0.3, 0.95, 0.85, 0.25, 1.0, 0.5, 0.95, 0.01, 1.0, 1.0, 1.0] }, + { input: [0.0, 0.0], output: [0.5, 0.2, 0.3, 0.8, 0.9, 0.6, 0.08, 0.1, 0.35, 0.8, 0.25, 0.15, 0.8, 0.5, 0.2, 0.35, 0.2, 0.15, 0.2, 0.25] }, + { input: [1.0, 1.0], output: [0.5, 0.2, 0.3, 0.2, 0.9, 0.6, 0.08, 0.1, 0.35, 0.8, 0.25, 0.15, 0.8, 0.5, 0.8, 0.35, 0.2, 0.15, 0.2, 0.25] }, + { input: [0.0, 1.0], output: [0.3, 0.4, 0.5, 0.4, 0.5, 0.4, 0.05, 0.5, 0.65, 0.5, 0.55, 0.45, 0.45, 0.2, 0.4, 0.7, 0.1, 0.5, 0.4, 0.55] }, + { input: [1.0, 0.0], output: [0.7, 0.4, 0.5, 0.0, 0.5, 0.4, 0.05, 0.5, 0.65, 0.5, 0.55, 0.45, 0.45, 0.2, 0.9, 0.7, 0.1, 0.5, 0.4, 0.55] }, + ], + 'spiral': [ + { input: [0.5, 0.5], output: [0.0, 0.6, 0.6, 0.3, 0.15, 0.2, 0.03, 0.7, 0.9, 0.25, 0.7, 0.6, 0.35, 0.8, 0.65, 0.9, 0.02, 0.3, 0.5, 0.7] }, + { input: [0.0, 0.0], output: [0.8, 0.3, 0.4, 0.7, 0.8, 0.5, 0.06, 0.2, 0.5, 0.7, 0.3, 0.2, 0.7, 0.3, 0.3, 0.4, 0.15, 0.1, 0.1, 0.3] }, + { input: [1.0, 1.0], output: [0.2, 0.3, 0.4, 0.1, 0.8, 0.5, 0.06, 0.2, 0.5, 0.7, 0.3, 0.2, 0.7, 0.3, 0.9, 0.4, 0.15, 0.1, 0.1, 0.3] }, + ], + 'embers': [ + { input: [0.5, 0.5], output: [0.1, 0.4, 0.2, 0.05, 0.05, 0.6, 0.02, 0.15, 0.6, 0.3, 0.15, 0.9, 0.5, 0.7, 0.1, 0.3, 0.2, 0.0, 0.0, 0.2] }, + { input: [0.2, 0.8], output: [0.5, 0.6, 0.15, 0.08, 0.08, 0.8, 0.015, 0.1, 0.8, 0.5, 0.1, 0.5, 0.8, 0.4, 0.05, 0.5, 0.1, 0.0, 0.0, 0.15] }, + { input: [0.8, 0.2], output: [0.9, 0.3, 0.35, 0.02, 0.12, 0.35, 0.04, 0.3, 0.4, 0.2, 0.3, 1.0, 0.3, 0.9, 0.2, 0.15, 0.3, 0.0, 0.0, 0.3] }, + ], +}; + +/** Get list of available visual preset names */ +export const VISUAL_PRESET_NAMES = Object.keys(VISUAL_PRESETS); + +/** Display labels for visual presets */ +export const VISUAL_PRESET_LABELS: Record = { + 'calm-to-chaotic': 'Calm/Chaos', + 'rainbow-sweep': 'Rainbow', + 'vortex': 'Vortex', + 'spiral': 'Spiral', + 'embers': 'Embers', +}; + // ─── ML Store Interface ────────────────────────────────────────────── export interface MLState { @@ -100,6 +154,8 @@ export interface MLStore { getExampleCount(): number; getLoss(): number | null; getLossHistory(): number[]; + /** Load a visual preset: clears dataset, adds preset examples, trains, updates outputs */ + loadVisualPreset(name: string): void; saveState(): void; // Lifecycle @@ -388,6 +444,45 @@ export async function createMLStore(bus: SignalBus): Promise { getLossHistory: () => activeIml?.lossHistory.slice() ?? [], + loadVisualPreset: (name: string) => { + if (!imlJoy) return; + const preset = VISUAL_PRESETS[name]; + if (!preset) return; + + // Always apply to joystick IML (visual presets use 2 inputs) + imlJoy.clearDataset(); + + // Pad 20-element preset outputs to current output count with 0.5 defaults + const currentOutputCount = outputCount(); + for (const ex of preset) { + const paddedOutputs = new Array(currentOutputCount).fill(0.5); + for (let i = 0; i < ex.output.length && i < paddedOutputs.length; i++) { + paddedOutputs[i] = ex.output[i]; + } + imlJoy.addExample(ex.input.slice(), paddedOutputs); + } + + // Point to joystick IML for training + const prevActive = activeIml; + activeIml = imlJoy; + + // Sync training + activeIml.train(); + + // Run inference with current inputs + activeIml.process(); + const newOutputs = new Float32Array(activeIml.getOutputs()); + setOutputs(newOutputs); + outputsTopic.emit(newOutputs); + + // Update reactive signals + setExampleCountSignal(activeIml.exampleCount); + setLastLossSignal(activeIml.lastLoss); + + // Restore active IML pointer + activeIml = prevActive; + }, + saveState: () => { if (!activeIml) return; const data = { diff --git a/tests/solidjs/visual-presets.spec.ts b/tests/solidjs/visual-presets.spec.ts new file mode 100644 index 0000000..7fa1fa7 --- /dev/null +++ b/tests/solidjs/visual-presets.spec.ts @@ -0,0 +1,208 @@ +/** + * Visual Preset Chips tests — VAL-TRAIN-007 assertion. + * + * Covers feature f12-visual-presets: + * - Preset chips visible in training drawer + * - Clicking calm-to-chaotic loads 3 examples and triggers training + * - Status shows loss after preset loading + * - Each preset loads correct number of examples + */ + +import { test, expect } from '@playwright/test'; +import { loadSolidApp } from './helpers'; + +test.describe('Visual Presets (f12)', () => { + test.beforeEach(async ({ page }) => { + await loadSolidApp(page); + }); + + // ─── Preset chip visibility ─── + + test('VAL-TRAIN-007a: preset chips are visible in training drawer', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + await expect(page.locator('#drawer-training')).toBeVisible(); + + // All 5 preset chips should exist + const chips = page.locator('.preset-chip'); + await expect(chips).toHaveCount(5); + + // Check each preset chip is present + await expect(page.locator('.preset-chip[data-preset="calm-to-chaotic"]')).toBeVisible(); + await expect(page.locator('.preset-chip[data-preset="rainbow-sweep"]')).toBeVisible(); + await expect(page.locator('.preset-chip[data-preset="vortex"]')).toBeVisible(); + await expect(page.locator('.preset-chip[data-preset="spiral"]')).toBeVisible(); + await expect(page.locator('.preset-chip[data-preset="embers"]')).toBeVisible(); + }); + + test('VAL-TRAIN-007b: preset chips have readable labels', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + const calm = page.locator('.preset-chip[data-preset="calm-to-chaotic"]'); + await expect(calm).toHaveText(/Calm/i); + + const rainbow = page.locator('.preset-chip[data-preset="rainbow-sweep"]'); + await expect(rainbow).toHaveText(/Rain/i); + + const vortex = page.locator('.preset-chip[data-preset="vortex"]'); + await expect(vortex).toHaveText(/Vortex/i); + + const spiral = page.locator('.preset-chip[data-preset="spiral"]'); + await expect(spiral).toHaveText(/Spir/i); + + const embers = page.locator('.preset-chip[data-preset="embers"]'); + await expect(embers).toHaveText(/Ember/i); + }); + + // ─── VAL-TRAIN-007: calm-to-chaotic loads 3 examples + training ─── + + test('VAL-TRAIN-007c: clicking calm-to-chaotic loads 3 examples', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + // Verify fresh state + let count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(0); + + // Click calm-to-chaotic preset + await page.locator('.preset-chip[data-preset="calm-to-chaotic"]').click(); + + // Wait for training to complete (loadVisualPreset does training) + await page.waitForFunction( + () => (window as any).__nisps.getLoss() !== null, + { timeout: 15_000 } + ); + + count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(3); + }); + + test('VAL-TRAIN-007d: status shows loss after clicking calm-to-chaotic', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + await page.locator('.preset-chip[data-preset="calm-to-chaotic"]').click(); + + // Wait for training to complete + await page.waitForFunction( + () => (window as any).__nisps.getLoss() !== null, + { timeout: 15_000 } + ); + + // Status should show loss value + const statusText = await page.locator('#status-text').textContent(); + expect(statusText).toContain('3 examples'); + expect(statusText).toMatch(/loss \d/); + expect(statusText).not.toContain('untrained'); + + // Loss should be a finite number + const loss = await page.evaluate(() => (window as any).__nisps.getLoss()); + expect(loss).not.toBeNull(); + expect(typeof loss).toBe('number'); + expect(isFinite(loss as number)).toBe(true); + }); + + // ─── Other presets load correct example counts ─── + + test('VAL-TRAIN-007e: rainbow-sweep loads 3 examples', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + await page.locator('.preset-chip[data-preset="rainbow-sweep"]').click(); + + await page.waitForFunction( + () => (window as any).__nisps.getLoss() !== null, + { timeout: 15_000 } + ); + + const count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(3); + }); + + test('VAL-TRAIN-007f: vortex loads 5 examples', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + await page.locator('.preset-chip[data-preset="vortex"]').click(); + + await page.waitForFunction( + () => (window as any).__nisps.getLoss() !== null, + { timeout: 15_000 } + ); + + const count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(5); + }); + + test('VAL-TRAIN-007g: spiral loads 3 examples', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + await page.locator('.preset-chip[data-preset="spiral"]').click(); + + await page.waitForFunction( + () => (window as any).__nisps.getLoss() !== null, + { timeout: 15_000 } + ); + + const count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(3); + }); + + test('VAL-TRAIN-007h: embers loads 3 examples', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + await page.locator('.preset-chip[data-preset="embers"]').click(); + + await page.waitForFunction( + () => (window as any).__nisps.getLoss() !== null, + { timeout: 15_000 } + ); + + const count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(3); + }); + + // ─── Preset clears previous examples ─── + + test('VAL-TRAIN-007i: clicking a preset clears previous examples first', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + // Add a manual example first + await page.locator('#btn-add-example').click(); + let count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(1); + + // Click a preset — should clear and load its own examples + await page.locator('.preset-chip[data-preset="calm-to-chaotic"]').click(); + + await page.waitForFunction( + () => (window as any).__nisps.getLoss() !== null, + { timeout: 15_000 } + ); + + count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(3); // calm-to-chaotic has 3, not 4 + }); + + // ─── Preset changes outputs ─── + + test('VAL-TRAIN-007j: preset changes outputs from initial state', async ({ page }) => { + await page.locator('.dock-icon[data-drawer="training"]').click(); + + const outputsBefore = await page.evaluate(() => Array.from((window as any).__nisps.getOutputs())); + + await page.locator('.preset-chip[data-preset="calm-to-chaotic"]').click(); + + await page.waitForFunction( + () => (window as any).__nisps.getLoss() !== null, + { timeout: 15_000 } + ); + + const outputsAfter = await page.evaluate(() => Array.from((window as any).__nisps.getOutputs())); + + // At least some outputs should differ after preset loading + training + let changed = false; + for (let i = 0; i < Math.min(outputsBefore.length, outputsAfter.length); i++) { + if (Math.abs(outputsBefore[i] - outputsAfter[i]) > 0.001) { + changed = true; + break; + } + } + expect(changed).toBe(true); + }); +});