Files
TREK/server/tests/unit/nest/booking-import/kitinerary-extractor.service.test.ts
T
Maurice 55da861de6 Put the reservation imports behind one door
Two controllers declared the identical @Controller string from two modules and
each rebuilt the trip access check its own way: booking-import ran it as a
guard chain, integrations ran it by hand inside the handler, and
BookingImportService carried a third copy that nothing called at all.

They never actually collided - the four file-upload sub-paths and the one
AirTrail sub-path are disjoint - so this is not a bug fix. It is one access
path instead of three implementations of the same one, and the dead copy is
gone.

The addon requirement sits on the AirTrail handler rather than the class.
Class-level would have 404d the four upload routes whenever AirTrail happened
to be off. AddonGuard still runs first in the chain, because a disabled addon
has to answer 404 to an anonymous caller before anything answers 401, and
ADDON-GUARD-006 pins both halves of that.

One deliberate behaviour change: the AirTrail route now gets its trip 404 from
the guard, ahead of the body 400, because a guard runs before the pipe. That
is the ordering every other trip-scoped controller already has and nothing
pinned the other way for this route.

The class rename would have stopped the server booting: the body-contract gate
keys on ControllerClass.methodName and throws on stale entries. Three entries
renamed, and the list re-sorted - GATE-006 caught that the rename had broken
the ordering it asserts.

booking-import lost the controller from its coverage denominator, so the
branch floor was about to fall. It goes up instead: the off-request parse had
no case for its catch arm, so a throw would have left the job on 'running' and
the widget spinning, and the binary probe behind /api/health/features had no
test at all - that one boolean decides whether the client offers booking
import.
2026-08-10 17:08:54 +02:00

74 lines
2.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
/**
* The binary probe behind `GET /api/health/features`.
*
* It had no test at all, which matters more than the line count suggests: this
* one boolean is what the client reads to decide whether to offer booking import
* at all, and every branch that resolves it is a filesystem lookup that behaves
* differently on the three platforms TREK ships to.
*/
const { existsSync, readdirSync, readEnv } = vi.hoisted(() => ({
existsSync: vi.fn(),
readdirSync: vi.fn(),
readEnv: vi.fn(),
}));
vi.mock('node:fs', () => ({
existsSync,
readdirSync,
writeFileSync: vi.fn(),
unlinkSync: vi.fn(),
}));
vi.mock('../../../../src/app-config', () => ({ readEnv }));
import { KitineraryExtractorService } from '../../../../src/nest/booking-import/kitinerary-extractor.service';
function boot(env: { kitineraryExtractorPath?: string } = {}) {
readEnv.mockReturnValue({ integrations: env });
const svc = new KitineraryExtractorService();
svc.onModuleInit();
return svc;
}
beforeEach(() => {
vi.clearAllMocks();
existsSync.mockReturnValue(false);
readdirSync.mockReturnValue([]);
});
describe('KitineraryExtractorService binary probe', () => {
it('KIT-EXT-001: takes the configured path when it exists', () => {
existsSync.mockImplementation((p: string) => p === '/opt/kitinerary-extractor');
expect(boot({ kitineraryExtractorPath: '/opt/kitinerary-extractor' }).isAvailable()).toBe(true);
});
it('KIT-EXT-002: an explicitly configured path that is missing disables the feature outright', () => {
// It deliberately does NOT fall through to the search: somebody who set the
// variable meant that binary, and silently using another one would hide the
// typo behind a working feature.
const svc = boot({ kitineraryExtractorPath: '/nope/kitinerary-extractor' });
expect(svc.isAvailable()).toBe(false);
expect(readdirSync).not.toHaveBeenCalled();
});
it('KIT-EXT-003: finds the Debian multiarch location by scanning /usr/lib', () => {
readdirSync.mockReturnValue(['x86_64-linux-gnu'] as never);
existsSync.mockImplementation((p: string) => p.includes('x86_64-linux-gnu'));
expect(boot().isAvailable()).toBe(true);
});
it('KIT-EXT-004: survives a system with no /usr/lib at all', () => {
readdirSync.mockImplementation(() => { throw new Error('ENOENT'); });
expect(boot().isAvailable()).toBe(false);
});
it('KIT-EXT-005: reports unavailable when nothing is found, rather than throwing at boot', () => {
expect(boot().isAvailable()).toBe(false);
});
it('KIT-EXT-006: extracting without a binary fails loudly', async () => {
await expect(boot().extract(Buffer.from(''), 'x.pdf')).rejects.toThrow('not available');
});
});