diff --git a/src/App.tsx b/src/App.tsx index e34b507..40df8ff 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,6 +1,7 @@ import { onMount, onCleanup, createSignal } from 'solid-js'; import { WasmIML } from './core/iml'; import { createSignalBus, type SignalBus } from './bus/signal-bus'; +import { exposeDebugProbe } from './probe/debug-probe'; const N_INPUTS = 2; const HIDDEN_LAYERS = [32, 48, 64]; @@ -46,10 +47,7 @@ export default function App() { setStatus(`Ready — ${outputs.length} outputs loaded`); // Expose debug probe if ?debug=1 - const params = new URLSearchParams(window.location.search); - if (params.get('debug') === '1') { - exposeDebugProbe(iml); - } + exposeDebugProbe(iml); } catch (err) { setStatus(`Error: ${err}`); console.error('WASM init failed:', err); @@ -88,50 +86,3 @@ export default function App() { ); } - -/** - * Expose window.__nisps debug probe for Playwright tests. - * All methods are synchronous (bypass SolidJS batching). - */ -function exposeDebugProbe(iml: WasmIML): void { - const probe = { - getOutputs: () => iml.getOutputs(), - getLoss: () => iml.lastLoss, - getWeights: () => iml._getFlatWeights(), - getExampleCount: () => iml.exampleCount, - setInputs: (x: number, y: number) => { - iml.setInput(0, Math.max(0, Math.min(1, x))); - iml.setInput(1, Math.max(0, Math.min(1, y))); - iml.process(); - }, - thumbsUp: () => { - iml.addExample(iml.inputState.slice(), iml.outputState.slice()); - return iml.trainAsync(); - }, - thumbsDown: () => { - const spread = 0.6; - const speed = 0.15 * (1 - spread) + 0.05 * spread; - iml.moveWeights(speed, spread); - }, - train: () => iml.train(), - trainAsync: () => iml.trainAsync(), - randomise: () => iml.randomiseWeights(0.6), - clearExamples: () => iml.clearDataset(), - saveState: () => { - const state = { - weights: iml._getFlatWeights(), - inputState: iml.inputState.slice(), - outputState: iml.outputState.slice(), - exampleCount: iml.exampleCount, - lossHistory: iml.lossHistory.slice(), - }; - localStorage.setItem('nisps-a-immersive', JSON.stringify(state)); - }, - evalLoss: () => iml.evalLoss(), - inferBatch: (points: number[][]) => iml.inferBatch(points), - getLayerStats: () => iml.getLayerStats(), - }; - - (window as any).__nisps = probe; - console.log('[NISPS] Debug probe exposed at window.__nisps'); -} diff --git a/src/probe/debug-probe.ts b/src/probe/debug-probe.ts new file mode 100644 index 0000000..fa3748b --- /dev/null +++ b/src/probe/debug-probe.ts @@ -0,0 +1,109 @@ +/** + * Debug probe — window.__nisps exposed when ?debug=1 is in the URL. + * Used by Playwright e2e tests. Zero footprint in production. + * + * All methods are synchronous (bypass SolidJS batching with batch()/untrack()). + */ + +import type { WasmIML } from '../core/iml'; + +export interface DebugProbe { + getOutputs: () => number[]; + getLoss: () => number | null; + getWeights: () => number[]; + getExampleCount: () => number; + setInputs: (x: number, y: number) => void; + thumbsUp: () => Promise; + thumbsDown: () => void; + train: () => number | null; + trainAsync: () => Promise; + randomise: () => void; + clearExamples: () => void; + saveState: () => void; + evalLoss: () => number | null; + inferBatch: (points: number[][]) => number[][]; + getLayerStats: () => Array<{ + meanAbs: number; + maxAbs: number; + deadFrac: number; + satFrac: number; + }>; +} + +/** + * Check if the debug probe should be activated. + */ +export function isDebugMode(): boolean { + const params = new URLSearchParams(window.location.search); + return params.get('debug') === '1'; +} + +/** + * Create the debug probe object, wired to a WasmIML instance. + */ +export function createDebugProbe(iml: WasmIML): DebugProbe { + return { + getOutputs: () => iml.getOutputs(), + + getLoss: () => iml.lastLoss, + + getWeights: () => iml._getFlatWeights(), + + getExampleCount: () => iml.exampleCount, + + setInputs: (x: number, y: number) => { + iml.setInput(0, Math.max(0, Math.min(1, x))); + iml.setInput(1, Math.max(0, Math.min(1, y))); + iml.process(); + }, + + thumbsUp: () => { + iml.addExample(iml.inputState.slice(), iml.outputState.slice()); + return iml.trainAsync(); + }, + + thumbsDown: () => { + const spread = 0.6; + const speed = 0.15 * (1 - spread) + 0.05 * spread; + iml.moveWeights(speed, spread); + }, + + train: () => iml.train(), + + trainAsync: () => iml.trainAsync(), + + randomise: () => iml.randomiseWeights(0.6), + + clearExamples: () => iml.clearDataset(), + + saveState: () => { + const state = { + weights: iml._getFlatWeights(), + inputState: iml.inputState.slice(), + outputState: iml.outputState.slice(), + exampleCount: iml.exampleCount, + lossHistory: iml.lossHistory.slice(), + }; + localStorage.setItem('nisps-a-immersive', JSON.stringify(state)); + }, + + evalLoss: () => iml.evalLoss(), + + inferBatch: (points: number[][]) => iml.inferBatch(points), + + getLayerStats: () => iml.getLayerStats(), + }; +} + +/** + * Expose window.__nisps if ?debug=1 is present. + * Returns the probe object (or undefined if not in debug mode). + */ +export function exposeDebugProbe(iml: WasmIML): DebugProbe | undefined { + if (!isDebugMode()) return undefined; + + const probe = createDebugProbe(iml); + (window as any).__nisps = probe; + console.log('[NISPS] Debug probe exposed at window.__nisps'); + return probe; +} diff --git a/tests/solidjs/debug-probe.spec.ts b/tests/solidjs/debug-probe.spec.ts new file mode 100644 index 0000000..2f116b8 --- /dev/null +++ b/tests/solidjs/debug-probe.spec.ts @@ -0,0 +1,288 @@ +/** + * e2e tests for f03-debug-probe-scaffold. + * Validates: + * VAL-PROJ-005: Debug probe exposed with ?debug=1 + * VAL-PROJ-006: Debug probe NOT exposed without ?debug=1 + */ +import { test, expect } from '@playwright/test'; +import { loadSolidApp } from './helpers'; + +const EXPECTED_METHODS = [ + 'getOutputs', + 'getLoss', + 'getWeights', + 'getExampleCount', + 'setInputs', + 'thumbsUp', + 'thumbsDown', + 'train', + 'trainAsync', + 'randomise', + 'clearExamples', + 'saveState', + 'evalLoss', + 'inferBatch', + 'getLayerStats', +] as const; + +test.describe('Debug Probe (f03)', () => { + + // ─── VAL-PROJ-005: window.__nisps exposed with ?debug=1 ─── + + test('VAL-PROJ-005a: typeof window.__nisps === "object" with ?debug=1', async ({ page }) => { + await loadSolidApp(page); + + const probeType = await page.evaluate(() => typeof (window as any).__nisps); + expect(probeType).toBe('object'); + }); + + test('VAL-PROJ-005b: window.__nisps has all required methods', async ({ page }) => { + await loadSolidApp(page); + + const methodNames = await page.evaluate(() => { + const probe = (window as any).__nisps; + return Object.keys(probe).filter(k => typeof probe[k] === 'function'); + }); + + for (const method of EXPECTED_METHODS) { + expect(methodNames).toContain(method); + } + + // Verify we have at least the expected number of methods + expect(methodNames.length).toBeGreaterThanOrEqual(EXPECTED_METHODS.length); + }); + + test('VAL-PROJ-005c: enumerate all methods are callable', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const probe = (window as any).__nisps; + const checks: Record = {}; + const expectedMethods = [ + 'getOutputs', 'getLoss', 'getWeights', 'getExampleCount', + 'setInputs', 'thumbsUp', 'thumbsDown', 'train', 'trainAsync', + 'randomise', 'clearExamples', 'saveState', 'evalLoss', + 'inferBatch', 'getLayerStats', + ]; + for (const name of expectedMethods) { + checks[name] = typeof probe[name] === 'function'; + } + return checks; + }); + + for (const [method, isFunction] of Object.entries(result)) { + expect(isFunction, `Method "${method}" should be a function`).toBe(true); + } + }); + + // ─── VAL-PROJ-006: window.__nisps is undefined without ?debug=1 ─── + + test('VAL-PROJ-006: window.__nisps is undefined without ?debug=1', async ({ page }) => { + await page.addInitScript(() => { + localStorage.removeItem('nisps-a-immersive'); + localStorage.setItem('nisps-help-seen', '1'); + }); + // Load WITHOUT ?debug=1 + await page.goto('/'); + await page.waitForFunction(() => { + const root = document.getElementById('root'); + return root && root.textContent && root.textContent.includes('MEMLNaut'); + }, { timeout: 10_000 }); + + const probeType = await page.evaluate(() => typeof (window as any).__nisps); + expect(probeType).toBe('undefined'); + }); + + // ─── Probe method smoke tests ─── + + test('probe.getOutputs returns array of 126 bounded values', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const outputs = (window as any).__nisps.getOutputs(); + return { + isArray: Array.isArray(outputs), + length: outputs.length, + allBounded: outputs.every((v: number) => v >= 0 && v <= 1), + }; + }); + + expect(result.isArray).toBe(true); + expect(result.length).toBe(126); + expect(result.allBounded).toBe(true); + }); + + test('probe.getLoss returns null on fresh load', async ({ page }) => { + await loadSolidApp(page); + + const loss = await page.evaluate(() => (window as any).__nisps.getLoss()); + expect(loss).toBeNull(); + }); + + test('probe.getExampleCount returns 0 on fresh load', async ({ page }) => { + await loadSolidApp(page); + + const count = await page.evaluate(() => (window as any).__nisps.getExampleCount()); + expect(count).toBe(0); + }); + + test('probe.setInputs updates outputs synchronously', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const probe = (window as any).__nisps; + const before = [...probe.getOutputs()]; + probe.setInputs(0.1, 0.9); + const after = [...probe.getOutputs()]; + return { changed: !before.every((v: number, i: number) => v === after[i]), bounded: after.every((v: number) => v >= 0 && v <= 1) }; + }); + + expect(result.changed).toBe(true); + expect(result.bounded).toBe(true); + }); + + test('probe.randomise changes all outputs', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const probe = (window as any).__nisps; + const before = [...probe.getOutputs()]; + probe.randomise(); + const after = [...probe.getOutputs()]; + return { + changed: !before.every((v: number, i: number) => v === after[i]), + bounded: after.every((v: number) => v >= 0 && v <= 1), + }; + }); + + expect(result.changed).toBe(true); + expect(result.bounded).toBe(true); + }); + + test('probe.thumbsDown changes outputs', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const probe = (window as any).__nisps; + probe.setInputs(0.5, 0.5); + const before = [...probe.getOutputs()]; + probe.thumbsDown(); + const after = [...probe.getOutputs()]; + return { changed: !before.every((v: number, i: number) => v === after[i]) }; + }); + + expect(result.changed).toBe(true); + }); + + test('probe.train returns null with no examples', async ({ page }) => { + await loadSolidApp(page); + + const loss = await page.evaluate(() => (window as any).__nisps.train()); + expect(loss).toBeNull(); + }); + + test('probe.evalLoss returns null with no examples', async ({ page }) => { + await loadSolidApp(page); + + const loss = await page.evaluate(() => (window as any).__nisps.evalLoss()); + expect(loss).toBeNull(); + }); + + test('probe.inferBatch returns correct shape', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const probe = (window as any).__nisps; + const batch = probe.inferBatch([[0.5, 0.5], [0.25, 0.75], [0.1, 0.9]]); + return { + length: batch.length, + eachLength: batch.map((r: number[]) => r.length), + allBounded: batch.flat().every((v: number) => v >= 0 && v <= 1), + }; + }); + + expect(result.length).toBe(3); + expect(result.eachLength).toEqual([126, 126, 126]); + expect(result.allBounded).toBe(true); + }); + + test('probe.getLayerStats returns valid stats', async ({ page }) => { + await loadSolidApp(page); + + const stats = await page.evaluate(() => (window as any).__nisps.getLayerStats()); + + // Architecture is [3, 32, 48, 64, 126] → 4 layers + expect(stats.length).toBe(4); + for (const layer of stats) { + expect(typeof layer.meanAbs).toBe('number'); + expect(typeof layer.maxAbs).toBe('number'); + expect(typeof layer.deadFrac).toBe('number'); + expect(typeof layer.satFrac).toBe('number'); + expect(layer.deadFrac).toBeGreaterThanOrEqual(0); + expect(layer.deadFrac).toBeLessThanOrEqual(1); + expect(layer.satFrac).toBeGreaterThanOrEqual(0); + expect(layer.satFrac).toBeLessThanOrEqual(1); + } + }); + + test('probe.saveState writes valid JSON to localStorage', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const probe = (window as any).__nisps; + probe.saveState(); + const raw = localStorage.getItem('nisps-a-immersive'); + if (!raw) return { valid: false, reason: 'no data' }; + try { + const parsed = JSON.parse(raw); + return { + valid: true, + hasWeights: Array.isArray(parsed.weights), + hasInputState: Array.isArray(parsed.inputState), + hasOutputState: Array.isArray(parsed.outputState), + hasExampleCount: typeof parsed.exampleCount === 'number', + }; + } catch (e) { + return { valid: false, reason: String(e) }; + } + }); + + expect(result.valid).toBe(true); + if ('hasWeights' in result) { + expect(result.hasWeights).toBe(true); + expect(result.hasInputState).toBe(true); + expect(result.hasOutputState).toBe(true); + expect(result.hasExampleCount).toBe(true); + } + }); + + test('probe.clearExamples resets example count', async ({ page }) => { + await loadSolidApp(page); + + const count = await page.evaluate(() => { + const probe = (window as any).__nisps; + // Even though we haven't added examples, clearExamples should work + probe.clearExamples(); + return probe.getExampleCount(); + }); + + expect(count).toBe(0); + }); + + test('probe.getWeights returns array', async ({ page }) => { + await loadSolidApp(page); + + const result = await page.evaluate(() => { + const weights = (window as any).__nisps.getWeights(); + return { + isArray: Array.isArray(weights), + length: weights.length, + allNumbers: weights.every((v: number) => typeof v === 'number' && Number.isFinite(v)), + }; + }); + + expect(result.isArray).toBe(true); + expect(result.length).toBeGreaterThan(0); + expect(result.allNumbers).toBe(true); + }); +});