mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
06b08cd9d5
The second run of the globe Atlas was red on: - Client lint: the engine getter is a stable callback now, so the hooks that call it name it (and the refs and setters they read); an unused navigate and a seven parameter label helper are gone. - Atlas coverage: the mesh controller and the people and flights routes get unit tests, an unused region name helper is removed. - The French "Atlas" is marked as the same word, index.css is back at its length, the server people service is formatted, and the baselines drop the deleted AtlasPage and follow the lower counts.
137 lines
4.8 KiB
TypeScript
137 lines
4.8 KiB
TypeScript
import { AtlasMeshController } from '../../../src/nest/atlas/atlas-mesh.controller';
|
|
import { NotFoundException } from '@nestjs/common';
|
|
|
|
import type { Response } from 'express';
|
|
import fs from 'node:fs';
|
|
import { PassThrough, Readable } from 'node:stream';
|
|
import zlib from 'node:zlib';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
// The geometry files are built at install time (scripts/build-atlas-mesh.mjs) and
|
|
// not committed, so the file system is stubbed: the controller's own job is which
|
|
// file it names, which headers it sets and how it sends the bytes.
|
|
|
|
/** A writable stand-in for express's Response that records what the controller does with it. */
|
|
class FakeResponse extends PassThrough {
|
|
headers: Record<string, string> = {};
|
|
sent: { name: string; options: Record<string, unknown> } | null = null;
|
|
statusCode = 200;
|
|
headersSent = false;
|
|
body = '';
|
|
|
|
constructor() {
|
|
super();
|
|
this.on('data', (chunk: Buffer) => {
|
|
this.headersSent = true;
|
|
this.body += chunk.toString('utf8');
|
|
});
|
|
}
|
|
|
|
setHeader(name: string, value: string): this {
|
|
this.headers[name] = value;
|
|
return this;
|
|
}
|
|
|
|
sendFile(name: string, options: Record<string, unknown>): void {
|
|
this.sent = { name, options };
|
|
}
|
|
|
|
status(code: number): this {
|
|
this.statusCode = code;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
const FILE = 'world-110m.0123456789ab.json';
|
|
const asRes = (r: FakeResponse) => r as unknown as Response;
|
|
const finished = (r: FakeResponse) =>
|
|
new Promise<void>((resolve) => {
|
|
r.on('finish', resolve);
|
|
r.on('close', resolve);
|
|
});
|
|
|
|
afterEach(() => vi.restoreAllMocks());
|
|
|
|
describe('AtlasMeshController', () => {
|
|
const controller = new AtlasMeshController();
|
|
|
|
it('ATLAS-MESH-001: sends the manifest uncached when it is built', () => {
|
|
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
|
|
const res = new FakeResponse();
|
|
|
|
controller.manifest(asRes(res));
|
|
|
|
expect(res.headers['Cache-Control']).toBe('no-cache');
|
|
expect(res.sent?.name).toBe('manifest.json');
|
|
});
|
|
|
|
it('ATLAS-MESH-002: answers 404 while the geometry is not built', () => {
|
|
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
|
|
|
|
expect(() => controller.manifest(asRes(new FakeResponse()))).toThrow(NotFoundException);
|
|
});
|
|
|
|
it('ATLAS-MESH-003: refuses any file name the build script does not write, before touching the disk', () => {
|
|
const exists = vi.spyOn(fs, 'existsSync').mockReturnValue(true);
|
|
|
|
for (const bad of ['../manifest.json', 'world.json', 'world-110m.0123456789ab.json.br', 'a/b.0123456789ab.json']) {
|
|
expect(() => controller.world(bad, 'br', asRes(new FakeResponse()))).toThrow(NotFoundException);
|
|
}
|
|
expect(exists).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('ATLAS-MESH-004: answers 404 for a well-formed name with no file behind it', () => {
|
|
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
|
|
|
|
expect(() => controller.regions('DE.0123456789ab.json', 'br', asRes(new FakeResponse()))).toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
it('ATLAS-MESH-005: sends the stored Brotli file as it is to a client that accepts br, cached for a year', () => {
|
|
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
|
|
const res = new FakeResponse();
|
|
|
|
controller.regions('DE.0123456789ab.json', 'gzip, deflate, br', asRes(res));
|
|
|
|
expect(res.headers['Content-Encoding']).toBe('br');
|
|
expect(res.headers['Cache-Control']).toBe('public, max-age=31536000, immutable');
|
|
expect(res.headers.Vary).toBe('Accept-Encoding');
|
|
expect(res.sent?.name).toBe('DE.0123456789ab.json.br');
|
|
expect(String(res.sent?.options.root)).toMatch(/atlas-mesh[\\/]regions$/);
|
|
});
|
|
|
|
it('ATLAS-MESH-006: decompresses as a stream for a client without br', async () => {
|
|
const json = JSON.stringify({ type: 'Topology', objects: {} });
|
|
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
|
|
vi.spyOn(fs, 'createReadStream').mockReturnValue(
|
|
Readable.from([zlib.brotliCompressSync(Buffer.from(json))]) as unknown as fs.ReadStream,
|
|
);
|
|
const res = new FakeResponse();
|
|
const done = finished(res);
|
|
|
|
controller.world(FILE, 'gzip', asRes(res));
|
|
await done;
|
|
|
|
expect(res.headers['Content-Encoding']).toBeUndefined();
|
|
expect(res.headers['Content-Type']).toBe('application/json; charset=utf-8');
|
|
expect(res.sent).toBeNull();
|
|
expect(res.body).toBe(json);
|
|
});
|
|
|
|
it('ATLAS-MESH-007: answers 500 when the stored file cannot be read before anything was sent', async () => {
|
|
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
|
|
vi.spyOn(fs, 'createReadStream').mockReturnValue(
|
|
Readable.from([Buffer.from('not brotli at all')]) as unknown as fs.ReadStream,
|
|
);
|
|
const res = new FakeResponse();
|
|
const done = finished(res);
|
|
|
|
controller.world(FILE, undefined, asRes(res));
|
|
await done;
|
|
|
|
expect(res.statusCode).toBe(500);
|
|
expect(res.body).toBe('');
|
|
});
|
|
});
|