Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

869 B
Raw Blame
import type { RequestHandlerOptions } from "msw";
import type { SetupServer } from "msw/node";

import { mockEndpointBuilder } from "./mockEndpointBuilder";

export interface MockServerOptions {
    baseUrl: string;
    server: SetupServer;
}

export class MockServer {
    private readonly server: SetupServer;
    public readonly baseUrl: string;

    constructor({ baseUrl, server }: MockServerOptions) {
        this.baseUrl = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
        this.server = server;
    }

    public mockEndpoint(options?: RequestHandlerOptions): ReturnType<typeof mockEndpointBuilder> {
        const builder = mockEndpointBuilder({
            once: options?.once ?? true,
            onBuild: (handler) => {
                this.server.use(handler);
            },
        }).baseUrl(this.baseUrl);
        return builder;
    }
}