Files
buzz/desktop/src/features/communities/useNestNotifications.test.mjs
npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 ff16a80b5b fix(desktop): pass-2 review corrections (round 3) — captured respawn, behavioral tests, residue
Global-config respawn: replace live-scope wrappers with one transition epoch.
restart_under_captured_epoch acquires managed_agent_runtime_transition +
managed_agents_store_lock, validates captured generation, stops, builds a
captured spawn context (records/personas/global config/owner hex/scope_id
from captured_scope), spawns/registers, saves — no live wrapper past the stop.
persist_last_error takes captured_scope, validates generation under its own
acquired store lock, never called while that lock is held, fails closed on
poison. Two tauri::test::mock_app() tests: fresh-scope no-runtime → Skipped
(proves generation guard passes + path proceeds), stale-scope → Skipped at
generation step (switch-between-stop-and-spawn test, calls production fn).

Behavioral tests on production paths: tauri = { version = "2", features = ["test"] }
added to dev-dependencies. compensate_drain_for extracted with injectable
start_fn; tests call it directly (compensate_drain_for is the production core).
capture_agent_snapshot_import_entry and capture_team_snapshot_import_entry
extracted as testable entry guards; tests call the real production functions
(no-scope reject, owner-mismatch reject, matching-owner passes + relay verified,
stale-generation rejects validate_scope_generation). registerNestNotifications
extracted from useNestNotifications; test imports and calls the real function,
asserts all three event registrations, workspace-degraded toast payload,
unlisten cleanup per event. fail_if_client_mesh_active and mesh_stop_client
tested via tauri::test::mock_builder() with real AppHandle (no-runtime,
client-runtime, no-runtime stop paths). Generalized fail_if_client_mesh_active
and mesh_stop_client to tauri::Runtime to allow mock_app usage.

Residue: _compensation_gate_removed placeholder deleted from AppState.
identity.rs:346-348 comment corrected (guard passed by value, not dropped).
mesh_llm_scope.rs:59-64 stale re-arm comment replaced with accurate Option A note.
Duplicate generation bump at identity.rs:544 removed (clear_active_scope
calls next_scope_generation internally; no second bump needed).

File-size gate: AgentSnapshotImportEntry + capture_agent_snapshot_import_entry
extracted to import_entry.rs (import.rs: 995 lines); TeamSnapshotImportEntry +
capture_team_snapshot_import_entry extracted to team_snapshot_entry.rs
(team_snapshot.rs: 997 lines). Both within the 1000-line ratchet.

Co-authored-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
2026-08-04 01:57:40 -04:00

140 lines
4.4 KiB
JavaScript

/**
* Behavioral tests for useNestNotifications / registerNestNotifications.
*
* Tests call `registerNestNotifications` — the extracted production
* registration helper used by `useNestNotifications` inside its `useEffect`.
* This is the real production function, not a reconstruction of its logic.
*
* Proves:
* - `registerNestNotifications` registers listeners for all three event names
* (repos-dir-error, legacy-nest-migrated, workspace-degraded) by inspecting
* the `listenFn` call record.
* - When `workspace-degraded` fires, `toast.error` is called with the payload.
* - The returned cleanup function calls every unlisten function.
* - The event name and payload wiring survive rename/refactor of the production
* code (the test would fail if the event name or toast call were deleted).
*/
import assert from "node:assert/strict";
import test from "node:test";
import { registerNestNotifications } from "./useNestNotifications.ts";
test("registerNestNotifications: registers listeners for all three events", () => {
const registeredEvents = [];
const unlistenFns = [];
const mockListen = (event, _handler) => {
registeredEvents.push(event);
const unlistenFn = () => unlistenFns.push(event);
return Promise.resolve(unlistenFn);
};
const mockToast = { error: () => {}, success: () => {} };
const cleanup = registerNestNotifications(mockListen, mockToast);
assert.deepEqual(
registeredEvents.sort(),
["legacy-nest-migrated", "repos-dir-error", "workspace-degraded"].sort(),
"must register listeners for all three events",
);
// Cleanup calls all three unlisten functions.
return Promise.resolve()
.then(() => {
cleanup();
return new Promise((resolve) => setTimeout(resolve, 0));
})
.then(() => {
assert.equal(
unlistenFns.length,
3,
"cleanup must call unlisten for all three events",
);
});
});
test("registerNestNotifications: workspace-degraded fires toast.error with payload", () => {
const toastCalls = [];
let degradedHandler = null;
const mockListen = (event, handler) => {
if (event === "workspace-degraded") {
degradedHandler = handler;
}
return Promise.resolve(() => {});
};
const mockToast = {
error: (title, opts) => toastCalls.push({ title, opts }),
success: () => {},
};
registerNestNotifications(mockListen, mockToast);
assert.ok(degradedHandler, "workspace-degraded handler must be registered");
// Fire the event as the Tauri event system would.
degradedHandler({
payload: "restore failed: agent runtime could not be restarted",
});
assert.equal(toastCalls.length, 1, "toast.error must be called once");
assert.equal(toastCalls[0].title, "Workspace partially degraded");
assert.equal(
toastCalls[0].opts.description,
"restore failed: agent runtime could not be restarted",
);
});
test("registerNestNotifications: cleanup calls all unlisten functions", () => {
let unlistenCallCount = 0;
const unlisten = () => {
unlistenCallCount++;
};
const mockListen = (_event, _handler) => Promise.resolve(unlisten);
const mockToast = { error: () => {}, success: () => {} };
const cleanup = registerNestNotifications(mockListen, mockToast);
// Call cleanup after promises resolve.
return new Promise((resolve) => setTimeout(resolve, 0))
.then(() => {
cleanup();
return new Promise((resolve) => setTimeout(resolve, 0));
})
.then(() => {
assert.equal(
unlistenCallCount,
3,
"cleanup must call unlisten for all three registered listeners",
);
});
});
test("registerNestNotifications: workspace-degraded handler passes raw payload as description", () => {
const cases = ["", "a".repeat(500), "error: file not found\npath: /foo/bar"];
for (const payload of cases) {
const toastArgs = [];
let handler = null;
const mockListen = (event, h) => {
if (event === "workspace-degraded") handler = h;
return Promise.resolve(() => {});
};
const mockToast = {
error: (title, opts) =>
toastArgs.push({ title, description: opts?.description }),
success: () => {},
};
registerNestNotifications(mockListen, mockToast);
assert.ok(handler, "workspace-degraded handler must be set");
handler({ payload });
assert.equal(toastArgs.length, 1);
assert.equal(toastArgs[0].description, payload);
}
});