gphotos-scraper
Version:
A tool to extract public url and metadata from shared album
69 lines (59 loc) • 1.87 kB
text/typescript
import { beforeEach, describe, expect, it, vi } from "vitest";
import { apiClient } from "./axiosClient";
import { getPhotos } from "./getPhotos";
vi.mock("./axiosClient", () => ({
apiClient: {
post: vi.fn(),
},
}));
describe("getPhotos", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("should return an empty array if parsing fails", async () => {
(apiClient.post as any).mockResolvedValue({ data: "invalid json" });
const photos = await getPhotos("test-key", ["id1"]);
expect(photos).toEqual([]);
});
it("should parse valid photo data correctly", async () => {
// RPCID must match "fDcn4b" from getPhotos.ts
// The structure inside the stringified JSON must match:
// [id, description, filename, createdAt, unknown, size, width, height]
const innerJson1 = JSON.stringify([
["Item1", null, "filename.jpg", 1234567890, null, 1024, 800, 600],
]);
const innerJson2 = JSON.stringify([
["Item2", null, "image.png", 1234567890, null, 2048, 1920, 1080],
]);
const mockResponseData =
`)]}'` +
JSON.stringify([
["w", "fDcn4b", innerJson1],
["w", "fDcn4b", innerJson2],
]);
(apiClient.post as any).mockResolvedValue({ data: mockResponseData });
const photos = await getPhotos("test-key", ["id1", "id2"]);
expect(photos).toHaveLength(2);
expect(photos[0]).toEqual({
id: "Item1",
url: "",
description: null,
filename: "filename.jpg",
createdAt: 1234567890,
size: 1024,
width: 800,
height: 600,
mimeType: "image/jpeg",
});
expect(photos[1].mimeType).toBe("image/png");
});
it("should ignore items with incorrect RPCID", async () => {
const mockResponseData = `)]}'
[
["wrongID", "[]"]
]`;
(apiClient.post as any).mockResolvedValue({ data: mockResponseData });
const photos = await getPhotos("test-key", ["id1"]);
expect(photos).toEqual([]);
});
});