All files / src/onboarding persona-resolver.ts

100% Statements 26/26
100% Branches 9/9
100% Functions 2/2
100% Lines 26/26

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                                          1x   1x 28x 28x 16x 28x 2x 2x 2x 2x 28x 10x 1x 1x 10x 6x 6x 3x 3x 3x 3x 28x 28x   21x 21x 21x  
import type { Environment } from "../config";
import type { Persona } from "../telemetry-events";
 
export type { Persona };
 
export type PersonaSelection =
    | { persona: "enterprise"; customerCode: string }
    | { persona: "cloud" }
    | { persona: "internal_dev"; subOption: "staging" | "local" };
 
export type ResolvedSettings = {
    customerDomain: string | null;
    environment: Environment | undefined;
};
 
export type ResolveError = "enterprise_code_empty" | "enterprise_code_invalid";
 
export type ResolveResult =
    | { ok: true; resolved: ResolvedSettings }
    | { ok: false; error: ResolveError };
 
const ENTERPRISE_CODE_PATTERN = /^[a-z0-9-]+$/;
 
export function resolve(selection: PersonaSelection): ResolveResult {
    switch (selection.persona) {
        case "cloud":
            return ok({ customerDomain: null, environment: "prod" });
        case "internal_dev":
            return ok({
                customerDomain: null,
                environment: selection.subOption,
            });
        case "enterprise":
            if (selection.customerCode === "") {
                return { ok: false, error: "enterprise_code_empty" };
            }
            if (!ENTERPRISE_CODE_PATTERN.test(selection.customerCode)) {
                return { ok: false, error: "enterprise_code_invalid" };
            }
            return ok({
                customerDomain: selection.customerCode,
                environment: "prod",
            });
    }
}
 
function ok(resolved: ResolvedSettings): ResolveResult {
    return { ok: true, resolved };
}