Skip to content

Commit

Permalink
remove phash
Browse files Browse the repository at this point in the history
  • Loading branch information
tzvielwix committed Jan 29, 2025
1 parent 3c6e2cb commit fd678ee
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 292 deletions.
116 changes: 0 additions & 116 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@
"gm": "^1.25.0",
"ora": "^5.4.1",
"png-js": "^1.0.0",
"sharp": "^0.33.5",
"sharp-phash": "^2.2.0",
"winston": "^3.17.0"
}
}
23 changes: 5 additions & 18 deletions src/integration tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,11 @@ import { SnapshotComparator } from "@/utils/SnapshotComparator";

jest.mock("crypto");
jest.mock("fs");
jest.mock("gm", () => {
const resizeMock = jest.fn().mockReturnThis();
const writeMock = jest.fn(
(outputPath: string, callback: (error: Error | null) => void) => {
callback(null);
},
);

const gmMock = jest.fn(() => ({
resize: resizeMock,
write: writeMock,
}));

(gmMock as any).resizeMock = resizeMock;
(gmMock as any).writeMock = writeMock;

return gmMock;
});
jest.mock("../utils/ImageScaler", () => ({
downscaleImage: jest
.fn()
.mockImplementation((path: string) => Promise.resolve(path)),
}));

describe("Copilot Integration Tests", () => {
let mockedCachedSnapshotHash: SnapshotHashObject;
Expand Down
39 changes: 39 additions & 0 deletions src/utils/ImageScaler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import gm from "gm";
const MAX_PIXELS = 2000000;

export async function downscaleImage(imagePath: string): Promise<string> {
return new Promise<string>((resolve, reject) => {

Check failure on line 5 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
gm(imagePath)

Check failure on line 6 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎····.size(function·(err:·any,·size:·{·width:·number;·height:·number·}` with `.size(function·(⏎······err:·any,⏎······size:·{·width:·number;·height:·number·},⏎····`
.size(function (err: any, size: { width: number; height: number }) {
if (err) {

Check failure on line 8 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
console.error("Error getting image size:", err);
reject(err);
} else {

Check failure on line 11 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `········` with `······`
const originalWidth = size.width;
const originalHeight = size.height;
const originalTotalPixels = originalWidth * originalHeight;

if (originalTotalPixels <= MAX_PIXELS) {
resolve(imagePath);

Check failure on line 17 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
} else {
const aspectRatio = originalWidth / originalHeight;

Check failure on line 19 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
const newHeight = Math.sqrt(MAX_PIXELS / aspectRatio);

Check failure on line 20 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
const newWidth = newHeight * aspectRatio;

Check failure on line 21 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `············` with `··········`
const roundedWidth = Math.round(newWidth);

Check failure on line 22 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
const roundedHeight = Math.round(newHeight);

Check failure on line 23 in src/utils/ImageScaler.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `············` with `··········`

gm(imagePath)
.resize(roundedWidth, roundedHeight)
.write(imagePath, (err: any) => {
if (err) {
console.error("Error downscaling image:", err);
reject(err);
} else {
resolve(imagePath);
}
});
}
}
});
});
}
75 changes: 34 additions & 41 deletions src/utils/SnapshotManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SnapshotManager } from "./SnapshotManager";
import { TestingFrameworkDriver } from "@/types";
import { SnapshotComparator } from "./SnapshotComparator";
import { SnapshotComparator } from "@/utils/SnapshotComparator";
import crypto from "crypto";
import gm from "gm";
import { downscaleImage } from "./ImageScaler";

jest.mock("crypto", () => ({
createHash: jest.fn().mockReturnValue({
Expand All @@ -11,28 +11,15 @@ jest.mock("crypto", () => ({
}),
}));

jest.mock("gm", () => {
const resizeMock: jest.Mock = jest.fn().mockReturnThis();
const writeMock = jest.fn(
(outputPath: string, callback: (error: Error | null) => void) => {
callback(null);
},
);

const gmMock = jest.fn(() => ({
resize: resizeMock,
write: writeMock,
}));

(gmMock as any).resizeMock = resizeMock;
(gmMock as any).writeMock = writeMock;
jest.mock("./ImageScaler", () => ({
downscaleImage: jest
.fn()
.mockImplementation((path: string) => Promise.resolve(path)),
}));

return gmMock;
});
const gmMock = gm as unknown as jest.Mock & {
resizeMock: jest.MockedFunction<any>;
writeMock: jest.MockedFunction<any>;
};
const downscaleImageMock = downscaleImage as jest.MockedFunction<
typeof downscaleImage
>;

describe("SnapshotManager", () => {
let mockDriver: jest.Mocked<TestingFrameworkDriver>;
Expand Down Expand Up @@ -83,7 +70,10 @@ describe("SnapshotManager", () => {

mockSnapshotComparator.generateHashes.mockImplementation(
async (snapshot: string) => {
return { BlockHash: imagePathToHash[snapshot] };
return {
BlockHash: imagePathToHash[snapshot],
Phash: imagePathToHash[snapshot],
};
},
);

Expand All @@ -95,10 +85,7 @@ describe("SnapshotManager", () => {

const capturePromise = snapshotManager.captureSnapshotImage(100);

// Fast-forward time to trigger the first two different snapshots
await jest.advanceTimersByTimeAsync(200);

// Fast-forward to get the stable snapshot
await jest.advanceTimersByTimeAsync(100);

const result = await capturePromise;
Expand All @@ -108,14 +95,19 @@ describe("SnapshotManager", () => {
expect(mockSnapshotComparator.generateHashes).toHaveBeenCalledTimes(4);
expect(mockSnapshotComparator.compareSnapshot).toHaveBeenCalledTimes(2);

// Verify that gm was called with correct parameters
expect(gmMock).toHaveBeenCalledTimes(3);
expect(gmMock).toHaveBeenNthCalledWith(1, "/path/to/snapshot1.png");
expect(gmMock).toHaveBeenNthCalledWith(2, "/path/to/snapshot2.png");
expect(gmMock).toHaveBeenNthCalledWith(3, "/path/to/snapshot2.png");
expect(gmMock.resizeMock).toHaveBeenCalledTimes(3);
expect(gmMock.resizeMock).toHaveBeenCalledWith(800);
expect(gmMock.writeMock).toHaveBeenCalledTimes(3);
expect(downscaleImageMock).toHaveBeenCalledTimes(3);
expect(downscaleImageMock).toHaveBeenNthCalledWith(
1,
"/path/to/snapshot1.png",
);
expect(downscaleImageMock).toHaveBeenNthCalledWith(
2,
"/path/to/snapshot2.png",
);
expect(downscaleImageMock).toHaveBeenNthCalledWith(
3,
"/path/to/snapshot2.png",
);
});

it("should return last snapshot when timeout is reached", async () => {
Expand All @@ -140,7 +132,10 @@ describe("SnapshotManager", () => {

mockSnapshotComparator.generateHashes.mockImplementation(
async (snapshot: string) => {
return { BlockHash: imagePathToHash[snapshot] };
return {
BlockHash: imagePathToHash[snapshot],
Phash: imagePathToHash[snapshot],
};
},
);

Expand All @@ -156,10 +151,8 @@ describe("SnapshotManager", () => {
expect(result).toBe("/path/to/snapshot3.png");
expect(mockDriver.captureSnapshotImage).toHaveBeenCalledTimes(3);

// Verify that gm was called
expect(gmMock).toHaveBeenCalledTimes(3);
expect(gmMock.resizeMock).toHaveBeenCalledTimes(3);
expect(gmMock.writeMock).toHaveBeenCalledTimes(3);
// Verify that downscaleImage was called
expect(downscaleImageMock).toHaveBeenCalledTimes(3);
});

it("should handle undefined snapshots", async () => {
Expand All @@ -170,7 +163,7 @@ describe("SnapshotManager", () => {
expect(result).toBeUndefined();
expect(mockDriver.captureSnapshotImage).toHaveBeenCalledTimes(1);
expect(mockSnapshotComparator.generateHashes).not.toHaveBeenCalled();
expect(gmMock).not.toHaveBeenCalled();
expect(downscaleImageMock).not.toHaveBeenCalled();
});
});

Expand Down
Loading

0 comments on commit fd678ee

Please sign in to comment.