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

feat(test): global vitest setup mocks getPeers → [] to block live peer fan-out

Adam Eivyd1a346f8ff96
diff --git a/server/services/instances.test.js b/server/services/instances.test.js
index 7b74bed850..b973214461 100644
--- a/server/services/instances.test.js
+++ b/server/services/instances.test.js
@@ -1,5 +1,12 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+// This suite tests the real instances.js implementation — cancel the global
+// vitest.setup.js mock so the real getPeers (and all other exports) are used.
+// The global mock stubs getPeers → [] to prevent live peer fan-out in tests
+// that create records; here we instead mock instances.js's own dependencies
+// (fileUtils, asyncMutex, etc.) to make the real code deterministic.
+vi.unmock('./instances.js');
+
// Mock dependencies before importing the module
vi.mock('fs/promises', () => ({
writeFile: vi.fn().mockResolvedValue(undefined),
diff --git a/server/vitest.config.js b/server/vitest.config.js
index f5eb6e6d0c..314c6d1078 100644
--- a/server/vitest.config.js
+++ b/server/vitest.config.js
@@ -40,6 +40,9 @@ export default defineConfig({
statements: 30
}
},
- globals: true
+ globals: true,
+ // Global setup: mocks getPeers → [] so test-created records never fan out
+ // to live sync peers. Per-suite vi.mock('./instances.js', …) overrides win.
+ setupFiles: ['./vitest.setup.js'],
}
});
diff --git a/server/vitest.setup.js b/server/vitest.setup.js
new file mode 100644
index 0000000000..d97f84b2bc
--- /dev/null
+++ b/server/vitest.setup.js
@@ -0,0 +1,50 @@
+/**
+ * Global Vitest setup — peer fan-out firewall.
+ *
+ * WHY: `createUniverse` / `createSeries` / `createIssue` fire a non-awaited
+ * `autoSubscribeRecordToAllPeers` call after creation. That path reads the
+ * real `data/instances.json` via `getPeers()` and then issues live HTTP POSTs
+ * to any registered peers (e.g. the user's `null` sync machine). Without a
+ * global guard, every `npm test` run creates spurious records on live peers.
+ *
+ * WHAT: This file is loaded by Vitest as a `setupFiles` entry (see
+ * `vitest.config.js`). It registers a global `vi.mock` for
+ * `services/instances.js` that forces `getPeers` to return `[]` while
+ * preserving all other real exports via `importActual`. Uses the shared
+ * `mockNoPeers` helper from `lib/mockPathsDataRoot.js` — the same factory
+ * the existing point-fix mocks use — so the behavior is identical.
+ *
+ * SCOPE: The mock is applied before every test file. All test files that
+ * already call `vi.mock('./instances.js', …)` (or `vi.mock('../instances.js',
+ * …)`) at their own file level will have their per-suite factory win over this
+ * global one — Vitest resolves both to the same module path and the last
+ * registered factory for a module wins per-suite. Those existing point-fix
+ * mocks therefore remain harmless and do not need to be removed.
+ *
+ * OPT-OUT (rare — only needed when a suite tests the real `instances.js`
+ * implementation, such as `instances.test.js`):
+ *
+ * Option A — cancel the global mock so the real module is used; mock its
+ * own dependencies instead to make behaviour deterministic:
+ *
+ * vi.unmock('./instances.js'); // path relative to the test file; hoisted
+ *
+ * Option B — keep the mock but replace getPeers with a vi.fn() so the suite
+ * can control per-test return values (used by syncOrchestrator, peerSync, …):
+ *
+ * vi.mock('./instances.js', () => ({ getPeers: vi.fn(), … }));
+ *
+ * Both forms are standard Vitest patterns; no helper or flag is needed.
+ * See `server/services/instances.test.js` (Option A) and the sharing/
+ * suites (Option B) for working examples.
+ */
+
+import { mockNoPeers } from './lib/mockPathsDataRoot.js';
+
+// Path is relative to the project root (server/) as required by Vitest
+// setupFiles resolution — it resolves to the same module as any
+// `./instances.js` or `../instances.js` reference used in test files.
+vi.mock('./services/instances.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ return mockNoPeers(actual);
+});