Skip to content

Split any YouTube song into stems

TypeScript

test: share ts import resolution hook across tests

David Navia695688066d16
diff --git a/tests/engine.test.mjs b/tests/engine.test.mjs
index ed19f59..e656707 100644
--- a/tests/engine.test.mjs
+++ b/tests/engine.test.mjs
@@ -1,18 +1,8 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
-import { registerHooks } from 'node:module'
+import { registerTsHooks } from './helpers/ts-resolve.mjs'
-// renderer modules use extensionless relative imports (bundler resolution);
-// node needs the .ts spelled out
-const srcRoot = new URL('../src/', import.meta.url).href
-const hooks = registerHooks({
- resolve(specifier, context, next) {
- if (context.parentURL?.startsWith(srcRoot) && specifier.startsWith('.') && !/\.\w+$/.test(specifier)) {
- return { shortCircuit: true, url: new URL(`${specifier}.ts`, context.parentURL).href }
- }
- return next(specifier, context)
- }
-})
+const hooks = registerTsHooks()
const { StemEngine } = await import('../src/renderer/src/lib/engine.ts')
hooks.deregister()
diff --git a/tests/helpers/ts-resolve.mjs b/tests/helpers/ts-resolve.mjs
new file mode 100644
index 0000000..463449a
--- /dev/null
+++ b/tests/helpers/ts-resolve.mjs
@@ -0,0 +1,22 @@
+import { registerHooks } from 'node:module'
+
+const srcRoot = new URL('../../src/', import.meta.url).href
+
+// src/ uses extensionless relative imports (bundler resolution); node needs the .ts spelled out.
+// With stubEnv, './env' is replaced by a data-URL module with that source (it touches Electron).
+// Deregister the returned hooks once the dynamic imports are done.
+export function registerTsHooks({ stubEnv } = {}) {
+ return registerHooks({
+ resolve(specifier, context, next) {
+ if (context.parentURL?.startsWith(srcRoot) && specifier.startsWith('.')) {
+ if (stubEnv !== undefined && specifier === './env') {
+ return { shortCircuit: true, url: `data:text/javascript,${encodeURIComponent(stubEnv)}` }
+ }
+ if (!/\.\w+$/.test(specifier)) {
+ return { shortCircuit: true, url: new URL(`${specifier}.ts`, context.parentURL).href }
+ }
+ }
+ return next(specifier, context)
+ }
+ })
+}
diff --git a/tests/key-storage.test.mjs b/tests/key-storage.test.mjs
index 90d06a2..6eca89b 100644
--- a/tests/key-storage.test.mjs
+++ b/tests/key-storage.test.mjs
@@ -1,6 +1,6 @@
import { test, after } from 'node:test'
import assert from 'node:assert/strict'
-import { registerHooks } from 'node:module'
+import { registerTsHooks } from './helpers/ts-resolve.mjs'
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -8,33 +8,10 @@ import { join } from 'node:path'
const root = mkdtempSync(join(tmpdir(), 'stemkit-key-'))
after(() => rmSync(root, { recursive: true, force: true }))
const src = (f) => new URL(`../src/main/${f}`, import.meta.url).href
-const parents = new Set([src('library.ts'), src('keydetect.ts')])
const envStub = `export const userDataDir = () => ${JSON.stringify(root)}
export const venvPython = () => 'python3'
export const keyScript = () => 'key.py'`
-const hooks = registerHooks({
- resolve(specifier, context, next) {
- if (parents.has(context.parentURL) && specifier === './env') {
- return { shortCircuit: true, url: `data:text/javascript,${encodeURIComponent(envStub)}` }
- }
- if (parents.has(context.parentURL) && specifier === '../shared/types') {
- return { shortCircuit: true, url: new URL('../src/shared/types.ts', import.meta.url).href }
- }
- if (parents.has(context.parentURL) && specifier === '../shared/keys') {
- return { shortCircuit: true, url: new URL('../src/shared/keys.ts', import.meta.url).href }
- }
- if (context.parentURL === src('keydetect.ts') && specifier === './library') {
- return { shortCircuit: true, url: src('library.ts') }
- }
- if (context.parentURL === src('keydetect.ts') && specifier === './separation-slots') {
- return { shortCircuit: true, url: src('separation-slots.ts') }
- }
- if (parents.has(context.parentURL) && specifier === '../shared/pitch-range') {
- return { shortCircuit: true, url: new URL('../src/shared/pitch-range.ts', import.meta.url).href }
- }
- return next(specifier, context)
- }
-})
+const hooks = registerTsHooks({ stubEnv: envStub })
const { saveSongs, loadSongs, patchSongKey, setKeyOverride } = await import(src('library.ts'))
const { keyInputs, detectOnce, withOverride } = await import(src('keydetect.ts'))
hooks.deregister()
diff --git a/tests/pan-storage.test.mjs b/tests/pan-storage.test.mjs
index 14c4809..e8641a0 100644
--- a/tests/pan-storage.test.mjs
+++ b/tests/pan-storage.test.mjs
@@ -1,6 +1,6 @@
import { test, after } from 'node:test'
import assert from 'node:assert/strict'
-import { registerHooks } from 'node:module'
+import { registerTsHooks } from './helpers/ts-resolve.mjs'
import { mkdtempSync, mkdirSync, writeFileSync, existsSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -8,23 +8,7 @@ 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 }
- }
- if (context.parentURL === libraryUrl && specifier === '../shared/keys') {
- return { shortCircuit: true, url: new URL('../src/shared/keys.ts', import.meta.url).href }
- }
- if (context.parentURL === libraryUrl && specifier === '../shared/pitch-range') {
- return { shortCircuit: true, url: new URL('../src/shared/pitch-range.ts', import.meta.url).href }
- }
- return next(specifier, context)
- }
-})
+const hooks = registerTsHooks({ stubEnv: `export const userDataDir = () => ${JSON.stringify(root)}` })
const { saveSongs, loadStemPan, saveStemPan, removeSong } = await import(libraryUrl)
hooks.deregister()
const song = { videoId: 'song-one', stems: ['vocals', 'drums'], title: 'Test', duration: 10, addedAt: 1 }
diff --git a/tests/pitch-storage.test.mjs b/tests/pitch-storage.test.mjs
index efdd665..559f31d 100644
--- a/tests/pitch-storage.test.mjs
+++ b/tests/pitch-storage.test.mjs
@@ -1,6 +1,6 @@
import { test, after } from 'node:test'
import assert from 'node:assert/strict'
-import { registerHooks } from 'node:module'
+import { registerTsHooks } from './helpers/ts-resolve.mjs'
import { mkdtempSync, mkdirSync, writeFileSync, existsSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -8,23 +8,7 @@ import { join } from 'node:path'
const root = mkdtempSync(join(tmpdir(), 'stemkit-pitch-'))
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 }
- }
- if (context.parentURL === libraryUrl && specifier === '../shared/keys') {
- return { shortCircuit: true, url: new URL('../src/shared/keys.ts', import.meta.url).href }
- }
- if (context.parentURL === libraryUrl && specifier === '../shared/pitch-range') {
- return { shortCircuit: true, url: new URL('../src/shared/pitch-range.ts', import.meta.url).href }
- }
- return next(specifier, context)
- }
-})
+const hooks = registerTsHooks({ stubEnv: `export const userDataDir = () => ${JSON.stringify(root)}` })
const { saveSongs, loadPitch, savePitch, removeSong } = await import(libraryUrl)
hooks.deregister()
const song = { videoId: 'song-one', stems: ['vocals', 'drums'], title: 'Test', duration: 10, addedAt: 1 }