Split any YouTube song into stems
TypeScript
test: cover pan playback and storage
omere7bf449e8132
diff --git a/package.json b/package.jsonindex 8fac151..dcf0bb4 100644--- a/package.json+++ b/package.json@@ -14,6 +14,7 @@"typecheck:node": "node scripts/run.cjs typecheck:node","typecheck:web": "node scripts/run.cjs typecheck:web","typecheck": "node scripts/run.cjs typecheck",+ "test:audio": "node scripts/run.cjs test:audio","dist": "node scripts/run.cjs dist","dist:win": "node scripts/run.cjs dist:win","dist:linux": "node scripts/run.cjs dist:linux",diff --git a/scripts/run.cjs b/scripts/run.cjsindex bda2e58..49a4683 100644--- a/scripts/run.cjs+++ b/scripts/run.cjs@@ -73,6 +73,7 @@ function findNode(requiredMajor) {}const STEPS = {+ 'test:audio': [['node', '--experimental-strip-types', '--test', 'tests/engine.test.mjs', 'tests/pan-storage.test.mjs']],dev: [['electron-vite', 'dev']],build: [['electron-vite', 'build']],typecheck: [diff --git a/tests/engine.test.mjs b/tests/engine.test.mjsnew file mode 100644index 0000000..4689af5--- /dev/null+++ b/tests/engine.test.mjs@@ -0,0 +1,70 @@+import { test } from 'node:test'+import assert from 'node:assert/strict'+import { StemEngine } from '../src/renderer/src/lib/engine.ts'++class Param {+ value = 0+ setTargetAtTime(value) { this.value = value }+}+class AudioNode {+ outputs = []+ connect(node) { this.outputs.push(node) }+ disconnect() { this.outputs = [] }+}+class Context {+ static last+ currentTime = 1+ destination = new AudioNode()+ sources = []+ constructor() { Context.last = this }+ createGain() { return Object.assign(new AudioNode(), { gain: new Param() }) }+ createStereoPanner() { return Object.assign(new AudioNode(), { pan: new Param() }) }+ createBufferSource() {+ const node = Object.assign(new AudioNode(), { playbackRate: new Param(), start() {}, stop() {} })+ this.sources.push(node)+ return node+ }+ async resume() {}+}+globalThis.AudioContext = Context+const buffers = { vocals: { duration: 10 }, drums: { duration: 10 } }+const nodes = (source) => {+ const gain = source.outputs[0]+ const pan = gain.outputs[0]+ return { gain, pan, master: pan.outputs[0] }+}++test('saved mix applies on first playback and pan stays independent of mute/solo', () => {+ const engine = new StemEngine()+ engine.setBuffers(buffers)+ engine.applyMix({ vocals: 0.4, drums: 0.8 }, new Set(), new Set(['vocals']), 0.6, { vocals: -1, drums: 1 })+ engine.setPlaying(true, 0)+ const [voice, drums] = Context.last.sources.map(nodes)+ assert.equal(voice.pan.pan.value, -1)+ assert.equal(drums.pan.pan.value, 1)+ assert.equal(voice.gain.gain.value, 0.4)+ assert.equal(drums.gain.gain.value, 0)+ assert.equal(voice.master.gain.value, 0.6)+ engine.applyMix({ vocals: 0.4, drums: 0.8 }, new Set(['vocals']), new Set(), 0.6, { vocals: 0, drums: 1 })+ assert.equal(voice.pan.pan.value, 0)+ assert.equal(drums.pan.pan.value, 1)+ assert.equal(voice.gain.gain.value, 0)+ assert.equal(drums.gain.gain.value, 0.8)+})++test('seeking and resume preserve pan; switching songs disconnects and resets it', () => {+ const engine = new StemEngine()+ engine.setBuffers(buffers)+ engine.applyMix({}, new Set(), new Set(), 0.9, { vocals: 0.55 })+ engine.setPlaying(true, 0)+ const original = nodes(Context.last.sources[0])+ engine.align(4)+ assert.equal(nodes(Context.last.sources[2]).pan, original.pan)+ engine.setPlaying(false, 4)+ engine.setPlaying(true, 4)+ assert.equal(nodes(Context.last.sources[4]).pan.pan.value, 0.55)+ engine.setBuffers(buffers)+ assert.deepEqual(original.pan.outputs, [])+ engine.setPlaying(true, 0)+ assert.equal(nodes(Context.last.sources[6]).pan.pan.value, 0)+})diff --git a/tests/pan-storage.test.mjs b/tests/pan-storage.test.mjsnew file mode 100644index 0000000..9d2657d--- /dev/null+++ b/tests/pan-storage.test.mjs@@ -0,0 +1,42 @@+import { test, after } from 'node:test'+import assert from 'node:assert/strict'+import { registerHooks } from 'node:module'+import { mkdtempSync, mkdirSync, writeFileSync, existsSync, rmSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'++const root = mkdtempSync(join(tmpdir(), 'stemkit-pan-'))+after(() => rmSync(root, { recursive: true, force: true }))+const libraryUrl = new URL('../src/main/library.ts', import.meta.url).href+const hooks = registerHooks({+ resolve(specifier, context, next) {+ if (context.parentURL === libraryUrl && specifier === './env') {+ return { shortCircuit: true, url: `data:text/javascript,${encodeURIComponent(`export const userDataDir = () => ${JSON.stringify(root)}`)}` }+ }+ if (context.parentURL === libraryUrl && specifier === '../shared/types') {+ return { shortCircuit: true, url: new URL('../src/shared/types.ts', import.meta.url).href }+ }+ return next(specifier, context)+ }+})+const { saveSongs, loadStemPan, saveStemPan, removeSong } = await import(libraryUrl)+hooks.deregister()+const song = { videoId: 'song-one', stems: ['vocals', 'drums'], title: 'Test', duration: 10, addedAt: 1 }++test('pan persists per song, validates values, handles old/corrupt files, and deletes with song', () => {+ saveSongs([song, { ...song, videoId: 'song-two' }])+ mkdirSync(join(root, 'songs', song.videoId), { recursive: true })+ assert.deepEqual(loadStemPan(song.videoId), {})+ saveStemPan(song.videoId, { vocals: -0.5, drums: 3, other: 0.8 })+ assert.deepEqual(loadStemPan(song.videoId), { vocals: -0.5, drums: 1 })+ assert.deepEqual(loadStemPan('song-two'), {})+ saveStemPan(song.videoId, { vocals: NaN, drums: 'left' })+ assert.deepEqual(loadStemPan(song.videoId), {})+ writeFileSync(join(root, 'songs', song.videoId, 'pan.json'), '{broken')+ assert.deepEqual(loadStemPan(song.videoId), {})+ assert.throws(() => saveStemPan('../escape', {}), /Unknown song/)+ removeSong(song.videoId)+ assert.equal(existsSync(join(root, 'songs', song.videoId)), false)+ assert.throws(() => saveStemPan(song.videoId, { vocals: 1 }), /Unknown song/)+ assert.equal(existsSync(join(root, 'songs', song.videoId)), false)+})