Files
ForgeFlow/tests/repository-monitor.test.mjs
T
NuklearRabbitandClaude Opus 5 9260d35957 fix: repair broken IPC wiring and cut the cost of repository polling
Three handlers referenced a dependency they were never given, which made them
throw a ReferenceError as soon as they ran:

- deployment:preflight for Gitea Actions profiles (`preflight` was passed to
  registerOperationsIpc but not to registerDeploymentIpc)
- Unraid write-access repair (`safeRelativeRemoteFile` was missing from
  createUnraidAccessMethods)
- a dead reference of the same name in unraid-state-methods

no-undef and no-unused-vars were disabled for every file, which is why none of
these were caught. Both are now enabled for src/main and src/shared, where the
dependency graph is explicit. The renderer keeps them off because its functions
are deliberately cross-script globals.

Performance:

- git.status() spawned three processes (rev-parse, status, remote get-url) per
  call. A directory holding its own .git is by definition the work tree root, so
  rev-parse is unnecessary, and the remote URL is cached against the mtime of
  .git/config, including the failure for a repository without that remote.
- git status runs with --no-optional-locks so a read no longer rewrites the
  index. That stops it fighting a concurrent Git command for the index lock, and
  is what makes filesystem watching viable at all.
- One commit issued four `git status` reads; callers that already hold the
  status now pass it on, leaving two.
- The repository monitor is event driven. A watched repository is read on
  filesystem activity, with a 30s safety net for watchers that stop delivering
  and a 1s floor so a busy tree cannot drive a read per event. Repositories that
  cannot be watched keep using the interval. Idle cost for one repository over
  35s: 24 git processes before, 3 after.
- Resolving one repository by name no longer refreshes the whole workspace.
- Concurrent configuration saves share a single write of the latest state.
- Repository discovery follows directory junctions again. The filter that
  skipped them made the realpath cycle guard dead code, and hid any project
  folder reached through a junction.

Renderer:

- render() replaced the whole shell on every poll, discarding focus, caret and
  scroll position while the user was typing. Those are preserved now, and an
  unchanged render leaves the DOM alone entirely.
- The four sections that enhanceRenderedUi() injected after render moved into
  the views, so the rendered markup is the single source of truth.
- The monitor no longer keeps a repository paused forever when it is unlinked
  mid-mutation, scheduleAutoRefresh honours its delay argument, the demo bridges
  no longer block startup, and #app is no longer an aria-live region announcing
  the entire UI on every render.

IPC channel plumbing moved to src/main/ipc/channel.cjs, replacing a module-level
mutable diagnostics singleton with an argument.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-23 14:31:58 +02:00

105 lines
3.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import monitorModule from '../src/main/repository-monitor.cjs';
const { RepositoryMonitor } = monitorModule;
test('repository monitor establishes a baseline and emits only on later changes', async () => {
let revision = 1;
const changes = [];
const git = {
status: async (localPath) => ({ localPath, revision }),
statusFingerprint: (status) => String(status.revision)
};
const store = { data: { preferences: { autoRefresh: true, repositoryPollSeconds: 2 } } };
const monitor = new RepositoryMonitor({ store, git, onChange: (change) => changes.push(change) });
monitor.setPaths(['/repo']);
await monitor.tick();
assert.equal(changes.length, 0);
revision = 2;
await monitor.tick();
assert.equal(changes.length, 1);
assert.equal(changes[0].reason, 'working-tree-changed');
monitor.pause('/repo');
revision = 3;
await monitor.tick();
assert.equal(changes.length, 1);
monitor.resume('/repo');
await monitor.tick();
assert.equal(changes.length, 2);
});
test('repository monitor checks multiple repositories concurrently with a bounded worker pool', async () => {
let active = 0;
let peak = 0;
const git = {
status: async (localPath) => {
active += 1;
peak = Math.max(peak, active);
await new Promise((resolve) => setTimeout(resolve, 15));
active -= 1;
return { localPath, revision: 1 };
},
statusFingerprint: (status) => String(status.revision)
};
const store = { data: { preferences: { autoRefresh: true, repositoryPollSeconds: 2 } } };
const monitor = new RepositoryMonitor({ store, git });
monitor.setPaths(Array.from({ length: 10 }, (_, index) => `/repo-${index}`));
await monitor.tick();
assert.equal(peak, 4);
assert.equal(active, 0);
assert.equal(monitor.fingerprints.size, 10);
});
test('a watched repository is read on filesystem activity instead of on every interval', async (context) => {
const { mkdtemp, mkdir, writeFile, rm } = await import('node:fs/promises');
const os = await import('node:os');
const path = await import('node:path');
const root = await mkdtemp(path.join(os.tmpdir(), 'forgeflow-watch-'));
context.after(() => rm(root, { recursive: true, force: true }));
await mkdir(path.join(root, '.git'), { recursive: true });
let revision = 1;
const reads = [];
const changes = [];
const git = {
status: async (localPath) => { reads.push(localPath); return { localPath, revision }; },
statusFingerprint: (status) => String(status.revision)
};
const store = { data: { preferences: { autoRefresh: true, repositoryPollSeconds: 2 } } };
const monitor = new RepositoryMonitor({ store, git, onChange: (change) => changes.push(change) });
context.after(() => monitor.stop());
monitor.restart();
monitor.setPaths([root]);
if (!monitor.watchers.has(root)) {
context.skip('this platform does not support recursive directory watching');
return;
}
await monitor.tick();
assert.equal(reads.length, 1, 'the baseline is established once');
// Without filesystem activity the interval must not spawn another read.
await monitor.tick();
assert.equal(reads.length, 1);
revision = 2;
await writeFile(path.join(root, 'feature.txt'), 'changed\n');
// The watcher debounce and the per-repository cooldown both apply here.
const deadline = Date.now() + 5_000;
while (changes.length === 0 && Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
assert.ok(reads.length > 1, 'filesystem activity triggers a read');
assert.equal(changes.length, 1);
assert.equal(changes[0].reason, 'working-tree-changed');
const readsAfterChange = reads.length;
await new Promise((resolve) => setTimeout(resolve, 800));
assert.equal(reads.length, readsAfterChange, 'a quiet repository is not read again');
monitor.stop();
assert.equal(monitor.watchers.size, 0, 'stopping releases every watcher');
});