1
1
// @vitest -environment happy-dom
2
2
3
- import { beforeEach , describe , expect , it , vi , type Mock } from 'vitest' ;
3
+ import { beforeEach , describe , expect , it , vi } from 'vitest' ;
4
4
import {
5
5
Classification as PBClassification ,
6
6
Detection as PBDetection ,
@@ -17,68 +17,121 @@ const visionClientName = 'test-vision';
17
17
18
18
let vision : VisionClient ;
19
19
20
+ const classification : Classification = {
21
+ className : 'face' ,
22
+ confidence : 0.995_482_683_181_762_7 ,
23
+ } ;
24
+ const pbClassification = ( ( ) => {
25
+ const pb = new PBClassification ( ) ;
26
+ pb . setClassName ( classification . className ) ;
27
+ pb . setConfidence ( classification . confidence ) ;
28
+ return pb ;
29
+ } ) ( ) ;
30
+
31
+ const detection : Detection = {
32
+ xMin : 251 ,
33
+ yMin : 225 ,
34
+ xMax : 416 ,
35
+ yMax : 451 ,
36
+ confidence : 0.995_482_683_181_762_7 ,
37
+ className : 'face' ,
38
+ } ;
39
+ const pbDetection = ( ( ) => {
40
+ const pb = new PBDetection ( ) ;
41
+ pb . setClassName ( detection . className ) ;
42
+ pb . setConfidence ( detection . confidence ) ;
43
+ pb . setXMin ( detection . xMin ) ;
44
+ pb . setXMax ( detection . xMax ) ;
45
+ pb . setYMin ( detection . yMin ) ;
46
+ pb . setYMax ( detection . yMax ) ;
47
+ return pb ;
48
+ } ) ( ) ;
49
+
50
+ const pco : PointCloudObject = {
51
+ pointCloud : 'This is a PointCloudObject' ,
52
+ geometries : undefined ,
53
+ } ;
54
+ const pbPCO = ( ( ) => {
55
+ const pb = new PBPointCloudObject ( ) ;
56
+ pb . setPointCloud ( pco . pointCloud ) ;
57
+ return pb ;
58
+ } ) ( ) ;
59
+
20
60
describe ( 'VisionClient Tests' , ( ) => {
21
61
beforeEach ( ( ) => {
22
62
RobotClient . prototype . createServiceClient = vi
23
63
. fn ( )
24
64
. mockImplementation ( ( ) => new VisionServiceClient ( visionClientName ) ) ;
25
65
66
+ VisionServiceClient . prototype . getDetections = vi
67
+ . fn ( )
68
+ . mockImplementation ( ( _req , _md , cb ) => {
69
+ cb ( null , {
70
+ getDetectionsList : ( ) => [ pbDetection ] ,
71
+ } ) ;
72
+ } ) ;
73
+ VisionServiceClient . prototype . getDetectionsFromCamera = vi
74
+ . fn ( )
75
+ . mockImplementation ( ( _req , _md , cb ) => {
76
+ cb ( null , {
77
+ getDetectionsList : ( ) => [ pbDetection ] ,
78
+ } ) ;
79
+ } ) ;
80
+ VisionServiceClient . prototype . getClassifications = vi
81
+ . fn ( )
82
+ . mockImplementation ( ( _req , _md , cb ) => {
83
+ cb ( null , {
84
+ getClassificationsList : ( ) => [ pbClassification ] ,
85
+ } ) ;
86
+ } ) ;
87
+ VisionServiceClient . prototype . getClassificationsFromCamera = vi
88
+ . fn ( )
89
+ . mockImplementation ( ( _req , _md , cb ) => {
90
+ cb ( null , {
91
+ getClassificationsList : ( ) => [ pbClassification ] ,
92
+ } ) ;
93
+ } ) ;
94
+ VisionServiceClient . prototype . getObjectPointClouds = vi
95
+ . fn ( )
96
+ . mockImplementation ( ( _req , _md , cb ) => {
97
+ cb ( null , {
98
+ getObjectsList : ( ) => [ pbPCO ] ,
99
+ } ) ;
100
+ } ) ;
101
+ VisionServiceClient . prototype . getProperties = vi
102
+ . fn ( )
103
+ . mockImplementation ( ( _req , _md , cb ) => {
104
+ cb ( null , {
105
+ getClassificationsSupported : ( ) => true ,
106
+ getDetectionsSupported : ( ) => true ,
107
+ getObjectPointCloudsSupported : ( ) => true ,
108
+ } ) ;
109
+ } ) ;
110
+ VisionServiceClient . prototype . captureAllFromCamera = vi
111
+ . fn ( )
112
+ . mockImplementation ( ( _req , _md , cb ) => {
113
+ cb ( null , {
114
+ getImage : ( ) => undefined ,
115
+ getClassificationsList : ( ) => [ pbClassification ] ,
116
+ getDetectionsList : ( ) => [ pbDetection ] ,
117
+ getObjectsList : ( ) => [ pbPCO ] ,
118
+ } ) ;
119
+ } ) ;
120
+
26
121
vision = new VisionClient ( new RobotClient ( 'host' ) , visionClientName ) ;
27
122
} ) ;
28
123
29
124
describe ( 'Detection Tests' , ( ) => {
30
- const testDetection : Detection = {
31
- xMin : 251 ,
32
- yMin : 225 ,
33
- xMax : 416 ,
34
- yMax : 451 ,
35
- confidence : 0.995_482_683_181_762_7 ,
36
- className : 'face' ,
37
- } ;
38
- let detection : Mock < [ ] , PBDetection > ;
39
- const encodeDetection = ( det : Detection ) => {
40
- const pbDetection = new PBDetection ( ) ;
41
- pbDetection . setClassName ( det . className ) ;
42
- pbDetection . setConfidence ( det . confidence ) ;
43
- pbDetection . setXMin ( det . xMin ) ;
44
- pbDetection . setXMax ( det . xMax ) ;
45
- pbDetection . setYMin ( det . yMin ) ;
46
- pbDetection . setYMax ( det . yMax ) ;
47
- return pbDetection ;
48
- } ;
49
-
50
- beforeEach ( ( ) => {
51
- VisionServiceClient . prototype . getDetections = vi
52
- . fn ( )
53
- . mockImplementation ( ( _req , _md , cb ) => {
54
- cb ( null , {
55
- getDetectionsList : ( ) => [ detection ( ) ] ,
56
- } ) ;
57
- } ) ;
58
-
59
- VisionServiceClient . prototype . getDetectionsFromCamera = vi
60
- . fn ( )
61
- . mockImplementation ( ( _req , _md , cb ) => {
62
- cb ( null , {
63
- getDetectionsList : ( ) => [ detection ( ) ] ,
64
- } ) ;
65
- } ) ;
66
- } ) ;
67
-
68
125
it ( 'returns detections from a camera' , async ( ) => {
69
- detection = vi . fn ( ( ) => encodeDetection ( testDetection ) ) ;
70
-
71
- const expected = [ testDetection ] ;
126
+ const expected = [ detection ] ;
72
127
73
128
await expect (
74
129
vision . getDetectionsFromCamera ( 'camera' )
75
130
) . resolves . toStrictEqual ( expected ) ;
76
131
} ) ;
77
132
78
133
it ( 'returns detections from an image' , async ( ) => {
79
- detection = vi . fn ( ( ) => encodeDetection ( testDetection ) ) ;
80
-
81
- const expected = [ testDetection ] ;
134
+ const expected = [ detection ] ;
82
135
83
136
await expect (
84
137
vision . getDetections ( new Uint8Array ( ) , 1 , 1 , 'image/jpeg' )
@@ -87,50 +140,16 @@ describe('VisionClient Tests', () => {
87
140
} ) ;
88
141
89
142
describe ( 'Classification Tests' , ( ) => {
90
- const testClassification : Classification = {
91
- className : 'face' ,
92
- confidence : 0.995_482_683_181_762_7 ,
93
- } ;
94
- let classification : Mock < [ ] , PBClassification > ;
95
- const encodeClassification = ( cls : Classification ) => {
96
- const pbClassification = new PBClassification ( ) ;
97
- pbClassification . setClassName ( cls . className ) ;
98
- pbClassification . setConfidence ( cls . confidence ) ;
99
- return pbClassification ;
100
- } ;
101
-
102
- beforeEach ( ( ) => {
103
- VisionServiceClient . prototype . getClassifications = vi
104
- . fn ( )
105
- . mockImplementation ( ( _req , _md , cb ) => {
106
- cb ( null , {
107
- getClassificationsList : ( ) => [ classification ( ) ] ,
108
- } ) ;
109
- } ) ;
110
-
111
- VisionServiceClient . prototype . getClassificationsFromCamera = vi
112
- . fn ( )
113
- . mockImplementation ( ( _req , _md , cb ) => {
114
- cb ( null , {
115
- getClassificationsList : ( ) => [ classification ( ) ] ,
116
- } ) ;
117
- } ) ;
118
- } ) ;
119
-
120
143
it ( 'returns classifications from a camera' , async ( ) => {
121
- classification = vi . fn ( ( ) => encodeClassification ( testClassification ) ) ;
122
-
123
- const expected = [ testClassification ] ;
144
+ const expected = [ classification ] ;
124
145
125
146
await expect (
126
147
vision . getClassificationsFromCamera ( 'camera' , 1 )
127
148
) . resolves . toStrictEqual ( expected ) ;
128
149
} ) ;
129
150
130
151
it ( 'returns classifications from an image' , async ( ) => {
131
- classification = vi . fn ( ( ) => encodeClassification ( testClassification ) ) ;
132
-
133
- const expected = [ testClassification ] ;
152
+ const expected = [ classification ] ;
134
153
135
154
await expect (
136
155
vision . getClassifications ( new Uint8Array ( ) , 1 , 1 , 'image/jpeg' , 1 )
@@ -139,35 +158,40 @@ describe('VisionClient Tests', () => {
139
158
} ) ;
140
159
141
160
describe ( 'Object Point Cloud Tests' , ( ) => {
142
- const testPCO : PointCloudObject = {
143
- pointCloud : 'This is a PointCloudObject' ,
144
- geometries : undefined ,
145
- } ;
146
- let pointCloudObject : Mock < [ ] , PBPointCloudObject > ;
147
- const encodePCO = ( pco : PointCloudObject ) => {
148
- const pbPCO = new PBPointCloudObject ( ) ;
149
- pbPCO . setPointCloud ( pco . pointCloud ) ;
150
- return pbPCO ;
151
- } ;
152
-
153
- beforeEach ( ( ) => {
154
- VisionServiceClient . prototype . getObjectPointClouds = vi
155
- . fn ( )
156
- . mockImplementation ( ( _req , _md , cb ) => {
157
- cb ( null , {
158
- getObjectsList : ( ) => [ pointCloudObject ( ) ] ,
159
- } ) ;
160
- } ) ;
161
- } ) ;
162
-
163
161
it ( 'returns a PointCloudObject from a camera' , async ( ) => {
164
- pointCloudObject = vi . fn ( ( ) => encodePCO ( testPCO ) ) ;
165
-
166
- const expected = [ testPCO ] ;
162
+ const expected = [ pco ] ;
167
163
168
164
await expect (
169
165
vision . getObjectPointClouds ( 'camera' )
170
166
) . resolves . toStrictEqual ( expected ) ;
171
167
} ) ;
172
168
} ) ;
169
+
170
+ describe ( 'Properties' , ( ) => {
171
+ it ( 'returns properties' , async ( ) => {
172
+ await expect ( vision . getProperties ( ) ) . resolves . toStrictEqual ( {
173
+ classificationsSupported : true ,
174
+ detectionsSupported : true ,
175
+ objectPointCloudsSupported : true ,
176
+ } ) ;
177
+ } ) ;
178
+ } ) ;
179
+
180
+ describe ( 'Capture All' , ( ) => {
181
+ it ( 'returns captured values' , async ( ) => {
182
+ await expect (
183
+ vision . captureAllFromCamera ( 'camera' , {
184
+ returnImage : true ,
185
+ returnClassifications : true ,
186
+ returnDetections : true ,
187
+ returnObjectPointClouds : true ,
188
+ } )
189
+ ) . resolves . toStrictEqual ( {
190
+ image : undefined ,
191
+ classifications : [ classification ] ,
192
+ detections : [ detection ] ,
193
+ objectPointClouds : [ pco ] ,
194
+ } ) ;
195
+ } ) ;
196
+ } ) ;
173
197
} ) ;
0 commit comments