@@ -29,59 +29,57 @@ describe("isToolAllowedForMode", () => {
2929 describe ( "file restrictions" , ( ) => {
3030 it ( "allows editing matching files" , ( ) => {
3131 // Test markdown editor mode
32- const mdResult = isToolAllowedForMode ( "write_to_file" , "markdown-editor" , customModes , undefined , "test.md" )
32+ const mdResult = isToolAllowedForMode ( "write_to_file" , "markdown-editor" , customModes , undefined , {
33+ path : "test.md" ,
34+ } )
3335 expect ( mdResult ) . toBe ( true )
3436
3537 // Test CSS editor mode
36- const cssResult = isToolAllowedForMode ( "write_to_file" , "css-editor" , customModes , undefined , "styles.css" )
38+ const cssResult = isToolAllowedForMode ( "write_to_file" , "css-editor" , customModes , undefined , {
39+ path : "styles.css" ,
40+ } )
3741 expect ( cssResult ) . toBe ( true )
3842 } )
3943
4044 it ( "rejects editing non-matching files" , ( ) => {
4145 // Test markdown editor mode with non-markdown file
42- const mdError = isToolAllowedForMode ( "write_to_file" , "markdown-editor" , customModes , undefined , "test.js" )
43- expect ( mdError ) . toBeInstanceOf ( FileRestrictionError )
44- expect ( ( mdError as FileRestrictionError ) . message ) . toContain ( "\\.md$" )
46+ expect ( ( ) =>
47+ isToolAllowedForMode ( "write_to_file" , "markdown-editor" , customModes , undefined , { path : "test.js" } ) ,
48+ ) . toThrow ( FileRestrictionError )
49+ expect ( ( ) =>
50+ isToolAllowedForMode ( "write_to_file" , "markdown-editor" , customModes , undefined , { path : "test.js" } ) ,
51+ ) . toThrow ( / \\ .m d \$ / )
4552
4653 // Test CSS editor mode with non-CSS file
47- const cssError = isToolAllowedForMode ( "write_to_file" , "css-editor" , customModes , undefined , "test.js" )
48- expect ( cssError ) . toBeInstanceOf ( FileRestrictionError )
49- expect ( ( cssError as FileRestrictionError ) . message ) . toContain ( "\\.css$" )
50- } )
51-
52- it ( "requires file path for restricted edit operations" , ( ) => {
53- const result = isToolAllowedForMode ( "write_to_file" , "markdown-editor" , customModes )
54- expect ( result ) . toBeInstanceOf ( FileRestrictionError )
55- expect ( ( result as FileRestrictionError ) . message ) . toContain ( "\\.md$" )
54+ expect ( ( ) =>
55+ isToolAllowedForMode ( "write_to_file" , "css-editor" , customModes , undefined , { path : "test.js" } ) ,
56+ ) . toThrow ( FileRestrictionError )
57+ expect ( ( ) =>
58+ isToolAllowedForMode ( "write_to_file" , "css-editor" , customModes , undefined , { path : "test.js" } ) ,
59+ ) . toThrow ( / \\ .c s s \$ / )
5660 } )
5761
5862 it ( "applies restrictions to both write_to_file and apply_diff" , ( ) => {
5963 // Test write_to_file
60- const writeResult = isToolAllowedForMode (
61- "write_to_file" ,
62- "markdown-editor" ,
63- customModes ,
64- undefined ,
65- "test.md" ,
66- )
64+ const writeResult = isToolAllowedForMode ( "write_to_file" , "markdown-editor" , customModes , undefined , {
65+ path : "test.md" ,
66+ } )
6767 expect ( writeResult ) . toBe ( true )
6868
6969 // Test apply_diff
70- const diffResult = isToolAllowedForMode ( "apply_diff" , "markdown-editor" , customModes , undefined , "test.md" )
70+ const diffResult = isToolAllowedForMode ( "apply_diff" , "markdown-editor" , customModes , undefined , {
71+ path : "test.md" ,
72+ } )
7173 expect ( diffResult ) . toBe ( true )
7274
7375 // Test both with non-matching file
74- const writeError = isToolAllowedForMode (
75- "write_to_file" ,
76- "markdown-editor" ,
77- customModes ,
78- undefined ,
79- "test.js" ,
80- )
81- expect ( writeError ) . toBeInstanceOf ( FileRestrictionError )
76+ expect ( ( ) =>
77+ isToolAllowedForMode ( "write_to_file" , "markdown-editor" , customModes , undefined , { path : "test.js" } ) ,
78+ ) . toThrow ( FileRestrictionError )
8279
83- const diffError = isToolAllowedForMode ( "apply_diff" , "markdown-editor" , customModes , undefined , "test.js" )
84- expect ( diffError ) . toBeInstanceOf ( FileRestrictionError )
80+ expect ( ( ) =>
81+ isToolAllowedForMode ( "apply_diff" , "markdown-editor" , customModes , undefined , { path : "test.js" } ) ,
82+ ) . toThrow ( FileRestrictionError )
8583 } )
8684
8785 it ( "uses description in file restriction error for custom modes" , ( ) => {
@@ -99,60 +97,57 @@ describe("isToolAllowedForMode", () => {
9997 ]
10098
10199 // Test write_to_file with non-matching file
102- const writeError = isToolAllowedForMode (
103- "write_to_file" ,
104- "docs-editor" ,
105- customModesWithDescription ,
106- undefined ,
107- "test.js" ,
108- )
109- expect ( writeError ) . toBeInstanceOf ( FileRestrictionError )
110- expect ( ( writeError as FileRestrictionError ) . message ) . toContain ( "Documentation files only" )
100+ expect ( ( ) =>
101+ isToolAllowedForMode ( "write_to_file" , "docs-editor" , customModesWithDescription , undefined , {
102+ path : "test.js" ,
103+ } ) ,
104+ ) . toThrow ( FileRestrictionError )
105+ expect ( ( ) =>
106+ isToolAllowedForMode ( "write_to_file" , "docs-editor" , customModesWithDescription , undefined , {
107+ path : "test.js" ,
108+ } ) ,
109+ ) . toThrow ( / D o c u m e n t a t i o n f i l e s o n l y / )
111110
112111 // Test apply_diff with non-matching file
113- const diffError = isToolAllowedForMode (
114- "apply_diff" ,
115- "docs-editor" ,
116- customModesWithDescription ,
117- undefined ,
118- "test.js" ,
119- )
120- expect ( diffError ) . toBeInstanceOf ( FileRestrictionError )
121- expect ( ( diffError as FileRestrictionError ) . message ) . toContain ( "Documentation files only" )
112+ expect ( ( ) =>
113+ isToolAllowedForMode ( "apply_diff" , "docs-editor" , customModesWithDescription , undefined , {
114+ path : "test.js" ,
115+ } ) ,
116+ ) . toThrow ( FileRestrictionError )
117+ expect ( ( ) =>
118+ isToolAllowedForMode ( "apply_diff" , "docs-editor" , customModesWithDescription , undefined , {
119+ path : "test.js" ,
120+ } ) ,
121+ ) . toThrow ( / D o c u m e n t a t i o n f i l e s o n l y / )
122122
123123 // Test that matching files are allowed
124- const mdResult = isToolAllowedForMode (
125- "write_to_file" ,
126- "docs-editor" ,
127- customModesWithDescription ,
128- undefined ,
129- "test.md" ,
130- )
131- expect ( mdResult ) . toBe ( true )
132-
133- const txtResult = isToolAllowedForMode (
134- "write_to_file" ,
135- "docs-editor" ,
136- customModesWithDescription ,
137- undefined ,
138- "test.txt" ,
139- )
140- expect ( txtResult ) . toBe ( true )
124+ expect (
125+ isToolAllowedForMode ( "write_to_file" , "docs-editor" , customModesWithDescription , undefined , {
126+ path : "test.md" ,
127+ } ) ,
128+ ) . toBe ( true )
129+
130+ expect (
131+ isToolAllowedForMode ( "write_to_file" , "docs-editor" , customModesWithDescription , undefined , {
132+ path : "test.txt" ,
133+ } ) ,
134+ ) . toBe ( true )
141135 } )
142136
143137 it ( "allows ask mode to edit markdown files only" , ( ) => {
144138 // Should allow editing markdown files
145- const mdResult = isToolAllowedForMode ( "write_to_file" , "ask" , [ ] , undefined , "test.md" )
146- expect ( mdResult ) . toBe ( true )
139+ expect ( isToolAllowedForMode ( "write_to_file" , "ask" , [ ] , undefined , { path : "test.md" } ) ) . toBe ( true )
147140
148141 // Should allow applying diffs to markdown files
149- const diffResult = isToolAllowedForMode ( "apply_diff" , "ask" , [ ] , undefined , "readme.md" )
150- expect ( diffResult ) . toBe ( true )
142+ expect ( isToolAllowedForMode ( "apply_diff" , "ask" , [ ] , undefined , { path : "readme.md" } ) ) . toBe ( true )
151143
152144 // Should reject non-markdown files
153- const jsResult = isToolAllowedForMode ( "write_to_file" , "ask" , [ ] , undefined , "test.js" )
154- expect ( jsResult ) . toBeInstanceOf ( FileRestrictionError )
155- expect ( ( jsResult as FileRestrictionError ) . message ) . toContain ( "Markdown files only" )
145+ expect ( ( ) => isToolAllowedForMode ( "write_to_file" , "ask" , [ ] , undefined , { path : "test.js" } ) ) . toThrow (
146+ FileRestrictionError ,
147+ )
148+ expect ( ( ) => isToolAllowedForMode ( "write_to_file" , "ask" , [ ] , undefined , { path : "test.js" } ) ) . toThrow (
149+ / M a r k d o w n f i l e s o n l y / ,
150+ )
156151
157152 // Should maintain read capabilities
158153 expect ( isToolAllowedForMode ( "read_file" , "ask" , [ ] ) ) . toBe ( true )
@@ -176,14 +171,18 @@ describe("isToolAllowedForMode", () => {
176171
177172describe ( "FileRestrictionError" , ( ) => {
178173 it ( "formats error message with pattern when no description provided" , ( ) => {
179- const error = new FileRestrictionError ( "Markdown Editor" , "\\.md$" )
180- expect ( error . message ) . toBe ( "This mode (Markdown Editor) can only edit files matching the pattern: \\.md$" )
174+ const error = new FileRestrictionError ( "Markdown Editor" , "\\.md$" , undefined , "test.js" )
175+ expect ( error . message ) . toBe (
176+ "This mode (Markdown Editor) can only edit files matching pattern: \\.md$. Got: test.js" ,
177+ )
181178 expect ( error . name ) . toBe ( "FileRestrictionError" )
182179 } )
183180
184181 it ( "formats error message with description when provided" , ( ) => {
185- const error = new FileRestrictionError ( "Markdown Editor" , "\\.md$" , "Markdown files only" )
186- expect ( error . message ) . toBe ( "This mode (Markdown Editor) can only edit files matching Markdown files only" )
182+ const error = new FileRestrictionError ( "Markdown Editor" , "\\.md$" , "Markdown files only" , "test.js" )
183+ expect ( error . message ) . toBe (
184+ "This mode (Markdown Editor) can only edit files matching pattern: \\.md$ (Markdown files only). Got: test.js" ,
185+ )
187186 expect ( error . name ) . toBe ( "FileRestrictionError" )
188187 } )
189188} )
0 commit comments