diff --git a/packages/lambda-tiler/src/routes/__test__/health.test.ts b/packages/lambda-tiler/src/routes/__test__/health.test.ts index b19ad6617..9464dac25 100644 --- a/packages/lambda-tiler/src/routes/__test__/health.test.ts +++ b/packages/lambda-tiler/src/routes/__test__/health.test.ts @@ -1,10 +1,12 @@ +import { GoogleTms } from '@basemaps/geo'; import { LogConfig } from '@basemaps/shared'; import { LambdaAlbRequest, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda'; import { Context } from 'aws-lambda'; import o from 'ospec'; import sinon from 'sinon'; +import { TileSets } from '../../tile.set.cache.js'; +import { TileSetRaster } from '../../tile.set.raster.js'; import { getTestBuffer, Health, TestTiles } from '../health.js'; -import { TileRoute } from '../tile.js'; const ctx: LambdaHttpRequest = new LambdaAlbRequest( { @@ -19,14 +21,21 @@ const ctx: LambdaHttpRequest = new LambdaAlbRequest( ); o.spec('health', async () => { + const sandbox = sinon.createSandbox(); + + const tileSet = new TileSetRaster('health', GoogleTms); + o.beforeEach(() => { + sandbox.stub(TileSets, 'get').resolves(tileSet); + }); + o.afterEach(() => { - sinon.restore(); + sandbox.restore(); }); o('Should return bad response', async () => { // Given ... a bad get tile response const BadResponse = new LambdaHttpResponse(500, 'Can not get Tile Set.'); - sinon.stub(TileRoute, 'tile').resolves(BadResponse); + sandbox.stub(tileSet, 'tile').resolves(BadResponse); // When ... const res = await Health(ctx); @@ -51,7 +60,7 @@ o.spec('health', async () => { o.timeout(500); // Given ... a series good get tile response - const callback = sinon.stub(TileRoute, 'tile'); + const callback = sandbox.stub(tileSet, 'tile'); callback.onCall(0).resolves(Response1); callback.onCall(1).resolves(Response2); @@ -66,7 +75,7 @@ o.spec('health', async () => { o('Should return mis-match tile response', async () => { o.timeout(500); // Given ... a bad get tile response for second get tile - const callback = sinon.stub(TileRoute, 'tile'); + const callback = sandbox.stub(tileSet, 'tile'); callback.onCall(0).resolves(Response1); callback.onCall(1).resolves(Response1); diff --git a/packages/lambda-tiler/src/routes/health.ts b/packages/lambda-tiler/src/routes/health.ts index 39a2444d9..e70280f73 100644 --- a/packages/lambda-tiler/src/routes/health.ts +++ b/packages/lambda-tiler/src/routes/health.ts @@ -1,32 +1,28 @@ -import { GoogleTms, Nztm2000QuadTms, Tile, TileMatrixSet } from '@basemaps/geo'; +import { GoogleTms, Nztm2000QuadTms } from '@basemaps/geo'; +import { TileDataXyz, TileType } from '@basemaps/shared'; import { ImageFormat } from '@basemaps/tiler'; -import { HttpHeader, LambdaAlbRequest, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda'; -import { Context } from 'aws-lambda'; +import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda'; import * as fs from 'fs'; import * as path from 'path'; import PixelMatch from 'pixelmatch'; import Sharp from 'sharp'; import url from 'url'; -import { TileRoute } from './tile.js'; +import { TileSets } from '../tile.set.cache.js'; -interface TestTile { - tms: TileMatrixSet; - buf: null | Buffer; - format: ImageFormat; - testTile: Tile; +interface TestTile extends TileDataXyz { + buf?: Buffer; } export const TestTiles: TestTile[] = [ - { tms: GoogleTms, format: ImageFormat.PNG, testTile: { x: 252, y: 156, z: 8 }, buf: null }, - { tms: Nztm2000QuadTms, format: ImageFormat.PNG, testTile: { x: 30, y: 33, z: 6 }, buf: null }, + { type: TileType.Tile, name: 'health', tileMatrix: GoogleTms, ext: ImageFormat.PNG, x: 252, y: 156, z: 8 }, + { type: TileType.Tile, name: 'health', tileMatrix: Nztm2000QuadTms, ext: ImageFormat.PNG, x: 30, y: 33, z: 6 }, ]; const TileSize = 256; export async function getTestBuffer(test: TestTile): Promise { if (Buffer.isBuffer(test.buf)) return test.buf; - const tile = test.testTile; - const expectedFile = `static/expected_tile_${test.tms.identifier}_${tile.x}_${tile.y}_z${tile.z}.${test.format}`; + const expectedFile = `static/expected_tile_${test.tileMatrix.identifier}_${test.x}_${test.y}_z${test.z}.${test.ext}`; // Initiate test img buffer if not defined try { return await fs.promises.readFile(expectedFile); @@ -38,8 +34,7 @@ export async function getTestBuffer(test: TestTile): Promise { } export async function updateExpectedTile(test: TestTile, newTileData: Buffer, difference: Buffer): Promise { - const tile = test.testTile; - const expectedFileName = `static/expected_tile_${test.tms.identifier}_${tile.x}_${tile.y}_z${tile.z}.${test.format}`; + const expectedFileName = `static/expected_tile_${test.tileMatrix.identifier}_${test.x}_${test.y}_z${test.z}.${test.ext}`; await fs.promises.writeFile(expectedFileName, newTileData); const imgPng = await Sharp(difference, { raw: { width: TileSize, height: TileSize, channels: 4 } }) .png() @@ -56,27 +51,12 @@ export async function updateExpectedTile(test: TestTile, newTileData: Buffer, di */ export async function Health(req: LambdaHttpRequest): Promise { for (const test of TestTiles) { - const tms = test.tms; - const testTile = test.testTile; - const format = test.format; - const path = `/v1/tiles/health/${tms.identifier}/${testTile.z}/${testTile.x}/${testTile.y}.${format}`; - - const ctx: LambdaHttpRequest = new LambdaAlbRequest( - { - requestContext: null as any, - httpMethod: 'get', - path: path, - body: null, - isBase64Encoded: false, - }, - {} as Context, - req.log, - ); - + const tileSet = await TileSets.get('health', test.tileMatrix); + if (tileSet == null) throw new LambdaHttpResponse(500, 'TileSet: "health" not found'); // Get the parse response tile to raw buffer - const response = await TileRoute.tile(ctx); - if (response.status !== 200) return new LambdaHttpResponse(response.status, response.statusDescription); - if (!Buffer.isBuffer(response._body)) throw new LambdaHttpResponse(404, 'Not a Buffer response content.'); + const response = await tileSet.tile(req, test); + if (response.status !== 200) return new LambdaHttpResponse(500, response.statusDescription); + if (!Buffer.isBuffer(response._body)) throw new LambdaHttpResponse(500, 'Not a Buffer response content.'); const resImgBuffer = await Sharp(response._body).raw().toBuffer(); // Get test tile to compare @@ -90,11 +70,7 @@ export async function Health(req: LambdaHttpRequest): Promise