Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

songs: resolve metronome AudioContext lazily so node-env tests don't hit undefined window

[._.]/ Adam Eivy9263b3ddeb62
diff --git a/client/src/lib/metronome.js b/client/src/lib/metronome.js
index 5590eed6dc..b9b2db46d4 100644
--- a/client/src/lib/metronome.js
+++ b/client/src/lib/metronome.js
@@ -32,7 +32,15 @@ const START_LEAD_S = 0.1; // small lead before the first beat absorbs jitt
let sharedCtx = null;
function audioContext() {
if (!sharedCtx) {
- const Ctor = window.AudioContext || window.webkitAudioContext;
+ // Resolve the constructor lazily and never touch a bare `window` at module
+ // load — the server's vitest run globs this file's tests in the node
+ // environment (no jsdom), where `window` is undefined. Guard with
+ // `typeof window` and fall back to `globalThis` so the pure exports import
+ // cleanly and tests can inject a fake via globalThis.AudioContext.
+ const Ctor =
+ (typeof window !== 'undefined' && (window.AudioContext || window.webkitAudioContext)) ||
+ globalThis.AudioContext ||
+ globalThis.webkitAudioContext;
sharedCtx = new Ctor();
}
return sharedCtx;
diff --git a/client/src/lib/metronome.test.js b/client/src/lib/metronome.test.js
index ab111539a1..3d8738ecb2 100644
--- a/client/src/lib/metronome.test.js
+++ b/client/src/lib/metronome.test.js
@@ -43,7 +43,12 @@ function makeFakeAudioContext() {
// The metronome module caches one shared AudioContext, so we reuse a single
// fake across tests (resetting its state each time) rather than swapping it.
-// `window.AudioContext` must be a real constructor — an arrow fn isn't `new`able.
+// The fake must be a real constructor — an arrow fn isn't `new`able.
+//
+// Inject it via `vi.stubGlobal` on globalThis (the source falls back to
+// globalThis.AudioContext when `window` is absent) rather than `window.…`, so
+// this suite runs under the server's node test environment too — that run globs
+// client lib tests with no jsdom, so a bare `window` reference would throw.
let fakeCtx = makeFakeAudioContext();
beforeEach(() => {
@@ -51,11 +56,12 @@ beforeEach(() => {
fakeCtx.state = 'running';
fakeCtx.started.length = 0;
function FakeAudioContext() { return fakeCtx; }
- window.AudioContext = FakeAudioContext;
+ vi.stubGlobal('AudioContext', FakeAudioContext);
});
afterEach(() => {
vi.useRealTimers();
+ vi.unstubAllGlobals();
});
describe('clampBpm', () => {