Files
ApiCore/ApiCore-Backend/src/backup/backup.controller.spec.ts
T
Nick SzaunigandClaude Opus 5.5 cdd30cf3ee feat: platform backbone and field capture complete (checkpoint)
Platform backbone (plan 2026-10-13-plattform-rueckgrat), all tasks reviewed:
- webhooks with delivery table, retries and stable delivery id
- calendar handlers; a finished task removes its Outlook event
- tasks, automations, inspections, varroa, treatments, feedings,
  harvests, colonies, wintering, stock and bottling record domain events
  in the same transaction; legacy post-commit calls removed
- row locks through one helper (lockRowForUpdate, lockRowsForShare,
  lockRowsForUpdate)
- job framework with JobRun, cli exit codes, cleanup, task templates,
  daily evaluation, hourly app-backup and the rekey job
- system API and page "Verwaltung > System" with retry
- @nestjs/schedule removed, guard tests against in-process schedulers

Field capture T3 (plan 2026-10-13-t3-erfassung-am-stand), all tasks reviewed:
- inspection photo offline without metadata
- bulk capture for treatments, feedings and varroa (backend + frontend)
- offline stand mode with service worker, offline pages and packs
- offline session, cold start and logout on shared devices

Docs: ARCHITECTURE.md assumptions and sections updated, TASKS.md.
Gates: backend lint, typecheck, 1770 unit, 420 e2e, build, no schema
drift; frontend lint, typecheck, 913 tests, build. Not rolled out.

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

109 lines
3.7 KiB
TypeScript

import { Logger } from '@nestjs/common';
import type { Response } from 'express';
import { BackupController } from './backup.controller';
import { BackupService } from './backup.service';
import { BackupSchedulerService } from './backup-scheduler.service';
import { BackupSettingsService } from './backup-settings.service';
import { ReauthService } from '../auth/reauth/reauth.service';
import type { AuthenticatedUser } from '../auth/types/authenticated-user';
describe('BackupController', () => {
const originalLimit = process.env.BACKUP_MAX_IMPORT_BYTES;
let controller: BackupController;
let backupService: { importFile: jest.Mock; exportSealed: jest.Mock };
let scheduler: Record<string, never>;
let settings: { update: jest.Mock };
let reauth: { verify: jest.Mock };
const actor = { id: 'admin-1' } as AuthenticatedUser;
const file = {
buffer: Buffer.from(JSON.stringify({ format: 'apicore-backup' })),
} as Express.Multer.File;
beforeEach(() => {
delete process.env.BACKUP_MAX_IMPORT_BYTES;
backupService = {
importFile: jest.fn().mockResolvedValue({ imported: {}, skipped: [] }),
exportSealed: jest.fn().mockResolvedValue({
format: 'apicore-backup',
exportedAt: '2026-10-12T03:00:00.000Z',
ciphertext: 'x'.repeat(2000),
}),
};
scheduler = {};
settings = { update: jest.fn() };
reauth = { verify: jest.fn().mockResolvedValue(undefined) };
controller = new BackupController(
backupService as unknown as BackupService,
scheduler as unknown as BackupSchedulerService,
settings as unknown as BackupSettingsService,
reauth as unknown as ReauthService,
);
});
afterEach(() => {
if (originalLimit === undefined) {
delete process.env.BACKUP_MAX_IMPORT_BYTES;
} else {
process.env.BACKUP_MAX_IMPORT_BYTES = originalLimit;
}
});
// The app-backup job reads the settings on every run, so nothing in the
// API has to be told about a restore or a settings change. The scheduler
// double has no methods: any call on it fails these tests.
it('imports without touching the schedule', async () => {
await expect(
controller.import(file, { password: 'secret' }, actor),
).resolves.toEqual({ imported: {}, skipped: [] });
expect(backupService.importFile).toHaveBeenCalledTimes(1);
});
it('saves settings without touching the schedule', async () => {
const saved = { id: 'singleton', cronSchedule: '0 4 * * *' };
settings.update.mockResolvedValue(saved);
await expect(
controller.updateSettings({ cronSchedule: '0 4 * * *' }, actor),
).resolves.toBe(saved);
expect(settings.update).toHaveBeenCalledWith(
{ cronSchedule: '0 4 * * *' },
'admin-1',
);
});
describe('export', () => {
let warn: jest.SpyInstance;
let res: { setHeader: jest.Mock; send: jest.Mock };
beforeEach(() => {
warn = jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => {});
res = { setHeader: jest.fn(), send: jest.fn() };
});
afterEach(() => {
warn.mockRestore();
});
it('still delivers a backup larger than the import limit, but logs a warning', async () => {
process.env.BACKUP_MAX_IMPORT_BYTES = '1000';
await controller.export(res as unknown as Response);
expect(res.send).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('Import-Limit'),
);
});
it('logs nothing for a backup within the import limit', async () => {
await controller.export(res as unknown as Response);
expect(res.send).toHaveBeenCalledTimes(1);
expect(warn).not.toHaveBeenCalled();
});
});
});