@@ -5,17 +5,23 @@ import {
55} from '@theia/core/shared/inversify' ;
66import { assert , expect } from 'chai' ;
77import fetch from 'cross-fetch' ;
8+ import { rejects } from 'node:assert' ;
89import { posix } from 'node:path' ;
10+ import queryString from 'query-string' ;
911import { v4 } from 'uuid' ;
1012import { ArduinoPreferences } from '../../browser/arduino-preferences' ;
1113import { AuthenticationClientService } from '../../browser/auth/authentication-client-service' ;
1214import { CreateApi } from '../../browser/create/create-api' ;
1315import { splitSketchPath } from '../../browser/create/create-paths' ;
14- import { Create , CreateError } from '../../browser/create/typings' ;
16+ import {
17+ Create ,
18+ CreateError ,
19+ isNotFound ,
20+ isUnprocessableContent ,
21+ } from '../../browser/create/typings' ;
1522import { SketchCache } from '../../browser/widgets/cloud-sketchbook/cloud-sketch-cache' ;
1623import { SketchesService } from '../../common/protocol' ;
1724import { AuthenticationSession } from '../../node/auth/types' ;
18- import queryString from 'query-string' ;
1925
2026/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
2127/* eslint-disable @typescript-eslint/no-non-null-assertion */
@@ -229,6 +235,48 @@ describe('create-api', () => {
229235 expect ( findByName ( otherName , sketches ) ) . to . be . not . undefined ;
230236 } ) ;
231237
238+ it ( 'should fail with HTTP 422 when reading a file but is a directory' , async ( ) => {
239+ const name = v4 ( ) ;
240+ const content = 'void setup(){} void loop(){}' ;
241+ const posixPath = toPosix ( name ) ;
242+
243+ await createApi . createSketch ( posixPath , content ) ;
244+ const resources = await createApi . readDirectory ( posixPath ) ;
245+ expect ( resources ) . to . be . not . empty ;
246+
247+ await rejects ( createApi . readFile ( posixPath ) , ( thrown ) =>
248+ isUnprocessableContent ( thrown )
249+ ) ;
250+ } ) ;
251+
252+ it ( 'should fail with HTTP 422 when listing a directory but is a file' , async ( ) => {
253+ const name = v4 ( ) ;
254+ const content = 'void setup(){} void loop(){}' ;
255+ const posixPath = toPosix ( name ) ;
256+
257+ await createApi . createSketch ( posixPath , content ) ;
258+ const mainSketchFilePath = posixPath + posixPath + '.ino' ;
259+ const sketchContent = await createApi . readFile ( mainSketchFilePath ) ;
260+ expect ( sketchContent ) . to . be . equal ( content ) ;
261+
262+ await rejects ( createApi . readDirectory ( mainSketchFilePath ) , ( thrown ) =>
263+ isUnprocessableContent ( thrown )
264+ ) ;
265+ } ) ;
266+
267+ it ( "should fail with HTTP 404 when deleting a non-existing directory via the '/files/d' endpoint" , async ( ) => {
268+ const name = v4 ( ) ;
269+ const posixPath = toPosix ( name ) ;
270+
271+ const sketches = await createApi . sketches ( ) ;
272+ const sketch = findByName ( name , sketches ) ;
273+ expect ( sketch ) . to . be . undefined ;
274+
275+ await rejects ( createApi . deleteDirectory ( posixPath ) , ( thrown ) =>
276+ isNotFound ( thrown )
277+ ) ;
278+ } ) ;
279+
232280 [ '.' , '-' , '_' ] . map ( ( char ) => {
233281 it ( `should create a new sketch with '${ char } ' in the sketch folder name although it's disallowed from the Create Editor` , async ( ) => {
234282 const name = `sketch${ char } ` ;
0 commit comments