Skip to content

🌱 Add unit tests for useLocalAgent, progress hooks, and useMCS#3966

Merged
clubanderson merged 1 commit intomainfrom
feat/unit-tests-hooks
Mar 31, 2026
Merged

🌱 Add unit tests for useLocalAgent, progress hooks, and useMCS#3966
clubanderson merged 1 commit intomainfrom
feat/unit-tests-hooks

Conversation

@clubanderson
Copy link
Copy Markdown
Collaborator

Summary

  • Add unit tests for useLocalAgent hook (8 tests): initial state, connected/disconnected transitions, polling, cleanup on unmount
  • Add unit tests for useClusterProgress hook (10 tests): WebSocket message parsing, status transitions, dismiss, reconnect, cleanup
  • Add unit tests for useUpdateProgress hook (11 tests): WebSocket message parsing, step history tracking, stale detection, dismiss, cleanup
  • Add unit tests for useMCS hooks (26 tests): useMCSStatus, useServiceExports, useServiceImports — API fetching, error handling, BackendUnavailableError, demo mode fallback, polling, cleanup

Test plan

  • All 55 tests pass: npx vitest run src/hooks/__tests__/useLocalAgent.test.ts src/hooks/__tests__/useClusterProgress.test.ts src/hooks/__tests__/useUpdateProgress.test.ts src/hooks/__tests__/useMCS.test.ts
  • Build passes (npm run build)
  • CI build/lint pass

Fixes #3955 #3956 #3957

…ress, useMCS

Fixes #3955 #3956 #3957

Signed-off-by: Andrew Anderson <andy@clubanderson.com>
Copilot AI review requested due to automatic review settings March 31, 2026 13:16
@kubestellar-prow kubestellar-prow bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Mar 31, 2026
@kubestellar-prow
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign clubanderson for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@netlify
Copy link
Copy Markdown

netlify bot commented Mar 31, 2026

Deploy Preview for kubestellarconsole ready!

Name Link
🔨 Latest commit b96718a
🔍 Latest deploy log https://app.netlify.com/projects/kubestellarconsole/deploys/69cbc934be625f00086eb170
😎 Deploy Preview https://deploy-preview-3966.console-deploy-preview.kubestellar.io
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@kubestellar-prow kubestellar-prow bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Mar 31, 2026
@github-actions
Copy link
Copy Markdown
Contributor

👋 Hey @clubanderson — thanks for opening this PR!

🤖 This project is developed exclusively using AI coding assistants.

Please do not attempt to code anything for this project manually.
All contributions should be authored using an AI coding tool such as:

This ensures consistency in code style, architecture patterns, test coverage,
and commit quality across the entire codebase.


This is an automated message.

@clubanderson clubanderson merged commit 0f5c796 into main Mar 31, 2026
20 of 21 checks passed
@kubestellar-prow kubestellar-prow bot deleted the feat/unit-tests-hooks branch March 31, 2026 13:17
@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your contribution! Your PR has been merged.

Check out what's new:

Stay connected: Slack #kubestellar-dev | Multi-Cluster Survey

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds new Vitest unit test suites for several React hooks that manage local agent connectivity, WebSocket-driven progress updates, and Multi-Cluster Services (MCS) API polling, improving regression coverage for connection lifecycle, parsing, polling, and cleanup behavior.

Changes:

  • Add useLocalAgent unit tests covering connection state transitions, polling, and unmount cleanup.
  • Add/expand WebSocket hook tests for useClusterProgress and add a new suite for useUpdateProgress.
  • Add a comprehensive useMCS test suite for useMCSStatus, useServiceExports, and useServiceImports, including demo mode and polling scenarios.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
web/src/hooks/tests/useLocalAgent.test.ts New tests for local agent polling/state transitions and cleanup.
web/src/hooks/tests/useClusterProgress.test.ts New WebSocket parsing/reconnect/cleanup tests for cluster progress updates.
web/src/hooks/tests/useUpdateProgress.test.ts New WebSocket parsing, step-history, reconnect, and cleanup tests for update progress.
web/src/hooks/tests/useMCS.test.ts New tests for MCS hooks: fetch success/error paths, demo mode, refetch, polling, cleanup.

