Skip to content

test: setup unit and integration testing (Fixes #305)#377

Open
Ixotic27 wants to merge 7 commits into
AditthyaSS:mainfrom
Ixotic27:test/vitest-setup
Open

test: setup unit and integration testing (Fixes #305)#377
Ixotic27 wants to merge 7 commits into
AditthyaSS:mainfrom
Ixotic27:test/vitest-setup

Conversation

@Ixotic27

Copy link
Copy Markdown
Contributor

Fixes #305.

Description

Configured the application for robust unit testing to ensure scalability and reliability.

  • Installed \�itest\ as the primary test runner, along with @testing-library/react, \jsdom, and @testing-library/jest-dom\ for UI and hook testing.
  • Added test scripts to \package.json.
  • Updated \�ite.config.js\ to initialize the \jsdom\ test environment and inject the global test setup.
  • Created \src/setupTests.js\ for Jest DOM matchers initialization.

Baseline Tests Provided

  1. Core Utilities (\llmAdapter.test.js): Fully mocks the global \ etch\ API to test edge-cases such as API key validation, provider validation, checking \POST\ payloads to OpenAI (URL, Headers, Body parsing), and dynamic model resolution for Gemini.
  2. React Hooks (\useApiKey.test.jsx): Tests the global API Key Provider context wrapper using @testing-library/react, ensuring default values work and verifying that \sessionStorage\ properly updates only when \saveForSession\ is flagged.

Testing and Proof

Tests ran locally successfully with all 7 unit assertions passing gracefully.

Copilot AI review requested due to automatic review settings May 27, 2026 18:39
@Ixotic27 Ixotic27 requested a review from AditthyaSS as a code owner May 27, 2026 18:39
@vercel

vercel Bot commented May 27, 2026

Copy link
Copy Markdown

@Ixotic27 is attempting to deploy a commit to the aditthyass' projects Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Introduces app-wide providers/hooks for lazy-loading agent registry data and centralizing API key management, and adds a Vitest test setup.

Changes:

  • Added AgentsProvider/useAgents to lazy-load agent definitions via import.meta.glob and provide them through context.
  • Added ApiKeyProvider/useApiKey to centralize provider selection and per-provider API keys with optional session persistence.
  • Added Vitest + Testing Library setup and initial unit tests for useApiKey and llmAdapter.

Reviewed changes

Copilot reviewed 22 out of 23 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
vite.config.js Configures Vitest (jsdom, globals, setup file).
src/setupTests.js Adds jest-dom matchers for RTL assertions.
src/pages/WorkflowRunner.jsx Switches agent source to useAgents and relies on context-driven API key bar.
src/pages/WorkflowLibrary.jsx Uses useAgents inside AgentPill to resolve agent display info.
src/pages/WorkflowDetail.jsx Uses useAgents and passes agent list into AgentRow.
src/pages/WorkflowBuilder.jsx Uses useAgents instead of eager registry import.
src/pages/HomePage.jsx Uses useAgents and recalculates derived lists with correct memo deps.
src/pages/BattleModeSetup.jsx Switches agent source to useAgents and API key state to useApiKey.
src/pages/BattleModeArena.jsx Reads API keys from global context instead of router state.
src/pages/AgentPage.jsx Uses useAgents and adds loading UI while agents load.
src/main.jsx Wraps app with AgentsProvider and ApiKeyProvider.
src/lib/useApiKey.test.jsx Adds unit tests for new API key context/hook.
src/lib/useApiKey.jsx New API key context/provider with sessionStorage sync.
src/lib/useApiKey.js Removes previous single-provider hook implementation.
src/lib/useAgents.jsx New agents context/provider loading agents via registry loader.
src/lib/llmAdapter.test.js Adds unit tests for runAgent and fetchGeminiModels.
src/components/SuggestedChainPills.jsx Uses useAgents for resolving suggested chain metadata.
src/components/Sidebar.jsx Uses useAgents for sidebar agent list/filtering.
src/components/ApiKeyBar.jsx Refactors to read/update API key state from useApiKey internally.
src/components/AgentRunner.jsx Uses context-derived API key/provider and context-driven API key bar.
src/agents/registry.js Makes agent registry lazy-loaded and adds loadAllAgents() caching.
package.json Adds Vitest + Testing Library deps and npm test script.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib/useAgents.jsx Outdated
Comment on lines +4 to +7
const AgentsContext = createContext({
agents: [],
loading: true,
})
Comment thread src/lib/useAgents.jsx
Comment on lines +40 to +42
export function useAgents() {
return useContext(AgentsContext)
}
Comment thread src/lib/useAgents.jsx
Comment on lines +23 to +26
.catch((err) => {
console.error('Failed to load agents:', err)
setLoading(false)
})
Comment thread src/pages/BattleModeSetup.jsx Outdated
const [selectedAgentId, setSelectedAgentId] = useState('')
const [inputs, setInputs] = useState({})
const [apiKeys, setApiKeys] = useState({ openai: '', anthropic: '', gemini: '' })
const { apiKeys, setApiKeys, saveForSession, setSaveForSession } = useApiKey()
Comment on lines +84 to +86
const handleKeyChange = (providerId, value) => {
setApiKeys((prev) => ({ ...prev, [providerId]: value }))
}
Comment on lines +35 to +49
it('saves to sessionStorage when saveForSession is true', () => {
const { result } = renderHook(() => useApiKey(), { wrapper })

act(() => {
result.current.setSaveForSession(true)
})

act(() => {
result.current.setApiKeyForProvider('openai', 'sk-session')
})

const savedKeys = JSON.parse(sessionStorage.getItem('ila_apikeys') || '{}')
expect(savedKeys.openai).toBe('sk-session')
expect(sessionStorage.getItem('ila_save_session')).toBe('true')
})
Comment thread src/lib/llmAdapter.test.js Outdated
Comment on lines +5 to +6
const mockFetch = vi.fn()
global.fetch = mockFetch
Comment thread src/lib/useApiKey.jsx Outdated
Comment on lines +5 to +7
const STORAGE_KEYS_PREFIX = 'ila_apikeys'
const STORAGE_PROVIDER_PREFIX = 'ila_active_provider'
const STORAGE_SAVE_PREFIX = 'ila_save_session'
@Ixotic27

Copy link
Copy Markdown
Contributor Author

I have implemented the Copilot suggestions on PR #377:

  1. Context Initialization (useAgents.jsx): Changed the default context from { agents: [], loading: true } to
    ull and added a check to throw an error if useAgents is used outside of AgentsProvider.
  2. Error State and Retry Mechanism (useAgents.jsx): Added an explicit error state and a
    eloadAgents() method, which are exposed on the context, so UI consumers can display meaningful error states and allow user-triggered retries.
  3. Helper Usage in BattleModeSetup.jsx: Refactored handleKeyChange to leverage the centralized setApiKeyForProvider(prov, key) helper from useApiKey instead of duplicating the object spread logic.
  4. Test Flakiness (useApiKey.test.jsx): Wrapped the sessionStorage assertion in an �wait waitFor(...) to prevent order-of-execution flakiness regarding asynchronous React renders and useEffect hooks.
  5. Global Fetch Stubbing (llmAdapter.test.js): Safely mocked etch using �i.stubGlobal('fetch', mockFetch) and restored it within an �fterEach via �i.unstubAllGlobals() to stop test leakage.
  6. Constant Naming Conventions (useApiKey.jsx): Renamed STORAGE_PREFIX to STORAGE_KEY because they are full key strings, resolving any conceptual ambiguity.

@Ixotic27 Ixotic27 force-pushed the test/vitest-setup branch from ac53ccc to f2f7156 Compare May 28, 2026 06:32
@mergify

mergify Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

Hey @Ixotic27! 👋
Wow — your first contribution to iloveAgents! This is a big deal and I want you to know it means a lot. 🎊
Every agent on this platform started exactly like this — someone like you deciding to spend their time building something useful for everyone. That is something to be proud of.
A few things while you wait for the review:

  • Star the repo if you haven't already. Star it here
  • 📖 Check the Contributing Guide
  • 💬 Drop a comment if you get stuck — I reply within 24 hours
    Can't wait to ship this with you. 🚀
    Welcome to the iloveAgents family. 🙏
    @AditthyaSS

@Ixotic27

Copy link
Copy Markdown
Contributor Author

hi @AditthyaSS please check this pr

@mergify

mergify Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

⚠️ Hey @Ixotic27! This PR has a merge conflict that needs to be resolved before we can review or merge it.
Please sync your branch with the latest main and fix the conflicts.
Need help? Check out resolving merge conflicts.
@AditthyaSS

@Ixotic27 Ixotic27 force-pushed the test/vitest-setup branch from f2f7156 to ac2950b Compare June 3, 2026 18:12
@mergify

mergify Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

hey @Ixotic27! 👋
Your PR title doesn't follow our required format.
Please update it to:
type: short description
Valid types: feat, fix, docs, style, refactor, test, chore
Example: feat: add sales discovery agent
@AditthyaSS

@mergify mergify Bot added the needs-fix label Jun 4, 2026
@mergify

mergify Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

hey @Ixotic27! 👋
You changed package.json but package-lock.json was not updated.
Quick check before we review:
→ If you added or removed a package:
Please run npm install and commit the updated package-lock.json too.
Both files need to change together. ✅
→ If you only changed metadata (scripts, description, version etc):
You can safely ignore this message. ✅
Just wanted to make sure nothing was missed! 🙏
@AditthyaSS

@Ixotic27 Ixotic27 changed the title Infrastructure: Setup Unit & Integration Testing (Fixes #305) test: setup unit and integration testing (Fixes #305) Jun 9, 2026
@mergify

mergify Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

⚠️ This branch is out of date with main.
Please click "Update branch" to sync before merging.

@mergify mergify Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❌ Build is failing on this PR.
Please fix before merging:

  1. Run npm run build locally
  2. Fix any errors shown
  3. Push your fix — the check will re-run automatically
    Most common issue: broken registry import.
    Replace:
    import agents from '../agents/registry'
    With:
    import { useAgents } from '../lib/useAgents'
    const { agents } = useAgents()
    See CONTRIBUTING.md for help. 🙏

@mergify

mergify Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

⚠️ Hey @Ixotic27! This PR has a merge conflict that needs to be resolved before we can review or merge it.
Please sync your branch with the latest main and fix the conflicts.
Need help? Check out resolving merge conflicts.
@AditthyaSS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Infrastructure: Setup Unit & Integration Testing

3 participants