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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address Copilot review round 2: ESM-safe write spy + drop redundant page listener
Adam Eivy1b077c54571c
diff --git a/client/src/pages/Instances.jsx b/client/src/pages/Instances.jsxindex 72cdb59869..946386c716 100644--- a/client/src/pages/Instances.jsx+++ b/client/src/pages/Instances.jsx@@ -1049,20 +1049,15 @@ export default function Instances() {setPeers(updatedPeers);};socket.on('instances:peers:updated', handlePeersUpdated);- // Per-record peer-sync subscription blocked / unblocked → re-fetch so- // the SchemaGapBadge picks up the new `blockedBySchema` field. The server- // mutates the subscription state directly; the only cross-tab signal is- // the socket event, so we refetch peers + their sub lists. Cheap because- // it's already throttled by the user's edit cadence.- const handleSchemaSubChange = () => { fetchData(); };- socket.on('peerSync:subscription-blocked', handleSchemaSubChange);- socket.on('peerSync:subscription-unblocked', handleSchemaSubChange);+ // NOTE: `peerSync:subscription-blocked/unblocked` is intentionally NOT+ // handled here. Those events only mutate per-record subscription state+ // (`blockedBySchema`), which each PeerCard refetches for its own peer via+ // its peerSubs effect. A page-level fetchData() on every such event would+ // refetch all peers + syncStatus for a change that touches only one card.return () => {socket.emit('instances:unsubscribe');socket.off('instances:peers:updated', handlePeersUpdated);- socket.off('peerSync:subscription-blocked', handleSchemaSubChange);- socket.off('peerSync:subscription-unblocked', handleSchemaSubChange);};}, [fetchData]);diff --git a/server/lib/mockPathsDataRoot.js b/server/lib/mockPathsDataRoot.jsindex d1942daf67..8d62027962 100644--- a/server/lib/mockPathsDataRoot.js+++ b/server/lib/mockPathsDataRoot.js@@ -63,7 +63,7 @@ export function createTempDataRoot(prefix = 'portos-test-') {* - a function `(dataRoot) => overridesObject` — for cases where the* extra keys are derived (e.g. `images: join(dataRoot, 'images')`).*/-export function makePathsProxy(actual, { dataRoot, extraOverrides = null } = {}) {+export function makePathsProxy(actual, { dataRoot, extraOverrides = null, overrides = null } = {}) {const resolveRoot = typeof dataRoot === 'function' ? dataRoot : () => dataRoot;const buildOverrides = () => {const root = resolveRoot();@@ -75,6 +75,10 @@ export function makePathsProxy(actual, { dataRoot, extraOverrides = null } = {})return new Proxy(actual, {get(target, prop) {if (prop === 'PATHS') return buildOverrides();+ // Top-level export overrides (e.g. a delegating spy for atomicWrite).+ // Served through the get trap so callers never vi.spyOn a read-only+ // ESM namespace export — see mockPathsDataRoot's wrapExports option.+ if (overrides && prop in overrides) return overrides[prop];return target[prop];},});@@ -94,12 +98,42 @@ export function makePathsProxy(actual, { dataRoot, extraOverrides = null } = {})* return makeProxy(actual);* });* afterAll(cleanup);+ *+ * To inspect call counts on a fileUtils export (e.g. atomicWrite) WITHOUT+ * `vi.spyOn`-ing a read-only ESM namespace export, pass `wrapExports` plus+ * `makeSpy: vi.fn` (the test owns vitest; this module stays vitest-free since+ * it's barrel-exported and runtime-loaded). The wrapped exports are exposed+ * on the returned `spies` map, each a vi.fn delegating to the real impl:+ *+ * const { makeProxy, spies, cleanup } = mockPathsDataRoot({+ * wrapExports: ['atomicWrite'], makeSpy: vi.fn,+ * });+ * // ...later: spies.atomicWrite.mock.calls*/-export function mockPathsDataRoot({ prefix = 'portos-test-', extraOverrides = null } = {}) {+export function mockPathsDataRoot({+ prefix = 'portos-test-',+ extraOverrides = null,+ wrapExports = [],+ makeSpy = null,+} = {}) {const tempRoot = createTempDataRoot(prefix);+ const spies = {};return {tempRoot,- makeProxy: (actual) => makePathsProxy(actual, { dataRoot: tempRoot, extraOverrides }),+ spies,+ makeProxy: (actual) => {+ const overrides = {};+ if (wrapExports.length) {+ if (typeof makeSpy !== 'function') {+ throw new Error('mockPathsDataRoot: wrapExports requires a makeSpy (pass vi.fn)');+ }+ for (const name of wrapExports) {+ spies[name] = makeSpy((...args) => actual[name](...args));+ overrides[name] = spies[name];+ }+ }+ return makePathsProxy(actual, { dataRoot: tempRoot, extraOverrides, overrides });+ },cleanup: () => rmSync(tempRoot, { recursive: true, force: true }),};}diff --git a/server/services/universeBuilderPromote.test.js b/server/services/universeBuilderPromote.test.jsindex d1295641c6..435d03b70a 100644--- a/server/services/universeBuilderPromote.test.js+++ b/server/services/universeBuilderPromote.test.js@@ -2,7 +2,14 @@ import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest';import { rmSync, mkdirSync } from 'fs';import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';-const { tempRoot, makeProxy, cleanup } = mockPathsDataRoot({ prefix: 'portos-universe-promote-' });+// `wrapExports: ['atomicWrite']` exposes a delegating vi.fn at+// `spies.atomicWrite` so the atomicity test can count writes WITHOUT+// vi.spyOn-ing the read-only ESM export (which throws in Vitest).+const { tempRoot, makeProxy, cleanup, spies } = mockPathsDataRoot({+ prefix: 'portos-universe-promote-',+ wrapExports: ['atomicWrite'],+ makeSpy: vi.fn,+});afterAll(cleanup);vi.mock('../lib/fileUtils.js', async () => {@@ -40,7 +47,6 @@ assertProvider: (provider, { message, code, status = 503 } = {}) => {// the mock chain.const svc = await import('./universeBuilder.js');const promoteSvc = await import('./universeBuilderPromote.js');-const fileUtils = await import('../lib/fileUtils.js');const seedUniverseWithBucket = async (categories, canon = {}) => {const w = await svc.createUniverse({@@ -440,16 +446,14 @@ describe('universeBuilderPromote — atomicity', () => {// index.json) — saveTypeIndex writes that happen elsewhere in the test's// setup are excluded.const recordIndexPath = `${tempRoot}/universes/${w.id}/index.json`;- const writeSpy = vi.spyOn(fileUtils, 'atomicWrite');- const beforeCalls = writeSpy.mock.calls.length;+ const beforeCalls = spies.atomicWrite.mock.calls.length;await promoteSvc.promoteVariationToCanon(w.id, {category: 'landscapes',label: 'A',});- const recordWrites = writeSpy.mock.calls+ const recordWrites = spies.atomicWrite.mock.calls.slice(beforeCalls).filter(([path]) => path === recordIndexPath);- writeSpy.mockRestore();// Canon append AND variation removal must land in one persistence write// — not two. A two-write split would expose a half-state window where a// crash leaves the variation gone but the canon entry never written