All files / tests helpers.ts

86.66% Statements 39/45
74.19% Branches 23/31
92% Functions 23/25
92.1% Lines 35/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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                                                                                                                    116x       65x       3x       1x       2x       3x       3x       1x       38x       1x       1x       27x                       18x                   15x       11x       9x                       7x 7x         26x 26x       24x 24x 21x       10x 10x 10x 9x 9x         30x 30x 30x 30x                           30x 30x       26x    
// Re-export all helpers from the SDK plugin's test helpers (same AST builder utilities)
export 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';
 
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): OpParamNode {
    return { name, type, loc: loc(1, 'test.op') };
}
 
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: string = '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?: string): 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\((.+)\)$/);
    if (arrayMatch?.[1]) return { kind: 'array', item: refType(arrayMatch[1]) };
    return refType(s);
}
 
function normalizeParamSource(value: unknown): ParamSource {
    Iif (!value) return value as ParamSource;
    Iif (typeof value === 'string') return { kind: 'ref', name: value };
    if (Array.isArray(value)) return { kind: 'params', nodes: value as OpParamNode[] };
    const v = value as ParamSource;
    Eif (v.kind === 'params' || v.kind === 'ref' || v.kind === 'type') return v;
    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(1, 'test.op'),
        ...normalized,
    };
}
 
export function opRoute(
    path: string,
    operations: OpOperationNode[],
    params?: ParamSource | OpParamNode[] | string,
    modifiers?: RouteModifier[],
): OpRouteNode {
    const normalizedParams = params !== undefined ? normalizeParamSource(params) : undefined;
    return { path, params: normalizedParams, operations, modifiers, loc: loc(1, 'test.op') };
}
 
export function opRoot(routes: OpRouteNode[], file = 'payments.op.ck', meta: Record<string, string> = {}): OpRootNode {
    return { kind: 'opRoot', meta, routes, file };
}