1616package bricks
1717
1818import (
19+ "errors"
20+ "slices"
1921 "testing"
2022
2123 "github.com/arduino/go-paths-helper"
@@ -24,6 +26,7 @@ import (
2426
2527 "github.com/arduino/arduino-app-cli/internal/orchestrator/app"
2628 "github.com/arduino/arduino-app-cli/internal/orchestrator/bricksindex"
29+ "github.com/arduino/arduino-app-cli/internal/orchestrator/modelsindex"
2730)
2831
2932func TestBrickCreate (t * testing.T ) {
@@ -190,3 +193,96 @@ func TestGetBrickInstanceVariableDetails(t *testing.T) {
190193 })
191194 }
192195}
196+
197+ func TestBrickCreateWithModulesUpdate (t * testing.T ) {
198+ bricksIndex , err := bricksindex .GenerateBricksIndexFromFile (paths .New ("testdata" ))
199+ require .Nil (t , err )
200+ modelsIndex , err := modelsindex .GenerateModelsIndexFromFile (paths .New ("testdata" ))
201+ require .Nil (t , err )
202+ brickService := NewService (modelsIndex , bricksIndex , nil )
203+ model := "glass-breaking"
204+ missingModel := "missing-model"
205+
206+ tests := []struct {
207+ name string
208+ model string
209+ updateRequest BrickCreateUpdateRequest
210+ expectedErr error
211+ expectedBrickModelDescriptor string
212+ }{
213+ {
214+ name : "add a brick with a defined model to an App" ,
215+ model : "glass-breaking" ,
216+ updateRequest : BrickCreateUpdateRequest {
217+ ID : "arduino:audio_classification" ,
218+ Variables : nil ,
219+ Model : & model ,
220+ },
221+ expectedErr : nil ,
222+ expectedBrickModelDescriptor : "arduino:audio_classification" ,
223+ },
224+ {
225+ name : "add a brick with not existent model to an App" ,
226+ model : "glass-breaking" ,
227+ updateRequest : BrickCreateUpdateRequest {
228+ ID : "arduino:audio_classification" ,
229+ Variables : nil ,
230+ Model : & missingModel ,
231+ },
232+ expectedErr : errors .New ("model missing-model does not exist" ),
233+ expectedBrickModelDescriptor : "arduino:audio_classification" ,
234+ },
235+ }
236+
237+ for _ , tt := range tests {
238+ dummyApp := appDummySetup (t )
239+ t .Run (tt .name , func (t * testing.T ) {
240+
241+ brickErr := brickService .BrickCreate (tt .updateRequest , f .Must (app .Load (dummyApp .String ())))
242+ require .Equal (t , brickErr , tt .expectedErr )
243+
244+ after , err := app .Load (dummyApp .String ())
245+ require .Nil (t , err )
246+ brickAddedToApp := slices .ContainsFunc (after .Descriptor .Bricks , func (b app.Brick ) bool {
247+ return b .ID == tt .expectedBrickModelDescriptor
248+ })
249+ if brickErr != nil {
250+ require .False (t , brickAddedToApp )
251+ } else {
252+ require .True (t , brickAddedToApp )
253+ }
254+
255+ })
256+ }
257+ }
258+
259+ /*
260+ t.Run("add a brick to an App", func(t *testing.T) {
261+ dummyApp := appDummySetup(t)
262+
263+ model := "glass-breaking"
264+ req := BrickCreateUpdateRequest{
265+ ID: "arduino:audio_classification",
266+ Variables: nil,
267+ Model: &model,
268+ }
269+
270+ // act
271+ err = brickService.BrickCreate(req, f.Must(app.Load(dummyApp.String())))
272+ require.Nil(t, err)
273+
274+ // assert
275+ after, err := app.Load(dummyApp.String())
276+ require.Nil(t, err)
277+ require.Len(t, after.Descriptor.Bricks, 2)
278+ require.Equal(t, "arduino:audio_classification", after.Descriptor.Bricks[1].ID)
279+ })
280+ */
281+
282+ func appDummySetup (t * testing.T ) * paths.Path {
283+ tempDummyApp := paths .New ("testdata/dummy-app.temp" )
284+ err := tempDummyApp .RemoveAll ()
285+ require .Nil (t , err )
286+ require .Nil (t , paths .New ("testdata/dummy-app" ).CopyDirTo (tempDummyApp ))
287+ return tempDummyApp
288+ }
0 commit comments