Files
immich-redaktions-plugin/src/modules/activity/service.test.ts
T

85 lines
3.1 KiB
TypeScript

import { expect, test, vi } from "vitest";
import { createActivityService, normalizeActivityInput } from "./service";
import type { RateLimitPolicy, RateLimitResult } from "@/modules/security/rate-limit";
type Consume = (key: string, policy: RateLimitPolicy) => Promise<RateLimitResult>;
const editorialId = "1111111a-1111-4111-8111-11111111111a";
const albumId = "2222222b-2222-4222-8222-22222222222b";
test("normalizeActivityInput validates ids, types and query length", () => {
expect(
normalizeActivityInput({ editorialId, type: "album_view", albumId }),
).toEqual({ editorialId, type: "album_view", albumId });
expect(normalizeActivityInput({ editorialId: "nope", type: "album_view" })).toBeNull();
expect(
normalizeActivityInput({ editorialId, type: "download" as never }),
).toBeNull();
expect(
normalizeActivityInput({ editorialId, type: "search", albumId: "bad id" }),
).toBeNull();
const normalized = normalizeActivityInput({
editorialId: editorialId.toUpperCase(),
type: "search",
query: ` wob ${"x".repeat(300)} `,
});
expect(normalized?.editorialId).toBe(editorialId);
expect(normalized?.query?.length).toBeLessThanOrEqual(200);
expect(normalized?.query?.startsWith("wob")).toBe(true);
});
function createService(overrides: {
consume?: ReturnType<typeof vi.fn>;
insert?: ReturnType<typeof vi.fn>;
editorialExists?: boolean;
} = {}) {
const insert = overrides.insert ?? vi.fn().mockResolvedValue(undefined);
const consume = (overrides.consume ??
vi.fn().mockResolvedValue({ allowed: true, retryAfterSeconds: 0 })) as Consume;
const database = {
select: vi.fn().mockReturnValue({
from: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
limit: vi.fn().mockResolvedValue(
overrides.editorialExists === false ? [] : [{ id: editorialId }],
),
}),
}),
}),
insert: vi.fn().mockReturnValue({ values: insert }),
};
return {
service: createActivityService({ database: database as never, consume }),
insert,
consume,
};
}
test("record inserts a normalized activity row", async () => {
const { service, insert } = createService();
await service.record({ editorialId, type: "search", albumId, query: " wob " });
expect(insert).toHaveBeenCalledWith({
editorialAccountId: editorialId,
eventType: "search",
immichAlbumId: albumId,
immichAssetId: null,
query: "wob",
});
});
test("record stays silent when rate-limited, unknown editorial or DB fails", async () => {
const limited = createService({
consume: vi.fn().mockResolvedValue({ allowed: false, retryAfterSeconds: 10 }),
});
await limited.service.record({ editorialId, type: "album_view" });
expect(limited.insert).not.toHaveBeenCalled();
const unknown = createService({ editorialExists: false });
await unknown.service.record({ editorialId, type: "album_view" });
expect(unknown.insert).not.toHaveBeenCalled();
const broken = createService({ insert: vi.fn().mockRejectedValue(new Error("db down")) });
await expect(broken.service.record({ editorialId, type: "album_view" })).resolves.toBeUndefined();
});