Skip to content

Commit d8e846a

Browse files
committed
Update tests
1 parent 85776c9 commit d8e846a

6 files changed

+70
-67
lines changed

Diff for: index.e2e.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const close = async () => {
4444
describe("E2E screen test", () => {
4545
it("should throw on invalid images", async () => {
4646
jest.setTimeout(30000);
47-
await expect(screen.find("mouse.png")).rejects.toContain("Failed to load image");
47+
await expect(screen.find("mouse.png")).rejects.toHaveProperty("message", "Failed to load image from '/home/runner/work/nut.js/nut.js/mouse.png'");
4848
});
4949
});
5050

Diff for: lib/adapter/vision.adapter.class.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ describe("VisionAdapter class", () => {
100100
const request = new MatchRequest(
101101
new Image(100, 100, new ArrayBuffer(0), 3),
102102
"foo",
103+
Buffer.from([]),
103104
new Region(0, 0, 100, 100),
104105
0.99,
105106
true);

Diff for: lib/match-request.class.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe("MatchRequest", () => {
99
new ArrayBuffer(0), 3
1010
),
1111
"foo",
12+
Buffer.from([]),
1213
new Region(
1314
0,
1415
0,

Diff for: lib/provider/opencv/determine-searchregion.function.spec.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ describe("determineSearchRegion", () => {
1313
scaleY: 2.0
1414
}
1515
});
16-
const needlePath = "/path/to/needle";
16+
const needleId = "/path/to/needle";
17+
const needleData = Buffer.from([]);
1718
const inputSearchRegion = new Region(0, 0, 100, 100);
1819
const expectedSearchRegion = new Region(0, 0, 150, 200);
1920

2021
const matchRequest = new MatchRequest(
2122
imageMock,
22-
needlePath,
23+
needleId,
24+
needleData,
2325
inputSearchRegion,
2426
0.99
2527
);
@@ -40,13 +42,15 @@ describe("determineSearchRegion", () => {
4042
scaleY
4143
}
4244
});
43-
const needlePath = "/path/to/needle";
45+
const needleId = "/path/to/needle";
46+
const needleData = Buffer.from([]);
4447
const inputSearchRegion = new Region(0, 0, 100, 100);
4548
const expectedSearchRegion = new Region(0, 0, 100, 100);
4649

4750
const matchRequest = new MatchRequest(
4851
imageMock,
49-
needlePath,
52+
needleId,
53+
needleData,
5054
inputSearchRegion,
5155
0.99
5256
);

Diff for: lib/provider/opencv/template-matching-finder.class.spec.ts

+15-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from "path";
2-
import {Image} from "../../image.class";
2+
import { promises as fs } from 'fs'
33
import {MatchRequest} from "../../match-request.class";
44
import {Region} from "../../region.class";
55
import {ImageReader} from "./image-reader.class";
@@ -11,12 +11,13 @@ describe("Template-matching finder", () => {
1111
const imageLoader = new ImageReader();
1212
const SUT = new TemplateMatchingFinder();
1313
const haystackPath = path.resolve(__dirname, "./__mocks__/mouse.png");
14-
const needlePath = path.resolve(__dirname, "./__mocks__/needle.png");
14+
const needleId = path.resolve(__dirname, "./__mocks__/needle.png");
15+
const needleData = await fs.readFile(needleId);
1516
const haystack = await imageLoader.load(haystackPath);
16-
const needle = await imageLoader.load(needlePath);
17+
const needle = await imageLoader.load(needleId);
1718
const minConfidence = 0.99;
1819
const searchRegion = new Region(0, 0, haystack.width, haystack.height);
19-
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
20+
const matchRequest = new MatchRequest(haystack, needleId, needleData, searchRegion, minConfidence);
2021
const expectedResult = new Region(16, 31, needle.width, needle.height);
2122

2223
// WHEN
@@ -32,12 +33,13 @@ describe("Template-matching finder", () => {
3233
const imageLoader = new ImageReader();
3334
const SUT = new TemplateMatchingFinder();
3435
const haystackPath = path.resolve(__dirname, "./__mocks__/mouse.png");
35-
const needlePath = path.resolve(__dirname, "./__mocks__/needle.png");
36+
const needleId = path.resolve(__dirname, "./__mocks__/needle.png");
37+
const needleData = await fs.readFile(needleId);
3638
const haystack = await imageLoader.load(haystackPath);
37-
const needle = await imageLoader.load(needlePath);
39+
const needle = await imageLoader.load(needleId);
3840
const minConfidence = 0.99;
3941
const searchRegion = new Region(10, 20, 140, 100);
40-
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
42+
const matchRequest = new MatchRequest(haystack, needleId, needleData, searchRegion, minConfidence);
4143
const expectedResult = new Region(6, 11, needle.width, needle.height);
4244

4345
// WHEN
@@ -53,11 +55,12 @@ describe("Template-matching finder", () => {
5355
const imageLoader = new ImageReader();
5456
const SUT = new TemplateMatchingFinder();
5557
const haystackPath = path.resolve(__dirname, "./__mocks__/downloads.png");
56-
const needlePath = path.resolve(__dirname, "./__mocks__/coverage.png");
58+
const needleId = path.resolve(__dirname, "./__mocks__/coverage.png");
59+
const needleData = await fs.readFile(needleId);
5760
const haystack = await imageLoader.load(haystackPath);
5861
const minConfidence = 0.99;
5962
const searchRegion = new Region(0, 0, 320, 72);
60-
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
63+
const matchRequest = new MatchRequest(haystack, needleId, needleData, searchRegion, minConfidence);
6164
const expectedRejection = new RegExp(`^No match with required confidence ${minConfidence}. Best match: \\d.\\d* at \\(\\d*, \\d*, \\d*, \\d*\\)$`)
6265

6366
// WHEN
@@ -68,37 +71,17 @@ describe("Template-matching finder", () => {
6871
.toThrowError(expectedRejection);
6972
});
7073

71-
it("findMatch should throw on invalid image paths", async () => {
72-
// GIVEN
73-
const imageLoader = new ImageReader();
74-
const SUT = new TemplateMatchingFinder();
75-
const pathToNeedle = path.resolve(__dirname, "./__mocks__/mouse.png");
76-
const pathToHaystack = "./__mocks__/foo.png";
77-
const needle = await imageLoader.load(pathToNeedle);
78-
const minConfidence = 0.99;
79-
const searchRegion = new Region(0, 0, 100, 100);
80-
const haystack = new Image(needle.width, needle.height, needle.data, 3);
81-
const matchRequest = new MatchRequest(haystack, pathToHaystack, searchRegion, minConfidence);
82-
83-
// WHEN
84-
const result = SUT.findMatch(matchRequest);
85-
86-
// THEN
87-
await expect(result)
88-
.rejects
89-
.toThrowError(`Failed to load ${pathToHaystack}. Reason: 'Failed to load image from '${pathToHaystack}''.`);
90-
});
91-
9274
it("findMatch should reject, if needle was way lager than the haystack", async () => {
9375
// GIVEN
9476
const imageLoader = new ImageReader();
9577
const SUT = new TemplateMatchingFinder();
9678
const haystackPath = path.resolve(__dirname, "./__mocks__/mouse.png");
97-
const needlePath = path.resolve(__dirname, "./__mocks__/fat-needle.png");
79+
const needleId = path.resolve(__dirname, "./__mocks__/fat-needle.png");
80+
const needleData = await fs.readFile(needleId);
9881
const haystack = await imageLoader.load(haystackPath);
9982
const minConfidence = 0.99;
10083
const searchRegion = new Region(0, 0, haystack.width, haystack.height);
101-
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
84+
const matchRequest = new MatchRequest(haystack, needleId, needleData, searchRegion, minConfidence);
10285
const expectedRejection = new Error("The provided image sample is larger than the provided search region")
10386

10487
// WHEN

Diff for: lib/screen.class.spec.ts

+44-30
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ describe("Screen.", () => {
3636
const visionAdapterMock = new VisionAdapter();
3737

3838
const SUT = new Screen(visionAdapterMock);
39-
const imagePath = "test/path/to/image.png";
40-
await expect(SUT.find(imagePath)).resolves.toEqual(matchResult.location);
39+
const imageId = "test/path/to/image.png";
40+
const imageData = Buffer.from([]);
41+
await expect(SUT.findImage({ id: imageId, data: imageData })).resolves.toEqual(matchResult.location);
4142
const matchRequest = new MatchRequest(
4243
expect.any(Image),
43-
join(cwd(), imagePath),
44+
imageId,
45+
imageData,
4446
searchRegion,
4547
SUT.config.confidence,
4648
true);
@@ -56,9 +58,10 @@ describe("Screen.", () => {
5658

5759
const SUT = new Screen(visionAdapterMock);
5860
const testCallback = jest.fn(() => Promise.resolve());
59-
const imagePath = "test/path/to/image.png";
60-
SUT.on(imagePath, testCallback);
61-
await SUT.find(imagePath);
61+
const imageId = "test/path/to/image.png";
62+
const imageData = Buffer.from([]);
63+
SUT.on(imageId, testCallback);
64+
await SUT.findImage({ id: imageId, data: imageData });
6265
expect(testCallback).toBeCalledTimes(1);
6366
expect(testCallback).toBeCalledWith(matchResult);
6467
});
@@ -73,10 +76,11 @@ describe("Screen.", () => {
7376
const SUT = new Screen(visionAdapterMock);
7477
const testCallback = jest.fn(() => Promise.resolve());
7578
const secondCallback = jest.fn(() => Promise.resolve());
76-
const imagePath = "test/path/to/image.png";
77-
SUT.on(imagePath, testCallback);
78-
SUT.on(imagePath, secondCallback);
79-
await SUT.find(imagePath);
79+
const imageId = "test/path/to/image.png";
80+
const imageData = Buffer.from([]);
81+
SUT.on(imageId, testCallback);
82+
SUT.on(imageId, secondCallback);
83+
await SUT.findImage({ id: imageId, data: imageData });
8084
for (const callback of [testCallback, secondCallback]) {
8185
expect(callback).toBeCalledTimes(1);
8286
expect(callback).toBeCalledWith(matchResult);
@@ -93,10 +97,11 @@ describe("Screen.", () => {
9397
const visionAdapterMock = new VisionAdapter();
9498

9599
const SUT = new Screen(visionAdapterMock);
96-
const imagePath = "test/path/to/image.png";
97-
await expect(SUT.find(imagePath))
100+
const imageId = "test/path/to/image.png";
101+
const imageData = Buffer.from([]);
102+
await expect(SUT.findImage({ id: imageId, data: imageData }))
98103
.rejects
99-
.toEqual(`No match for ${imagePath}. Required: ${SUT.config.confidence}, given: ${matchResult.confidence}`);
104+
.toEqual(`No match for ${imageId}. Required: ${SUT.config.confidence}, given: ${matchResult.confidence}`);
100105
});
101106

102107
it("should reject when search fails.", async () => {
@@ -108,10 +113,11 @@ describe("Screen.", () => {
108113
const visionAdapterMock = new VisionAdapter();
109114

110115
const SUT = new Screen(visionAdapterMock);
111-
const imagePath = "test/path/to/image.png";
112-
await expect(SUT.find(imagePath))
116+
const imageId = "test/path/to/image.png";
117+
const imageData = Buffer.from([]);
118+
await expect(SUT.findImage({ id: imageId, data: imageData }))
113119
.rejects
114-
.toEqual(`Searching for ${imagePath} failed. Reason: '${rejectionReason}'`);
120+
.toEqual(`Searching for ${imageId} failed. Reason: '${rejectionReason}'`);
115121
});
116122

117123
it("should override default confidence value with parameter.", async () => {
@@ -126,12 +132,14 @@ describe("Screen.", () => {
126132

127133
const SUT = new Screen(visionAdapterMock);
128134

129-
const imagePath = "test/path/to/image.png";
135+
const imageId = "test/path/to/image.png";
136+
const imageData = Buffer.from([]);
130137
const parameters = new LocationParameters(undefined, minMatch);
131-
await expect(SUT.find(imagePath, parameters)).resolves.toEqual(matchResult.location);
138+
await expect(SUT.findImage({ id: imageId, data: imageData }, parameters)).resolves.toEqual(matchResult.location);
132139
const matchRequest = new MatchRequest(
133140
expect.any(Image),
134-
join(cwd(), imagePath),
141+
imageId,
142+
imageData,
135143
searchRegion,
136144
minMatch,
137145
true);
@@ -147,17 +155,19 @@ describe("Screen.", () => {
147155
});
148156
const visionAdapterMock = new VisionAdapter();
149157
const SUT = new Screen(visionAdapterMock);
150-
const imagePath = "test/path/to/image.png";
158+
const imageId = "test/path/to/image.png";
159+
const imageData = Buffer.from([]);
151160
const parameters = new LocationParameters(customSearchRegion);
152161
const expectedMatchRequest = new MatchRequest(
153162
expect.any(Image),
154-
join(cwd(), imagePath),
163+
imageId,
164+
imageData,
155165
customSearchRegion,
156166
SUT.config.confidence,
157167
true);
158168

159169
// WHEN
160-
await SUT.find(imagePath, parameters);
170+
await SUT.findImage({ id: imageId, data: imageData }, parameters);
161171

162172
// THEN
163173
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(expectedMatchRequest);
@@ -171,17 +181,19 @@ describe("Screen.", () => {
171181
});
172182
const visionAdapterMock = new VisionAdapter();
173183
const SUT = new Screen(visionAdapterMock);
174-
const imagePath = "test/path/to/image.png";
184+
const imageId = "test/path/to/image.png";
185+
const imageData = Buffer.from([]);
175186
const parameters = new LocationParameters(searchRegion, undefined, false);
176187
const expectedMatchRequest = new MatchRequest(
177188
expect.any(Image),
178-
join(cwd(), imagePath),
189+
imageId,
190+
imageData,
179191
searchRegion,
180192
SUT.config.confidence,
181193
false);
182194

183195
// WHEN
184-
await SUT.find(imagePath, parameters);
196+
await SUT.findImage({ id: imageId, data: imageData }, parameters);
185197

186198
// THEN
187199
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(expectedMatchRequest);
@@ -197,17 +209,19 @@ describe("Screen.", () => {
197209
});
198210
const visionAdapterMock = new VisionAdapter();
199211
const SUT = new Screen(visionAdapterMock);
200-
const imagePath = "test/path/to/image.png";
212+
const imageId = "test/path/to/image.png";
213+
const imageData = Buffer.from([]);
201214
const parameters = new LocationParameters(customSearchRegion, minMatch);
202215
const expectedMatchRequest = new MatchRequest(
203216
expect.any(Image),
204-
join(cwd(), imagePath),
217+
imageId,
218+
imageData,
205219
customSearchRegion,
206220
minMatch,
207221
true);
208222

209223
// WHEN
210-
await SUT.find(imagePath, parameters);
224+
await SUT.findImage({ id: imageId, data: imageData }, parameters);
211225

212226
// THEN
213227
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(expectedMatchRequest);
@@ -261,8 +275,8 @@ describe("Screen.", () => {
261275
const SUT = new Screen(new VisionAdapter());
262276

263277
// WHEN
264-
const matchRegion = await SUT.find(
265-
"test/path/to/image.png",
278+
const matchRegion = await SUT.findImage(
279+
{ id: "test/path/to/image.png", data: Buffer.from([]) },
266280
{
267281
searchRegion: limitedSearchRegion
268282
});

0 commit comments

Comments
 (0)