Skip to content

Commit

Permalink
Web renderer vfs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed Aug 13, 2023
1 parent 9f13d30 commit ca7fc2d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
3 changes: 2 additions & 1 deletion js/packages/web-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
],
"scripts": {
"wasm": "./scripts/prebuild.sh",
"build": "rollup -c rollup.config.js"
"build": "rollup -c rollup.config.js",
"serve": "vite -c test/vite.config.js test"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.2",
Expand Down
25 changes: 25 additions & 0 deletions js/packages/web-renderer/test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mocha Tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>

<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>

<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
</script>

<script src="vfs.js" type="module"></script>
<script class="mocha-exec">
mocha.run();
</script>
</body>
</html>
70 changes: 70 additions & 0 deletions js/packages/web-renderer/test/vfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import WebRenderer from '../dist/index.js';
import { el } from '@elemaudio/core';


const audioContext = (typeof window !== 'undefined') && new (window.AudioContext || window.webkitAudioContext)();


describe('Virtual File System', function () {
describe('list', function () {
it('should show registered entries', async function() {
const core = new WebRenderer();
const node = await core.initialize(audioContext, {
numberOfInputs: 0,
numberOfOutputs: 1,
outputChannelCount: [2],
processorOptions: {
virtualFileSystem: {
'test': Float32Array.from([1, 2, 3, 4, 5]),
},
},
});

node.connect(audioContext.destination);
chai.assert.deepEqual(await core.listVirtualFileSystem(), ['test']);

// We haven't rendered anything that holds a reference to the test entry
core.pruneVirtualFileSystem();
chai.assert.deepEqual(await core.listVirtualFileSystem(), []);

// Now we put something back in
core.updateVirtualFileSystem({
'test2': Float32Array.from([2, 3, 4, 5]),
});

// After we render something referencing the test2 entry, prune shouldn't touch it
chai.assert.deepEqual(await core.listVirtualFileSystem(), ['test2']);
chai.assert.include(core.render(el.table({key: 'a', path: 'test2'}, 0.5)), {
nodesAdded: 3,
edgesAdded: 2,
propsWritten: 4,
});;

core.pruneVirtualFileSystem();
chai.assert.deepEqual(await core.listVirtualFileSystem(), ['test2']);

// Now we'll put test3 in and remove our reference to test2
//
// TODO: It's hard to actually test this in an automated fashion in the web context
// because we need the audio context to avoid a suspended state, which it defaults to
// before any user interaction. We need samples flowing through the table node in order
// for it to take/drop its references to resources. Commenting this one out for now.
//
// core.updateVirtualFileSystem({
// 'test3': Float32Array.from([2, 3, 4]),
// });

// chai.assert.include(core.render(el.table({key: 'a', path: 'test3'}, 0.5)), {
// nodesAdded: 0,
// edgesAdded: 0,
// propsWritten: 1,
// });;

// core.pruneVirtualFileSystem();
// chai.assert.deepEqual(await core.listVirtualFileSystem(), ['test3']);

node.disconnect();
});
});
});

7 changes: 7 additions & 0 deletions js/packages/web-renderer/test/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'

export default defineConfig({
optimizeDeps: {
include: ['@elemaudio/core'],
},
})

0 comments on commit ca7fc2d

Please sign in to comment.