2026-04-27 00:57:13 +08:00
|
|
|
import { Buffer } from "node:buffer";
|
|
|
|
|
|
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
|
|
vi.mock("electron", () => ({
|
|
|
|
|
nativeImage: {
|
|
|
|
|
createFromBuffer: () => ({
|
|
|
|
|
isEmpty: () => false,
|
|
|
|
|
getSize: () => ({ width: 1, height: 1 }),
|
|
|
|
|
crop: () => ({
|
|
|
|
|
isEmpty: () => false,
|
|
|
|
|
getSize: () => ({ width: 1, height: 1 }),
|
|
|
|
|
toPNG: () => Buffer.from([]),
|
|
|
|
|
}),
|
|
|
|
|
toPNG: () => Buffer.from([]),
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../transport/api-client", () => ({
|
2026-04-27 12:21:02 +08:00
|
|
|
fetchDesktopApiURL: vi.fn(),
|
2026-04-27 00:57:13 +08:00
|
|
|
resolveDesktopApiURL: (path: string) => `http://127.0.0.1:8080${path}`,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
buildAssetURLCandidates,
|
|
|
|
|
getCenteredCropRect,
|
|
|
|
|
} from "./media-image";
|
|
|
|
|
|
|
|
|
|
describe("media image helpers", () => {
|
2026-04-27 12:21:02 +08:00
|
|
|
it("resolves public asset URLs through public and authenticated desktop candidates", () => {
|
|
|
|
|
expect(buildAssetURLCandidates("/api/public/assets/12")).toEqual([
|
2026-04-27 00:57:13 +08:00
|
|
|
"http://127.0.0.1:8080/api/public/assets/12?format=png",
|
2026-04-27 12:21:02 +08:00
|
|
|
"http://127.0.0.1:8080/api/desktop/content/assets/12?format=png",
|
|
|
|
|
"http://127.0.0.1:8080/api/public/assets/12",
|
|
|
|
|
]);
|
2026-04-27 00:57:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("centers crop rectangles at the requested ratio", () => {
|
|
|
|
|
const rect = getCenteredCropRect(1600, 1000, 4 / 3);
|
|
|
|
|
|
|
|
|
|
expect(rect.x).toBeCloseTo(133.333, 3);
|
|
|
|
|
expect(rect.y).toBeCloseTo(0);
|
|
|
|
|
expect(rect.width).toBeCloseTo(1333.333, 3);
|
|
|
|
|
expect(rect.height).toBeCloseTo(1000);
|
|
|
|
|
});
|
|
|
|
|
});
|