@apicircle/cli 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,640 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/index.ts
32
+ var src_exports = {};
33
+ __export(src_exports, {
34
+ buildProgram: () => buildProgram,
35
+ runCli: () => runCli
36
+ });
37
+ module.exports = __toCommonJS(src_exports);
38
+ var import_commander = require("commander");
39
+
40
+ // src/commands/mock.ts
41
+ var import_node_fs = require("fs");
42
+ var path = __toESM(require("path"), 1);
43
+ var import_kleur = __toESM(require("kleur"), 1);
44
+ var import_mock_server_core = require("@apicircle/mock-server-core");
45
+ var import_shared = require("@apicircle/shared");
46
+ function registerMockCommand(program) {
47
+ program.command("mock").description("Run a mock server from an OpenAPI / Postman / Insomnia file").argument("<spec>", "Path to the spec file").option("-p, --port <number>", "TCP port to bind (defaults to a free port)").option("-h, --host <host>", "Hostname to bind", "127.0.0.1").option("-t, --type <type>", "Source type: openapi | postman | insomnia | auto", "auto").option("-f, --format <format>", "OpenAPI format: json | yaml | auto", "auto").option("--cors", "Enable permissive CORS", true).action(async (spec, opts) => {
48
+ const absolute = path.resolve(spec);
49
+ const raw = await import_node_fs.promises.readFile(absolute, "utf-8");
50
+ const type = inferType(absolute, opts.type ?? "auto");
51
+ const format = type === "openapi" ? inferFormat(absolute, opts.format ?? "auto") : "json";
52
+ const source = makeSource(type, format, raw);
53
+ const parsed = await (0, import_mock_server_core.parseSourceToEndpoints)(source);
54
+ const mock = {
55
+ id: (0, import_shared.generateId)(),
56
+ name: path.basename(absolute),
57
+ source,
58
+ endpoints: parsed.endpoints,
59
+ defaultPort: opts.port ? Number(opts.port) : null,
60
+ cors: { enabled: opts.cors !== false, origins: ["*"] },
61
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
62
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
63
+ };
64
+ const handle = await (0, import_mock_server_core.startMockServer)(mock, {
65
+ port: opts.port ? Number(opts.port) : void 0,
66
+ host: opts.host
67
+ });
68
+ process.stdout.write(
69
+ `${import_kleur.default.green("Mock server")} listening on ${import_kleur.default.cyan(`http://${opts.host}:${handle.port}`)} with ${parsed.endpoints.length} endpoints (type=${type}). Press Ctrl-C to stop.
70
+ `
71
+ );
72
+ if (parsed.warnings.length) {
73
+ for (const w of parsed.warnings) {
74
+ process.stderr.write(`${import_kleur.default.yellow("warn")}: ${w}
75
+ `);
76
+ }
77
+ }
78
+ installShutdown(handle);
79
+ });
80
+ }
81
+ function inferType(filePath, hint) {
82
+ if (hint && hint !== "auto") return hint;
83
+ const lower = filePath.toLowerCase();
84
+ if (lower.includes("postman")) return "postman";
85
+ if (lower.includes("insomnia")) return "insomnia";
86
+ return "openapi";
87
+ }
88
+ function inferFormat(filePath, hint) {
89
+ if (hint && hint !== "auto") return hint;
90
+ const lower = filePath.toLowerCase();
91
+ return lower.endsWith(".yaml") || lower.endsWith(".yml") ? "yaml" : "json";
92
+ }
93
+ function makeSource(type, format, raw) {
94
+ switch (type) {
95
+ case "openapi":
96
+ return { kind: "openapi", spec: raw, format };
97
+ case "postman":
98
+ return { kind: "postman", collection: raw };
99
+ case "insomnia":
100
+ return { kind: "insomnia", export: raw };
101
+ }
102
+ }
103
+ function installShutdown(handle) {
104
+ let closing = false;
105
+ const shutdown = async () => {
106
+ if (closing) return;
107
+ closing = true;
108
+ await handle.close();
109
+ process.exit(0);
110
+ };
111
+ process.on("SIGINT", () => void shutdown());
112
+ process.on("SIGTERM", () => void shutdown());
113
+ }
114
+
115
+ // src/commands/mcp.ts
116
+ var path3 = __toESM(require("path"), 1);
117
+ var import_kleur2 = __toESM(require("kleur"), 1);
118
+ var import_mcp_server = require("@apicircle/mcp-server");
119
+
120
+ // src/util/loadWorkspace.ts
121
+ var path2 = __toESM(require("path"), 1);
122
+ var import_node_fs2 = require("fs");
123
+ var import_file_backed = require("@apicircle/core/workspace/file-backed");
124
+ var import_shared2 = require("@apicircle/shared");
125
+ async function ensureWorkspace(dir) {
126
+ const resolved = path2.resolve(dir);
127
+ await import_node_fs2.promises.mkdir(resolved, { recursive: true });
128
+ const existing = await (0, import_file_backed.loadFromFile)(resolved, { allowMissing: true });
129
+ if (existing) return existing;
130
+ const now = (/* @__PURE__ */ new Date()).toISOString();
131
+ const workspaceId = (0, import_shared2.generateId)();
132
+ const fresh = {
133
+ synced: {
134
+ schemaVersion: 1,
135
+ workspaceId,
136
+ collections: {
137
+ tree: { id: (0, import_shared2.generateId)(), type: "root", children: [] },
138
+ requests: {},
139
+ folders: {}
140
+ },
141
+ environments: { items: {}, activeName: null, priorityOrder: [] },
142
+ linkedWorkspaces: {},
143
+ linkedOverrides: { requests: {}, environmentVars: {} },
144
+ releases: { self: null, perLink: {} },
145
+ globalAssets: { schemas: {}, graphql: {} },
146
+ mockServers: {},
147
+ meta: { createdAt: now, updatedAt: now, appVersion: "1.0.0" }
148
+ },
149
+ local: {
150
+ schemaVersion: 1,
151
+ workspaceId,
152
+ executionPlans: {},
153
+ history: { requestRuns: [], planRuns: [] },
154
+ secretIndex: { entries: {} },
155
+ sessions: { github: { workspace: null, links: {} } },
156
+ connectedRepo: null,
157
+ workingBranch: null,
158
+ seededWorkspaceSha: null,
159
+ retiredBranch: null,
160
+ sync: { lastPulledSnapshot: null, lastPulledSha: null, lastPulledAt: null, dirtyKeys: [] },
161
+ linkedCollections: {},
162
+ globalContext: {},
163
+ mockRuntime: { active: {} },
164
+ ui: {
165
+ activeRequestId: null,
166
+ sidebarExpandedSections: [],
167
+ themeId: "studio-dark",
168
+ fontId: "system-mono",
169
+ fontSizePercent: import_shared2.FONT_SIZE_PERCENT_DEFAULT
170
+ },
171
+ settings: { validateOnSend: true, monacoConsumesWheel: false },
172
+ snapshots: { entries: [], maxBytes: 50 * 1024 * 1024 }
173
+ }
174
+ };
175
+ await (0, import_file_backed.saveToFile)(resolved, fresh);
176
+ return fresh;
177
+ }
178
+
179
+ // src/commands/mcp.ts
180
+ function registerMcpCommand(program) {
181
+ program.command("mcp").description("Run the APICircle MCP server (stdio transport)").option("-w, --workspace <dir>", "Workspace directory (defaults to current directory)").action(async (opts) => {
182
+ const dir = path3.resolve(opts.workspace ?? process.cwd());
183
+ try {
184
+ await ensureWorkspace(dir);
185
+ } catch (err) {
186
+ process.stderr.write(
187
+ `${import_kleur2.default.red("failed to initialise workspace")} at ${dir}: ${err instanceof Error ? err.message : String(err)}
188
+ `
189
+ );
190
+ process.exit(1);
191
+ }
192
+ const workspace = new import_mcp_server.FileBackedWorkspaceProvider(dir);
193
+ const mock = new import_mcp_server.InProcessMockController();
194
+ const host = (0, import_mcp_server.createMcpServer)({ workspace, mock });
195
+ process.stderr.write(`${import_kleur2.default.green("apicircle-mcp")} ready \xB7 workspace=${dir}
196
+ `);
197
+ await host.connect();
198
+ });
199
+ }
200
+
201
+ // src/commands/import.ts
202
+ var import_node_fs3 = require("fs");
203
+ var path4 = __toESM(require("path"), 1);
204
+ var import_kleur3 = __toESM(require("kleur"), 1);
205
+ var import_core = require("@apicircle/core");
206
+ var import_file_backed2 = require("@apicircle/core/workspace/file-backed");
207
+ var import_mock_server_core2 = require("@apicircle/mock-server-core");
208
+ var import_shared3 = require("@apicircle/shared");
209
+ function registerImportCommand(program) {
210
+ program.command("import").description("Import a spec into a workspace folder").argument("<type>", "Source type: openapi | postman | insomnia | curl").argument("<input>", "Path to a spec file, or `-` to read from stdin").option("-w, --workspace <dir>", "Workspace directory (defaults to current directory)").option("-f, --format <format>", "OpenAPI format: json | yaml", "json").action(async (type, input, opts) => {
211
+ const dir = path4.resolve(opts.workspace ?? process.cwd());
212
+ const raw = await readInput(input);
213
+ const state = await ensureWorkspace(dir);
214
+ let nextSynced = state.synced;
215
+ let nextLocal = state.local;
216
+ const created = [];
217
+ const append = (req) => {
218
+ const out = (0, import_core.applyMutation)(
219
+ { synced: nextSynced, local: nextLocal },
220
+ { kind: "request.create", request: req }
221
+ );
222
+ nextSynced = out.next.synced;
223
+ nextLocal = out.next.local;
224
+ created.push(req.id);
225
+ };
226
+ if (type === "curl") {
227
+ const { parseCurl } = await import("@apicircle/core");
228
+ const parsed = parseCurl(raw);
229
+ append(
230
+ blankRequest({
231
+ name: `cURL ${parsed.method} ${parsed.url}`.slice(0, 80),
232
+ method: parsed.method,
233
+ url: parsed.url,
234
+ headers: parsed.headers,
235
+ query: parsed.query,
236
+ body: parsed.body,
237
+ auth: parsed.auth
238
+ })
239
+ );
240
+ } else if (type === "openapi") {
241
+ const parsed = await (0, import_mock_server_core2.parseOpenApiToEndpoints)(raw, opts.format ?? "json");
242
+ for (const ep of parsed.endpoints) {
243
+ append(
244
+ blankRequest({
245
+ name: ep.example ?? `${ep.method} ${ep.pathPattern}`,
246
+ method: ep.method,
247
+ url: ep.pathPattern
248
+ })
249
+ );
250
+ }
251
+ } else if (type === "postman") {
252
+ const parsed = (0, import_mock_server_core2.parsePostmanToEndpoints)(raw);
253
+ for (const ep of parsed.endpoints) {
254
+ append(
255
+ blankRequest({
256
+ name: ep.example ?? `${ep.method} ${ep.pathPattern}`,
257
+ method: ep.method,
258
+ url: ep.pathPattern
259
+ })
260
+ );
261
+ }
262
+ } else if (type === "insomnia") {
263
+ const parsed = (0, import_mock_server_core2.parseInsomniaToEndpoints)(raw);
264
+ for (const ep of parsed.endpoints) {
265
+ append(
266
+ blankRequest({
267
+ name: ep.example ?? `${ep.method} ${ep.pathPattern}`,
268
+ method: ep.method,
269
+ url: ep.pathPattern
270
+ })
271
+ );
272
+ }
273
+ } else {
274
+ process.stderr.write(`${import_kleur3.default.red("error")}: unknown type '${String(type)}'
275
+ `);
276
+ process.exit(2);
277
+ }
278
+ await (0, import_file_backed2.saveToFile)(dir, { synced: nextSynced, local: nextLocal });
279
+ process.stdout.write(
280
+ `${import_kleur3.default.green("imported")} ${created.length} request${created.length === 1 ? "" : "s"} into ${dir}
281
+ `
282
+ );
283
+ });
284
+ }
285
+ async function readInput(p) {
286
+ if (p === "-") {
287
+ return new Promise((resolve7, reject) => {
288
+ let data = "";
289
+ process.stdin.setEncoding("utf-8");
290
+ process.stdin.on("data", (chunk) => {
291
+ data += typeof chunk === "string" ? chunk : chunk.toString("utf-8");
292
+ });
293
+ process.stdin.on("end", () => resolve7(data));
294
+ process.stdin.on("error", reject);
295
+ });
296
+ }
297
+ return import_node_fs3.promises.readFile(path4.resolve(p), "utf-8");
298
+ }
299
+ function blankRequest(partial) {
300
+ const now = (/* @__PURE__ */ new Date()).toISOString();
301
+ return {
302
+ id: (0, import_shared3.generateId)(),
303
+ folderId: null,
304
+ headers: [],
305
+ query: [],
306
+ body: { type: "none", content: "" },
307
+ auth: { type: "none" },
308
+ contextVars: [],
309
+ extractions: [],
310
+ assertions: [],
311
+ createdAt: now,
312
+ updatedAt: now,
313
+ ...partial
314
+ };
315
+ }
316
+
317
+ // src/commands/run.ts
318
+ var os = __toESM(require("os"), 1);
319
+ var path6 = __toESM(require("path"), 1);
320
+ var import_kleur4 = __toESM(require("kleur"), 1);
321
+ var import_core2 = require("@apicircle/core");
322
+ var import_file_backed3 = require("@apicircle/core/workspace/file-backed");
323
+
324
+ // src/util/secrets.ts
325
+ var path5 = __toESM(require("path"), 1);
326
+ var import_node_fs4 = require("fs");
327
+ var DEFAULT_PREFIX = "APICIRCLE_SECRET_";
328
+ async function buildSecretsFromCli(options = {}) {
329
+ const env = options.env ?? process.env;
330
+ const prefix = options.envPrefix ?? DEFAULT_PREFIX;
331
+ const byId = {};
332
+ if (options.secretsFile) {
333
+ const resolved = path5.resolve(options.secretsFile);
334
+ const raw = await import_node_fs4.promises.readFile(resolved, "utf8");
335
+ const parsed = JSON.parse(raw);
336
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
337
+ throw new Error(
338
+ `--secrets ${options.secretsFile}: expected an object mapping secretKeyId \u2192 value`
339
+ );
340
+ }
341
+ for (const [id, value] of Object.entries(parsed)) {
342
+ if (typeof value !== "string") {
343
+ throw new Error(`--secrets ${options.secretsFile}: value for "${id}" must be a string`);
344
+ }
345
+ byId[id] = value;
346
+ }
347
+ }
348
+ for (const [name, value] of Object.entries(env)) {
349
+ if (!name.startsWith(prefix) || typeof value !== "string") continue;
350
+ const id = name.slice(prefix.length);
351
+ if (id) byId[id] = value;
352
+ }
353
+ return { byId };
354
+ }
355
+
356
+ // src/commands/run.ts
357
+ var REPORTERS = ["text", "json", "junit"];
358
+ function registerRunCommand(program) {
359
+ program.command("run").description("Run a saved execution plan from a workspace and report the result").argument("<plan>", "Plan name or id to run").option("-w, --workspace <dir>", "Workspace directory (defaults to current directory)").option("--no-assertions", "Run requests without evaluating their assertions").option("-s, --secrets <file>", "JSON file mapping secretKeyId \u2192 plaintext value").option("--no-save", "Do not write the plan run to workspace history").option("--reporter <format>", "Report format: text | json | junit", "text").option("--bail", "Stop the run at the first failed step").option("-e, --env <name>", "Layer a local environment on top of the run").option("--as <actor>", "Override the recorded runner identity").action(async (planRef, opts) => {
360
+ const dir = path6.resolve(opts.workspace ?? process.cwd());
361
+ const reporter = opts.reporter ?? "text";
362
+ if (!isReporter(reporter)) {
363
+ fail(`unknown --reporter "${reporter}" (expected: ${REPORTERS.join(", ")})`);
364
+ return;
365
+ }
366
+ const state = await (0, import_file_backed3.loadFromFile)(dir, { allowMissing: true });
367
+ if (!state) {
368
+ fail(`no workspace found at ${dir} (expected workspace.synced.json)`);
369
+ return;
370
+ }
371
+ const ref = (0, import_core2.resolvePlanRef)(state.synced, planRef);
372
+ if (!ref.ok) {
373
+ fail(ref.error);
374
+ if (ref.available.length > 0) {
375
+ process.stderr.write(`Available plans: ${ref.available.join(", ")}
376
+ `);
377
+ }
378
+ return;
379
+ }
380
+ if (opts.env && !state.synced.environments.items[opts.env]) {
381
+ const names = Object.keys(state.synced.environments.items);
382
+ fail(`no environment named "${opts.env}" in this workspace`);
383
+ if (names.length > 0) {
384
+ process.stderr.write(`Available environments: ${names.join(", ")}
385
+ `);
386
+ }
387
+ return;
388
+ }
389
+ let secretsById;
390
+ try {
391
+ secretsById = (await buildSecretsFromCli({ secretsFile: opts.secrets })).byId;
392
+ } catch (err) {
393
+ fail(err instanceof Error ? err.message : String(err));
394
+ return;
395
+ }
396
+ const actor = resolveActor(state.local, opts.as);
397
+ const withAssertions = opts.assertions !== false;
398
+ const text = reporter === "text";
399
+ const controller = new AbortController();
400
+ const onSigint = () => controller.abort(new Error("aborted by SIGINT"));
401
+ process.on("SIGINT", onSigint);
402
+ if (text) process.stdout.write(formatHeader(ref.plan, actor, withAssertions, opts));
403
+ let result;
404
+ try {
405
+ result = await (0, import_core2.runPlan)(state, ref.id, {
406
+ withAssertions,
407
+ bail: opts.bail === true,
408
+ env: opts.env,
409
+ secretsById,
410
+ actor,
411
+ signal: controller.signal,
412
+ authorize: checkRunPermission,
413
+ onStep: text ? (step) => process.stdout.write(formatStepLine(step)) : void 0
414
+ });
415
+ } catch (err) {
416
+ process.off("SIGINT", onSigint);
417
+ if (err instanceof import_core2.PlanRunDeniedError) {
418
+ fail(err.message, 3, "denied");
419
+ return;
420
+ }
421
+ throw err;
422
+ }
423
+ process.off("SIGINT", onSigint);
424
+ const aborted = controller.signal.aborted;
425
+ const saved = opts.save !== false;
426
+ if (saved) await (0, import_file_backed3.saveToFile)(dir, result.nextState);
427
+ if (reporter === "json") {
428
+ process.stdout.write(
429
+ JSON.stringify(
430
+ buildJsonReport(dir, ref.id, ref.plan, actor, result, saved, aborted),
431
+ null,
432
+ 2
433
+ ) + "\n"
434
+ );
435
+ } else if (reporter === "junit") {
436
+ process.stdout.write(buildJunitReport(ref.plan, result));
437
+ } else {
438
+ process.stdout.write(formatSummary(result, saved, aborted));
439
+ }
440
+ process.exitCode = result.passed && !aborted ? 0 : 1;
441
+ });
442
+ }
443
+ function isReporter(value) {
444
+ return REPORTERS.includes(value);
445
+ }
446
+ function resolveActor(local, override) {
447
+ const explicit = override?.trim();
448
+ if (explicit) return { kind: "unknown", name: explicit };
449
+ const login = local.sessions.github.workspace?.accountLogin;
450
+ if (login) return { kind: "github", name: login };
451
+ try {
452
+ const username = os.userInfo().username;
453
+ if (username) return { kind: "os", name: username };
454
+ } catch {
455
+ }
456
+ return import_core2.ANONYMOUS_ACTOR;
457
+ }
458
+ function checkRunPermission(_ctx) {
459
+ }
460
+ function formatHeader(plan, actor, withAssertions, opts) {
461
+ const enabled = plan.steps.filter((s) => s.enabled !== false).length;
462
+ const flags = [
463
+ withAssertions ? "assertions on" : "assertions off",
464
+ opts.bail ? "bail" : null,
465
+ opts.env ? `env=${opts.env}` : null
466
+ ].filter((f) => f !== null);
467
+ return `${import_kleur4.default.bold("Plan")} ${plan.name} ${import_kleur4.default.dim(
468
+ `(${enabled}/${plan.steps.length} steps \xB7 ${flags.join(" \xB7 ")})`
469
+ )}
470
+ ${import_kleur4.default.dim("Run by")} ${actor.name} ${import_kleur4.default.dim(`(${actor.kind})`)}
471
+
472
+ `;
473
+ }
474
+ function formatStepLine(step) {
475
+ const n = `${step.stepIndex + 1}.`.padEnd(3);
476
+ const method = (step.requestMethod || "\u2014").padEnd(7);
477
+ if (step.skipped) {
478
+ return ` ${import_kleur4.default.dim("\u2013")} ${import_kleur4.default.dim(n)} ${import_kleur4.default.dim(method)} ${import_kleur4.default.dim(
479
+ `${step.requestName} skipped`
480
+ )}
481
+ `;
482
+ }
483
+ const mark = step.passed ? import_kleur4.default.green("\u2713") : import_kleur4.default.red("\u2717");
484
+ const status = step.result?.status != null ? String(step.result.status) : "\u2014";
485
+ const duration = step.result ? `${step.result.durationMs}ms` : "";
486
+ const name = step.requestName.padEnd(28);
487
+ let line = ` ${mark} ${n} ${method} ${name} ${status.padEnd(4)} ${import_kleur4.default.dim(duration)}`;
488
+ if (step.assertionResults.length > 0) {
489
+ const passed = step.assertionResults.filter((a) => a.passed).length;
490
+ line += ` ${import_kleur4.default.dim(`${passed}/${step.assertionResults.length} assertions`)}`;
491
+ }
492
+ line += "\n";
493
+ if (step.error) {
494
+ line += ` ${import_kleur4.default.red(step.error)}
495
+ `;
496
+ }
497
+ for (const a of step.assertionResults) {
498
+ if (!a.passed) line += ` ${import_kleur4.default.red("\u2717")} ${a.detail ?? `${a.kind} ${a.op}`}
499
+ `;
500
+ }
501
+ if (step.missingVariables.length > 0) {
502
+ line += ` ${import_kleur4.default.yellow("\u26A0")} unresolved: ${step.missingVariables.map((v) => `{{${v}}}`).join(", ")}
503
+ `;
504
+ }
505
+ return line;
506
+ }
507
+ function tally(result) {
508
+ let passed = 0;
509
+ let failed = 0;
510
+ let skipped = 0;
511
+ for (const s of result.steps) {
512
+ if (s.skipped) skipped++;
513
+ else if (s.passed) passed++;
514
+ else failed++;
515
+ }
516
+ return { passed, failed, skipped };
517
+ }
518
+ function formatSummary(result, saved, aborted) {
519
+ if (result.steps.length === 0) {
520
+ return `
521
+ ${import_kleur4.default.yellow("Plan has no steps.")}
522
+ `;
523
+ }
524
+ const { passed, failed, skipped } = tally(result);
525
+ const parts = [
526
+ import_kleur4.default.green(`${passed} passed`),
527
+ failed > 0 ? import_kleur4.default.red(`${failed} failed`) : import_kleur4.default.dim(`${failed} failed`),
528
+ import_kleur4.default.dim(`${skipped} skipped`)
529
+ ];
530
+ const verdict = result.passed && !aborted ? import_kleur4.default.green("PASS") : import_kleur4.default.red("FAIL");
531
+ let out = `
532
+ ${verdict} ${parts.join(import_kleur4.default.dim(" \xB7 "))} ${import_kleur4.default.dim(
533
+ `\xB7 ${result.planRun.durationMs}ms`
534
+ )}
535
+ `;
536
+ if (aborted) out += `${import_kleur4.default.yellow("Run aborted before every step finished.")}
537
+ `;
538
+ out += saved ? import_kleur4.default.dim("Plan run saved to workspace history.\n") : import_kleur4.default.dim("Plan run not saved (--no-save).\n");
539
+ return out;
540
+ }
541
+ function buildJsonReport(workspace, planId, plan, actor, result, saved, aborted) {
542
+ return {
543
+ workspace,
544
+ plan: { id: planId, name: plan.name },
545
+ actor,
546
+ withAssertions: result.planRun.withAssertions,
547
+ passed: result.passed && !aborted,
548
+ aborted,
549
+ durationMs: result.planRun.durationMs,
550
+ saved,
551
+ counts: tally(result),
552
+ steps: result.steps.map((s) => ({
553
+ step: s.stepIndex + 1,
554
+ request: s.requestName,
555
+ method: s.requestMethod,
556
+ skipped: s.skipped,
557
+ status: s.result?.status ?? null,
558
+ ok: s.result?.ok ?? false,
559
+ durationMs: s.result?.durationMs ?? 0,
560
+ passed: s.passed,
561
+ error: s.error ?? null,
562
+ missingVariables: s.missingVariables,
563
+ assertions: s.assertionResults.map((a) => ({
564
+ kind: a.kind,
565
+ op: a.op,
566
+ target: a.target,
567
+ expected: a.expected,
568
+ passed: a.passed,
569
+ detail: a.detail
570
+ }))
571
+ }))
572
+ };
573
+ }
574
+ function xmlEscape(value) {
575
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
576
+ }
577
+ function buildJunitReport(plan, result) {
578
+ const { failed, skipped } = tally(result);
579
+ const total = result.steps.length;
580
+ const suite = xmlEscape(plan.name);
581
+ const suiteTime = (result.planRun.durationMs / 1e3).toFixed(3);
582
+ const cases = result.steps.map((s) => {
583
+ const name = xmlEscape(`${s.stepIndex + 1}. ${s.requestName}`);
584
+ const time = ((s.result?.durationMs ?? 0) / 1e3).toFixed(3);
585
+ const open = ` <testcase name="${name}" classname="${suite}" time="${time}"`;
586
+ if (s.skipped) return `${open}>
587
+ <skipped/>
588
+ </testcase>`;
589
+ if (s.passed) return `${open}/>`;
590
+ const reasons = [];
591
+ if (s.error) reasons.push(s.error);
592
+ for (const a of s.assertionResults) {
593
+ if (!a.passed) reasons.push(a.detail ?? `assertion ${a.kind} ${a.op} failed`);
594
+ }
595
+ if (s.result && !s.result.ok && s.result.status != null) {
596
+ reasons.push(`HTTP ${s.result.status}`);
597
+ }
598
+ const detail = xmlEscape(reasons.join("\n") || "step failed");
599
+ const summary = detail.split("\n")[0];
600
+ return `${open}>
601
+ <failure message="${summary}">${detail}</failure>
602
+ </testcase>`;
603
+ });
604
+ return `<?xml version="1.0" encoding="UTF-8"?>
605
+ <testsuites name="${suite}" tests="${total}" failures="${failed}" skipped="${skipped}" time="${suiteTime}">
606
+ <testsuite name="${suite}" tests="${total}" failures="${failed}" skipped="${skipped}" time="${suiteTime}">
607
+ ${cases.join("\n")}
608
+ </testsuite>
609
+ </testsuites>
610
+ `;
611
+ }
612
+ function fail(message, code = 2, kind = "error") {
613
+ process.stderr.write(`${import_kleur4.default.red(kind)}: ${message}
614
+ `);
615
+ process.exitCode = code;
616
+ }
617
+
618
+ // src/index.ts
619
+ function buildProgram() {
620
+ const program = new import_commander.Command();
621
+ program.name("apicircle").description("Command-line companion to APICircle Studio.").version("1.0.0");
622
+ registerMockCommand(program);
623
+ registerMcpCommand(program);
624
+ registerImportCommand(program);
625
+ registerRunCommand(program);
626
+ return program;
627
+ }
628
+ async function runCli(argv = process.argv) {
629
+ await buildProgram().parseAsync(argv);
630
+ }
631
+ var entry = process.argv[1] ?? "";
632
+ if (entry.endsWith("apicircle") || entry.endsWith("index.cjs") || entry.endsWith("index.ts")) {
633
+ void runCli();
634
+ }
635
+ // Annotate the CommonJS export names for ESM import in node:
636
+ 0 && (module.exports = {
637
+ buildProgram,
638
+ runCli
639
+ });
640
+ //# sourceMappingURL=index.cjs.map