@@ -15,6 +15,9 @@ import {
15
15
} from "./provider" ;
16
16
import { OptionalSearchParameters } from "./optionalsearchparameters.class" ;
17
17
import { NoopLogProvider } from "./provider/log/noop-log-provider.class" ;
18
+ import { TextQuery , WindowQuery } from "./query.class" ;
19
+ import { TextFinderInterface } from "./provider/text-finder.interface" ;
20
+ import { WindowFinderInterface } from "./provider/window-finder.interface" ;
18
21
19
22
jest . mock ( "jimp" , ( ) => { } ) ;
20
23
@@ -49,6 +52,104 @@ beforeEach(() => {
49
52
50
53
describe ( "Screen." , ( ) => {
51
54
describe ( "find" , ( ) => {
55
+ describe ( "queries" , ( ) => {
56
+ it ( "should choose the correct finder implementation for images" , async ( ) => {
57
+ // GIVEN
58
+ const matchResult = new MatchResult ( 0.99 , searchRegion ) ;
59
+
60
+ const SUT = new ScreenClass ( providerRegistryMock ) ;
61
+ const needle = new Image (
62
+ 100 ,
63
+ 100 ,
64
+ Buffer . from ( [ ] ) ,
65
+ 3 ,
66
+ "needle_image" ,
67
+ 4 ,
68
+ 100 * 4
69
+ ) ;
70
+ const needlePromise = Promise . resolve ( needle ) ;
71
+
72
+ const findMatchMock = jest . fn ( ( ) => Promise . resolve ( matchResult ) ) ;
73
+ providerRegistryMock . getImageFinder = jest . fn ( ( ) =>
74
+ mockPartial < ImageFinderInterface > ( {
75
+ findMatch : findMatchMock ,
76
+ } )
77
+ ) ;
78
+ providerRegistryMock . getLogProvider = ( ) => new NoopLogProvider ( ) ;
79
+
80
+ // WHEN
81
+ await SUT . find ( needlePromise ) ;
82
+
83
+ // THEN
84
+ expect ( findMatchMock ) . toHaveBeenCalledTimes ( 1 ) ;
85
+ } ) ;
86
+
87
+ it ( "should choose the correct finder implementation for window queries" , async ( ) => {
88
+ // GIVEN
89
+ const SUT = new ScreenClass ( providerRegistryMock ) ;
90
+ const needle : WindowQuery = {
91
+ id : "window-query" ,
92
+ type : "window" ,
93
+ by : {
94
+ title : "query" ,
95
+ } ,
96
+ } ;
97
+
98
+ const findMatchMock = jest . fn ( ( ) => Promise . resolve ( 1234 ) ) ;
99
+ providerRegistryMock . getWindowFinder = jest . fn ( ( ) =>
100
+ mockPartial < WindowFinderInterface > ( {
101
+ findMatch : findMatchMock ,
102
+ } )
103
+ ) ;
104
+ providerRegistryMock . getLogProvider = ( ) => new NoopLogProvider ( ) ;
105
+
106
+ // WHEN
107
+ await SUT . find ( needle ) ;
108
+
109
+ // THEN
110
+ expect ( findMatchMock ) . toHaveBeenCalledTimes ( 1 ) ;
111
+ } ) ;
112
+
113
+ it . each < TextQuery > ( [
114
+ {
115
+ id : "dummy" ,
116
+ type : "text" ,
117
+ by : {
118
+ word : "dummy-query" ,
119
+ } ,
120
+ } ,
121
+ {
122
+ id : "dummy" ,
123
+ type : "text" ,
124
+ by : {
125
+ line : "dummy-query" ,
126
+ } ,
127
+ } ,
128
+ ] ) (
129
+ "should choose the correct finder implementation for text queries" ,
130
+ async ( needle : TextQuery ) => {
131
+ // GIVEN
132
+ const matchResult = new MatchResult ( 0.99 , searchRegion ) ;
133
+
134
+ const SUT = new ScreenClass ( providerRegistryMock ) ;
135
+
136
+ const findMatchMock = jest . fn ( ( ) => Promise . resolve ( matchResult ) ) ;
137
+ providerRegistryMock . getTextFinder = jest . fn ( ( ) =>
138
+ mockPartial < TextFinderInterface > ( {
139
+ findMatch : findMatchMock ,
140
+ } )
141
+ ) ;
142
+ providerRegistryMock . getLogProvider = ( ) => new NoopLogProvider ( ) ;
143
+
144
+ // WHEN
145
+ await SUT . find ( needle ) ;
146
+
147
+ // THEN
148
+ expect ( findMatchMock ) . toHaveBeenCalledTimes ( 1 ) ;
149
+ }
150
+ ) ;
151
+ } ) ;
152
+
52
153
it ( "should resolve with sufficient confidence." , async ( ) => {
53
154
// GIVEN
54
155
const matchResult = new MatchResult ( 0.99 , searchRegion ) ;
@@ -408,6 +509,103 @@ describe("Screen.", () => {
408
509
} ) ;
409
510
410
511
describe ( "findAll" , ( ) => {
512
+ describe ( "queries" , ( ) => {
513
+ it ( "should choose the correct finder implementation for images" , async ( ) => {
514
+ // GIVEN
515
+ const matchResult = new MatchResult ( 0.99 , searchRegion ) ;
516
+
517
+ const SUT = new ScreenClass ( providerRegistryMock ) ;
518
+ const needle = new Image (
519
+ 100 ,
520
+ 100 ,
521
+ Buffer . from ( [ ] ) ,
522
+ 3 ,
523
+ "needle_image" ,
524
+ 4 ,
525
+ 100 * 4
526
+ ) ;
527
+ const needlePromise = Promise . resolve ( needle ) ;
528
+
529
+ const findMatchMock = jest . fn ( ( ) => Promise . resolve ( [ matchResult ] ) ) ;
530
+ providerRegistryMock . getImageFinder = jest . fn ( ( ) =>
531
+ mockPartial < ImageFinderInterface > ( {
532
+ findMatches : findMatchMock ,
533
+ } )
534
+ ) ;
535
+ providerRegistryMock . getLogProvider = ( ) => new NoopLogProvider ( ) ;
536
+
537
+ // WHEN
538
+ await SUT . findAll ( needlePromise ) ;
539
+
540
+ // THEN
541
+ expect ( findMatchMock ) . toHaveBeenCalledTimes ( 1 ) ;
542
+ } ) ;
543
+
544
+ it ( "should choose the correct finder implementation for window queries" , async ( ) => {
545
+ // GIVEN
546
+ const SUT = new ScreenClass ( providerRegistryMock ) ;
547
+ const needle : WindowQuery = {
548
+ id : "window-query" ,
549
+ type : "window" ,
550
+ by : {
551
+ title : "query" ,
552
+ } ,
553
+ } ;
554
+
555
+ const findMatchMock = jest . fn ( ( ) => Promise . resolve ( [ 1234 ] ) ) ;
556
+ providerRegistryMock . getWindowFinder = jest . fn ( ( ) =>
557
+ mockPartial < WindowFinderInterface > ( {
558
+ findMatches : findMatchMock ,
559
+ } )
560
+ ) ;
561
+ providerRegistryMock . getLogProvider = ( ) => new NoopLogProvider ( ) ;
562
+
563
+ // WHEN
564
+ await SUT . findAll ( needle ) ;
565
+
566
+ // THEN
567
+ expect ( findMatchMock ) . toHaveBeenCalledTimes ( 1 ) ;
568
+ } ) ;
569
+
570
+ it . each < TextQuery > ( [
571
+ {
572
+ id : "dummy" ,
573
+ type : "text" ,
574
+ by : {
575
+ word : "dummy-query" ,
576
+ } ,
577
+ } ,
578
+ {
579
+ id : "dummy" ,
580
+ type : "text" ,
581
+ by : {
582
+ line : "dummy-query" ,
583
+ } ,
584
+ } ,
585
+ ] ) (
586
+ "should choose the correct finder implementation for text queries" ,
587
+ async ( needle : TextQuery ) => {
588
+ // GIVEN
589
+ const matchResult = new MatchResult ( 0.99 , searchRegion ) ;
590
+
591
+ const SUT = new ScreenClass ( providerRegistryMock ) ;
592
+
593
+ const findMatchMock = jest . fn ( ( ) => Promise . resolve ( [ matchResult ] ) ) ;
594
+ providerRegistryMock . getTextFinder = jest . fn ( ( ) =>
595
+ mockPartial < TextFinderInterface > ( {
596
+ findMatches : findMatchMock ,
597
+ } )
598
+ ) ;
599
+ providerRegistryMock . getLogProvider = ( ) => new NoopLogProvider ( ) ;
600
+
601
+ // WHEN
602
+ await SUT . findAll ( needle ) ;
603
+
604
+ // THEN
605
+ expect ( findMatchMock ) . toHaveBeenCalledTimes ( 1 ) ;
606
+ }
607
+ ) ;
608
+ } ) ;
411
609
it ( "should call registered hook before resolve" , async ( ) => {
412
610
// GIVEN
413
611
const matchResult = new MatchResult ( 0.99 , searchRegion ) ;
0 commit comments