forked from sudip-mondal-2002/Amplitron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamplitron.spec.ts
More file actions
614 lines (492 loc) · 21.3 KB
/
Copy pathamplitron.spec.ts
File metadata and controls
614 lines (492 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/**
* Playwright end-to-end tests for the Amplitron web demo.
*
* The suite validates the complete WASM lifecycle as seen in a real browser:
* 1. Page load & WASM initialisation
* 2. Audio-unlock prompt
* 3. Canvas rendering
* 4. SharedArrayBuffer / COI service-worker availability
* 5. Absence of runtime errors
*
* All tests share the same local static server (configured in playwright.config.ts)
* that serves the Emscripten build with the required COOP/COEP headers.
*/
import { test, expect, Page, ConsoleMessage } from '@playwright/test';
// Emscripten injects `Module` into the page's global scope at runtime.
// Declare it here so TypeScript doesn't report ts(2304) errors.
// Overloads narrow the return type based on the `returnType` string literal.
declare const Module: {
ccall(ident: string, returnType: 'number', argTypes: string[], args: (number | string | boolean)[]): number;
ccall(ident: string, returnType: 'boolean', argTypes: string[], args: (number | string | boolean)[]): boolean;
ccall(ident: string, returnType: 'string', argTypes: string[], args: (number | string | boolean)[]): string;
ccall(ident: string, returnType: null, argTypes: string[], args: (number | string | boolean)[]): void;
};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Collect every console message and page error that fires during a test. */
interface PageLog {
messages: ConsoleMessage[];
errors: Error[];
}
function attachLogger(page: Page): PageLog {
const log: PageLog = { messages: [], errors: [] };
page.on('console', msg => log.messages.push(msg));
page.on('pageerror', err => log.errors.push(err));
return log;
}
/** Return the text of every console message of type 'error'. */
function consoleErrors(log: PageLog): string[] {
return log.messages
.filter(m => m.type() === 'error')
.map(m => m.text());
}
/** Return the texts of all log.messages regardless of type. */
function allMessageTexts(log: PageLog): string[] {
return log.messages.map(m => m.text());
}
// ---------------------------------------------------------------------------
// 1. Page Load & WASM Initialisation
// ---------------------------------------------------------------------------
test.describe('Page Load & WASM Initialisation', () => {
test('page loads without JavaScript errors', async ({ page }) => {
const log = attachLogger(page);
await page.goto('/');
// The page must be present and reachable
await expect(page).toHaveTitle(/Amplitron/i);
// No uncaught JS exceptions on the initial page load
expect(log.errors).toHaveLength(0);
});
test('loading overlay is visible on initial page load', async ({ page }) => {
// Only wait for DOMContentLoaded — WASM initialises asynchronously after
// that, so the overlay must still be visible at this point.
await page.goto('/', { waitUntil: 'domcontentloaded' });
const loading = page.locator('#loading');
// The overlay should exist
await expect(loading).toBeAttached();
// It must NOT already be hidden (class 'hidden' is only added after WASM init)
const classes = await loading.getAttribute('class');
expect(classes ?? '').not.toContain('hidden');
});
test('progress bar fills as WASM data downloads', async ({ page }) => {
// Collect recorded widths from the fill element via polling
await page.goto('/');
// Wait until WASM finishes (progress bar may animate very quickly on a
// fast local server, so we just assert it reaches 100% at some point)
await page.waitForFunction(
() => {
const fill = document.getElementById('progress-fill');
if (!fill) return false;
const w = parseFloat(fill.style.width || '0');
return w >= 100;
},
{ timeout: 60_000, polling: 200 }
);
const width = await page.locator('#progress-fill').evaluate(
(el: HTMLElement) => parseFloat(el.style.width || '0')
);
expect(width).toBeGreaterThanOrEqual(100);
});
test('loading overlay disappears once WASM runtime is ready', async ({ page }) => {
await page.goto('/');
// Wait for the 'hidden' class to be applied (Module.setStatus('') callback)
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
// The overlay must be invisible to the user (CSS: opacity:0; pointer-events:none)
await expect(page.locator('#loading')).toHaveClass(/hidden/);
});
test('WASM runtime logs "[Amplitron] WASM runtime ready." to the console', async ({
page,
}) => {
const log = attachLogger(page);
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
expect(allMessageTexts(log)).toContain('[Amplitron] WASM runtime ready.');
});
});
// ---------------------------------------------------------------------------
// 2. Audio Unlock Prompt
// ---------------------------------------------------------------------------
test.describe('Audio Unlock Prompt', () => {
test('audio-unlock overlay appears after WASM finishes loading', async ({
page,
}) => {
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
const overlay = page.locator('#audio-unlock');
// The overlay becomes display:flex inside Module.setStatus('')
await expect(overlay).toBeVisible({ timeout: 10_000 });
});
test('clicking the audio-unlock overlay dismisses it', async ({ page }) => {
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
const overlay = page.locator('#audio-unlock');
await expect(overlay).toBeVisible({ timeout: 10_000 });
await overlay.click();
// After click, onclick sets display:'none' → the overlay is no longer visible
await expect(overlay).toBeHidden({ timeout: 5_000 });
});
});
// ---------------------------------------------------------------------------
// 3. Canvas Rendering
// ---------------------------------------------------------------------------
test.describe('Canvas Rendering', () => {
test('canvas element exists and has non-zero dimensions', async ({ page }) => {
await page.goto('/');
const canvas = page.locator('#canvas');
await expect(canvas).toBeAttached();
const dims = await canvas.evaluate((el: HTMLCanvasElement) => ({
offsetWidth: el.offsetWidth,
offsetHeight: el.offsetHeight,
}));
expect(dims.offsetWidth).toBeGreaterThan(0);
expect(dims.offsetHeight).toBeGreaterThan(0);
});
test('canvas has non-zero internal (backing-buffer) dimensions after WASM init', async ({
page,
}) => {
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
// Dismiss audio unlock so the ImGui main-loop has a chance to render
const overlay = page.locator('#audio-unlock');
if (await overlay.isVisible()) {
await overlay.click();
}
// Wait until the canvas has been given non-zero dimensions by the Emscripten runtime
await page.waitForFunction(() => {
const canvas = document.getElementById('canvas') as HTMLCanvasElement | null;
return canvas !== null && canvas.width > 0 && canvas.height > 0;
}, { timeout: 10_000 });
const { width, height } = await page.locator('#canvas').evaluate(
(el: HTMLCanvasElement) => ({ width: el.width, height: el.height })
);
expect(width).toBeGreaterThan(0);
expect(height).toBeGreaterThan(0);
});
test('canvas is rendered (pixel content is not entirely transparent/black)', async ({
page,
}) => {
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
const overlay = page.locator('#audio-unlock');
if (await overlay.isVisible()) {
await overlay.click();
}
// Give the ImGui loop a few frames to populate the colour buffer
await page.waitForTimeout(1_000);
const hasNonBlackPixels = await page.evaluate(() => {
const canvas = document.getElementById('canvas') as HTMLCanvasElement;
// The canvas uses a WebGL2 context (Emscripten default)
const gl =
(canvas.getContext('webgl2') as WebGL2RenderingContext | null) ||
(canvas.getContext('webgl') as WebGLRenderingContext | null);
if (!gl) return false;
// Sample a strip of pixels across the centre of the canvas
const sampleCount = 32;
const pixels = new Uint8Array(sampleCount * 4);
gl.readPixels(
Math.max(0, Math.floor(canvas.width / 2) - sampleCount / 2),
Math.floor(canvas.height / 2),
sampleCount,
1,
gl.RGBA,
gl.UNSIGNED_BYTE,
pixels
);
return pixels.some(v => v !== 0);
});
// Soft assertion: may legitimately be false when the GPU is fully emulated
// and the canvas back-buffer is cleared before readPixels is called.
expect.soft(hasNonBlackPixels).toBe(true);
});
});
// ---------------------------------------------------------------------------
// 4. SharedArrayBuffer & Service Worker
// ---------------------------------------------------------------------------
test.describe('SharedArrayBuffer & Service Worker', () => {
test('SharedArrayBuffer is available in the page context', async ({ page }) => {
const log = attachLogger(page);
await page.goto('/');
const sabAvailable = await page.evaluate(
() => typeof SharedArrayBuffer !== 'undefined'
);
expect(sabAvailable).toBe(true);
// Must not produce any "SharedArrayBuffer is not defined" console errors
const sabErrors = consoleErrors(log).filter(msg =>
msg.toLowerCase().includes('sharedarraybuffer')
);
expect(sabErrors).toHaveLength(0);
});
test('coi-serviceworker.js is fetched successfully', async ({ page }) => {
// Track network requests to the service-worker script
const swRequests: number[] = [];
page.on('response', response => {
if (response.url().includes('coi-serviceworker.js')) {
swRequests.push(response.status());
}
});
await page.goto('/');
// The shell.html loads coi-serviceworker.js via a <script> tag
expect(swRequests.length).toBeGreaterThan(0);
expect(swRequests.every(status => status === 200)).toBe(true);
});
test('service worker is registered or COOP/COEP headers enable SAB directly', async ({
page,
}) => {
await page.goto('/');
// If the server already sends COOP/COEP headers the page is cross-origin
// isolated from the start and coi-serviceworker.js skips SW registration,
// so navigator.serviceWorker.ready would hang forever. Exit early in that
// case; otherwise wait for the service worker to become active.
await page.waitForFunction(async () => {
if (window.crossOriginIsolated) return true;
try {
await navigator.serviceWorker.ready;
return true;
} catch {
return false;
}
}, { timeout: 15_000 });
const [crossOriginIsolated, swCount] = await page.evaluate(async () => {
const regs = await navigator.serviceWorker.getRegistrations();
return [window.crossOriginIsolated, regs.length] as [boolean, number];
});
// The page must be cross-origin isolated (either via COOP/COEP headers from
// the server or via the COI service worker after a reload)
expect(crossOriginIsolated).toBe(true);
// When crossOriginIsolated is satisfied by the server headers alone,
// coi-serviceworker.js registers 0 service workers; log for debugging
expect(typeof swCount).toBe('number');
});
});
// ---------------------------------------------------------------------------
// 5. No Runtime Errors
// ---------------------------------------------------------------------------
test.describe('No Runtime Errors', () => {
test('no uncaught exceptions during the full load cycle', async ({ page }) => {
const log = attachLogger(page);
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
expect(log.errors).toHaveLength(0);
});
test('no WASM abort() or RuntimeError: unreachable panics', async ({ page }) => {
const log = attachLogger(page);
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
const wasmPanics = log.errors.filter(
err =>
err.message.includes('abort') ||
err.message.includes('RuntimeError') ||
err.message.includes('unreachable') ||
err.message.includes('memory access out of bounds')
);
expect(wasmPanics).toHaveLength(0);
});
test('no SharedArrayBuffer errors in the console during load', async ({ page }) => {
const log = attachLogger(page);
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
const sabErrors = [
...consoleErrors(log),
...log.errors.map(e => e.message),
].filter(msg => msg.toLowerCase().includes('sharedarraybuffer'));
expect(sabErrors).toHaveLength(0);
});
});
// ---------------------------------------------------------------------------
// 6. Web MIDI Support
// ---------------------------------------------------------------------------
test.describe('Web MIDI Support', () => {
test('MIDI CC11 controls output gain', async ({ page }) => {
// Set up mock BEFORE page loads
await page.addInitScript(() => {
// Store the captured listener
let capturedListener: ((event: any) => void) | null = null;
// Create a mock input port that properly captures addEventListener calls
const mockInput = {
name: 'Mock MIDI Controller',
state: 'connected',
id: 'mock-device-id',
manufacturer: 'Test',
addEventListener: (eventName: string, callback: any) => {
if (eventName === 'midimessage') {
capturedListener = callback;
console.log('[TEST-MOCK] Listener captured for midimessage');
}
},
removeEventListener: () => {},
};
// Create mock MIDI access that returns our mock device
const mockMidiAccess = {
inputs: new Map([['mock-device-id', mockInput]]),
outputs: new Map(),
addEventListener: () => {},
removeEventListener: () => {},
sysexEnabled: true,
};
// Override navigator.requestMIDIAccess BEFORE the page requests it
(window.navigator as any).requestMIDIAccess = async () => {
console.log('[TEST-MOCK] requestMIDIAccess called');
// Simulate a brief delay (like real MIDI access)
await new Promise(resolve => setTimeout(resolve, 50));
// Schedule the mock MIDI message to fire AFTER the listener is attached
setTimeout(() => {
if (capturedListener) {
console.log('[TEST-MOCK] Sending mock CC11 message');
const mockEvent = {
data: new Uint8Array([0xB0, 11, 64]), // CC11, value 64
};
capturedListener(mockEvent);
} else {
console.warn('[TEST-MOCK] Listener not yet captured!');
}
}, 200); // Wait 200ms to ensure listener is attached
return mockMidiAccess;
};
console.log('[TEST-MOCK] Mock MIDI API injected');
});
// Load the page
await page.goto('/');
// Wait for WASM to load
await page.waitForSelector('#loading.hidden', { timeout: 10000 });
// Click to unlock audio AND trigger MIDI initialization
await page.click('#audio-unlock');
// Wait for MIDI status to appear with the device name
// This is the key assertion — if it passes, MIDI is working
const midiStatus = page.locator('#midi-status');
await expect(midiStatus).toContainText('MIDI Active: Mock MIDI Controller', {
timeout: 5000
});
// Verify no errors occurred
const errors: string[] = [];
page.on('console', (msg) => {
if (msg.type() === 'error') {
errors.push(msg.text());
console.log('[BROWSER-ERROR]', msg.text());
}
});
// Give it time to report any errors
await page.waitForTimeout(500);
expect(errors).toHaveLength(0);
});
test('Gracefully handles missing Web MIDI support', async ({ page }) => {
// Remove Web MIDI API from the browser
await page.addInitScript(() => {
delete (window.navigator as any).requestMIDIAccess;
console.log('[TEST-MOCK] Web MIDI API removed');
});
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 10000 });
await page.click('#audio-unlock');
// Should show the unsupported message
const midiStatus = page.locator('#midi-status');
await expect(midiStatus).toContainText('MIDI not supported', {
timeout: 5000
});
});
});
test.describe('Modular Graph Canvas Interactions', () => {
async function waitForRuntime(page: Page) {
page.on('console', msg => console.log('BROWSER LOG:', msg.text()));
page.on('pageerror', err => console.error('BROWSER ERROR:', err.message));
await page.goto('/');
await page.waitForSelector('#loading.hidden', { timeout: 60_000 });
const overlay = page.locator('#audio-unlock');
if (await overlay.isVisible()) await overlay.click();
await page.waitForTimeout(500);
}
test('canvas pan via right-click drag shifts scrolling', async ({ page }) => {
await waitForRuntime(page);
const before = await page.evaluate(() => ({
x: Module.ccall('get_canvas_scroll_x', 'number', [], []),
y: Module.ccall('get_canvas_scroll_y', 'number', [], []),
}));
const canvas = page.locator('#canvas');
const box = await canvas.boundingBox();
if (!box) throw new Error('canvas not visible');
const cx = box.x + box.width / 2;
const cy = box.y + box.height / 2;
await page.mouse.click(cx, cy, { button: 'right' });
await page.mouse.move(cx, cy);
await page.mouse.down({ button: 'right' });
await page.mouse.move(cx + 80, cy + 60, { steps: 10 });
await page.mouse.up({ button: 'right' });
await page.waitForTimeout(200);
const after = await page.evaluate(() => ({
x: Module.ccall('get_canvas_scroll_x', 'number', [], []),
y: Module.ccall('get_canvas_scroll_y', 'number', [], []),
}));
expect(after.x).not.toBeCloseTo(before.x, 0);
expect(after.y).not.toBeCloseTo(before.y, 0);
});
test('two-finger touch gesture pans and zooms the canvas', async ({ page }) => {
await waitForRuntime(page);
const before = await page.evaluate(() => ({
zoom: Module.ccall('get_canvas_zoom', 'number', [], []),
sx: Module.ccall('get_canvas_scroll_x', 'number', [], []),
}));
await page.evaluate(() => {
Module.ccall('on_canvas_touch_gesture', null, ['number','number','number','number','number'], [30, 20, 0.15, 640, 360]);
});
const after = await page.evaluate(() => ({
zoom: Module.ccall('get_canvas_zoom', 'number', [], []),
sx: Module.ccall('get_canvas_scroll_x', 'number', [], []),
}));
expect(after.zoom).toBeGreaterThan(before.zoom);
expect(after.sx).not.toBeCloseTo(before.sx, 0);
});
test('adding a Splitter node increases the node count', async ({ page }) => {
await waitForRuntime(page);
const countBefore: number = await page.evaluate(() =>
Module.ccall('get_node_count', 'number', [], [])
);
await page.evaluate(() =>
Module.ccall('trigger_add_splitter_node', 'number', [], [])
);
await page.waitForTimeout(200);
const countAfter: number = await page.evaluate(() =>
Module.ccall('get_node_count', 'number', [], [])
);
expect(countAfter).toBe(countBefore + 1);
const hasSplitter: boolean = await page.evaluate(() =>
Module.ccall('has_node_of_type', 'boolean', ['number'], [1])
);
expect(hasSplitter).toBe(true);
});
test('drawing a cable between two nodes increases link count', async ({ page }) => {
await waitForRuntime(page);
const linksBefore: number = await page.evaluate(() =>
Module.ccall('get_link_count', 'number', [], [])
);
await page.evaluate(() => {
Module.ccall('trigger_add_splitter_node', 'number', [], []);
});
await page.waitForTimeout(100);
const result: number = await page.evaluate(() => {
const srcPin = Module.ccall('get_node_output_pin_by_index', 'number', ['number', 'number'], [2, 0]);
const dstPin = Module.ccall('get_node_input_pin_by_index', 'number', ['number', 'number'], [3, 0]);
return Module.ccall('trigger_add_link', 'number', ['number', 'number'], [srcPin, dstPin]);
});
const linksAfter: number = await page.evaluate(() =>
Module.ccall('get_link_count', 'number', [], [])
);
expect(linksAfter).toBeGreaterThan(linksBefore);
});
test('deleting a node decreases the node count', async ({ page }) => {
await waitForRuntime(page);
await page.evaluate(() =>
Module.ccall('trigger_add_splitter_node', 'number', [], [])
);
await page.waitForTimeout(100);
const countBefore: number = await page.evaluate(() =>
Module.ccall('get_node_count', 'number', [], [])
);
const deleted: boolean = await page.evaluate(() =>
Module.ccall('trigger_delete_last_node', 'boolean', [], [])
);
expect(deleted).toBe(true);
const countAfter: number = await page.evaluate(() =>
Module.ccall('get_node_count', 'number', [], [])
);
expect(countAfter).toBe(countBefore - 1);
});
});