diff --git a/src/renderer/components/team/useTeamAgentRuntimeWatcher.ts b/src/renderer/components/team/useTeamAgentRuntimeWatcher.ts index e65cbcfb..979c83ca 100644 --- a/src/renderer/components/team/useTeamAgentRuntimeWatcher.ts +++ b/src/renderer/components/team/useTeamAgentRuntimeWatcher.ts @@ -6,6 +6,19 @@ import { useShallow } from 'zustand/react/shallow'; const TEAM_AGENT_RUNTIME_REFRESH_MS = 5_000; +export function shouldWatchTeamAgentRuntime(input: { + enabled: boolean; + isTeamProvisioning: boolean | undefined; + isTeamAlive: boolean | undefined; + leadActivity: 'active' | 'idle' | 'offline' | undefined; +}): boolean { + if (!input.enabled) return false; + if (input.isTeamProvisioning) return true; + if (input.isTeamAlive === true) return true; + if (input.isTeamAlive === false) return false; + return input.leadActivity === 'active' || input.leadActivity === 'idle'; +} + interface TeamAgentRuntimeWatcherOptions { teamName: string; enabled: boolean; @@ -33,12 +46,12 @@ export function useTeamAgentRuntimeWatcher({ const effectiveIsTeamProvisioning = isTeamProvisioning ?? storeIsTeamProvisioning; useEffect(() => { - if (!enabled) return; - const shouldWatch = - effectiveIsTeamProvisioning || - effectiveIsTeamAlive === true || - leadActivity === 'active' || - leadActivity === 'idle'; + const shouldWatch = shouldWatchTeamAgentRuntime({ + enabled, + isTeamProvisioning: effectiveIsTeamProvisioning, + isTeamAlive: effectiveIsTeamAlive, + leadActivity, + }); if (!shouldWatch) return; void fetchTeamAgentRuntime(teamName); diff --git a/test/renderer/components/team/useTeamAgentRuntimeWatcher.test.ts b/test/renderer/components/team/useTeamAgentRuntimeWatcher.test.ts new file mode 100644 index 00000000..30bac788 --- /dev/null +++ b/test/renderer/components/team/useTeamAgentRuntimeWatcher.test.ts @@ -0,0 +1,64 @@ +import { shouldWatchTeamAgentRuntime } from '@renderer/components/team/useTeamAgentRuntimeWatcher'; +import { describe, expect, it } from 'vitest'; + +describe('shouldWatchTeamAgentRuntime', () => { + it('does not poll runtime for explicitly offline teams with stale lead activity', () => { + expect( + shouldWatchTeamAgentRuntime({ + enabled: true, + isTeamProvisioning: false, + isTeamAlive: false, + leadActivity: 'idle', + }) + ).toBe(false); + expect( + shouldWatchTeamAgentRuntime({ + enabled: true, + isTeamProvisioning: false, + isTeamAlive: false, + leadActivity: 'active', + }) + ).toBe(false); + }); + + it('keeps runtime polling for live and provisioning teams', () => { + expect( + shouldWatchTeamAgentRuntime({ + enabled: true, + isTeamProvisioning: false, + isTeamAlive: true, + leadActivity: 'offline', + }) + ).toBe(true); + expect( + shouldWatchTeamAgentRuntime({ + enabled: true, + isTeamProvisioning: true, + isTeamAlive: false, + leadActivity: 'offline', + }) + ).toBe(true); + }); + + it('allows lead activity to request polling while liveness is still unknown', () => { + expect( + shouldWatchTeamAgentRuntime({ + enabled: true, + isTeamProvisioning: false, + isTeamAlive: undefined, + leadActivity: 'idle', + }) + ).toBe(true); + }); + + it('stays disabled for hidden tabs', () => { + expect( + shouldWatchTeamAgentRuntime({ + enabled: false, + isTeamProvisioning: true, + isTeamAlive: true, + leadActivity: 'active', + }) + ).toBe(false); + }); +});