11import { describe , it , expect } from 'vitest' ;
2- import * as z from 'zod' ;
32import { schema , erase_simsLogic } from '../erase_sims.ts' ;
43import { createMockExecutor } from '../../../../test-utils/mock-executors.ts' ;
4+ import { allText , createMockToolHandlerContext } from '../../../../test-utils/test-helpers.ts' ;
5+
6+ const runLogic = async ( logic : ( ) => Promise < unknown > ) => {
7+ const { result, run } = createMockToolHandlerContext ( ) ;
8+ const response = await run ( logic ) ;
9+
10+ if (
11+ response &&
12+ typeof response === 'object' &&
13+ 'content' in ( response as Record < string , unknown > )
14+ ) {
15+ return response as {
16+ content : Array < { type : string ; text ?: string ; data ?: string ; mimeType ?: string } > ;
17+ isError ?: boolean ;
18+ nextStepParams ?: unknown ;
19+ } ;
20+ }
21+
22+ const text = result . text ( ) ;
23+ const textContent = text . length > 0 ? [ { type : 'text' as const , text } ] : [ ] ;
24+ const imageContent = result . attachments . map ( ( attachment ) => ( {
25+ type : 'image' as const ,
26+ data : attachment . data ,
27+ mimeType : attachment . mimeType ,
28+ } ) ) ;
29+
30+ return {
31+ content : [ ...textContent , ...imageContent ] ,
32+ isError : result . isError ( ) ? true : undefined ,
33+ nextStepParams : result . nextStepParams ,
34+ attachments : result . attachments ,
35+ text,
36+ } ;
37+ } ;
538
639describe ( 'erase_sims tool (single simulator)' , ( ) => {
7- describe ( 'Schema Validation' , ( ) => {
8- it ( 'should validate schema fields (shape only)' , ( ) => {
9- const schemaObj = z . object ( schema ) ;
10- expect ( schemaObj . safeParse ( { shutdownFirst : true } ) . success ) . toBe ( true ) ;
11- expect ( schemaObj . safeParse ( { } ) . success ) . toBe ( true ) ;
40+ describe ( 'Plugin Structure' , ( ) => {
41+ it ( 'should expose schema' , ( ) => {
42+ expect ( schema ) . toBeDefined ( ) ;
1243 } ) ;
1344 } ) ;
1445
1546 describe ( 'Single mode' , ( ) => {
1647 it ( 'erases a simulator successfully' , async ( ) => {
1748 const mock = createMockExecutor ( { success : true , output : 'OK' } ) ;
18- const res = await erase_simsLogic ( { simulatorId : 'UD1' } , mock ) ;
19- expect ( res ) . toEqual ( {
20- content : [ { type : 'text' , text : 'Successfully erased simulator UD1' } ] ,
21- } ) ;
49+ const res = await runLogic ( ( ) => erase_simsLogic ( { simulatorId : 'UD1' } , mock ) ) ;
50+ expect ( res . isError ) . toBeFalsy ( ) ;
2251 } ) ;
2352
2453 it ( 'returns failure when erase fails' , async ( ) => {
2554 const mock = createMockExecutor ( { success : false , error : 'Booted device' } ) ;
26- const res = await erase_simsLogic ( { simulatorId : 'UD1' } , mock ) ;
27- expect ( res ) . toEqual ( {
28- content : [ { type : 'text' , text : 'Failed to erase simulator: Booted device' } ] ,
29- } ) ;
55+ const res = await runLogic ( ( ) => erase_simsLogic ( { simulatorId : 'UD1' } , mock ) ) ;
56+ expect ( res . isError ) . toBe ( true ) ;
3057 } ) ;
3158
3259 it ( 'adds tool hint when booted error occurs without shutdownFirst' , async ( ) => {
3360 const bootedError =
3461 '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' ;
3562 const mock = createMockExecutor ( { success : false , error : bootedError } ) ;
36- const res = await erase_simsLogic ( { simulatorId : 'UD1' } , mock ) ;
37- expect ( ( res . content ?. [ 1 ] as any ) . text ) . toContain ( 'Tool hint' ) ;
38- expect ( ( res . content ?. [ 1 ] as any ) . text ) . toContain ( 'shutdownFirst: true' ) ;
63+ const res = await runLogic ( ( ) => erase_simsLogic ( { simulatorId : 'UD1' } , mock ) ) ;
64+ const text = allText ( res ) ;
65+ expect ( text ) . toContain ( 'shutdownFirst: true' ) ;
66+ expect ( res . isError ) . toBe ( true ) ;
3967 } ) ;
4068
4169 it ( 'performs shutdown first when shutdownFirst=true' , async ( ) => {
@@ -44,14 +72,14 @@ describe('erase_sims tool (single simulator)', () => {
4472 calls . push ( cmd ) ;
4573 return { success : true , output : 'OK' , error : '' , process : { pid : 1 } as any } ;
4674 } ;
47- const res = await erase_simsLogic ( { simulatorId : 'UD1' , shutdownFirst : true } , exec as any ) ;
75+ const res = await runLogic ( ( ) =>
76+ erase_simsLogic ( { simulatorId : 'UD1' , shutdownFirst : true } , exec as any ) ,
77+ ) ;
4878 expect ( calls ) . toEqual ( [
4979 [ 'xcrun' , 'simctl' , 'shutdown' , 'UD1' ] ,
5080 [ 'xcrun' , 'simctl' , 'erase' , 'UD1' ] ,
5181 ] ) ;
52- expect ( res ) . toEqual ( {
53- content : [ { type : 'text' , text : 'Successfully erased simulator UD1' } ] ,
54- } ) ;
82+ expect ( res . isError ) . toBeFalsy ( ) ;
5583 } ) ;
5684 } ) ;
5785} ) ;
0 commit comments