All files / tests helpers.ts

86.48% Statements 32/37
60% Branches 12/20
100% Functions 21/21
93.33% Lines 28/30

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                                                              74x       42x       9x       1x       1x       4x       3x       1x       1x       20x       1x       1x       23x                       15x                   23x                       1x       2x                     15x           4x 4x 1x           18x 18x 18x 18x                 17x 17x 17x    
import type {
    CkRootNode,
    ModelNode,
    FieldNode,
    ContractTypeNode,
    ScalarTypeNode,
    ArrayTypeNode,
    TupleTypeNode,
    RecordTypeNode,
    EnumTypeNode,
    LiteralTypeNode,
    UnionTypeNode,
    IntersectionTypeNode,
    ModelRefTypeNode,
    InlineObjectTypeNode,
    LazyTypeNode,
    SourceLocation,
    OpRouteNode,
    OpOperationNode,
    OpParamNode,
    OpRequestNode,
    OpResponseNode,
    HttpMethod,
    ParamSource,
    RouteModifier,
    SecurityNode,
} 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 intersectionType(...members: ContractTypeNode[]): IntersectionTypeNode {
    return { kind: 'intersection', 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 ckRoot(overrides?: Partial<CkRootNode>): CkRootNode {
    return {
        kind: 'ckRoot',
        meta: {},
        services: {},
        models: [],
        routes: [],
        file: 'test.ck',
        ...overrides,
    };
}
 
export function opParam(name: string, type: ContractTypeNode, overrides?: Partial<OpParamNode>): OpParamNode {
    return { name, optional: false, nullable: false, type, loc: loc(), ...overrides };
}
 
export function opRequest(bodyType: ContractTypeNode, contentType: string = 'application/json'): OpRequestNode {
    return {
        bodies: [
            {
                contentType: contentType as OpRequestNode['bodies'][number]['contentType'],
                bodyType,
            },
        ],
    };
}
 
export function opResponse(statusCode: number, bodyType?: ContractTypeNode, contentType?: 'application/json'): OpResponseNode {
    return { statusCode, contentType, bodyType };
}
 
/** 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;
    if (typeof value === 'string') return { kind: 'ref', name: value };
    Eif (Array.isArray(value)) return { kind: 'params', nodes: value };
    if (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);
    Iif (overrides?.headers !== undefined) normalized.headers = normalizeParamSource(overrides.headers);
    return {
        method,
        responses: [],
        loc: loc(),
        ...normalized,
    };
}
 
export function opRoute(path: string, operations: OpOperationNode[], overrides?: Partial<OpRouteNode> & { params?: unknown }): OpRouteNode {
    const normalized = { ...overrides } as Partial<OpRouteNode>;
    if (overrides?.params !== undefined) normalized.params = normalizeParamSource(overrides.params);
    return { path, operations, loc: loc(), ...normalized };
}