All files / tests helpers.ts

75% Statements 33/44
77.41% Branches 24/31
76% Functions 19/25
78.94% Lines 30/38

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                                            288x       53x       1x                       6x                       12x       10x               39x                       11x                   10x       19x       13x       5x                     22x 22x         10x 10x       1x 1x     1x           18x 18x 18x 18x         115x 115x 115x 115x                             104x 104x       101x    
import type {
    ContractRootNode,
    ModelNode,
    FieldNode,
    ContractTypeNode,
    ScalarTypeNode,
    ArrayTypeNode,
    TupleTypeNode,
    RecordTypeNode,
    EnumTypeNode,
    LiteralTypeNode,
    UnionTypeNode,
    ModelRefTypeNode,
    InlineObjectTypeNode,
    LazyTypeNode,
    SourceLocation,
    OpRootNode,
    OpRouteNode,
    OpOperationNode,
    OpParamNode,
    OpRequestNode,
    OpResponseNode,
    HttpMethod,
    ParamSource,
    RouteModifier,
} from '@contractkit/core';
 
// ─── AST Builder Helpers ────────────────────────────────────────────────────
 
export function loc(line = 1, file = 'test.ck'): SourceLocation {
    return { file, line };
}
 
export function scalarType(name: ScalarTypeNode['name'], mods?: Partial<ScalarTypeNode>): ScalarTypeNode {
    return { kind: 'scalar', name, ...mods };
}
 
export function arrayType(item: ContractTypeNode, mods?: { min?: number; max?: number }): ArrayTypeNode {
    return { kind: 'array', item, ...mods };
}
 
export function tupleType(...items: ContractTypeNode[]): TupleTypeNode {
    return { kind: 'tuple', items };
}
 
export function recordType(key: ContractTypeNode, value: ContractTypeNode): RecordTypeNode {
    return { kind: 'record', key, value };
}
 
export function enumType(...values: string[]): EnumTypeNode {
    return { kind: 'enum', values };
}
 
export function literalType(value: string | number | boolean): LiteralTypeNode {
    return { kind: 'literal', value };
}
 
export function unionType(...members: ContractTypeNode[]): UnionTypeNode {
    return { kind: 'union', members };
}
 
export function refType(name: string): ModelRefTypeNode {
    return { kind: 'ref', name };
}
 
export function inlineObjectType(fields: FieldNode[]): InlineObjectTypeNode {
    return { kind: 'inlineObject', fields };
}
 
export function lazyType(inner: ContractTypeNode): LazyTypeNode {
    return { kind: 'lazy', inner };
}
 
export function field(name: string, type: ContractTypeNode, overrides?: Partial<FieldNode>): FieldNode {
    return {
        name,
        optional: false,
        nullable: false,
        visibility: 'normal',
        type,
        loc: loc(),
        ...overrides,
    };
}
 
export function model(name: string, fields: FieldNode[], overrides?: Partial<ModelNode>): ModelNode {
    return {
        kind: 'model',
        name,
        fields,
        loc: loc(),
        ...overrides,
    };
}
 
export function contractRoot(models: ModelNode[], file = 'test.ck'): ContractRootNode {
    return { kind: 'contractRoot', meta: {}, models, file };
}
 
export function opParam(name: string, type: ContractTypeNode, overrides?: Partial<OpParamNode>): OpParamNode {
    return { name, type, loc: loc(1, 'test.op'), ...overrides };
}
 
export function paramNodes(nodes: OpParamNode[]): ParamSource {
    return { kind: 'params', nodes };
}
 
export function paramRef(name: string): ParamSource {
    return { kind: 'ref', name };
}
 
export function paramType(node: ContractTypeNode): ParamSource {
    return { kind: 'type', node };
}
 
export function opRequest(
    bodyType: string | ContractTypeNode,
    contentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded' = 'application/json',
): OpRequestNode {
    const bt: ContractTypeNode = typeof bodyType === 'string' ? refType(bodyType) : bodyType;
    return { bodies: [{ contentType, bodyType: bt }] };
}
 
export function opResponse(statusCode: number, bodyType?: string | ContractTypeNode, contentType?: 'application/json'): OpResponseNode {
    const bt: ContractTypeNode | undefined =
        bodyType === undefined ? undefined : typeof bodyType === 'string' ? parseBodyTypeString(bodyType) : bodyType;
    return { statusCode, contentType, bodyType: bt };
}
 
function parseBodyTypeString(s: string): ContractTypeNode {
    const arrayMatch = s.match(/^array\((.+)\)$/);
    Iif (arrayMatch?.[1]) {
        return { kind: 'array', item: refType(arrayMatch[1]) };
    }
    return refType(s);
}
 
/** Normalize a raw param value (old bare format or new discriminated union) to ParamSource. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function normalizeParamSource(value: any): ParamSource {
    Iif (!value) return value;
    Iif (typeof value === 'string') return { kind: 'ref', name: value };
    Iif (Array.isArray(value)) return { kind: 'params', nodes: value };
    Eif (value.kind === 'params' || value.kind === 'ref' || value.kind === 'type') return value as ParamSource;
    return { kind: 'type', node: value as ContractTypeNode };
}
 
export function opOperation(method: HttpMethod, overrides?: Partial<OpOperationNode> & { query?: unknown; headers?: unknown }): OpOperationNode {
    const normalized = { ...overrides } as Partial<OpOperationNode>;
    if (overrides?.query !== undefined) normalized.query = normalizeParamSource(overrides.query);
    if (overrides?.headers !== undefined) normalized.headers = normalizeParamSource(overrides.headers);
    return {
        method,
        responses: [],
        loc: loc(1, 'test.op'),
        ...normalized,
    };
}
 
export function opRoute(
    path: string,
    operations: OpOperationNode[],
    params?: ParamSource | OpParamNode[] | string,
    modifiers?: RouteModifier[],
    overrides?: Partial<OpRouteNode>,
): OpRouteNode {
    const normalizedParams = params !== undefined ? normalizeParamSource(params) : undefined;
    return { path, params: normalizedParams, operations, modifiers, loc: loc(1, 'test.op'), ...overrides };
}
 
export function opRoot(routes: OpRouteNode[], file = 'users.op', meta: Record<string, string> = {}): OpRootNode {
    return { kind: 'opRoot', meta, routes, file };
}