All files / tests helpers.ts

90.38% Statements 47/52
90.9% Branches 30/33
89.28% Functions 25/28
93.47% Lines 43/46

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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248                                                              832x       303x       29x       3x       3x       5x       10x       6x       2x       203x       10x       4x       237x                       160x                   130x       43x                               30x 30x       6x 12x                 114x 114x       100x 100x 7x   93x           52x 52x 43x 5x 5x       201x 201x 201x 201x                           191x 191x       185x         6x                   6x               6x           6x                                           6x                              
import type {
    ContractRootNode,
    ModelNode,
    FieldNode,
    ContractTypeNode,
    ScalarTypeNode,
    ArrayTypeNode,
    TupleTypeNode,
    RecordTypeNode,
    EnumTypeNode,
    LiteralTypeNode,
    UnionTypeNode,
    DiscriminatedUnionTypeNode,
    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 discriminatedUnionType(discriminator: string, ...members: ContractTypeNode[]): DiscriminatedUnionTypeNode {
    return { kind: 'discriminatedUnion', discriminator, 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 opMultiRequest(entries: Array<[string, string | ContractTypeNode]>): OpRequestNode {
    return {
        bodies: entries.map(([contentType, body]) => ({
            contentType,
            bodyType: typeof body === 'string' ? refType(body) : body,
        })),
    };
}
 
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);
}
 
/** 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 };
    if (Array.isArray(value)) return { kind: 'params', nodes: value };
    Iif (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[],
): 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 = 'users.op', meta: Record<string, string> = {}): OpRootNode {
    return { kind: 'opRoot', meta, routes, file };
}
 
// ─── DSL Fixture Strings ────────────────────────────────────────────────────
 
export const SIMPLE_USER_CONTRACT = `\
contract User: {
    id: readonly uuid
    name: string
    email: email
    age?: number
    active: boolean = true
}
`;
 
export const VISIBILITY_CONTRACT = `\
contract User: {
    id: readonly uuid
    name: string
    password: writeonly string
}
`;
 
export const INHERITANCE_CONTRACT = `\
contract Admin: User & {
    role: enum(admin, superadmin)
}
`;
 
export const SIMPLE_USERS_OP = `\
operation /users: {
    get: {
        response: {
            200: {
                application/json: array(User)
            }
        }
    }
    post: {
        request: {
            application/json: CreateUserInput
        }
        response: {
            201: {
                application/json: User
            }
        }
    }
}
`;
 
export const PARAMETERIZED_OP = `\
operation /users/{id}: {
    params: {
        id: uuid
    }
    get: {
        response: {
            200: {
                application/json: User
            }
        }
    }
    delete: {}
}
`;