Comment on lines +1 to +10
/**
* Tests for useClusterProgress hook.
*
* Validates WebSocket connection, message parsing for local_cluster_progress
* events, dismiss behaviour, and cleanup on unmount.
*/

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { renderHook, act } from '@testing-library/react'

Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suite duplicates coverage with the existing web/src/hooks/useClusterProgress.test.ts (same hook, overlapping cases). Keeping two test files for the same hook increases maintenance cost and can make future refactors harder (updates must be applied in two places). Consider consolidating into a single test file (preferably under __tests__/) and removing/merging the older one.

Copilot uses AI. Check for mistakes.
Comment on lines +61 to +66
// Assign mock to global before importing the hook
Object.defineProperty(globalThis, 'WebSocket', {
writable: true,
value: MockWebSocket,
})

Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.defineProperty(globalThis, 'WebSocket', ...) is done without configurable: true and the mock is never restored. This can leak the mocked WebSocket into other test files and may prevent other suites from re-stubbing WebSocket cleanly. Prefer vi.stubGlobal('WebSocket', MockWebSocket) and vi.unstubAllGlobals() in afterEach (or at least set configurable: true and restore the original).

Copilot uses AI. Check for mistakes.
Comment on lines +66 to +70
// Assign mock to global before importing the hook
Object.defineProperty(globalThis, 'WebSocket', {
writable: true,
value: MockWebSocket,
})
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.defineProperty(globalThis, 'WebSocket', ...) is done without configurable: true and the mock is never restored. This can leak the mocked WebSocket into other test files and may prevent other suites from re-stubbing WebSocket cleanly. Prefer vi.stubGlobal('WebSocket', MockWebSocket) and vi.unstubAllGlobals() in afterEach (or at least set configurable: true and restore the original).

Copilot uses AI. Check for mistakes.
Comment on lines +56 to +63
vi.useFakeTimers()
vi.resetModules()

// Set up fresh global fetch mock before importing
global.fetch = vi.fn()

const mod = await import('../useLocalAgent')
useLocalAgent = mod.useLocalAgent
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test file assigns global.fetch = vi.fn() but never restores the original fetch. Depending on Vitest worker reuse, this can leak into other suites. Prefer vi.stubGlobal('fetch', vi.fn()) and vi.unstubAllGlobals() (or restore the original in afterEach) to keep globals isolated.

Copilot uses AI. Check for mistakes.
@clubanderson
Copy link
Copy Markdown
Collaborator Author

🔄 Auto-Applying Copilot Code Review

Copilot code review found 0 code suggestion(s) and 4 general comment(s).

Also address these general comments:

  • web/src/hooks/__tests__/useClusterProgress.test.ts (line 10): This suite duplicates coverage with the existing web/src/hooks/useClusterProgress.test.ts (same hook, overlapping case
  • web/src/hooks/__tests__/useClusterProgress.test.ts (line 66): Object.defineProperty(globalThis, 'WebSocket', ...) is done without configurable: true and the mock is never restore
  • web/src/hooks/__tests__/useUpdateProgress.test.ts (line 70): Object.defineProperty(globalThis, 'WebSocket', ...) is done without configurable: true and the mock is never restore
  • web/src/hooks/__tests__/useLocalAgent.test.ts (line 63): This test file assigns global.fetch = vi.fn() but never restores the original fetch. Depending on Vitest worker reus

Push all fixes in a single commit. Run cd web && npm run build && npm run lint before committing.


Auto-generated by copilot-review-apply workflow.

clubanderson added a commit that referenced this pull request Mar 31, 2026
…plicate test

- Replace Object.defineProperty/global assignment with vi.stubGlobal()
  for WebSocket and fetch mocks so vitest tracks and restores them
- Add vi.unstubAllGlobals() to afterEach in all 3 test files
- Re-establish WebSocket mock in beforeEach (cleared by unstubAllGlobals)
- Delete duplicate useClusterProgress.test.ts from hooks/ (canonical
  copy lives in hooks/__tests__/)

Signed-off-by: Andrew Anderson <andy@clubanderson.com>
clubanderson added a commit that referenced this pull request Mar 31, 2026
…plicate test (#3971)

- Replace Object.defineProperty/global assignment with vi.stubGlobal()
  for WebSocket and fetch mocks so vitest tracks and restores them
- Add vi.unstubAllGlobals() to afterEach in all 3 test files
- Re-establish WebSocket mock in beforeEach (cleared by unstubAllGlobals)
- Delete duplicate useClusterProgress.test.ts from hooks/ (canonical
  copy lives in hooks/__tests__/)

Signed-off-by: Andrew Anderson <andy@clubanderson.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates the PR's author has signed the DCO. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🌱 Add unit tests for useLocalAgent hook

3 participants