Skip to content
Open
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
40 changes: 40 additions & 0 deletions apps/backend/src/__tests__/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {

Check failure on line 1 in apps/backend/src/__tests__/analytics.test.ts

View workflow job for this annotation

GitHub Actions / backend-ci

There should be no empty line within import group
describe,
it,
expect,
Expand All @@ -7,13 +7,13 @@
vi,
} from 'vitest';

import Fastify, {

Check failure on line 10 in apps/backend/src/__tests__/analytics.test.ts

View workflow job for this annotation

GitHub Actions / backend-ci

`fastify` import should occur before import of `vitest`
type FastifyInstance,
} from 'fastify';

import type { PrismaClient } from '@prisma/client';

import { analyticsRoutes } from '../routes/analytics';

Check failure on line 16 in apps/backend/src/__tests__/analytics.test.ts

View workflow job for this annotation

GitHub Actions / backend-ci

`../routes/analytics` import should occur before type import of `@prisma/client`

// ─── Shared mock data ────────────────────────────────────────────────────────

Expand All @@ -34,7 +34,7 @@

// ─── App factory ─────────────────────────────────────────────────────────────

let mockJwtVerify = vi.fn();

Check failure on line 37 in apps/backend/src/__tests__/analytics.test.ts

View workflow job for this annotation

GitHub Actions / backend-ci

'mockJwtVerify' is never reassigned. Use 'const' instead

async function buildApp(): Promise<FastifyInstance> {
const app = Fastify({
Expand Down Expand Up @@ -188,7 +188,7 @@

expect(
res.statusCode
).toBe(200);

Check failure on line 191 in apps/backend/src/__tests__/analytics.test.ts

View workflow job for this annotation

GitHub Actions / backend-ci

src/__tests__/analytics.test.ts > Analytics API > GET /api/analytics/overview > 200 — returns analytics overview

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ src/__tests__/analytics.test.ts:191:15

const body =
res.json();
Expand Down Expand Up @@ -427,6 +427,46 @@
}
);

it(
'200 — clamps non-numeric page to 1',
async () => {
prismaMock.cardView.count.mockResolvedValue(0);
prismaMock.cardView.findMany.mockResolvedValue([]);

const res = await app.inject({
method: 'GET',
url: '/api/analytics/views?page=abc',
headers: authHeader(),
});

expect(res.statusCode).toBe(200);
expect(
prismaMock.cardView.findMany.mock.calls[0][0]
).toMatchObject({ skip: 0, take: 20 });
expect(res.json().meta.page).toBe(1);
}
);

it(
'200 — clamps negative page to 1',
async () => {
prismaMock.cardView.count.mockResolvedValue(0);
prismaMock.cardView.findMany.mockResolvedValue([]);

const res = await app.inject({
method: 'GET',
url: '/api/analytics/views?page=-5',
headers: authHeader(),
});

expect(res.statusCode).toBe(200);
expect(
prismaMock.cardView.findMany.mock.calls[0][0]
).toMatchObject({ skip: 0, take: 20 });
expect(res.json().meta.page).toBe(1);
}
);

it(
'401 — rejects unauthenticated request',
async () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/routes/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function analyticsRoutes(
_reply: FastifyReply
) => {
const userId = request.user.id;
const page = parseInt(request.query.page || '1', 10);
const page = Math.max(1, parseInt(request.query.page || '1', 10) || 1);
const limit = 20;
const skip = (page - 1) * limit;

Expand Down
Loading