All files / src/onboarding sign-in-runner.ts

90% Statements 45/50
61.53% Branches 8/13
100% Functions 5/5
90% Lines 45/50

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 52 53 54 55 56 57 58 59 60 61 62  1x 1x         1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       1x 1x 5x 5x 5x   1x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 6x   1x 6x 6x 6x 4x   4x 4x 4x 6x           4x 6x 1x  
import type { AuthConfig, AuthResult } from "@dataflint/mcp-server";
import { Logger } from "../logger";
import { probeAfterSignIn } from "./tenant-probe";
import type { TenantProbeDeps, TenantResolution } from "./tenant-probe";
 
export type SignInFailReason = "oauth_failed";
 
export class SignInFailed extends Error {
    readonly name = "SignInFailed";
    constructor(
        readonly reason: SignInFailReason,
        cause?: unknown
    ) {
        super(`SignInFailed(${reason})`);
        if (cause !== undefined) {
            (this as { cause?: unknown }).cause = cause;
        }
    }
}
 
export type PerformSignIn = (authConfig: AuthConfig) => Promise<AuthResult>;
 
export class SignInRunner {
    constructor(
        private readonly performSignIn: PerformSignIn,
        private readonly probeDeps: TenantProbeDeps
    ) {}
 
    async signIn(authConfig: AuthConfig): Promise<AuthResult> {
        const logger = Logger.tryGetInstance();
        logger?.info(
            `[wizard] sign-in: starting PKCE via ${authConfig.domain}`
        );
        try {
            return await this.performSignIn(authConfig);
        } catch (err) {
            logger?.warn(
                `[wizard] sign-in: PKCE failed: ${err instanceof Error ? err.message : String(err)}`
            );
            throw new SignInFailed("oauth_failed", err);
        }
    }
 
    async signInAndResolveTenant(
        authConfig: AuthConfig
    ): Promise<TenantResolution> {
        const authResult = await this.signIn(authConfig);
        const logger = Logger.tryGetInstance();
 
        let resolution: TenantResolution;
        try {
            resolution = await probeAfterSignIn(authResult, this.probeDeps);
        } catch (err) {
            logger?.warn(
                `[wizard] sign-in: probe routing threw: ${err instanceof Error ? err.message : String(err)}`
            );
            return { ok: false, reason: "error", cause: err };
        }
        return resolution;
    }
}