-
-
Notifications
You must be signed in to change notification settings - Fork 194
feat(simulator-management): add consolidated erase_sims tool (UDID or all) #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+265
−11
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e0d82e7
feat(simulator-management): add consolidated erase_sims tool
cameroncooke e93cf4c
docs: update TOOLS.md and README workflow counts; document erase_sims…
cameroncooke 4a5ebd3
feat(simulator-management): add shutdownFirst option and tool hints f…
cameroncooke dc695f5
docs(TOOLS): reflect erase_sims shutdownFirst option (no default)
cameroncooke 90dc951
refactor(simulator-management): adopt UDID terminology (simulatorUdid…
cameroncooke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/mcp/tools/simulator-management/__tests__/erase_sims.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { z } from 'zod'; | ||
| import eraseSims, { erase_simsLogic } from '../erase_sims.ts'; | ||
| import { createMockExecutor } from '../../../../test-utils/mock-executors.ts'; | ||
|
|
||
| describe('erase_sims tool (UDID or ALL only)', () => { | ||
| describe('Export Field Validation (Literal)', () => { | ||
| it('should have correct name', () => { | ||
| expect(eraseSims.name).toBe('erase_sims'); | ||
| }); | ||
|
|
||
| it('should have correct description', () => { | ||
| expect(eraseSims.description).toContain('Provide exactly one of: simulatorUdid or all=true'); | ||
| expect(eraseSims.description).toContain('shutdownFirst'); | ||
| }); | ||
|
|
||
| it('should have handler function', () => { | ||
| expect(typeof eraseSims.handler).toBe('function'); | ||
| }); | ||
|
|
||
| it('should validate schema fields (shape only)', () => { | ||
| const schema = z.object(eraseSims.schema); | ||
| // Valid | ||
| expect( | ||
| schema.safeParse({ simulatorUdid: '123e4567-e89b-12d3-a456-426614174000' }).success, | ||
| ).toBe(true); | ||
| expect(schema.safeParse({ all: true }).success).toBe(true); | ||
| // Shape-level schema does not enforce selection rules; handler validation covers that. | ||
| }); | ||
| }); | ||
|
|
||
| describe('Single mode', () => { | ||
| it('erases a simulator successfully', async () => { | ||
| const mock = createMockExecutor({ success: true, output: 'OK' }); | ||
| const res = await erase_simsLogic({ simulatorUdid: 'UD1' }, mock); | ||
| expect(res).toEqual({ | ||
| content: [{ type: 'text', text: 'Successfully erased simulator UD1' }], | ||
| }); | ||
| }); | ||
|
|
||
| it('returns failure when erase fails', async () => { | ||
| const mock = createMockExecutor({ success: false, error: 'Booted device' }); | ||
| const res = await erase_simsLogic({ simulatorUdid: 'UD1' }, mock); | ||
| expect(res).toEqual({ | ||
| content: [{ type: 'text', text: 'Failed to erase simulator: Booted device' }], | ||
| }); | ||
| }); | ||
|
|
||
| it('adds tool hint when booted error occurs without shutdownFirst', async () => { | ||
| const bootedError = | ||
| 'An error was encountered processing the command (domain=com.apple.CoreSimulator.SimError, code=405):\nUnable to erase contents and settings in current state: Booted\n'; | ||
| const mock = createMockExecutor({ success: false, error: bootedError }); | ||
| const res = await erase_simsLogic({ simulatorUdid: 'UD1' }, mock); | ||
| expect((res.content?.[1] as any).text).toContain('Tool hint'); | ||
| expect((res.content?.[1] as any).text).toContain('shutdownFirst: true'); | ||
| }); | ||
|
|
||
| it('performs shutdown first when shutdownFirst=true', async () => { | ||
| const calls: any[] = []; | ||
| const exec = async (cmd: string[]) => { | ||
| calls.push(cmd); | ||
| return { success: true, output: 'OK', error: '', process: { pid: 1 } as any }; | ||
| }; | ||
| const res = await erase_simsLogic({ simulatorUdid: 'UD1', shutdownFirst: true }, exec as any); | ||
| expect(calls).toEqual([ | ||
| ['xcrun', 'simctl', 'shutdown', 'UD1'], | ||
| ['xcrun', 'simctl', 'erase', 'UD1'], | ||
| ]); | ||
| expect(res).toEqual({ | ||
| content: [{ type: 'text', text: 'Successfully erased simulator UD1' }], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('All mode', () => { | ||
| it('erases all simulators successfully', async () => { | ||
| const exec = createMockExecutor({ success: true, output: 'OK' }); | ||
| const res = await erase_simsLogic({ all: true }, exec); | ||
| expect(res).toEqual({ | ||
| content: [{ type: 'text', text: 'Successfully erased all simulators' }], | ||
| }); | ||
| }); | ||
|
|
||
| it('returns failure when erase all fails', async () => { | ||
| const exec = createMockExecutor({ success: false, error: 'Denied' }); | ||
| const res = await erase_simsLogic({ all: true }, exec); | ||
| expect(res).toEqual({ | ||
| content: [{ type: 'text', text: 'Failed to erase all simulators: Denied' }], | ||
| }); | ||
| }); | ||
|
|
||
| it('performs shutdown all when shutdownFirst=true', async () => { | ||
| const calls: any[] = []; | ||
| const exec = async (cmd: string[]) => { | ||
| calls.push(cmd); | ||
| return { success: true, output: 'OK', error: '', process: { pid: 1 } as any }; | ||
| }; | ||
| const res = await erase_simsLogic({ all: true, shutdownFirst: true }, exec as any); | ||
| expect(calls).toEqual([ | ||
| ['xcrun', 'simctl', 'shutdown', 'all'], | ||
| ['xcrun', 'simctl', 'erase', 'all'], | ||
| ]); | ||
| expect(res).toEqual({ | ||
| content: [{ type: 'text', text: 'Successfully erased all simulators' }], | ||
| }); | ||
| }); | ||
|
|
||
| it('adds tool hint on booted error without shutdownFirst (all mode)', async () => { | ||
| const bootedError = 'Unable to erase contents and settings in current state: Booted'; | ||
| const exec = createMockExecutor({ success: false, error: bootedError }); | ||
| const res = await erase_simsLogic({ all: true }, exec); | ||
| expect((res.content?.[1] as any).text).toContain('Tool hint'); | ||
| expect((res.content?.[1] as any).text).toContain('shutdownFirst: true'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { z } from 'zod'; | ||
| import { ToolResponse, type ToolResponseContent } from '../../../types/common.ts'; | ||
| import { log } from '../../../utils/logging/index.ts'; | ||
| import { CommandExecutor, getDefaultCommandExecutor } from '../../../utils/execution/index.ts'; | ||
| import { createTypedTool } from '../../../utils/typed-tool-factory.ts'; | ||
|
|
||
| const eraseSimsBaseSchema = z.object({ | ||
| simulatorUdid: z.string().uuid().optional().describe('UDID of the simulator to erase.'), | ||
| all: z.boolean().optional().describe('When true, erases all simulators.'), | ||
| shutdownFirst: z | ||
| .boolean() | ||
| .optional() | ||
| .describe('If true, shuts down the target (UDID or all) before erasing.'), | ||
| }); | ||
|
|
||
| const eraseSimsSchema = eraseSimsBaseSchema.refine( | ||
| (v) => { | ||
| const selectors = (v.simulatorUdid ? 1 : 0) + (v.all === true ? 1 : 0); | ||
| return selectors === 1; | ||
| }, | ||
| { message: 'Provide exactly one of: simulatorUdid or all=true.' }, | ||
| ); | ||
|
|
||
| type EraseSimsParams = z.infer<typeof eraseSimsSchema>; | ||
|
|
||
| export async function erase_simsLogic( | ||
| params: EraseSimsParams, | ||
| executor: CommandExecutor, | ||
| ): Promise<ToolResponse> { | ||
| try { | ||
| if (params.simulatorUdid) { | ||
| const udid = params.simulatorUdid; | ||
| log( | ||
| 'info', | ||
| `Erasing simulator ${udid}${params.shutdownFirst ? ' (shutdownFirst=true)' : ''}`, | ||
| ); | ||
|
|
||
| if (params.shutdownFirst) { | ||
| try { | ||
| await executor( | ||
| ['xcrun', 'simctl', 'shutdown', udid], | ||
| 'Shutdown Simulator', | ||
| true, | ||
| undefined, | ||
| ); | ||
| } catch { | ||
| // ignore shutdown errors; proceed to erase attempt | ||
| } | ||
| } | ||
|
|
||
| const result = await executor( | ||
| ['xcrun', 'simctl', 'erase', udid], | ||
| 'Erase Simulator', | ||
| true, | ||
| undefined, | ||
| ); | ||
| if (result.success) { | ||
| return { content: [{ type: 'text', text: `Successfully erased simulator ${udid}` }] }; | ||
| } | ||
|
|
||
| // Add tool hint if simulator is booted and shutdownFirst was not requested | ||
| const errText = result.error ?? 'Unknown error'; | ||
| if (/Unable to erase contents and settings.*Booted/i.test(errText) && !params.shutdownFirst) { | ||
| return { | ||
| content: [ | ||
| { type: 'text', text: `Failed to erase simulator: ${errText}` }, | ||
| { | ||
| type: 'text', | ||
| text: `Tool hint: The simulator appears to be Booted. Re-run erase_sims with { simulatorUdid: '${udid}', shutdownFirst: true } to shut it down before erasing.`, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| content: [{ type: 'text', text: `Failed to erase simulator: ${errText}` }], | ||
| }; | ||
| } | ||
|
|
||
| if (params.all === true) { | ||
| log('info', `Erasing ALL simulators${params.shutdownFirst ? ' (shutdownFirst=true)' : ''}`); | ||
| if (params.shutdownFirst) { | ||
| try { | ||
| await executor( | ||
| ['xcrun', 'simctl', 'shutdown', 'all'], | ||
| 'Shutdown All Simulators', | ||
| true, | ||
| undefined, | ||
| ); | ||
| } catch { | ||
| // ignore and continue to erase | ||
| } | ||
| } | ||
|
|
||
| const result = await executor( | ||
| ['xcrun', 'simctl', 'erase', 'all'], | ||
| 'Erase All Simulators', | ||
| true, | ||
| undefined, | ||
| ); | ||
| if (!result.success) { | ||
| const errText = result.error ?? 'Unknown error'; | ||
| const content: ToolResponseContent[] = [ | ||
| { type: 'text', text: `Failed to erase all simulators: ${errText}` }, | ||
| ]; | ||
| if ( | ||
| /Unable to erase contents and settings.*Booted/i.test(errText) && | ||
| !params.shutdownFirst | ||
| ) { | ||
| content.push({ | ||
| type: 'text', | ||
| text: 'Tool hint: One or more simulators appear to be Booted. Re-run erase_sims with { all: true, shutdownFirst: true } to shut them down before erasing.', | ||
| }); | ||
| } | ||
| return { content }; | ||
| } | ||
| return { content: [{ type: 'text', text: 'Successfully erased all simulators' }] }; | ||
| } | ||
|
|
||
| return { | ||
| content: [{ type: 'text', text: 'Invalid parameters: provide simulatorUdid or all=true.' }], | ||
| }; | ||
| } catch (error: unknown) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| log('error', `Error erasing simulators: ${message}`); | ||
| return { content: [{ type: 'text', text: `Failed to erase simulators: ${message}` }] }; | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| name: 'erase_sims', | ||
| description: | ||
| 'Erases simulator content and settings. Provide exactly one of: simulatorUdid or all=true. Optional: shutdownFirst to shut down before erasing.', | ||
| schema: eraseSimsBaseSchema.shape, | ||
| handler: createTypedTool(eraseSimsSchema, erase_simsLogic, getDefaultCommandExecutor), | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Spec mismatch: shutdownFirst included but PR objectives say extras are intentionally omitted.
Either (a) remove
shutdownFirstand associated logic, or (b) keep it but update PR/docs/workflow to advertise the option. Current state diverges from stated scope.Option A — remove the option (sample of necessary edits):
If you prefer Option B, please update PR description/docs accordingly. Let me know and I’ll draft the doc changes.
Also applies to: 38-49, 80-93, 105-111, 131-131
🏁 Script executed:
Length of output: 3106
Spec mismatch — remove
shutdownFirstor update PR/docsshutdownFirstis present in code and tests, which contradicts the PR objective to omit extras. Either remove the option and all related logic/messages (and update tests), or keep it and update the PR description/docs/workflow to advertise the option.Locations to fix:
Choose A (remove option + update tests) or B (keep option + update PR/docs/tests) and update accordingly.
🤖 Prompt for AI Agents