All files / src/onboarding tenant-probe.ts

96% Statements 48/50
75% Branches 6/8
100% Functions 1/1
96% Lines 48/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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                    8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 2x 2x 2x 2x 2x   4x 4x 4x   4x 4x 4x 4x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x      
import type { AuthResult } from "@dataflint/mcp-server";
 
export type TenantId = "cloud" | "self";
 
export type TenantResolution =
    | {
          ok: true;
          tenant: TenantId;
          customerDomain: string | null;
          auth: AuthResult;
      }
    | { ok: false; reason: "not_in_any_tenant" }
    | { ok: false; reason: "error"; cause: unknown };
 
export interface ProbeOutcome {
    ok: boolean;
    errorCode?: string;
    cause?: unknown;
}
 
export interface TenantProbeLogger {
    warn: (message: string, ...rest: unknown[]) => void;
    info: (message: string, ...rest: unknown[]) => void;
}
 
export interface TenantProbeDeps {
    probeOne: (args: {
        customerDomain: string | null;
        accessToken: string;
    }) => Promise<ProbeOutcome>;
    setStickyHint?: (tenant: TenantId) => void;
    logger: TenantProbeLogger;
}
 
export async function probeAfterSignIn(
    auth: AuthResult,
    deps: TenantProbeDeps
): Promise<TenantResolution> {
    const cloud = await deps.probeOne({
        customerDomain: null,
        accessToken: auth.accessToken,
    });
    if (cloud.ok) {
        deps.logger.info(
            "[probe] Cloud probe succeeded; committing tenant=cloud"
        );
        deps.setStickyHint?.("cloud");
        return {
            ok: true,
            tenant: "cloud",
            customerDomain: null,
            auth,
        };
    }
    if (cloud.errorCode !== "user_not_registered") {
        deps.logger.warn(
            `[probe] Cloud probe failed with non-routable error (errorCode=${cloud.errorCode ?? "<none>"}); no Self fallback`
        );
        return { ok: false, reason: "error", cause: cloud.cause };
    }
 
    deps.logger.info(
        "[probe] Cloud probe returned user_not_registered; falling back to Self with the same access token"
    );
 
    const self = await deps.probeOne({
        customerDomain: "self",
        accessToken: auth.accessToken,
    });
    if (self.ok) {
        deps.logger.info(
            "[probe] Self probe succeeded; committing tenant=self"
        );
        deps.setStickyHint?.("self");
        return {
            ok: true,
            tenant: "self",
            customerDomain: "self",
            auth,
        };
    }
    if (self.errorCode === "user_not_registered") {
        return { ok: false, reason: "not_in_any_tenant" };
    }
    return { ok: false, reason: "error", cause: self.cause };
}