Add convenience builders for agents + some tests - #672
Conversation
|
CodeAnt AI is reviewing your PR. Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
|
One change we probably need - I wasn't sure where the best place was for these tests, so the new 'sdk' directory is just a placeholder. LMK if there is a better spot? |
Nitpicks 🔍
|
| let mockAgentData: AgentView; | ||
|
|
||
| beforeEach(() => { | ||
| // Create mock client |
There was a problem hiding this comment.
Suggestion: Tests reuse module-level mocks across examples; not clearing mock state between runs can lead to flaky tests (leftover call counts or mock implementations). Call jest.clearAllMocks() at the start of beforeEach to reset mock call history and implementations. [possible bug]
Severity Level: Critical 🚨
| // Create mock client | |
| jest.clearAllMocks(); |
Why it matters? ⭐
Good catch. While mockClient's member mocks are recreated in beforeEach, the module-level mock (Agent.create) retains call history across tests unless cleared. Adding jest.clearAllMocks() at the top of beforeEach prevents stale call counts or previous mock implementations from leaking between tests and makes assertions like toHaveBeenCalledWith deterministic. This is a small, safe improvement to test hygiene.
Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** tests/sdk/agent-ops.test.ts
**Line:** 14:14
**Comment:**
*Possible Bug: Tests reuse module-level mocks across examples; not clearing mock state between runs can lead to flaky tests (leftover call counts or mock implementations). Call `jest.clearAllMocks()` at the start of `beforeEach` to reset mock call history and implementations.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.| }; | ||
|
|
||
| // Mock Agent.create to return a mock Agent instance | ||
| (Agent.create as jest.Mock).mockResolvedValue(new Agent(mockClient, 'agent-123')); |
There was a problem hiding this comment.
Suggestion: Mocking Agent.create by resolving to new Agent(...) can fail when the Agent export is mocked by Jest (the mocked constructor may not behave like the real class or may not be a constructor), and casting to jest.Mock is brittle. Create an explicit lightweight mock instance (plain object typed as Agent) and use jest.spyOn(Agent, 'create') to stub the static method; this avoids calling the mocked constructor and ensures the stub is a proper mock function. [possible bug]
Severity Level: Critical 🚨
| (Agent.create as jest.Mock).mockResolvedValue(new Agent(mockClient, 'agent-123')); | |
| const mockAgentInstance = { id: 'agent-123', getInfo: jest.fn() } as unknown as Agent; | |
| jest.spyOn(Agent as any, 'create').mockResolvedValue(mockAgentInstance); |
Why it matters? ⭐
The suggestion is valid. The test file calls jest.mock('../../src/sdk/agent') at module scope, so Agent is a mocked export; calling new Agent(...) will invoke the mocked constructor which may not behave like the real class and can lead to brittle tests or surprises. Returning a plain, explicitly-shaped object and spying on Agent.create avoids invoking the mocked constructor and makes the test intent clearer and more robust. This change fixes a real fragility in the test setup rather than being purely cosmetic.
Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** tests/sdk/agent-ops.test.ts
**Line:** 40:40
**Comment:**
*Possible Bug: Mocking `Agent.create` by resolving to `new Agent(...)` can fail when the `Agent` export is mocked by Jest (the mocked constructor may not behave like the real class or may not be a constructor), and casting to `jest.Mock` is brittle. Create an explicit lightweight mock instance (plain object typed as `Agent`) and use `jest.spyOn(Agent, 'create')` to stub the static method; this avoids calling the mocked constructor and ensures the stub is a proper mock function.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.|
CodeAnt AI finished reviewing your PR. |
| * | ||
| * @param {object} params - Parameters for creating the agent. | ||
| * @param {string} params.package_name - NPM package name. | ||
| * @param {string} [params.npm_version] - NPM version constraint. |
There was a problem hiding this comment.
examples would be good here
| * Create an agent from a storage object. | ||
| * | ||
| * @param {object} params - Parameters for creating the agent. | ||
| * @param {string} params.object_id - Storage object ID. |
There was a problem hiding this comment.
string | storage object would be good here
| * | ||
| * @param {object} params - Parameters for creating the agent. | ||
| * @param {string} params.package_name - Pip package name. | ||
| * @param {string} [params.pip_version] - Pip version constraint. |
There was a problem hiding this comment.
these examples also with the examples so people can see what would go here
|
CodeAnt AI is running Incremental review Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
|
| Metric | Coverage | Required | Status |
|---|---|---|---|
| Functions | 99.22% | 100% | ❌ |
| Lines | 87.11% | - | ℹ️ |
| Branches | 47.66% | - | ℹ️ |
| Statements | 85.12% | - | ℹ️ |
Coverage Requirement: 100% function coverage (all public methods must be called in smoke tests)
View detailed coverage report
Coverage reports are available in the workflow artifacts. Lines/branches/statements coverage is tracked but not required to be 100%.
CodeAnt-AI Description
Create agents from NPM, Pip, Git, or storage objects
What Changed
Impact
✅ Shorter agent registration from NPM/Pip/Git/storage✅ Fewer payload construction errors when creating agents✅ Faster developer iteration thanks to unit tests for builders💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.