Files
ApiCore/ApiCore-Backend/test/calendar.e2e-spec.ts
T
Nick SzaunigandClaude Opus 5.5 c2f71bcb03
Backend Image / build-and-push (push) Failing after 2m45s
Frontend Image / build-and-push (push) Successful in 5m52s
fix: T4a browser check findings
- Withdrawal wording from one backend helper everywhere ("bis
  einschließlich TT.MM. – ab TT.MM. wieder erntefähig", year when it
  crosses New Year); withdrawal-status returns lastBlockedDay and the
  ready text, the frontend no longer computes dates
- Calendar events name colony or apiary, withdrawal events carry
  product and detail; compact withdrawal rows on mobile
- One bottling warning per colony and treatment; pre-save hint
- Offline: online-only actions disabled with a reason, cached data
  shows its timestamp, open sheets block submit offline
- Mobile: no horizontal scroll at 360 px, touch targets >= 44 px,
  toasts and the add button no longer cover content
- Cleanup sheet without destructive preselection; natural sort of
  colony codes; collapsible automation rule cards; varroa list newest
  first with chart axes; text fixes

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-14 18:26:09 +02:00

226 lines
7.5 KiB
TypeScript

import { INestApplication } from '@nestjs/common';
import { withdrawalRangeText } from '../src/withdrawal/withdrawal-text';
import * as bcrypt from 'bcryptjs';
import request from 'supertest';
import { App } from 'supertest/types';
import { PrismaService } from '../src/prisma/prisma.service';
import { createTestApp } from './utils/create-test-app';
import { login } from './utils/api';
interface CalendarEventBody {
id: string;
type: string;
title: string;
date: string;
endDate?: string;
relatedType: string;
relatedId: string;
}
describe('Calendar (e2e)', () => {
let app: INestApplication<App>;
let prisma: PrismaService;
const beekeeperEmail = `calendar-e2e-beekeeper-${Date.now()}@example.com`;
const password = 'SuperSecret123!';
let beekeeperId: string;
let beekeeperToken: string;
let apiaryId: string;
let colonyId: string;
let taskId: string;
let treatmentId: string;
let colonyCode: string;
let calendarOnlyRoleId: string;
let calendarOnlyId: string;
let calendarOnlyToken: string;
beforeAll(async () => {
app = await createTestApp();
prisma = app.get(PrismaService);
const passwordHash = await bcrypt.hash(password, 12);
const beekeeper = await prisma.user.create({
data: {
email: beekeeperEmail,
passwordHash,
name: 'Beekeeper',
role: { connect: { key: 'BEEKEEPER' } },
},
});
beekeeperId = beekeeper.id;
beekeeperToken = (await login(app, beekeeperEmail, password)).accessToken;
const apiary = await prisma.apiary.create({
data: { name: `Stand ${Date.now()}` },
});
apiaryId = apiary.id;
const colony = await prisma.colony.create({
data: {
code: `V-${Date.now()}`,
apiaryId,
colonyType: 'PRODUCTION_COLONY',
},
});
colonyId = colony.id;
colonyCode = colony.code;
// 2027-06-10 + 10 days: harvestable again from 2027-06-20.
const treatment = await prisma.treatment.create({
data: {
colonyId,
userId: beekeeperId,
date: new Date('2027-06-10T00:00:00.000Z'),
product: 'Oxuvar',
withdrawalPeriodDays: 10,
},
});
treatmentId = treatment.id;
const calendarOnlyRole = await prisma.role.create({
data: {
key: `CAL_ONLY_${Date.now()}`,
name: 'Nur Kalender',
permissions: {
create: [{ permission: { connect: { key: 'calendar:read' } } }],
},
},
});
calendarOnlyRoleId = calendarOnlyRole.id;
const calendarOnly = await prisma.user.create({
data: {
email: `calendar-e2e-only-${Date.now()}@example.com`,
passwordHash,
name: 'Calendar only',
roleId: calendarOnlyRoleId,
},
});
calendarOnlyId = calendarOnly.id;
calendarOnlyToken = (await login(app, calendarOnly.email, password))
.accessToken;
const task = await prisma.task.create({
data: {
title: `Kalendertest ${Date.now()}`,
dueDate: new Date('2027-06-15'),
createdByUserId: beekeeperId,
},
});
taskId = task.id;
});
afterAll(async () => {
await prisma.task.deleteMany({ where: { id: taskId } });
await prisma.treatment.deleteMany({ where: { id: treatmentId } });
await prisma.colony.deleteMany({ where: { id: colonyId } });
await prisma.apiary.deleteMany({ where: { id: apiaryId } });
const userIds = [beekeeperId, calendarOnlyId];
await prisma.refreshToken.deleteMany({
where: { userId: { in: userIds } },
});
await prisma.auditLog.deleteMany({ where: { userId: { in: userIds } } });
await prisma.user.deleteMany({ where: { id: { in: userIds } } });
await prisma.role.deleteMany({ where: { id: calendarOnlyRoleId } });
await app.close();
});
it('rejects the request when from/to are missing', () => {
return request(app.getHttpServer())
.get('/api/v1/calendar')
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(400);
});
it('returns events for a task due within the queried range', async () => {
const response = await request(app.getHttpServer())
.get('/api/v1/calendar?from=2027-06-01&to=2027-06-30')
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(200);
const events = response.body as CalendarEventBody[];
expect(
events.some(
(event) =>
event.type === 'TASK' && event.title.includes('Kalendertest'),
),
).toBe(true);
});
it('lists WITHDRAWAL as a span with endDate for users with treatments:read', async () => {
const response = await request(app.getHttpServer())
.get('/api/v1/calendar?from=2027-06-01&to=2027-06-30')
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(200);
const events = response.body as CalendarEventBody[];
const span = events.find(
(event) =>
event.type === 'WITHDRAWAL' && event.id === `withdrawal:${treatmentId}`,
);
expect(span).toMatchObject({
title: `Wartezeit ${colonyCode} · Oxuvar`,
context: colonyCode,
product: 'Oxuvar',
detail: withdrawalRangeText('2027-06-20'),
relatedType: 'Colony',
relatedId: colonyId,
});
expect(span?.date.slice(0, 10)).toBe('2027-06-10');
// The last covered day, inclusive; 2027-06-20 is harvestable again.
expect(span?.endDate?.slice(0, 10)).toBe('2027-06-19');
});
it('shows the span in a range that only touches the end of the window', async () => {
const response = await request(app.getHttpServer())
.get('/api/v1/calendar?from=2027-06-19&to=2027-06-25')
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(200);
const events = response.body as CalendarEventBody[];
expect(
events.some((event) => event.id === `withdrawal:${treatmentId}`),
).toBe(true);
const later = await request(app.getHttpServer())
.get('/api/v1/calendar?from=2027-06-20&to=2027-06-25')
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(200);
expect(
(later.body as CalendarEventBody[]).some(
(event) => event.id === `withdrawal:${treatmentId}`,
),
).toBe(false);
});
it('hides WITHDRAWAL from users without treatments:read', async () => {
const response = await request(app.getHttpServer())
.get('/api/v1/calendar?from=2027-06-01&to=2027-06-30')
.set('Authorization', `Bearer ${calendarOnlyToken}`)
.expect(200);
expect(
(response.body as CalendarEventBody[]).filter(
(event) => event.type === 'WITHDRAWAL',
),
).toEqual([]);
});
it('lists TREATMENT events only for users with treatments:read', async () => {
const asBeekeeper = await request(app.getHttpServer())
.get('/api/v1/calendar?from=2027-06-01&to=2027-06-30')
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(200);
const asCalendarOnly = await request(app.getHttpServer())
.get('/api/v1/calendar?from=2027-06-01&to=2027-06-30')
.set('Authorization', `Bearer ${calendarOnlyToken}`)
.expect(200);
const treatmentEvent = (body: unknown) =>
(body as CalendarEventBody[]).filter(
(event) => event.type === 'TREATMENT' && event.id === treatmentId,
);
expect(treatmentEvent(asBeekeeper.body)).toHaveLength(1);
expect(treatmentEvent(asCalendarOnly.body)).toEqual([]);
});
it('rejects an invalid date range', () => {
return request(app.getHttpServer())
.get('/api/v1/calendar?from=nonsense&to=2027-06-30')
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(400);
});
});