All files / src/onboarding ready-runner.ts

100% Statements 33/33
100% Branches 18/18
100% Functions 4/4
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521x                               1x 1x 49x 49x 49x   1x 21x 21x 13x 21x 8x 8x 21x   1x 4x 4x 1x   8x 8x 2x 2x 8x 1x 1x 8x 2x 2x 6x 1x 1x 2x 2x  
import { isApiError, isAuthError, isNetworkError } from "@dataflint/mcp-server";
 
export type ReadyErrorCode =
    | "auth_failed"
    | "not_activated"
    | "probe_failed"
    | "port_in_use"
    | "unknown";
 
export type ReadyResult =
    | { ok: true }
    | { ok: false; errorCode: ReadyErrorCode; cause: unknown };
 
export type EnsureMcpServer = () => Promise<void>;
export type IsConnected = () => Promise<boolean>;
 
export class ReadyRunner {
    constructor(
        private readonly ensureMcpServer: EnsureMcpServer,
        private readonly isConnectedFn: IsConnected
    ) {}
 
    async runReadyCheck(): Promise<ReadyResult> {
        try {
            await this.ensureMcpServer();
            return { ok: true };
        } catch (cause) {
            return { ok: false, errorCode: classify(cause), cause };
        }
    }
 
    async isConnected(): Promise<boolean> {
        return this.isConnectedFn();
    }
}
 
function classify(error: unknown): ReadyErrorCode {
    if (error instanceof Error && error.message.includes("EADDRINUSE")) {
        return "port_in_use";
    }
    if (isAuthError(error)) {
        return "auth_failed";
    }
    if (isApiError(error) && error.errorCode === "user_not_registered") {
        return "not_activated";
    }
    if (isNetworkError(error)) {
        return "probe_failed";
    }
    return "unknown";
}