Skip to content

Maintenance/340/refine types #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/image.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ afterEach(() => {

describe("Image class", () => {
it("should return alphachannel = true for > 3 channels", () => {
const SUT = new Image(200, 200, 123, 4, "id");
const SUT = new Image(200, 200, Buffer.from([123]), 4, "id");
expect(SUT.hasAlphaChannel).toBeTruthy();
});

it("should return alphachannel = false for <= 3 channels", () => {
const SUT = new Image(200, 200, 123, 3, "id");
const SUT = new Image(200, 200, Buffer.from([123]), 3, "id");
expect(SUT.hasAlphaChannel).toBeFalsy();
});
it("should return alphachannel = false for <= 3 channels", () => {
const SUT = new Image(200, 200, 123, 2, "id");
const SUT = new Image(200, 200, Buffer.from([123]), 2, "id");
expect(SUT.hasAlphaChannel).toBeFalsy();
});
it("should return alphachannel = false for <= 3 channels", () => {
const SUT = new Image(200, 200, 123, 1, "id");
const SUT = new Image(200, 200, Buffer.from([123]), 1, "id");
expect(SUT.hasAlphaChannel).toBeFalsy();
});

it("should throw for <= 0 channels", () => {
expect(() => new Image(200, 200, 123, 0, "id")).toThrowError("Channel <= 0");
expect(() => new Image(200, 200, Buffer.from([123]), 0, "id")).toThrowError("Channel <= 0");
});

it("should have a default pixel density of 1.0", () => {
const SUT = new Image(200, 200, 123, 1, "id");
const SUT = new Image(200, 200, Buffer.from([123]), 1, "id");
expect(SUT.pixelDensity).toEqual({scaleX: 1.0, scaleY: 1.0});
});

Expand Down
2 changes: 1 addition & 1 deletion lib/image.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Image {
constructor(
public readonly width: number,
public readonly height: number,
public readonly data: any,
public readonly data: Buffer,
public readonly channels: number,
public readonly id: string,
public readonly colorMode: ColorMode = ColorMode.BGR,
Expand Down
4 changes: 2 additions & 2 deletions lib/match-request.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ describe("MatchRequest", () => {
new Image(
100,
100,
new ArrayBuffer(0),
Buffer.from([]),
3,
"haystack_image"
),
new Image(
100,
100,
new ArrayBuffer(0),
Buffer.from([]),
3,
"needle_image"
),
Expand Down
2 changes: 1 addition & 1 deletion lib/provider/image-writer.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Image} from "../image.class";
import {DataSinkInterface} from "./data-sink.interface";

export interface ImageWriterParameters {
data: Image,
image: Image,
path: string
}

Expand Down
2 changes: 1 addition & 1 deletion lib/provider/io/jimp-image-writer.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Jimp image writer', () => {
const SUT = new ImageWriter();

// WHEN
await SUT.store({data: outputFile, path: outputFileName});
await SUT.store({image: outputFile, path: outputFileName});

// THEN
expect(scanMock).toHaveBeenCalledTimes(1)
Expand Down
2 changes: 1 addition & 1 deletion lib/provider/io/jimp-image-writer.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {imageToJimp} from "./imageToJimp.function";
export default class implements ImageWriter {
store(parameters: ImageWriterParameters): Promise<void> {
return new Promise((resolve, reject) => {
const jimpImage = imageToJimp(parameters.data);
const jimpImage = imageToJimp(parameters.image);
jimpImage
.writeAsync(parameters.path)
.then(_ => resolve())
Expand Down
14 changes: 8 additions & 6 deletions lib/screen.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Region} from "./region.class";
import {ScreenClass} from "./screen.class";
import {mockPartial} from "sneer";
import {ProviderRegistry} from "./provider/provider-registry.class";
import {ImageFinderInterface, ImageWriter, ScreenProviderInterface} from "./provider";
import {ImageFinderInterface, ImageWriter, ImageWriterParameters, ScreenProviderInterface} from "./provider";
import {OptionalSearchParameters} from "./optionalsearchparameters.class";

jest.mock('jimp', () => {
Expand All @@ -19,7 +19,7 @@ const providerRegistryMock = mockPartial<ProviderRegistry>({
getScreen(): ScreenProviderInterface {
return mockPartial<ScreenProviderInterface>({
grabScreenRegion(): Promise<Image> {
return Promise.resolve(new Image(searchRegion.width, searchRegion.height, new ArrayBuffer(0), 3, "needle_image"));
return Promise.resolve(new Image(searchRegion.width, searchRegion.height, Buffer.from([]), 3, "needle_image"));
},
screenSize(): Promise<Region> {
return Promise.resolve(searchRegion);
Expand Down Expand Up @@ -614,7 +614,7 @@ describe("Screen.", () => {
describe("capture", () => {
it("should capture the whole screen and save image", async () => {
// GIVEN
const screenshot = mockPartial<Image>({data: "pretty pretty image"});
const screenshot = mockPartial<Image>({data: Buffer.from([])});
const grabScreenMock = jest.fn(() => Promise.resolve(screenshot));
const saveImageMock = jest.fn();
providerRegistryMock.getScreen = jest.fn(() => mockPartial<ScreenProviderInterface>({
Expand All @@ -627,21 +627,22 @@ describe("Screen.", () => {
const SUT = new ScreenClass(providerRegistryMock);
const imageName = "foobar.png"
const expectedImagePath = join(cwd(), imageName)
const expectedData: ImageWriterParameters = {image: screenshot, path: expectedImagePath}

// WHEN
const imagePath = await SUT.capture(imageName)

// THEN
expect(imagePath).toBe(expectedImagePath)
expect(grabScreenMock).toHaveBeenCalled()
expect(saveImageMock).toHaveBeenCalledWith({data: screenshot, path: expectedImagePath})
expect(saveImageMock).toHaveBeenCalledWith(expectedData);
});
})

describe("captureRegion", () => {
it("should capture the specified region of the screen and save image", async () => {
// GIVEN
const screenshot = mockPartial<Image>({data: "pretty partial image"});
const screenshot = mockPartial<Image>({data: Buffer.from([])});
const regionToCapture = mockPartial<Region>({top: 42, left: 9, height: 10, width: 3.14159265359})
const grabScreenMock = jest.fn(() => Promise.resolve(screenshot));
const saveImageMock = jest.fn();
Expand All @@ -655,14 +656,15 @@ describe("Screen.", () => {
const SUT = new ScreenClass(providerRegistryMock);
const imageName = "foobar.png"
const expectedImagePath = join(cwd(), imageName)
const expectedData: ImageWriterParameters = {image: screenshot, path: expectedImagePath}

// WHEN
const imagePath = await SUT.captureRegion(imageName, regionToCapture)

// THEN
expect(imagePath).toBe(expectedImagePath)
expect(grabScreenMock).toHaveBeenCalledWith(regionToCapture)
expect(saveImageMock).toHaveBeenCalledWith({data: screenshot, path: expectedImagePath})
expect(saveImageMock).toHaveBeenCalledWith(expectedData);
});
});
});
2 changes: 1 addition & 1 deletion lib/screen.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export class ScreenClass {
prefix: fileNamePrefix,
type: fileFormat,
});
await this.providerRegistry.getImageWriter().store({data: image, path: outputPath})
await this.providerRegistry.getImageWriter().store({image, path: outputPath})
return outputPath;
}
}
2 changes: 1 addition & 1 deletion lib/screen.colorAt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const providerRegistryMock = mockPartial<ProviderRegistry>({
getScreen(): ScreenProviderInterface {
return mockPartial<ScreenProviderInterface>({
grabScreenRegion(): Promise<Image> {
return Promise.resolve(new Image(searchRegion.width, searchRegion.height, new ArrayBuffer(0), 3, "needle_image"));
return Promise.resolve(new Image(searchRegion.width, searchRegion.height, Buffer.from([]), 3, "needle_image"));
},
screenSize(): Promise<Region> {
return Promise.resolve(searchRegion);
Expand Down