|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import eraseSims, { erase_simsLogic } from '../erase_sims.ts'; |
| 4 | +import { createMockExecutor } from '../../../../test-utils/mock-executors.ts'; |
| 5 | + |
| 6 | +describe('erase_sims tool (UDID or ALL only)', () => { |
| 7 | + describe('Export Field Validation (Literal)', () => { |
| 8 | + it('should have correct name', () => { |
| 9 | + expect(eraseSims.name).toBe('erase_sims'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should have correct description', () => { |
| 13 | + expect(eraseSims.description).toContain('Provide exactly one of: simulatorUdid or all=true'); |
| 14 | + expect(eraseSims.description).toContain('shutdownFirst'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should have handler function', () => { |
| 18 | + expect(typeof eraseSims.handler).toBe('function'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should validate schema fields (shape only)', () => { |
| 22 | + const schema = z.object(eraseSims.schema); |
| 23 | + // Valid |
| 24 | + expect( |
| 25 | + schema.safeParse({ simulatorUdid: '123e4567-e89b-12d3-a456-426614174000' }).success, |
| 26 | + ).toBe(true); |
| 27 | + expect(schema.safeParse({ all: true }).success).toBe(true); |
| 28 | + // Shape-level schema does not enforce selection rules; handler validation covers that. |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('Single mode', () => { |
| 33 | + it('erases a simulator successfully', async () => { |
| 34 | + const mock = createMockExecutor({ success: true, output: 'OK' }); |
| 35 | + const res = await erase_simsLogic({ simulatorUdid: 'UD1' }, mock); |
| 36 | + expect(res).toEqual({ |
| 37 | + content: [{ type: 'text', text: 'Successfully erased simulator UD1' }], |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns failure when erase fails', async () => { |
| 42 | + const mock = createMockExecutor({ success: false, error: 'Booted device' }); |
| 43 | + const res = await erase_simsLogic({ simulatorUdid: 'UD1' }, mock); |
| 44 | + expect(res).toEqual({ |
| 45 | + content: [{ type: 'text', text: 'Failed to erase simulator: Booted device' }], |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('adds tool hint when booted error occurs without shutdownFirst', async () => { |
| 50 | + const bootedError = |
| 51 | + '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'; |
| 52 | + const mock = createMockExecutor({ success: false, error: bootedError }); |
| 53 | + const res = await erase_simsLogic({ simulatorUdid: 'UD1' }, mock); |
| 54 | + expect((res.content?.[1] as any).text).toContain('Tool hint'); |
| 55 | + expect((res.content?.[1] as any).text).toContain('shutdownFirst: true'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('performs shutdown first when shutdownFirst=true', async () => { |
| 59 | + const calls: any[] = []; |
| 60 | + const exec = async (cmd: string[]) => { |
| 61 | + calls.push(cmd); |
| 62 | + return { success: true, output: 'OK', error: '', process: { pid: 1 } as any }; |
| 63 | + }; |
| 64 | + const res = await erase_simsLogic({ simulatorUdid: 'UD1', shutdownFirst: true }, exec as any); |
| 65 | + expect(calls).toEqual([ |
| 66 | + ['xcrun', 'simctl', 'shutdown', 'UD1'], |
| 67 | + ['xcrun', 'simctl', 'erase', 'UD1'], |
| 68 | + ]); |
| 69 | + expect(res).toEqual({ |
| 70 | + content: [{ type: 'text', text: 'Successfully erased simulator UD1' }], |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('All mode', () => { |
| 76 | + it('erases all simulators successfully', async () => { |
| 77 | + const exec = createMockExecutor({ success: true, output: 'OK' }); |
| 78 | + const res = await erase_simsLogic({ all: true }, exec); |
| 79 | + expect(res).toEqual({ |
| 80 | + content: [{ type: 'text', text: 'Successfully erased all simulators' }], |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns failure when erase all fails', async () => { |
| 85 | + const exec = createMockExecutor({ success: false, error: 'Denied' }); |
| 86 | + const res = await erase_simsLogic({ all: true }, exec); |
| 87 | + expect(res).toEqual({ |
| 88 | + content: [{ type: 'text', text: 'Failed to erase all simulators: Denied' }], |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('performs shutdown all when shutdownFirst=true', async () => { |
| 93 | + const calls: any[] = []; |
| 94 | + const exec = async (cmd: string[]) => { |
| 95 | + calls.push(cmd); |
| 96 | + return { success: true, output: 'OK', error: '', process: { pid: 1 } as any }; |
| 97 | + }; |
| 98 | + const res = await erase_simsLogic({ all: true, shutdownFirst: true }, exec as any); |
| 99 | + expect(calls).toEqual([ |
| 100 | + ['xcrun', 'simctl', 'shutdown', 'all'], |
| 101 | + ['xcrun', 'simctl', 'erase', 'all'], |
| 102 | + ]); |
| 103 | + expect(res).toEqual({ |
| 104 | + content: [{ type: 'text', text: 'Successfully erased all simulators' }], |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('adds tool hint on booted error without shutdownFirst (all mode)', async () => { |
| 109 | + const bootedError = 'Unable to erase contents and settings in current state: Booted'; |
| 110 | + const exec = createMockExecutor({ success: false, error: bootedError }); |
| 111 | + const res = await erase_simsLogic({ all: true }, exec); |
| 112 | + expect((res.content?.[1] as any).text).toContain('Tool hint'); |
| 113 | + expect((res.content?.[1] as any).text).toContain('shutdownFirst: true'); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments