Skip to content

High performance self-hosted photo and video management solution.

2.4 KB
Raw Blame
import request from 'supertest';
import { TimelineController } from 'src/controllers/timeline.controller.js';
import { TimelineService } from 'src/services/timeline.service.js';
import { errorDto } from 'test/medium/responses.js';
import { ControllerContext, controllerSetup, mockBaseService } from 'test/utils.js';

describe(TimelineController.name, () => {
  let ctx: ControllerContext;
  const service = mockBaseService(TimelineService);

  beforeAll(async () => {
    ctx = await controllerSetup(TimelineController, [{ provide: TimelineService, useValue: service }]);
    return () => ctx.close();
  });

  beforeEach(() => {
    service.resetAllMocks();
    ctx.reset();
  });

  describe('GET /timeline/buckets', () => {
    it('should parse bbox query string into an object', async () => {
      const { status } = await request(ctx.getHttpServer())
        .get('/timeline/buckets')
        .query({ bbox: '11.075683,49.416711,11.117589,49.454875' });

      expect(status).toBe(200);
      expect(service.getTimeBuckets).toHaveBeenCalledWith(
        undefined,
        expect.objectContaining({
          bbox: { west: 11.075683, south: 49.416711, east: 11.117589, north: 49.454875 },
        }),
      );
    });

    it('should reject incomplete bbox query string', async () => {
      const { status, body } = await request(ctx.getHttpServer()).get('/timeline/buckets').query({ bbox: '1,2,3' });
      expect(status).toBe(400);
      expect(body).toEqual(
        errorDto.validationError([
          { path: ['bbox'], message: 'bbox must have 4 comma-separated numbers: west,south,east,north' },
        ]),
      );
    });

    it('should reject invalid bbox query string', async () => {
      const { status, body } = await request(ctx.getHttpServer())
        .get('/timeline/buckets')
        .query({ bbox: '1,2,3,invalid' });
      expect(status).toBe(400);
      expect(body).toEqual(errorDto.validationError([{ path: ['bbox'], message: 'bbox parts must be valid numbers' }]));
    });
  });

  describe('GET /timeline/bucket', () => {
    // TODO enable date string validation while still accepting 5 digit years
    it.fails('should fail if time bucket is invalid', async () => {
      const { status, body } = await request(ctx.getHttpServer()).get('/timeline/bucket').query({ timeBucket: 'foo' });
      expect(status).toBe(400);
      expect(body).toEqual(errorDto.badRequest('Invalid time bucket format'));
    });
  });
});