Files
waggle-os/packages/server/tests/test-utils.ts
Oleg Maslov 0c3e2ead3b
Some checks failed
Installer Smoke / installer-smoke (push) Has been cancelled
moving
2026-09-02 10:10:29 +02:00

49 lines
1.3 KiB
TypeScript

/**
* Shared test utilities for server tests.
*
* Provides helpers for authenticated inject calls after SEC-011
* (local server bearer token authentication).
*/
import type { FastifyInstance, InjectOptions } from 'fastify';
/**
* Get the auth token from a server built with buildLocalServer.
*/
export function getAuthToken(server: FastifyInstance): string {
return server.agentState.wsSessionToken;
}
/**
* Create an authenticated inject options object.
* Merges the Authorization: Bearer header into the provided inject options.
*/
export function authInject(server: FastifyInstance, opts: InjectOptions): InjectOptions {
const token = getAuthToken(server);
const existingHeaders = (opts.headers ?? {}) as Record<string, string>;
return {
...opts,
headers: {
...existingHeaders,
authorization: `Bearer ${token}`,
},
};
}
/**
* Shorthand: inject with auth token. Returns the same result as server.inject().
*/
export function injectWithAuth(server: FastifyInstance, opts: InjectOptions) {
return server.inject(authInject(server, opts));
}
/**
* Reset the rate limiter state on the server.
* Call this in beforeEach() to prevent rate limit interference between tests.
*/
export function resetRateLimiter(server: FastifyInstance): void {
if (server.rateLimiter) {
server.rateLimiter.reset();
}
}