Skip to content

Commit

Permalink
test(lambda-tiler): implement test suite for tileset path function
Browse files Browse the repository at this point in the history
  • Loading branch information
tawera-manaena committed Oct 2, 2024
1 parent bb52962 commit 26dc285
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
99 changes: 99 additions & 0 deletions packages/lambda-tiler/src/routes/__tests__/link.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { strictEqual } from 'node:assert';
import { afterEach, before, describe, test } from 'node:test';

import { ConfigProviderMemory } from '@basemaps/config';
import { Epsg } from '@basemaps/geo';
import { createSandbox } from 'sinon';

import { FakeData, Imagery3857 } from '../../__tests__/config.data.js';
import { mockRequest } from '../../__tests__/xyz.util.js';
import { handler } from '../../index.js';
import { ConfigLoader } from '../../util/config.loader.js';

const NAME = 'tileset';
const PATH = `/v1/link/${NAME}`;

describe('/v1/link/:tileSet', () => {
const config = new ConfigProviderMemory();
const sandbox = createSandbox();

before(() => {
sandbox.stub(ConfigLoader, 'getDefaultConfig').resolves(config);
});

afterEach(() => {
config.objects.clear();
});

/**
* 3xx status responses
*/

// tileset found, has one layer, has '3857' entry, imagery found > 302 response
test('success: redirect to pre-zoomed imagery', async () => {
config.put(FakeData.tileSetRaster(NAME));
config.put(Imagery3857);

const req = mockRequest(PATH);
const res = await handler.router.handle(req);

strictEqual(res.status, 302);
strictEqual(res.statusDescription, 'Redirect to pre-zoomed imagery');
});

/**
* 4xx status responses
*/

// tileset not found > 404 response
test('failure: tileset not found', async () => {
const req = mockRequest(PATH);
const res = await handler.router.handle(req);

strictEqual(res.status, 404);
strictEqual(res.statusDescription, 'Tileset not found');
});

// tileset found, has more than one layer > 400 response
test('failure: too many layers', async () => {
const tileSet = FakeData.tileSetRaster(NAME);

// add another layer
tileSet.layers.push(tileSet.layers[0]);

config.put(tileSet);

const req = mockRequest(PATH);
const res = await handler.router.handle(req);

strictEqual(res.status, 400);
strictEqual(res.statusDescription, 'Too many layers');
});

// tileset found, has one layer, no '3857' entry > 400 response
test("failure: no imagery for '3857' projection", async () => {
const tileSet = FakeData.tileSetRaster(NAME);

// delete '3857' entry
delete tileSet.layers[0][Epsg.Google.code];

config.put(tileSet);

const req = mockRequest(PATH);
const res = await handler.router.handle(req);

strictEqual(res.status, 400);
strictEqual(res.statusDescription, "No imagery for '3857' projection");
});

// tileset found, has one layer, has '3857' entry, imagery not found > 400 response
test('failure: imagery not found', async () => {
config.put(FakeData.tileSetRaster(NAME));

const req = mockRequest(PATH);
const res = await handler.router.handle(req);

strictEqual(res.status, 400);
strictEqual(res.statusDescription, 'Imagery not found');
});
});
12 changes: 9 additions & 3 deletions packages/lambda-tiler/src/routes/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,36 @@ export interface LinkGet {

/**
* Given a tileset, this function redirects the client to a Basemaps URL that is already zoomed to
* the extent of the tileset's imagery. Otherwise, this function returns an 4XX status and message.
* the extent of the tileset's imagery. Otherwise, this function returns an 4xx status and message.
*
* @param tileSet - The id of the tileset.
* @example "ashburton-2023-0.1m"
*/
export async function linkGet(req: LambdaHttpRequest<LinkGet>): Promise<LambdaHttpResponse> {
const config = await ConfigLoader.load(req);

// get tileset

req.timer.start('tileset:load');
const tileSet = await config.TileSet.get(req.params.tileSet);
req.timer.end('tileset:load');
if (tileSet == null) return new LambdaHttpResponse(404, 'Tileset not found');

if (tileSet == null) return new LambdaHttpResponse(404, 'Tileset not found');
if (tileSet.layers.length !== 1) return new LambdaHttpResponse(400, 'Too many layers');

// get imagery

const imageryId = tileSet.layers[0][Epsg.Google.code];
if (imageryId === undefined) return new LambdaHttpResponse(400, "No imagery for '3857' projection");

const imagery = await config.Imagery.get(imageryId);
if (imagery == null) return new LambdaHttpResponse(400, 'Imagery not found');

// do redirect

const url = getPreviewUrl({ imagery });

return new LambdaHttpResponse(302, 'Redirecting to pre-zoomed imagery', {
return new LambdaHttpResponse(302, 'Redirect to pre-zoomed imagery', {
location: `/${url.slug}?i=${url.name}`,
});
}

0 comments on commit 26dc285

Please sign in to comment.