1+ /* global Feature, DataTable, Data, locate, Scenario */
2+ const assert = require ( "assert" ) ;
3+
4+ Feature ( "Brush relations" ) . tag ( "@regress" ) ;
5+
6+ const IMAGE = "https://user.fm/files/v2-901310d5cb3fa90e0616ca10590bacb3/spacexmoon-800x501.jpg" ;
7+
8+ const config = `<View>
9+ <Image name="img" value="$image"></Image>
10+ <BrushLabels name="tag" toName="img">
11+ <Label value="Test" background="orange"></Label>
12+ </BrushLabels>
13+ </View>` ;
14+
15+ function getPointAtSpiral ( t , v , w ) {
16+ return { x : v * t * Math . cos ( w * t ) , y : v * t * Math . sin ( w * t ) } ;
17+ }
18+
19+ function generateSpiralPoints ( x0 , y0 , R , v , w ) {
20+ let t = 1 , x , y ;
21+ const points = [ ] ;
22+
23+ do {
24+ ( { x, y } = getPointAtSpiral ( t ++ , v , w ) ) ;
25+ points . push ( [ x + x0 , y + y0 ] ) ;
26+ } while ( x ** 2 + y ** 2 < R ** 2 ) ;
27+ return points ;
28+ }
29+
30+ Scenario ( "Brush relations shouldn't crash everything" , async ( { I, LabelStudio, AtImageView, AtSidebar, ErrorsCollector } ) => {
31+ const params = {
32+ config,
33+ data : { image : IMAGE } ,
34+ } ;
35+
36+ I . amOnPage ( "/" ) ;
37+ await ErrorsCollector . run ( ) ;
38+ LabelStudio . init ( params ) ;
39+ AtImageView . waitForImage ( ) ;
40+ AtSidebar . seeRegions ( 0 ) ;
41+ await AtImageView . lookForStage ( ) ;
42+ const canvasSize = await AtImageView . getCanvasSize ( ) ;
43+ const regionsCentralPoints = [ ] ;
44+
45+ // create 4 brush regions
46+ for ( let i = 0 ; i < 4 ; i ++ ) {
47+ // find start position
48+ const x = canvasSize . width / 4 * ( ( i % 2 ) * 2 + 1 ) ;
49+ const y = canvasSize . height / 4 * ( ( Math . floor ( i / 2 ) % 2 ) * 2 + 1 ) ;
50+ // generate points in a spiral
51+ const points = generateSpiralPoints ( x , y , Math . min ( canvasSize . width / 6 , canvasSize . height / 6 ) , .4 , Math . PI / 18 ) ;
52+
53+ // select the brush label
54+ I . pressKey ( "1" ) ;
55+ // draw a brush region
56+ AtImageView . drawThroughPoints ( points ) ;
57+ AtSidebar . seeRegions ( i + 1 ) ;
58+ // unselect the region
59+ I . pressKey ( "u" ) ;
60+ // save the central point
61+ regionsCentralPoints . push ( { x, y } ) ;
62+ }
63+
64+ // Check that we can create a relation between the brush regions
65+ {
66+ // switch to the move tool for easy region selecting
67+ I . pressKey ( "v" ) ;
68+ // select the first region
69+ AtImageView . clickAt ( regionsCentralPoints [ 0 ] . x , regionsCentralPoints [ 0 ] . y ) ;
70+ // create relation to the second region
71+ I . pressKey ( [ "alt" , "r" ] ) ;
72+ AtImageView . clickAt ( regionsCentralPoints [ 1 ] . x , regionsCentralPoints [ 1 ] . y ) ;
73+ // check that the relation has been created
74+ AtSidebar . seeRelations ( 1 ) ;
75+ }
76+
77+ // Check that relations work fine on a brush restoration (from rle)
78+ {
79+ // export annotation
80+ const annotation = await LabelStudio . serialize ( ) ;
81+
82+ // reload LS with that datalabel studio logo
83+ LabelStudio . init ( {
84+ ...params ,
85+ annotations : [ { id : "imported" , result : annotation } ] ,
86+ } ) ;
87+
88+ AtImageView . waitForImage ( ) ;
89+ // Check that relation still exist
90+ AtSidebar . seeRelations ( 1 ) ;
91+
92+ // Try to create new relation with restored regions
93+ {
94+ // switch to the move tool for easy region selecting
95+ I . pressKey ( "v" ) ;
96+ // select the third region
97+ AtImageView . clickAt ( regionsCentralPoints [ 2 ] . x , regionsCentralPoints [ 2 ] . y ) ;
98+ // create relation to the fourth region
99+ I . pressKey ( [ "alt" , "r" ] ) ;
100+ AtImageView . clickAt ( regionsCentralPoints [ 3 ] . x , regionsCentralPoints [ 3 ] . y ) ;
101+ // check that the relation has been created
102+ AtSidebar . seeRelations ( 2 ) ;
103+ }
104+
105+ // Check the we didn't get any errors
106+ const errors = await ErrorsCollector . grabErrors ( ) ;
107+
108+ if ( errors . length ) {
109+ assert . fail ( `Got an error: ${ errors [ 0 ] } ` ) ;
110+ }
111+ }
112+ } ) ;
0 commit comments