loadam 0.2.0 → 0.2.2
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/bin.js +944 -115
- package/dist/bin.js.map +1 -1
- package/dist/index.js +944 -115
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/bin.js
CHANGED
|
@@ -271,6 +271,257 @@ function readStdin() {
|
|
|
271
271
|
});
|
|
272
272
|
}
|
|
273
273
|
|
|
274
|
+
// src/commands/clean.ts
|
|
275
|
+
import { resolve as resolve2 } from "path";
|
|
276
|
+
|
|
277
|
+
// src/session/session.ts
|
|
278
|
+
import { createHash } from "crypto";
|
|
279
|
+
import { mkdir, readFile as readFile2, readdir, rm, stat, writeFile } from "fs/promises";
|
|
280
|
+
import { join, resolve } from "path";
|
|
281
|
+
var SESSION_SCHEMA_VERSION = 1;
|
|
282
|
+
var SESSIONS_DIR = "sessions";
|
|
283
|
+
async function createSession(input) {
|
|
284
|
+
const startedAt = /* @__PURE__ */ new Date();
|
|
285
|
+
const slug = input.slug ? sanitizeSlug(input.slug) : input.command;
|
|
286
|
+
const id = `${formatTimestamp(startedAt)}-${slug}`;
|
|
287
|
+
const root = resolve(input.outRoot, SESSIONS_DIR);
|
|
288
|
+
const dir = resolve(root, id);
|
|
289
|
+
await mkdir(dir, { recursive: true });
|
|
290
|
+
await ensureGitignore(root);
|
|
291
|
+
const specHash = sha256(input.specSource);
|
|
292
|
+
const irDigest = sha256(input.irJson);
|
|
293
|
+
const meta = {
|
|
294
|
+
schemaVersion: SESSION_SCHEMA_VERSION,
|
|
295
|
+
id,
|
|
296
|
+
command: input.command,
|
|
297
|
+
flags: sanitizeFlags(input.flags ?? {}),
|
|
298
|
+
spec: {
|
|
299
|
+
path: input.specPath,
|
|
300
|
+
sha256: specHash,
|
|
301
|
+
title: input.ir.meta.title,
|
|
302
|
+
version: input.ir.meta.version
|
|
303
|
+
},
|
|
304
|
+
irDigest,
|
|
305
|
+
target: input.target ?? null,
|
|
306
|
+
envVars: input.envVars ?? [],
|
|
307
|
+
startedAt: startedAt.toISOString(),
|
|
308
|
+
artefacts: ["ir.json", "meta.json"]
|
|
309
|
+
};
|
|
310
|
+
await writeFile(join(dir, "ir.json"), input.irJson, "utf8");
|
|
311
|
+
await writeFile(join(dir, "meta.json"), JSON.stringify(meta, null, 2), "utf8");
|
|
312
|
+
return {
|
|
313
|
+
id,
|
|
314
|
+
dir,
|
|
315
|
+
meta,
|
|
316
|
+
async addArtefact(relPath, contents) {
|
|
317
|
+
const full = join(dir, relPath);
|
|
318
|
+
await mkdir(join(full, "..").replace(/\/+$/, ""), { recursive: true });
|
|
319
|
+
await writeFile(full, contents);
|
|
320
|
+
if (!meta.artefacts.includes(relPath)) meta.artefacts.push(relPath);
|
|
321
|
+
},
|
|
322
|
+
async finalize({ exitCode, thresholds, summary }) {
|
|
323
|
+
const endedAt = /* @__PURE__ */ new Date();
|
|
324
|
+
meta.endedAt = endedAt.toISOString();
|
|
325
|
+
meta.durationMs = endedAt.getTime() - startedAt.getTime();
|
|
326
|
+
meta.exitCode = exitCode;
|
|
327
|
+
if (thresholds) meta.thresholds = thresholds;
|
|
328
|
+
if (summary) meta.summary = summary;
|
|
329
|
+
await writeFile(join(dir, "meta.json"), JSON.stringify(meta, null, 2), "utf8");
|
|
330
|
+
return meta;
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
async function listSessions(outRoot) {
|
|
335
|
+
const root = resolve(outRoot, SESSIONS_DIR);
|
|
336
|
+
let entries;
|
|
337
|
+
try {
|
|
338
|
+
entries = await readdir(root);
|
|
339
|
+
} catch {
|
|
340
|
+
return [];
|
|
341
|
+
}
|
|
342
|
+
const metas = [];
|
|
343
|
+
for (const entry of entries) {
|
|
344
|
+
const meta = await loadMeta(join(root, entry));
|
|
345
|
+
if (meta) metas.push(meta);
|
|
346
|
+
}
|
|
347
|
+
return metas.sort((a, b) => a.startedAt < b.startedAt ? 1 : -1);
|
|
348
|
+
}
|
|
349
|
+
async function resolveSessionId(outRoot, idOrPrefix) {
|
|
350
|
+
const all = await listSessions(outRoot);
|
|
351
|
+
if (all.length === 0) {
|
|
352
|
+
throw new Error("No sessions found. Run `loadam test`/`contract`/`diff` first.");
|
|
353
|
+
}
|
|
354
|
+
if (idOrPrefix === "latest") return all[0];
|
|
355
|
+
const exact = all.find((m) => m.id === idOrPrefix);
|
|
356
|
+
if (exact) return exact;
|
|
357
|
+
const prefix = all.filter((m) => m.id.startsWith(idOrPrefix));
|
|
358
|
+
if (prefix.length === 0) throw new Error(`No session matched "${idOrPrefix}".`);
|
|
359
|
+
if (prefix.length > 1) {
|
|
360
|
+
const ids = prefix.map((m) => m.id).join("\n ");
|
|
361
|
+
throw new Error(`Ambiguous session prefix "${idOrPrefix}", matches:
|
|
362
|
+
${ids}`);
|
|
363
|
+
}
|
|
364
|
+
return prefix[0];
|
|
365
|
+
}
|
|
366
|
+
async function cleanSessions(outRoot, opts) {
|
|
367
|
+
const all = await listSessions(outRoot);
|
|
368
|
+
const root = resolve(outRoot, SESSIONS_DIR);
|
|
369
|
+
const now = Date.now();
|
|
370
|
+
const hasAge = typeof opts.olderThanMs === "number";
|
|
371
|
+
const hasKeep = typeof opts.keep === "number";
|
|
372
|
+
const ageCut = opts.olderThanMs ?? Number.POSITIVE_INFINITY;
|
|
373
|
+
const keep = opts.keep ?? 0;
|
|
374
|
+
if (!hasAge && !hasKeep) {
|
|
375
|
+
return { deleted: [], kept: all.map((m) => m.id) };
|
|
376
|
+
}
|
|
377
|
+
const deleted = [];
|
|
378
|
+
const kept = [];
|
|
379
|
+
all.forEach((m, index) => {
|
|
380
|
+
if (hasKeep && index < keep) {
|
|
381
|
+
kept.push(m.id);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const age = now - new Date(m.startedAt).getTime();
|
|
385
|
+
const expired = hasAge && age >= ageCut;
|
|
386
|
+
const excess = hasKeep && index >= keep;
|
|
387
|
+
if (expired || excess) deleted.push(m.id);
|
|
388
|
+
else kept.push(m.id);
|
|
389
|
+
});
|
|
390
|
+
if (opts.apply) {
|
|
391
|
+
for (const id of deleted) {
|
|
392
|
+
await rm(join(root, id), { recursive: true, force: true });
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return { deleted, kept };
|
|
396
|
+
}
|
|
397
|
+
async function loadMeta(dir) {
|
|
398
|
+
try {
|
|
399
|
+
const stats = await stat(dir);
|
|
400
|
+
if (!stats.isDirectory()) return null;
|
|
401
|
+
const text = await readFile2(join(dir, "meta.json"), "utf8");
|
|
402
|
+
const parsed = JSON.parse(text);
|
|
403
|
+
if (parsed?.schemaVersion === SESSION_SCHEMA_VERSION && typeof parsed.id === "string") {
|
|
404
|
+
return parsed;
|
|
405
|
+
}
|
|
406
|
+
} catch {
|
|
407
|
+
}
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
function sha256(text) {
|
|
411
|
+
return createHash("sha256").update(text).digest("hex");
|
|
412
|
+
}
|
|
413
|
+
function formatTimestamp(d) {
|
|
414
|
+
return d.toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
|
415
|
+
}
|
|
416
|
+
async function ensureGitignore(root) {
|
|
417
|
+
const path = join(root, ".gitignore");
|
|
418
|
+
try {
|
|
419
|
+
await readFile2(path, "utf8");
|
|
420
|
+
} catch {
|
|
421
|
+
await writeFile(
|
|
422
|
+
path,
|
|
423
|
+
"# Auto-generated by loadam \u2014 sessions can contain secrets, do not commit.\n*\n!.gitignore\n",
|
|
424
|
+
"utf8"
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function sanitizeSlug(s) {
|
|
429
|
+
return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 40);
|
|
430
|
+
}
|
|
431
|
+
var SECRET_LIKE = /(token|secret|password|pass|key|credential|auth)/i;
|
|
432
|
+
function sanitizeFlags(flags) {
|
|
433
|
+
const out = {};
|
|
434
|
+
for (const [k, v] of Object.entries(flags)) {
|
|
435
|
+
if (SECRET_LIKE.test(k)) {
|
|
436
|
+
out[k] = "[redacted]";
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (v === null || typeof v === "boolean" || typeof v === "number") {
|
|
440
|
+
out[k] = v;
|
|
441
|
+
} else if (typeof v === "string") {
|
|
442
|
+
out[k] = v;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return out;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// src/util/output.ts
|
|
449
|
+
import { consola as consola3 } from "consola";
|
|
450
|
+
function makeOutput(jsonMode) {
|
|
451
|
+
if (jsonMode) {
|
|
452
|
+
return {
|
|
453
|
+
json: true,
|
|
454
|
+
start: () => void 0,
|
|
455
|
+
info: () => void 0,
|
|
456
|
+
success: () => void 0,
|
|
457
|
+
step: () => void 0,
|
|
458
|
+
result(payload) {
|
|
459
|
+
process.stdout.write(`${JSON.stringify(payload, null, 2)}
|
|
460
|
+
`);
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
json: false,
|
|
466
|
+
start: (m) => consola3.start(m),
|
|
467
|
+
info: (m) => consola3.info(m),
|
|
468
|
+
success: (m) => consola3.success(m),
|
|
469
|
+
step: (m) => consola3.log(m),
|
|
470
|
+
result: () => void 0
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// src/commands/clean.ts
|
|
475
|
+
var DURATION_RE = /^(\d+)([smhd])$/;
|
|
476
|
+
function parseDuration(raw) {
|
|
477
|
+
const m = DURATION_RE.exec(raw.trim());
|
|
478
|
+
if (!m) {
|
|
479
|
+
throw new Error(`Invalid --older-than "${raw}". Use 30d, 12h, 45m, 90s.`);
|
|
480
|
+
}
|
|
481
|
+
const n = Number.parseInt(m[1], 10);
|
|
482
|
+
const unit = m[2];
|
|
483
|
+
const factor = unit === "s" ? 1e3 : unit === "m" ? 6e4 : unit === "h" ? 36e5 : 864e5;
|
|
484
|
+
return n * factor;
|
|
485
|
+
}
|
|
486
|
+
function registerCleanCommand(program) {
|
|
487
|
+
program.command("clean").description("Delete archived sessions older than a threshold or beyond a keep count.").option("--root <dir>", "session archive root", "./loadam-out").option("--keep <n>", "always keep at most N most-recent sessions").option("--older-than <duration>", "delete sessions older than this (e.g. 30d, 12h)", "30d").option("-y, --yes", "actually delete (default: dry-run)", false).option("--json", "emit a JSON summary on stdout", false).action(withFriendlyErrors(runClean));
|
|
488
|
+
}
|
|
489
|
+
async function runClean(opts) {
|
|
490
|
+
const out = makeOutput(!!opts.json);
|
|
491
|
+
const root = resolve2(opts.root);
|
|
492
|
+
const olderThanMs = opts.olderThan ? parseDuration(opts.olderThan) : void 0;
|
|
493
|
+
const keep = opts.keep ? Number.parseInt(opts.keep, 10) : void 0;
|
|
494
|
+
const result = await cleanSessions(root, {
|
|
495
|
+
olderThanMs,
|
|
496
|
+
keep: Number.isFinite(keep) ? keep : void 0,
|
|
497
|
+
apply: !!opts.yes
|
|
498
|
+
});
|
|
499
|
+
if (out.json) {
|
|
500
|
+
out.result({
|
|
501
|
+
command: "clean",
|
|
502
|
+
root,
|
|
503
|
+
apply: !!opts.yes,
|
|
504
|
+
kept: result.kept.length,
|
|
505
|
+
deleted: result.deleted.length,
|
|
506
|
+
deletedIds: result.deleted
|
|
507
|
+
});
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (result.deleted.length === 0) {
|
|
511
|
+
out.info(`Nothing to clean. ${result.kept.length} session(s) kept.`);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (opts.yes) {
|
|
515
|
+
out.success(`Deleted ${result.deleted.length} session(s); ${result.kept.length} kept.`);
|
|
516
|
+
} else {
|
|
517
|
+
out.info(
|
|
518
|
+
`Dry-run: would delete ${result.deleted.length} session(s); ${result.kept.length} kept.`
|
|
519
|
+
);
|
|
520
|
+
out.info("Pass --yes to actually delete.");
|
|
521
|
+
}
|
|
522
|
+
for (const id of result.deleted) out.info(` - ${id}`);
|
|
523
|
+
}
|
|
524
|
+
|
|
274
525
|
// src/commands/completion.ts
|
|
275
526
|
var BASH = `# loadam bash completion. Install:
|
|
276
527
|
# loadam completion bash > ~/.loadam-completion.bash
|
|
@@ -343,13 +594,13 @@ function registerCompletionCommand(program) {
|
|
|
343
594
|
}
|
|
344
595
|
|
|
345
596
|
// src/commands/contract.ts
|
|
346
|
-
import { mkdir, readFile as
|
|
347
|
-
import { dirname, resolve as
|
|
597
|
+
import { mkdir as mkdir2, readFile as readFile4, writeFile as writeFile2 } from "fs/promises";
|
|
598
|
+
import { dirname, resolve as resolve4 } from "path";
|
|
348
599
|
|
|
349
600
|
// ../core/dist/index.js
|
|
350
601
|
import { z } from "zod";
|
|
351
|
-
import { readFile as
|
|
352
|
-
import { resolve } from "path";
|
|
602
|
+
import { readFile as readFile3 } from "fs/promises";
|
|
603
|
+
import { resolve as resolve3 } from "path";
|
|
353
604
|
import SwaggerParser from "@apidevtools/swagger-parser";
|
|
354
605
|
import { parse as parseYaml } from "yaml";
|
|
355
606
|
var IR_VERSION = "1";
|
|
@@ -607,8 +858,8 @@ var HTTP_METHODS = [
|
|
|
607
858
|
"OPTIONS"
|
|
608
859
|
];
|
|
609
860
|
async function fromOpenApiFile(path, opts = {}) {
|
|
610
|
-
const absolute =
|
|
611
|
-
const raw = await
|
|
861
|
+
const absolute = resolve3(path);
|
|
862
|
+
const raw = await readFile3(absolute, "utf8");
|
|
612
863
|
const doc = parseDocument(raw);
|
|
613
864
|
const bundled = await SwaggerParser.bundle(doc);
|
|
614
865
|
return fromOpenApi(bundled, { sourceUri: opts.sourceUri ?? absolute });
|
|
@@ -1355,32 +1606,6 @@ function slugify(s) {
|
|
|
1355
1606
|
return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
1356
1607
|
}
|
|
1357
1608
|
|
|
1358
|
-
// src/util/output.ts
|
|
1359
|
-
import { consola as consola3 } from "consola";
|
|
1360
|
-
function makeOutput(jsonMode) {
|
|
1361
|
-
if (jsonMode) {
|
|
1362
|
-
return {
|
|
1363
|
-
json: true,
|
|
1364
|
-
start: () => void 0,
|
|
1365
|
-
info: () => void 0,
|
|
1366
|
-
success: () => void 0,
|
|
1367
|
-
step: () => void 0,
|
|
1368
|
-
result(payload) {
|
|
1369
|
-
process.stdout.write(`${JSON.stringify(payload, null, 2)}
|
|
1370
|
-
`);
|
|
1371
|
-
}
|
|
1372
|
-
};
|
|
1373
|
-
}
|
|
1374
|
-
return {
|
|
1375
|
-
json: false,
|
|
1376
|
-
start: (m) => consola3.start(m),
|
|
1377
|
-
info: (m) => consola3.info(m),
|
|
1378
|
-
success: (m) => consola3.success(m),
|
|
1379
|
-
step: (m) => consola3.log(m),
|
|
1380
|
-
result: () => void 0
|
|
1381
|
-
};
|
|
1382
|
-
}
|
|
1383
|
-
|
|
1384
1609
|
// src/commands/contract.ts
|
|
1385
1610
|
function registerContractCommand(program) {
|
|
1386
1611
|
program.command("contract").description("Compile an OpenAPI spec into a Schemathesis property-based contract suite.").argument("<spec>", "path to OpenAPI 3.x spec (YAML or JSON)").option(
|
|
@@ -1391,28 +1616,52 @@ function registerContractCommand(program) {
|
|
|
1391
1616
|
}
|
|
1392
1617
|
async function runContract(spec, opts) {
|
|
1393
1618
|
const out = makeOutput(!!opts.json);
|
|
1394
|
-
const specPath =
|
|
1619
|
+
const specPath = resolve4(spec);
|
|
1395
1620
|
out.start(`Parsing OpenAPI spec: ${specPath}`);
|
|
1396
1621
|
const ir = await fromOpenApiFile(specPath);
|
|
1397
1622
|
inferResourceGraph(ir);
|
|
1398
1623
|
parseIR(ir);
|
|
1399
|
-
const specSource = await
|
|
1624
|
+
const specSource = await readFile4(specPath, "utf8");
|
|
1400
1625
|
const examples = Number.parseInt(opts.examples ?? "25", 10);
|
|
1401
1626
|
const result = compileContract(ir, specSource, {
|
|
1402
1627
|
baseUrl: opts.target,
|
|
1403
1628
|
examples: Number.isFinite(examples) ? examples : 25
|
|
1404
1629
|
});
|
|
1405
|
-
const outDir =
|
|
1406
|
-
await
|
|
1630
|
+
const outDir = resolve4(opts.output);
|
|
1631
|
+
await mkdir2(outDir, { recursive: true });
|
|
1407
1632
|
for (const [rel, contents] of Object.entries(result.files)) {
|
|
1408
|
-
const full =
|
|
1409
|
-
await
|
|
1410
|
-
await
|
|
1633
|
+
const full = resolve4(outDir, rel);
|
|
1634
|
+
await mkdir2(dirname(full), { recursive: true });
|
|
1635
|
+
await writeFile2(full, contents, "utf8");
|
|
1411
1636
|
}
|
|
1412
1637
|
const fileCount = Object.keys(result.files).length;
|
|
1413
1638
|
out.success(`Wrote ${fileCount} files to ${outDir}`);
|
|
1414
1639
|
out.info(` ${ir.operations.length} operations to fuzz`);
|
|
1640
|
+
const session = await createSession({
|
|
1641
|
+
command: "contract",
|
|
1642
|
+
outRoot: resolve4(outDir, ".."),
|
|
1643
|
+
specPath,
|
|
1644
|
+
specSource,
|
|
1645
|
+
ir,
|
|
1646
|
+
irJson: JSON.stringify(ir, null, 2),
|
|
1647
|
+
target: opts.target ?? null,
|
|
1648
|
+
envVars: result.envVars ?? [],
|
|
1649
|
+
flags: {
|
|
1650
|
+
target: opts.target ?? null,
|
|
1651
|
+
examples
|
|
1652
|
+
},
|
|
1653
|
+
slug: `contract-${ir.meta.title ?? "run"}`
|
|
1654
|
+
});
|
|
1655
|
+
await session.finalize({
|
|
1656
|
+
exitCode: 0,
|
|
1657
|
+
thresholds: { passed: [], failed: [] },
|
|
1658
|
+
summary: {
|
|
1659
|
+
files: fileCount,
|
|
1660
|
+
operations: ir.operations.length
|
|
1661
|
+
}
|
|
1662
|
+
});
|
|
1415
1663
|
if (!out.json) {
|
|
1664
|
+
out.info(` Session: ${session.id}`);
|
|
1416
1665
|
out.step("");
|
|
1417
1666
|
out.step("Next steps:");
|
|
1418
1667
|
out.step(` cd ${opts.output}`);
|
|
@@ -1425,13 +1674,14 @@ async function runContract(spec, opts) {
|
|
|
1425
1674
|
outputDir: outDir,
|
|
1426
1675
|
files: fileCount,
|
|
1427
1676
|
operations: ir.operations.length,
|
|
1428
|
-
envVars: result.envVars
|
|
1677
|
+
envVars: result.envVars,
|
|
1678
|
+
sessionId: session.id
|
|
1429
1679
|
});
|
|
1430
1680
|
}
|
|
1431
1681
|
|
|
1432
1682
|
// src/commands/diff.ts
|
|
1433
|
-
import { writeFile as
|
|
1434
|
-
import { resolve as
|
|
1683
|
+
import { readFile as readFile5, writeFile as writeFile3 } from "fs/promises";
|
|
1684
|
+
import { resolve as resolve5 } from "path";
|
|
1435
1685
|
|
|
1436
1686
|
// ../test-drift/dist/index.js
|
|
1437
1687
|
import Ajv from "ajv";
|
|
@@ -1686,8 +1936,9 @@ function registerDiffCommand(program) {
|
|
|
1686
1936
|
}
|
|
1687
1937
|
async function runDiff(spec, opts) {
|
|
1688
1938
|
const out = makeOutput(!!opts.json);
|
|
1689
|
-
const specPath =
|
|
1939
|
+
const specPath = resolve5(spec);
|
|
1690
1940
|
out.start(`Parsing OpenAPI spec: ${specPath}`);
|
|
1941
|
+
const specSource = await readFile5(specPath, "utf8");
|
|
1691
1942
|
const ir = await fromOpenApiFile(specPath);
|
|
1692
1943
|
inferResourceGraph(ir);
|
|
1693
1944
|
parseIR(ir);
|
|
@@ -1704,16 +1955,40 @@ async function runDiff(spec, opts) {
|
|
|
1704
1955
|
});
|
|
1705
1956
|
const findings = compareProbes({ ir, probes });
|
|
1706
1957
|
const counts = countBySeverity(findings);
|
|
1958
|
+
const session = await createSession({
|
|
1959
|
+
command: "diff",
|
|
1960
|
+
outRoot: resolve5("./loadam-out"),
|
|
1961
|
+
specPath,
|
|
1962
|
+
specSource,
|
|
1963
|
+
ir,
|
|
1964
|
+
irJson: JSON.stringify(ir, null, 2),
|
|
1965
|
+
target: opts.target,
|
|
1966
|
+
envVars: [],
|
|
1967
|
+
flags: {
|
|
1968
|
+
target: opts.target,
|
|
1969
|
+
timeoutMs,
|
|
1970
|
+
mutating: !!opts.mutating,
|
|
1971
|
+
failOn: opts.failOn ?? "error",
|
|
1972
|
+
headers: Object.keys(headers),
|
|
1973
|
+
pathParams: Object.keys(pathParams)
|
|
1974
|
+
},
|
|
1975
|
+
slug: `diff-${ir.meta.title ?? "run"}`
|
|
1976
|
+
});
|
|
1977
|
+
const report = renderMarkdownReport({
|
|
1978
|
+
ir,
|
|
1979
|
+
baseUrl: opts.target,
|
|
1980
|
+
findings,
|
|
1981
|
+
skipped
|
|
1982
|
+
});
|
|
1983
|
+
await session.addArtefact("drift.md", report);
|
|
1984
|
+
await session.addArtefact(
|
|
1985
|
+
"findings.json",
|
|
1986
|
+
JSON.stringify({ findings, skipped, counts }, null, 2)
|
|
1987
|
+
);
|
|
1707
1988
|
if (!out.json) {
|
|
1708
|
-
const report = renderMarkdownReport({
|
|
1709
|
-
ir,
|
|
1710
|
-
baseUrl: opts.target,
|
|
1711
|
-
findings,
|
|
1712
|
-
skipped
|
|
1713
|
-
});
|
|
1714
1989
|
if (opts.output) {
|
|
1715
|
-
const outFile =
|
|
1716
|
-
await
|
|
1990
|
+
const outFile = resolve5(opts.output);
|
|
1991
|
+
await writeFile3(outFile, report, "utf8");
|
|
1717
1992
|
out.success(`Wrote drift report to ${outFile}`);
|
|
1718
1993
|
} else {
|
|
1719
1994
|
process.stdout.write(report);
|
|
@@ -1721,17 +1996,24 @@ async function runDiff(spec, opts) {
|
|
|
1721
1996
|
out.info(
|
|
1722
1997
|
` ${probes.length} probed \xB7 ${skipped.length} skipped \xB7 ${counts.error} error \xB7 ${counts.warning} warning \xB7 ${counts.info} info`
|
|
1723
1998
|
);
|
|
1999
|
+
out.info(` Session: ${session.id}`);
|
|
1724
2000
|
} else if (opts.output) {
|
|
1725
|
-
|
|
1726
|
-
ir,
|
|
1727
|
-
baseUrl: opts.target,
|
|
1728
|
-
findings,
|
|
1729
|
-
skipped
|
|
1730
|
-
});
|
|
1731
|
-
await writeFile2(resolve3(opts.output), report, "utf8");
|
|
2001
|
+
await writeFile3(resolve5(opts.output), report, "utf8");
|
|
1732
2002
|
}
|
|
1733
2003
|
const failOn = opts.failOn ?? "error";
|
|
1734
2004
|
const failed = failOn === "error" && counts.error > 0 || failOn === "warning" && counts.error + counts.warning > 0;
|
|
2005
|
+
await session.finalize({
|
|
2006
|
+
exitCode: failed ? 1 : 0,
|
|
2007
|
+
thresholds: { passed: [], failed: [] },
|
|
2008
|
+
summary: {
|
|
2009
|
+
probed: probes.length,
|
|
2010
|
+
skipped: skipped.length,
|
|
2011
|
+
findings: findings.length,
|
|
2012
|
+
error: counts.error,
|
|
2013
|
+
warning: counts.warning,
|
|
2014
|
+
info: counts.info
|
|
2015
|
+
}
|
|
2016
|
+
});
|
|
1735
2017
|
out.result({
|
|
1736
2018
|
command: "diff",
|
|
1737
2019
|
target: opts.target,
|
|
@@ -1739,9 +2021,10 @@ async function runDiff(spec, opts) {
|
|
|
1739
2021
|
skipped: skipped.length,
|
|
1740
2022
|
findings: findings.length,
|
|
1741
2023
|
counts,
|
|
1742
|
-
reportPath: opts.output ?
|
|
2024
|
+
reportPath: opts.output ? resolve5(opts.output) : null,
|
|
1743
2025
|
failOn,
|
|
1744
|
-
failed
|
|
2026
|
+
failed,
|
|
2027
|
+
sessionId: session.id
|
|
1745
2028
|
});
|
|
1746
2029
|
if (failOn !== "never" && failed) process.exit(1);
|
|
1747
2030
|
}
|
|
@@ -1757,21 +2040,54 @@ function parseKvPairs(values, sep) {
|
|
|
1757
2040
|
return out;
|
|
1758
2041
|
}
|
|
1759
2042
|
|
|
2043
|
+
// src/commands/history.ts
|
|
2044
|
+
import { resolve as resolve6 } from "path";
|
|
2045
|
+
function registerHistoryCommand(program) {
|
|
2046
|
+
program.command("history").description("List archived loadam sessions (newest first).").option("--root <dir>", "session archive root", "./loadam-out").option("--limit <n>", "show at most N sessions", "20").option("--command <cmd>", "filter by command (test|contract|diff)").option("--json", "emit a JSON array on stdout", false).action(withFriendlyErrors(runHistory));
|
|
2047
|
+
}
|
|
2048
|
+
async function runHistory(opts) {
|
|
2049
|
+
const out = makeOutput(!!opts.json);
|
|
2050
|
+
const root = resolve6(opts.root);
|
|
2051
|
+
const limit = Number.parseInt(opts.limit ?? "20", 10);
|
|
2052
|
+
const all = await listSessions(root);
|
|
2053
|
+
const filtered = opts.command ? all.filter((s) => s.command === opts.command) : all;
|
|
2054
|
+
const shown = filtered.slice(0, Number.isFinite(limit) ? limit : 20);
|
|
2055
|
+
if (out.json) {
|
|
2056
|
+
out.result({ command: "history", root, total: filtered.length, sessions: shown });
|
|
2057
|
+
return;
|
|
2058
|
+
}
|
|
2059
|
+
if (shown.length === 0) {
|
|
2060
|
+
out.info("No sessions found. Run `loadam test`/`contract`/`diff` first.");
|
|
2061
|
+
return;
|
|
2062
|
+
}
|
|
2063
|
+
out.info(" ID CMD EXIT WHEN");
|
|
2064
|
+
for (const s of shown) {
|
|
2065
|
+
const idCol = s.id.padEnd(60).slice(0, 60);
|
|
2066
|
+
const cmdCol = s.command.padEnd(8);
|
|
2067
|
+
const exitCol = (s.exitCode ?? "\u2014").toString().padEnd(6);
|
|
2068
|
+
const when = s.startedAt;
|
|
2069
|
+
out.info(` ${idCol} ${cmdCol} ${exitCol} ${when}`);
|
|
2070
|
+
}
|
|
2071
|
+
if (filtered.length > shown.length) {
|
|
2072
|
+
out.info(` \u2026 ${filtered.length - shown.length} more (raise --limit to see them)`);
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
|
|
1760
2076
|
// src/commands/init.ts
|
|
1761
|
-
import { writeFile as
|
|
1762
|
-
import { resolve as
|
|
2077
|
+
import { writeFile as writeFile4 } from "fs/promises";
|
|
2078
|
+
import { resolve as resolve7 } from "path";
|
|
1763
2079
|
function registerInitCommand(program) {
|
|
1764
2080
|
program.command("init").description("Parse a spec and emit loadam.ir.json with inferred resource graph").argument("<spec>", "path to OpenAPI 3.x spec (YAML or JSON)").option("-o, --output <path>", "output path for IR JSON", "./loadam.ir.json").option("--no-tree", "skip printing the resource-graph tree").option("--json", "emit a single JSON summary on stdout (CI mode)", false).action(withFriendlyErrors(runInit));
|
|
1765
2081
|
}
|
|
1766
2082
|
async function runInit(spec, opts) {
|
|
1767
2083
|
const out = makeOutput(!!opts.json);
|
|
1768
|
-
const specPath =
|
|
2084
|
+
const specPath = resolve7(spec);
|
|
1769
2085
|
out.start(`Parsing OpenAPI spec: ${specPath}`);
|
|
1770
2086
|
const ir = await fromOpenApiFile(specPath);
|
|
1771
2087
|
inferResourceGraph(ir);
|
|
1772
2088
|
parseIR(ir);
|
|
1773
|
-
const outPath =
|
|
1774
|
-
await
|
|
2089
|
+
const outPath = resolve7(opts.output);
|
|
2090
|
+
await writeFile4(outPath, `${JSON.stringify(ir, null, 2)}
|
|
1775
2091
|
`, "utf8");
|
|
1776
2092
|
const stats = graphStats(ir.resources);
|
|
1777
2093
|
out.success(`Wrote ${outPath}`);
|
|
@@ -1795,8 +2111,8 @@ async function runInit(spec, opts) {
|
|
|
1795
2111
|
}
|
|
1796
2112
|
|
|
1797
2113
|
// src/commands/mcp.ts
|
|
1798
|
-
import { mkdir as
|
|
1799
|
-
import { dirname as dirname2, resolve as
|
|
2114
|
+
import { mkdir as mkdir3, writeFile as writeFile5 } from "fs/promises";
|
|
2115
|
+
import { dirname as dirname2, resolve as resolve8 } from "path";
|
|
1800
2116
|
|
|
1801
2117
|
// ../mcp/dist/index.js
|
|
1802
2118
|
function emitAuth(ir) {
|
|
@@ -2304,7 +2620,7 @@ function registerMcpCommand(program) {
|
|
|
2304
2620
|
}
|
|
2305
2621
|
async function runMcp(spec, opts) {
|
|
2306
2622
|
const out = makeOutput(!!opts.json);
|
|
2307
|
-
const specPath =
|
|
2623
|
+
const specPath = resolve8(spec);
|
|
2308
2624
|
out.start(`Parsing OpenAPI spec: ${specPath}`);
|
|
2309
2625
|
const ir = await fromOpenApiFile(specPath);
|
|
2310
2626
|
inferResourceGraph(ir);
|
|
@@ -2314,12 +2630,12 @@ async function runMcp(spec, opts) {
|
|
|
2314
2630
|
includeWrites: !!opts.writes,
|
|
2315
2631
|
exclude: opts.exclude
|
|
2316
2632
|
});
|
|
2317
|
-
const outDir =
|
|
2318
|
-
await
|
|
2633
|
+
const outDir = resolve8(opts.output);
|
|
2634
|
+
await mkdir3(outDir, { recursive: true });
|
|
2319
2635
|
for (const [rel, contents] of Object.entries(result.files)) {
|
|
2320
|
-
const full =
|
|
2321
|
-
await
|
|
2322
|
-
await
|
|
2636
|
+
const full = resolve8(outDir, rel);
|
|
2637
|
+
await mkdir3(dirname2(full), { recursive: true });
|
|
2638
|
+
await writeFile5(full, contents, "utf8");
|
|
2323
2639
|
}
|
|
2324
2640
|
const fileCount = Object.keys(result.files).length;
|
|
2325
2641
|
out.success(`Wrote ${fileCount} files to ${outDir}`);
|
|
@@ -2345,9 +2661,407 @@ async function runMcp(spec, opts) {
|
|
|
2345
2661
|
});
|
|
2346
2662
|
}
|
|
2347
2663
|
|
|
2664
|
+
// src/commands/report.ts
|
|
2665
|
+
import { spawn } from "child_process";
|
|
2666
|
+
import { readFile as readFile6, writeFile as writeFile6 } from "fs/promises";
|
|
2667
|
+
import { join as join2, resolve as resolve9 } from "path";
|
|
2668
|
+
|
|
2669
|
+
// ../report/dist/index.js
|
|
2670
|
+
function renderReport(input) {
|
|
2671
|
+
const { meta, loadamVersion } = input;
|
|
2672
|
+
const passed = meta.exitCode === 0 && (meta.thresholds?.failed.length ?? 0) === 0;
|
|
2673
|
+
const dataPayload = {
|
|
2674
|
+
meta,
|
|
2675
|
+
k6Summaries: input.k6Summaries ?? null,
|
|
2676
|
+
contractFailures: input.contractFailures ?? null,
|
|
2677
|
+
loadamVersion
|
|
2678
|
+
};
|
|
2679
|
+
return [
|
|
2680
|
+
"<!doctype html>",
|
|
2681
|
+
`<html lang="en">`,
|
|
2682
|
+
"<head>",
|
|
2683
|
+
`<meta charset="utf-8" />`,
|
|
2684
|
+
`<meta name="viewport" content="width=device-width,initial-scale=1" />`,
|
|
2685
|
+
`<meta name="generator" content="loadam ${esc(loadamVersion)}" />`,
|
|
2686
|
+
`<title>${esc(reportTitle(meta))}</title>`,
|
|
2687
|
+
`<style>${STYLES}</style>`,
|
|
2688
|
+
"</head>",
|
|
2689
|
+
"<body>",
|
|
2690
|
+
renderHeader(meta, passed),
|
|
2691
|
+
renderSummary(meta, input.k6Summaries),
|
|
2692
|
+
renderThresholds(meta),
|
|
2693
|
+
renderK6Metrics(input.k6Summaries),
|
|
2694
|
+
input.driftMarkdown ? renderDrift(input.driftMarkdown) : "",
|
|
2695
|
+
input.contractFailures && input.contractFailures.length > 0 ? renderContract(input.contractFailures) : "",
|
|
2696
|
+
renderRunDetails(meta),
|
|
2697
|
+
renderFooter(meta, loadamVersion),
|
|
2698
|
+
`<script type="application/json" id="loadam-data">${escScript(JSON.stringify(dataPayload))}</script>`,
|
|
2699
|
+
"</body>",
|
|
2700
|
+
"</html>",
|
|
2701
|
+
""
|
|
2702
|
+
].join("\n");
|
|
2703
|
+
}
|
|
2704
|
+
function renderHeader(meta, passed) {
|
|
2705
|
+
const status = passed ? "PASSED" : "FAILED";
|
|
2706
|
+
const cls = passed ? "ok" : "bad";
|
|
2707
|
+
return `
|
|
2708
|
+
<header class="hdr">
|
|
2709
|
+
<div class="hdr-left">
|
|
2710
|
+
<div class="cmd">${esc(meta.command)}</div>
|
|
2711
|
+
<h1>${esc(meta.spec.title ?? "Untitled API")}</h1>
|
|
2712
|
+
<div class="sub">${esc(meta.spec.version ?? "")} \xB7 target <code>${esc(meta.target ?? "\u2014")}</code></div>
|
|
2713
|
+
</div>
|
|
2714
|
+
<div class="hdr-right">
|
|
2715
|
+
<span class="badge ${cls}">${status}</span>
|
|
2716
|
+
<div class="when">${esc(meta.startedAt)}</div>
|
|
2717
|
+
${typeof meta.durationMs === "number" ? `<div class="dur">${(meta.durationMs / 1e3).toFixed(2)}s</div>` : ""}
|
|
2718
|
+
</div>
|
|
2719
|
+
</header>`;
|
|
2720
|
+
}
|
|
2721
|
+
function renderSummary(meta, k6) {
|
|
2722
|
+
const cells = [];
|
|
2723
|
+
if (meta.summary) {
|
|
2724
|
+
for (const [k, v] of Object.entries(meta.summary)) {
|
|
2725
|
+
cells.push({ label: k, value: String(v) });
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
if (k6) {
|
|
2729
|
+
for (const [name, s] of Object.entries(k6)) {
|
|
2730
|
+
const dur = s.metrics?.http_req_duration?.values;
|
|
2731
|
+
if (dur && typeof dur["p(95)"] === "number") {
|
|
2732
|
+
cells.push({ label: `${name} p95`, value: `${dur["p(95)"].toFixed(0)} ms` });
|
|
2733
|
+
}
|
|
2734
|
+
const reqs = s.metrics?.http_reqs?.values;
|
|
2735
|
+
if (reqs && typeof reqs.count === "number") {
|
|
2736
|
+
cells.push({ label: `${name} reqs`, value: String(reqs.count) });
|
|
2737
|
+
}
|
|
2738
|
+
if (reqs && typeof reqs.rate === "number") {
|
|
2739
|
+
cells.push({ label: `${name} rps`, value: `${reqs.rate.toFixed(1)}/s` });
|
|
2740
|
+
}
|
|
2741
|
+
const failed = s.metrics?.http_req_failed?.values;
|
|
2742
|
+
if (failed && typeof failed.rate === "number") {
|
|
2743
|
+
cells.push({
|
|
2744
|
+
label: `${name} fail`,
|
|
2745
|
+
value: `${(failed.rate * 100).toFixed(2)}%`
|
|
2746
|
+
});
|
|
2747
|
+
}
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
if (cells.length === 0) return "";
|
|
2751
|
+
return `
|
|
2752
|
+
<section class="cards">
|
|
2753
|
+
${cells.map(
|
|
2754
|
+
(c) => `<div class="card"><div class="card-v">${esc(c.value)}</div><div class="card-l">${esc(c.label)}</div></div>`
|
|
2755
|
+
).join("\n ")}
|
|
2756
|
+
</section>`;
|
|
2757
|
+
}
|
|
2758
|
+
function renderThresholds(meta) {
|
|
2759
|
+
const t = meta.thresholds;
|
|
2760
|
+
if (!t || t.passed.length === 0 && t.failed.length === 0) return "";
|
|
2761
|
+
return `
|
|
2762
|
+
<section>
|
|
2763
|
+
<h2>Thresholds</h2>
|
|
2764
|
+
<ul class="thr">
|
|
2765
|
+
${t.failed.map((x) => `<li class="bad">\u2717 ${esc(x)}</li>`).join("\n ")}
|
|
2766
|
+
${t.passed.map((x) => `<li class="ok">\u2713 ${esc(x)}</li>`).join("\n ")}
|
|
2767
|
+
</ul>
|
|
2768
|
+
</section>`;
|
|
2769
|
+
}
|
|
2770
|
+
function renderK6Metrics(k6) {
|
|
2771
|
+
if (!k6 || Object.keys(k6).length === 0) return "";
|
|
2772
|
+
const blocks = [];
|
|
2773
|
+
for (const [name, s] of Object.entries(k6)) {
|
|
2774
|
+
const metrics = s.metrics ?? {};
|
|
2775
|
+
const dur = metrics.http_req_duration?.values;
|
|
2776
|
+
if (!dur) continue;
|
|
2777
|
+
const rows = [
|
|
2778
|
+
["min", fmtMs(dur.min)],
|
|
2779
|
+
["med", fmtMs(dur.med)],
|
|
2780
|
+
["avg", fmtMs(dur.avg)],
|
|
2781
|
+
["p(90)", fmtMs(dur["p(90)"])],
|
|
2782
|
+
["p(95)", fmtMs(dur["p(95)"])],
|
|
2783
|
+
["p(99)", fmtMs(dur["p(99)"])],
|
|
2784
|
+
["max", fmtMs(dur.max)]
|
|
2785
|
+
];
|
|
2786
|
+
blocks.push(`
|
|
2787
|
+
<div class="metric-block">
|
|
2788
|
+
<h3>${esc(name)} \xB7 http_req_duration</h3>
|
|
2789
|
+
<table class="kv">
|
|
2790
|
+
<tbody>
|
|
2791
|
+
${rows.map(([k, v]) => `<tr><th>${esc(k)}</th><td>${esc(v)}</td></tr>`).join("\n ")}
|
|
2792
|
+
</tbody>
|
|
2793
|
+
</table>
|
|
2794
|
+
${renderPercentileBars(dur)}
|
|
2795
|
+
</div>`);
|
|
2796
|
+
}
|
|
2797
|
+
if (blocks.length === 0) return "";
|
|
2798
|
+
return `<section><h2>Latency</h2>${blocks.join("\n")}</section>`;
|
|
2799
|
+
}
|
|
2800
|
+
function renderPercentileBars(dur) {
|
|
2801
|
+
const max = dur.max ?? Math.max(...Object.values(dur));
|
|
2802
|
+
if (!Number.isFinite(max) || max <= 0) return "";
|
|
2803
|
+
const bars = [
|
|
2804
|
+
["med", dur.med ?? 0],
|
|
2805
|
+
["p(90)", dur["p(90)"] ?? 0],
|
|
2806
|
+
["p(95)", dur["p(95)"] ?? 0],
|
|
2807
|
+
["p(99)", dur["p(99)"] ?? 0],
|
|
2808
|
+
["max", max]
|
|
2809
|
+
];
|
|
2810
|
+
return `
|
|
2811
|
+
<div class="bars">
|
|
2812
|
+
${bars.map(([k, v]) => {
|
|
2813
|
+
const pct = Math.max(2, Math.round(v / max * 100));
|
|
2814
|
+
return `<div class="bar-row"><span class="bar-l">${esc(k)}</span><div class="bar"><div class="bar-fill" style="width:${pct}%"></div></div><span class="bar-v">${esc(fmtMs(v))}</span></div>`;
|
|
2815
|
+
}).join("\n ")}
|
|
2816
|
+
</div>`;
|
|
2817
|
+
}
|
|
2818
|
+
function renderDrift(md) {
|
|
2819
|
+
return `
|
|
2820
|
+
<section>
|
|
2821
|
+
<h2>Drift findings</h2>
|
|
2822
|
+
<pre class="drift">${esc(md)}</pre>
|
|
2823
|
+
</section>`;
|
|
2824
|
+
}
|
|
2825
|
+
function renderContract(failures) {
|
|
2826
|
+
return `
|
|
2827
|
+
<section>
|
|
2828
|
+
<h2>Contract failures (${failures.length})</h2>
|
|
2829
|
+
<ul class="failures">
|
|
2830
|
+
${failures.map(
|
|
2831
|
+
(f) => `<li><div class="ftest">${esc(f.test)}</div>${f.message ? `<pre>${esc(f.message)}</pre>` : ""}</li>`
|
|
2832
|
+
).join("\n ")}
|
|
2833
|
+
</ul>
|
|
2834
|
+
</section>`;
|
|
2835
|
+
}
|
|
2836
|
+
function renderRunDetails(meta) {
|
|
2837
|
+
const flagRows = Object.entries(meta.flags).map(
|
|
2838
|
+
([k, v]) => `<tr><th>${esc(k)}</th><td>${esc(String(v))}</td></tr>`
|
|
2839
|
+
);
|
|
2840
|
+
const envRow = meta.envVars.length > 0 ? `<tr><th>env vars</th><td>${meta.envVars.map((e) => `<code>${esc(e)}</code>`).join(" ")}</td></tr>` : "";
|
|
2841
|
+
return `
|
|
2842
|
+
<section>
|
|
2843
|
+
<h2>Run details</h2>
|
|
2844
|
+
<table class="kv">
|
|
2845
|
+
<tbody>
|
|
2846
|
+
<tr><th>session id</th><td><code>${esc(meta.id)}</code></td></tr>
|
|
2847
|
+
<tr><th>spec</th><td><code>${esc(meta.spec.path)}</code></td></tr>
|
|
2848
|
+
<tr><th>spec sha256</th><td><code>${esc(meta.spec.sha256.slice(0, 16))}\u2026</code></td></tr>
|
|
2849
|
+
<tr><th>IR digest</th><td><code>${esc(meta.irDigest.slice(0, 16))}\u2026</code></td></tr>
|
|
2850
|
+
<tr><th>started</th><td>${esc(meta.startedAt)}</td></tr>
|
|
2851
|
+
${meta.endedAt ? `<tr><th>ended</th><td>${esc(meta.endedAt)}</td></tr>` : ""}
|
|
2852
|
+
${typeof meta.exitCode === "number" ? `<tr><th>exit code</th><td>${meta.exitCode}</td></tr>` : ""}
|
|
2853
|
+
${envRow}
|
|
2854
|
+
${flagRows.join("\n ")}
|
|
2855
|
+
</tbody>
|
|
2856
|
+
</table>
|
|
2857
|
+
</section>`;
|
|
2858
|
+
}
|
|
2859
|
+
function renderFooter(meta, version) {
|
|
2860
|
+
return `
|
|
2861
|
+
<footer>
|
|
2862
|
+
Generated by <a href="https://github.com/anonxhash/loadam">loadam ${esc(version)}</a> \xB7
|
|
2863
|
+
schema v${meta.schemaVersion} \xB7
|
|
2864
|
+
IR <code>${esc(meta.irDigest.slice(0, 12))}</code>
|
|
2865
|
+
</footer>`;
|
|
2866
|
+
}
|
|
2867
|
+
function reportTitle(meta) {
|
|
2868
|
+
const t = meta.spec.title ?? "loadam";
|
|
2869
|
+
return `${t} \xB7 ${meta.command} \xB7 ${meta.id}`;
|
|
2870
|
+
}
|
|
2871
|
+
function fmtMs(n) {
|
|
2872
|
+
if (typeof n !== "number" || !Number.isFinite(n)) return "\u2014";
|
|
2873
|
+
if (n >= 1e3) return `${(n / 1e3).toFixed(2)} s`;
|
|
2874
|
+
return `${n.toFixed(0)} ms`;
|
|
2875
|
+
}
|
|
2876
|
+
function esc(s) {
|
|
2877
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
2878
|
+
}
|
|
2879
|
+
function escScript(s) {
|
|
2880
|
+
return s.replace(/<\/script/gi, "<\\/script");
|
|
2881
|
+
}
|
|
2882
|
+
var STYLES = `
|
|
2883
|
+
:root{
|
|
2884
|
+
--bg:#fafafa;--fg:#111;--mut:#666;--brd:#e5e5e5;--card:#fff;
|
|
2885
|
+
--ok:#15803d;--ok-bg:#dcfce7;--bad:#b91c1c;--bad-bg:#fee2e2;
|
|
2886
|
+
--acc:#2563eb;--bar:#3b82f6;--code:#f4f4f5;
|
|
2887
|
+
}
|
|
2888
|
+
@media (prefers-color-scheme:dark){
|
|
2889
|
+
:root{
|
|
2890
|
+
--bg:#0a0a0a;--fg:#f5f5f5;--mut:#a1a1aa;--brd:#27272a;--card:#18181b;
|
|
2891
|
+
--ok:#86efac;--ok-bg:#14532d;--bad:#fca5a5;--bad-bg:#7f1d1d;
|
|
2892
|
+
--acc:#60a5fa;--bar:#3b82f6;--code:#27272a;
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
*{box-sizing:border-box}
|
|
2896
|
+
html,body{margin:0;padding:0;background:var(--bg);color:var(--fg);font:14px/1.5 system-ui,-apple-system,Segoe UI,Roboto,sans-serif}
|
|
2897
|
+
body{max-width:960px;margin:0 auto;padding:24px 20px 60px}
|
|
2898
|
+
h1{font-size:20px;margin:0 0 4px}
|
|
2899
|
+
h2{font-size:15px;margin:24px 0 12px;text-transform:uppercase;letter-spacing:.04em;color:var(--mut)}
|
|
2900
|
+
h3{font-size:14px;margin:16px 0 8px;font-weight:600}
|
|
2901
|
+
code{font:12px/1.4 ui-monospace,SFMono-Regular,Menlo,monospace;background:var(--code);padding:1px 6px;border-radius:3px}
|
|
2902
|
+
a{color:var(--acc);text-decoration:none}
|
|
2903
|
+
a:hover{text-decoration:underline}
|
|
2904
|
+
section{margin-top:8px}
|
|
2905
|
+
.hdr{display:flex;justify-content:space-between;align-items:flex-start;gap:24px;padding-bottom:16px;border-bottom:1px solid var(--brd)}
|
|
2906
|
+
.hdr-left .cmd{font-size:11px;text-transform:uppercase;letter-spacing:.08em;color:var(--mut);font-weight:600}
|
|
2907
|
+
.hdr-left .sub{color:var(--mut);font-size:13px;margin-top:4px}
|
|
2908
|
+
.hdr-right{text-align:right}
|
|
2909
|
+
.hdr-right .when,.hdr-right .dur{font-size:12px;color:var(--mut);margin-top:4px;font-variant-numeric:tabular-nums}
|
|
2910
|
+
.badge{display:inline-block;padding:4px 10px;border-radius:4px;font-size:11px;font-weight:700;letter-spacing:.06em}
|
|
2911
|
+
.badge.ok{background:var(--ok-bg);color:var(--ok)}
|
|
2912
|
+
.badge.bad{background:var(--bad-bg);color:var(--bad)}
|
|
2913
|
+
.cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:12px;margin-top:20px}
|
|
2914
|
+
.card{background:var(--card);border:1px solid var(--brd);border-radius:6px;padding:12px 14px}
|
|
2915
|
+
.card-v{font-size:18px;font-weight:600;font-variant-numeric:tabular-nums}
|
|
2916
|
+
.card-l{font-size:11px;color:var(--mut);text-transform:uppercase;letter-spacing:.04em;margin-top:2px}
|
|
2917
|
+
.thr{list-style:none;padding:0;margin:0}
|
|
2918
|
+
.thr li{padding:4px 8px;border-radius:3px;margin:2px 0;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px}
|
|
2919
|
+
.thr li.ok{color:var(--ok)}
|
|
2920
|
+
.thr li.bad{background:var(--bad-bg);color:var(--bad);font-weight:600}
|
|
2921
|
+
table.kv{border-collapse:collapse;width:100%;font-size:13px}
|
|
2922
|
+
table.kv th,table.kv td{padding:6px 10px;text-align:left;border-bottom:1px solid var(--brd);vertical-align:top}
|
|
2923
|
+
table.kv th{font-weight:500;color:var(--mut);width:160px;white-space:nowrap}
|
|
2924
|
+
.metric-block{background:var(--card);border:1px solid var(--brd);border-radius:6px;padding:12px 16px;margin-bottom:12px}
|
|
2925
|
+
.bars{margin-top:10px}
|
|
2926
|
+
.bar-row{display:grid;grid-template-columns:60px 1fr 80px;gap:10px;align-items:center;font-size:12px;font-variant-numeric:tabular-nums;margin:3px 0}
|
|
2927
|
+
.bar-l{color:var(--mut)}
|
|
2928
|
+
.bar{background:var(--code);height:8px;border-radius:4px;overflow:hidden}
|
|
2929
|
+
.bar-fill{background:var(--bar);height:100%}
|
|
2930
|
+
.bar-v{text-align:right;color:var(--fg)}
|
|
2931
|
+
.drift{background:var(--card);border:1px solid var(--brd);border-radius:6px;padding:12px 16px;overflow-x:auto;font:12px/1.5 ui-monospace,SFMono-Regular,Menlo,monospace;white-space:pre-wrap}
|
|
2932
|
+
.failures{list-style:none;padding:0;margin:0}
|
|
2933
|
+
.failures li{background:var(--bad-bg);border:1px solid var(--bad);border-radius:4px;padding:8px 12px;margin-bottom:6px}
|
|
2934
|
+
.failures .ftest{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;font-weight:600}
|
|
2935
|
+
.failures pre{margin:6px 0 0;font:11px/1.4 ui-monospace,SFMono-Regular,Menlo,monospace;white-space:pre-wrap}
|
|
2936
|
+
footer{margin-top:40px;padding-top:16px;border-top:1px solid var(--brd);color:var(--mut);font-size:12px;text-align:center}
|
|
2937
|
+
`;
|
|
2938
|
+
|
|
2939
|
+
// src/commands/report.ts
|
|
2940
|
+
var VERSION = true ? "0.2.2" : "0.0.0";
|
|
2941
|
+
function registerReportCommand(program) {
|
|
2942
|
+
program.command("report").description("Render a self-contained HTML report for an archived session.").argument("[id]", 'session ID, prefix, or "latest" (default: latest)', "latest").option("--root <dir>", "session archive root", "./loadam-out").option("-o, --output <path>", "write to this path instead of <session>/report.html").option("--open", "open the report in your default browser after writing", false).option("--json", "emit a JSON summary on stdout (CI mode)", false).action(withFriendlyErrors(runReport));
|
|
2943
|
+
}
|
|
2944
|
+
async function runReport(id, opts) {
|
|
2945
|
+
const out = makeOutput(!!opts.json);
|
|
2946
|
+
const root = resolve9(opts.root);
|
|
2947
|
+
const meta = await resolveSessionId(root, id);
|
|
2948
|
+
const dir = join2(root, "sessions", meta.id);
|
|
2949
|
+
const k6Summaries = {};
|
|
2950
|
+
for (const a of meta.artefacts) {
|
|
2951
|
+
if (a.startsWith("k6-") && a.endsWith("-summary.json")) {
|
|
2952
|
+
const label = a.replace(/^k6-/, "").replace(/-summary\.json$/, "");
|
|
2953
|
+
try {
|
|
2954
|
+
k6Summaries[label] = JSON.parse(await readFile6(join2(dir, a), "utf8"));
|
|
2955
|
+
} catch {
|
|
2956
|
+
}
|
|
2957
|
+
}
|
|
2958
|
+
}
|
|
2959
|
+
let driftMarkdown;
|
|
2960
|
+
if (meta.command === "diff") {
|
|
2961
|
+
try {
|
|
2962
|
+
driftMarkdown = await readFile6(join2(dir, "drift.md"), "utf8");
|
|
2963
|
+
} catch {
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
const html = renderReport({
|
|
2967
|
+
meta,
|
|
2968
|
+
k6Summaries: Object.keys(k6Summaries).length > 0 ? k6Summaries : void 0,
|
|
2969
|
+
driftMarkdown,
|
|
2970
|
+
loadamVersion: VERSION
|
|
2971
|
+
});
|
|
2972
|
+
const outPath = opts.output ? resolve9(opts.output) : join2(dir, "report.html");
|
|
2973
|
+
await writeFile6(outPath, html, "utf8");
|
|
2974
|
+
const sizeKb = Math.round(Buffer.byteLength(html, "utf8") / 1024);
|
|
2975
|
+
if (!out.json) {
|
|
2976
|
+
out.success(`Wrote ${outPath} (${sizeKb} KB)`);
|
|
2977
|
+
}
|
|
2978
|
+
out.result({
|
|
2979
|
+
command: "report",
|
|
2980
|
+
sessionId: meta.id,
|
|
2981
|
+
path: outPath,
|
|
2982
|
+
sizeBytes: Buffer.byteLength(html, "utf8"),
|
|
2983
|
+
opened: false
|
|
2984
|
+
});
|
|
2985
|
+
if (opts.open) {
|
|
2986
|
+
await openInBrowser(outPath);
|
|
2987
|
+
if (!out.json) out.info("Opened in default browser");
|
|
2988
|
+
}
|
|
2989
|
+
}
|
|
2990
|
+
async function openInBrowser(path) {
|
|
2991
|
+
const platform = process.platform;
|
|
2992
|
+
const cmd = platform === "darwin" ? "open" : platform === "win32" ? "start" : "xdg-open";
|
|
2993
|
+
const args = platform === "win32" ? ["", path] : [path];
|
|
2994
|
+
try {
|
|
2995
|
+
const child = spawn(cmd, args, {
|
|
2996
|
+
detached: true,
|
|
2997
|
+
stdio: "ignore",
|
|
2998
|
+
shell: platform === "win32"
|
|
2999
|
+
});
|
|
3000
|
+
child.unref();
|
|
3001
|
+
} catch {
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
|
|
3005
|
+
// src/commands/show.ts
|
|
3006
|
+
import { readFile as readFile7, readdir as readdir2 } from "fs/promises";
|
|
3007
|
+
import { join as join3, resolve as resolve10 } from "path";
|
|
3008
|
+
function registerShowCommand(program) {
|
|
3009
|
+
program.command("show").description("Show details of an archived session.").argument("<id>", 'session ID, prefix, or "latest"').option("--root <dir>", "session archive root", "./loadam-out").option("--json", "emit the meta.json payload on stdout", false).action(withFriendlyErrors(runShow));
|
|
3010
|
+
}
|
|
3011
|
+
async function runShow(id, opts) {
|
|
3012
|
+
const out = makeOutput(!!opts.json);
|
|
3013
|
+
const root = resolve10(opts.root);
|
|
3014
|
+
const meta = await resolveSessionId(root, id);
|
|
3015
|
+
const dir = join3(root, "sessions", meta.id);
|
|
3016
|
+
if (out.json) {
|
|
3017
|
+
out.result({ ...meta });
|
|
3018
|
+
return;
|
|
3019
|
+
}
|
|
3020
|
+
out.info(`Session ${meta.id}`);
|
|
3021
|
+
out.info(` Command: ${meta.command}`);
|
|
3022
|
+
out.info(` Started: ${meta.startedAt}`);
|
|
3023
|
+
if (meta.endedAt) out.info(` Ended: ${meta.endedAt}`);
|
|
3024
|
+
if (typeof meta.durationMs === "number") {
|
|
3025
|
+
out.info(` Duration: ${(meta.durationMs / 1e3).toFixed(2)}s`);
|
|
3026
|
+
}
|
|
3027
|
+
out.info(` Exit code: ${meta.exitCode ?? "\u2014"}`);
|
|
3028
|
+
out.info(` Spec: ${meta.spec.path} (${meta.spec.title ?? "?"} ${meta.spec.version ?? ""})`);
|
|
3029
|
+
out.info(` Spec sha: ${meta.spec.sha256.slice(0, 12)}\u2026`);
|
|
3030
|
+
out.info(` IR digest: ${meta.irDigest.slice(0, 12)}\u2026`);
|
|
3031
|
+
if (meta.target) out.info(` Target: ${meta.target}`);
|
|
3032
|
+
if (meta.envVars.length > 0) out.info(` Env vars: ${meta.envVars.join(", ")}`);
|
|
3033
|
+
if (meta.thresholds && (meta.thresholds.passed.length || meta.thresholds.failed.length)) {
|
|
3034
|
+
out.info(
|
|
3035
|
+
` Thresholds: ${meta.thresholds.passed.length} passed \xB7 ${meta.thresholds.failed.length} failed`
|
|
3036
|
+
);
|
|
3037
|
+
for (const t of meta.thresholds.failed) out.info(` \u2717 ${t}`);
|
|
3038
|
+
}
|
|
3039
|
+
if (meta.summary && Object.keys(meta.summary).length > 0) {
|
|
3040
|
+
out.info(" Summary:");
|
|
3041
|
+
for (const [k, v] of Object.entries(meta.summary)) {
|
|
3042
|
+
out.info(` ${k}: ${v}`);
|
|
3043
|
+
}
|
|
3044
|
+
}
|
|
3045
|
+
try {
|
|
3046
|
+
const entries = await readdir2(dir);
|
|
3047
|
+
out.info(` Artefacts (${entries.length}):`);
|
|
3048
|
+
for (const e of entries.sort()) out.info(` ${e}`);
|
|
3049
|
+
} catch {
|
|
3050
|
+
}
|
|
3051
|
+
if (meta.command === "diff") {
|
|
3052
|
+
try {
|
|
3053
|
+
const drift = await readFile7(join3(dir, "drift.md"), "utf8");
|
|
3054
|
+
out.info("");
|
|
3055
|
+
out.info("\u2500\u2500\u2500\u2500 drift.md \u2500\u2500\u2500\u2500");
|
|
3056
|
+
process.stdout.write(drift);
|
|
3057
|
+
} catch {
|
|
3058
|
+
}
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
|
|
2348
3062
|
// src/commands/test.ts
|
|
2349
|
-
import { mkdir as
|
|
2350
|
-
import { dirname as dirname3, resolve as
|
|
3063
|
+
import { mkdir as mkdir4, readFile as readFile9, writeFile as writeFile8 } from "fs/promises";
|
|
3064
|
+
import { dirname as dirname3, join as join5, resolve as resolve12 } from "path";
|
|
2351
3065
|
|
|
2352
3066
|
// ../data/dist/index.js
|
|
2353
3067
|
import { faker } from "@faker-js/faker";
|
|
@@ -2400,20 +3114,20 @@ function inferKindFromFieldName(field) {
|
|
|
2400
3114
|
const root = m[1];
|
|
2401
3115
|
return root.charAt(0).toUpperCase() + root.slice(1);
|
|
2402
3116
|
}
|
|
2403
|
-
function expandRefs(node,
|
|
2404
|
-
if (Array.isArray(node)) return node.map((n) => expandRefs(n,
|
|
3117
|
+
function expandRefs(node, resolve13, seen) {
|
|
3118
|
+
if (Array.isArray(node)) return node.map((n) => expandRefs(n, resolve13, seen));
|
|
2405
3119
|
if (!node || typeof node !== "object") return node;
|
|
2406
3120
|
const obj = node;
|
|
2407
3121
|
if (typeof obj.$ref === "string") {
|
|
2408
3122
|
if (seen.has(obj.$ref)) return {};
|
|
2409
|
-
const target =
|
|
3123
|
+
const target = resolve13(obj.$ref);
|
|
2410
3124
|
if (target === void 0) return obj;
|
|
2411
3125
|
const next = new Set(seen);
|
|
2412
3126
|
next.add(obj.$ref);
|
|
2413
|
-
return expandRefs(target,
|
|
3127
|
+
return expandRefs(target, resolve13, next);
|
|
2414
3128
|
}
|
|
2415
3129
|
const out = {};
|
|
2416
|
-
for (const [k, v] of Object.entries(obj)) out[k] = expandRefs(v,
|
|
3130
|
+
for (const [k, v] of Object.entries(obj)) out[k] = expandRefs(v, resolve13, seen);
|
|
2417
3131
|
return out;
|
|
2418
3132
|
}
|
|
2419
3133
|
|
|
@@ -2909,8 +3623,8 @@ function compileK6(ir, opts = {}) {
|
|
|
2909
3623
|
}
|
|
2910
3624
|
|
|
2911
3625
|
// src/util/interactive.ts
|
|
2912
|
-
import { chmod, readFile as
|
|
2913
|
-
import { resolve as
|
|
3626
|
+
import { chmod, readFile as readFile8, writeFile as writeFile7 } from "fs/promises";
|
|
3627
|
+
import { resolve as resolve11 } from "path";
|
|
2914
3628
|
import { confirm, password, select } from "@inquirer/prompts";
|
|
2915
3629
|
function isInteractive(noInteractiveFlag) {
|
|
2916
3630
|
if (noInteractiveFlag) return false;
|
|
@@ -2947,7 +3661,7 @@ async function confirmContinue(message) {
|
|
|
2947
3661
|
return await confirm({ message, default: true });
|
|
2948
3662
|
}
|
|
2949
3663
|
async function writeEnvFile(dir, baseUrl, vars, opts = {}) {
|
|
2950
|
-
const path =
|
|
3664
|
+
const path = resolve11(dir, ".env");
|
|
2951
3665
|
const existing = await readEnvFile(path);
|
|
2952
3666
|
const written = [];
|
|
2953
3667
|
const preserved = [];
|
|
@@ -2967,14 +3681,14 @@ async function writeEnvFile(dir, baseUrl, vars, opts = {}) {
|
|
|
2967
3681
|
}
|
|
2968
3682
|
const body = `${Object.entries(existing).map(([k, v]) => `${k}=${v}`).join("\n")}
|
|
2969
3683
|
`;
|
|
2970
|
-
await
|
|
3684
|
+
await writeFile7(path, body, "utf8");
|
|
2971
3685
|
await chmod(path, 384).catch(() => {
|
|
2972
3686
|
});
|
|
2973
3687
|
return { path, written, preserved };
|
|
2974
3688
|
}
|
|
2975
3689
|
async function readEnvFile(path) {
|
|
2976
3690
|
try {
|
|
2977
|
-
const text = await
|
|
3691
|
+
const text = await readFile8(path, "utf8");
|
|
2978
3692
|
const out = {};
|
|
2979
3693
|
for (const raw of text.split(/\r?\n/)) {
|
|
2980
3694
|
const line = raw.trim();
|
|
@@ -2992,16 +3706,16 @@ async function readEnvFile(path) {
|
|
|
2992
3706
|
}
|
|
2993
3707
|
|
|
2994
3708
|
// src/util/k6.ts
|
|
2995
|
-
import { spawn } from "child_process";
|
|
3709
|
+
import { spawn as spawn2 } from "child_process";
|
|
2996
3710
|
import { constants, access } from "fs/promises";
|
|
2997
|
-
import { delimiter, join } from "path";
|
|
3711
|
+
import { delimiter, join as join4 } from "path";
|
|
2998
3712
|
async function findK6Binary() {
|
|
2999
3713
|
const path = process.env.PATH ?? "";
|
|
3000
3714
|
const exts = process.platform === "win32" ? [".exe", ".cmd", ".bat", ""] : [""];
|
|
3001
3715
|
for (const dir of path.split(delimiter)) {
|
|
3002
3716
|
if (!dir) continue;
|
|
3003
3717
|
for (const ext of exts) {
|
|
3004
|
-
const candidate =
|
|
3718
|
+
const candidate = join4(dir, `k6${ext}`);
|
|
3005
3719
|
try {
|
|
3006
3720
|
await access(candidate, constants.X_OK);
|
|
3007
3721
|
return candidate;
|
|
@@ -3018,8 +3732,11 @@ async function runK6(opts) {
|
|
|
3018
3732
|
"k6 not found on PATH. Install it from https://grafana.com/docs/k6/latest/set-up/install-k6/ and try again."
|
|
3019
3733
|
);
|
|
3020
3734
|
}
|
|
3735
|
+
const args = ["run"];
|
|
3736
|
+
if (opts.summaryPath) args.push(`--summary-export=${opts.summaryPath}`);
|
|
3737
|
+
args.push(...opts.args ?? [], opts.script);
|
|
3021
3738
|
return await new Promise((resolveExit, reject) => {
|
|
3022
|
-
const child =
|
|
3739
|
+
const child = spawn2(bin, args, {
|
|
3023
3740
|
cwd: opts.cwd,
|
|
3024
3741
|
env: { ...process.env, ...opts.env },
|
|
3025
3742
|
stdio: "inherit"
|
|
@@ -3028,6 +3745,32 @@ async function runK6(opts) {
|
|
|
3028
3745
|
child.on("close", (code) => resolveExit(code ?? 0));
|
|
3029
3746
|
});
|
|
3030
3747
|
}
|
|
3748
|
+
async function digestK6Summary(path) {
|
|
3749
|
+
try {
|
|
3750
|
+
const { readFile: readFile10 } = await import("fs/promises");
|
|
3751
|
+
const text = await readFile10(path, "utf8");
|
|
3752
|
+
const data = JSON.parse(text);
|
|
3753
|
+
const passed = [];
|
|
3754
|
+
const failed = [];
|
|
3755
|
+
const metrics = {};
|
|
3756
|
+
for (const [name, m] of Object.entries(data.metrics ?? {})) {
|
|
3757
|
+
if (m.thresholds) {
|
|
3758
|
+
for (const [expr, info] of Object.entries(m.thresholds)) {
|
|
3759
|
+
(info.ok ? passed : failed).push(`${name}: ${expr}`);
|
|
3760
|
+
}
|
|
3761
|
+
}
|
|
3762
|
+
if (m.values) {
|
|
3763
|
+
if (typeof m.values["p(95)"] === "number") metrics[`${name}.p95`] = m.values["p(95)"];
|
|
3764
|
+
if (typeof m.values.rate === "number") metrics[`${name}.rate`] = m.values.rate;
|
|
3765
|
+
if (typeof m.values.count === "number") metrics[`${name}.count`] = m.values.count;
|
|
3766
|
+
if (typeof m.values.avg === "number") metrics[`${name}.avg`] = m.values.avg;
|
|
3767
|
+
}
|
|
3768
|
+
}
|
|
3769
|
+
return { passed, failed, metrics };
|
|
3770
|
+
} catch {
|
|
3771
|
+
return null;
|
|
3772
|
+
}
|
|
3773
|
+
}
|
|
3031
3774
|
|
|
3032
3775
|
// src/commands/test.ts
|
|
3033
3776
|
var VALID_MODES = ["smoke", "load", "both", "skip"];
|
|
@@ -3042,8 +3785,9 @@ function registerTestCommand(program) {
|
|
|
3042
3785
|
}
|
|
3043
3786
|
async function runTest(spec, opts) {
|
|
3044
3787
|
const out = makeOutput(!!opts.json);
|
|
3045
|
-
const specPath =
|
|
3788
|
+
const specPath = resolve12(spec);
|
|
3046
3789
|
out.start(`Parsing OpenAPI spec: ${specPath}`);
|
|
3790
|
+
const specSource = await readFile9(specPath, "utf8");
|
|
3047
3791
|
const ir = await fromOpenApiFile(specPath);
|
|
3048
3792
|
inferResourceGraph(ir);
|
|
3049
3793
|
parseIR(ir);
|
|
@@ -3054,12 +3798,12 @@ async function runTest(spec, opts) {
|
|
|
3054
3798
|
fixtureSize: Number.isFinite(fixtureSize) ? fixtureSize : 10,
|
|
3055
3799
|
seed: Number.isFinite(seed) ? seed : 1
|
|
3056
3800
|
});
|
|
3057
|
-
const outDir =
|
|
3058
|
-
await
|
|
3801
|
+
const outDir = resolve12(opts.output);
|
|
3802
|
+
await mkdir4(outDir, { recursive: true });
|
|
3059
3803
|
for (const [rel, contents] of Object.entries(result.files)) {
|
|
3060
|
-
const full =
|
|
3061
|
-
await
|
|
3062
|
-
await
|
|
3804
|
+
const full = resolve12(outDir, rel);
|
|
3805
|
+
await mkdir4(dirname3(full), { recursive: true });
|
|
3806
|
+
await writeFile8(full, contents, "utf8");
|
|
3063
3807
|
}
|
|
3064
3808
|
const fileCount = Object.keys(result.files).length;
|
|
3065
3809
|
const fixtureCount = Object.keys(result.fixtures.byOperationId).length;
|
|
@@ -3071,6 +3815,7 @@ async function runTest(spec, opts) {
|
|
|
3071
3815
|
const requestedMode = parseModeFlag(opts.mode);
|
|
3072
3816
|
let envAction = "not-needed";
|
|
3073
3817
|
let runResult = null;
|
|
3818
|
+
let mode;
|
|
3074
3819
|
if (interactive) {
|
|
3075
3820
|
if (result.auth.envVars.length > 0) {
|
|
3076
3821
|
out.step("");
|
|
@@ -3094,19 +3839,49 @@ async function runTest(spec, opts) {
|
|
|
3094
3839
|
out.success(`Wrote ${written.path}`);
|
|
3095
3840
|
envAction = "wrote";
|
|
3096
3841
|
}
|
|
3097
|
-
|
|
3098
|
-
runResult = await maybeRun(mode, outDir, out);
|
|
3842
|
+
mode = requestedMode ?? await promptForMode();
|
|
3099
3843
|
} else {
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3844
|
+
mode = requestedMode ?? "skip";
|
|
3845
|
+
}
|
|
3846
|
+
const session = await createSession({
|
|
3847
|
+
command: "test",
|
|
3848
|
+
outRoot: getSessionRoot(outDir),
|
|
3849
|
+
specPath,
|
|
3850
|
+
specSource,
|
|
3851
|
+
ir,
|
|
3852
|
+
irJson: JSON.stringify(ir, null, 2),
|
|
3853
|
+
target: opts.target ?? result.baseUrl,
|
|
3854
|
+
envVars: result.auth.envVars,
|
|
3855
|
+
flags: {
|
|
3856
|
+
mode,
|
|
3857
|
+
target: opts.target ?? null,
|
|
3858
|
+
fixtureSize,
|
|
3859
|
+
seed,
|
|
3860
|
+
noInteractive: !!opts.noInteractive,
|
|
3861
|
+
json: !!opts.json
|
|
3862
|
+
},
|
|
3863
|
+
slug: `${mode}-${ir.meta.title ?? "run"}`
|
|
3864
|
+
});
|
|
3865
|
+
if (!out.json) out.info(` Session: ${session.id}`);
|
|
3866
|
+
if (mode !== "skip") {
|
|
3867
|
+
runResult = await maybeRunWithSession({ mode, cwd: outDir, out, session });
|
|
3868
|
+
} else if (!interactive && !out.json) {
|
|
3869
|
+
out.step("");
|
|
3870
|
+
out.step("Next steps:");
|
|
3871
|
+
out.step(` cd ${opts.output}`);
|
|
3872
|
+
out.step(" cp .env.example .env # fill in credentials");
|
|
3873
|
+
out.step(" k6 run smoke.js");
|
|
3874
|
+
}
|
|
3875
|
+
if (mode === "skip") {
|
|
3876
|
+
await session.finalize({
|
|
3877
|
+
exitCode: 0,
|
|
3878
|
+
thresholds: { passed: [], failed: [] },
|
|
3879
|
+
summary: {
|
|
3880
|
+
operations: ir.operations.length,
|
|
3881
|
+
fixtureOps: fixtureCount,
|
|
3882
|
+
files: fileCount
|
|
3883
|
+
}
|
|
3884
|
+
});
|
|
3110
3885
|
}
|
|
3111
3886
|
out.result({
|
|
3112
3887
|
command: "test",
|
|
@@ -3118,7 +3893,8 @@ async function runTest(spec, opts) {
|
|
|
3118
3893
|
envVars: result.auth.envVars,
|
|
3119
3894
|
envAction,
|
|
3120
3895
|
interactive,
|
|
3121
|
-
run: runResult
|
|
3896
|
+
run: runResult,
|
|
3897
|
+
sessionId: session.id
|
|
3122
3898
|
});
|
|
3123
3899
|
if (runResult) {
|
|
3124
3900
|
if (runResult.smokeExit && runResult.smokeExit !== 0 || runResult.loadExit && runResult.loadExit !== 0) {
|
|
@@ -3133,34 +3909,83 @@ function parseModeFlag(raw) {
|
|
|
3133
3909
|
}
|
|
3134
3910
|
return raw;
|
|
3135
3911
|
}
|
|
3136
|
-
|
|
3912
|
+
function getSessionRoot(outDir) {
|
|
3913
|
+
return resolve12(outDir, "..");
|
|
3914
|
+
}
|
|
3915
|
+
async function maybeRunWithSession(input) {
|
|
3916
|
+
const { mode, cwd, out, session } = input;
|
|
3137
3917
|
if (mode === "skip") return { mode };
|
|
3138
3918
|
const bin = await findK6Binary();
|
|
3139
3919
|
if (!bin) {
|
|
3140
3920
|
out.info("");
|
|
3141
3921
|
out.info("k6 binary not found on PATH. Skipping run.");
|
|
3142
3922
|
out.info("Install: https://grafana.com/docs/k6/latest/set-up/install-k6/");
|
|
3923
|
+
await session.finalize({
|
|
3924
|
+
exitCode: 0,
|
|
3925
|
+
thresholds: { passed: [], failed: [] },
|
|
3926
|
+
summary: { skipped: "k6 not installed" }
|
|
3927
|
+
});
|
|
3143
3928
|
return { mode: "skip" };
|
|
3144
3929
|
}
|
|
3145
3930
|
const result = { mode };
|
|
3931
|
+
const allPassed = [];
|
|
3932
|
+
const allFailed = [];
|
|
3933
|
+
const summary = {};
|
|
3146
3934
|
if (mode === "smoke" || mode === "both") {
|
|
3147
3935
|
if (!out.json) {
|
|
3148
3936
|
out.step("");
|
|
3149
3937
|
out.step("Running k6 smoke test...");
|
|
3150
3938
|
}
|
|
3151
|
-
|
|
3939
|
+
const summaryPath = join5(session.dir, "k6-smoke-summary.json");
|
|
3940
|
+
result.smokeExit = await runK6({ cwd, script: "smoke.js", summaryPath });
|
|
3941
|
+
const digest = await digestK6Summary(summaryPath);
|
|
3942
|
+
if (digest) {
|
|
3943
|
+
allPassed.push(...digest.passed.map((t) => `smoke: ${t}`));
|
|
3944
|
+
allFailed.push(...digest.failed.map((t) => `smoke: ${t}`));
|
|
3945
|
+
if (typeof digest.metrics["http_req_duration.p95"] === "number") {
|
|
3946
|
+
summary.smokeP95 = Math.round(digest.metrics["http_req_duration.p95"]);
|
|
3947
|
+
}
|
|
3948
|
+
if (typeof digest.metrics["http_reqs.count"] === "number") {
|
|
3949
|
+
summary.smokeReqs = digest.metrics["http_reqs.count"];
|
|
3950
|
+
}
|
|
3951
|
+
}
|
|
3152
3952
|
}
|
|
3153
3953
|
if (mode === "load" || mode === "both") {
|
|
3154
3954
|
if (mode === "both") {
|
|
3155
3955
|
const cont = await confirmContinue("Smoke complete. Continue with load test?");
|
|
3156
|
-
if (!cont)
|
|
3956
|
+
if (!cont) {
|
|
3957
|
+
await session.finalize({
|
|
3958
|
+
exitCode: result.smokeExit ?? 0,
|
|
3959
|
+
thresholds: { passed: allPassed, failed: allFailed },
|
|
3960
|
+
summary
|
|
3961
|
+
});
|
|
3962
|
+
return result;
|
|
3963
|
+
}
|
|
3157
3964
|
}
|
|
3158
3965
|
if (!out.json) {
|
|
3159
3966
|
out.step("");
|
|
3160
3967
|
out.step("Running k6 load test...");
|
|
3161
3968
|
}
|
|
3162
|
-
|
|
3969
|
+
const summaryPath = join5(session.dir, "k6-load-summary.json");
|
|
3970
|
+
result.loadExit = await runK6({ cwd, script: "load.js", summaryPath });
|
|
3971
|
+
const digest = await digestK6Summary(summaryPath);
|
|
3972
|
+
if (digest) {
|
|
3973
|
+
allPassed.push(...digest.passed.map((t) => `load: ${t}`));
|
|
3974
|
+
allFailed.push(...digest.failed.map((t) => `load: ${t}`));
|
|
3975
|
+
if (typeof digest.metrics["http_req_duration.p95"] === "number") {
|
|
3976
|
+
summary.loadP95 = Math.round(digest.metrics["http_req_duration.p95"]);
|
|
3977
|
+
}
|
|
3978
|
+
if (typeof digest.metrics["http_reqs.count"] === "number") {
|
|
3979
|
+
summary.loadReqs = digest.metrics["http_reqs.count"];
|
|
3980
|
+
}
|
|
3981
|
+
}
|
|
3163
3982
|
}
|
|
3983
|
+
const finalExit = result.loadExit ?? result.smokeExit ?? 0;
|
|
3984
|
+
await session.finalize({
|
|
3985
|
+
exitCode: finalExit,
|
|
3986
|
+
thresholds: { passed: allPassed, failed: allFailed },
|
|
3987
|
+
summary
|
|
3988
|
+
});
|
|
3164
3989
|
return result;
|
|
3165
3990
|
}
|
|
3166
3991
|
|
|
@@ -3255,20 +4080,24 @@ ${dim}${TAGLINE}${reset}
|
|
|
3255
4080
|
}
|
|
3256
4081
|
|
|
3257
4082
|
// src/program.ts
|
|
3258
|
-
var
|
|
4083
|
+
var VERSION2 = true ? "0.2.2" : "0.0.0";
|
|
3259
4084
|
function buildProgram() {
|
|
3260
4085
|
const program = new Command();
|
|
3261
4086
|
program.name("loadam").description(
|
|
3262
4087
|
"Generate test rigs and MCP servers from any API spec. From spec to running rig in 60s."
|
|
3263
|
-
).version(
|
|
4088
|
+
).version(VERSION2, "-v, --version", "print version").addHelpText("beforeAll", banner());
|
|
3264
4089
|
registerInitCommand(program);
|
|
3265
4090
|
registerAuthCommands(program);
|
|
3266
4091
|
registerTestCommand(program);
|
|
3267
4092
|
registerContractCommand(program);
|
|
3268
4093
|
registerDiffCommand(program);
|
|
3269
4094
|
registerMcpCommand(program);
|
|
4095
|
+
registerHistoryCommand(program);
|
|
4096
|
+
registerShowCommand(program);
|
|
4097
|
+
registerReportCommand(program);
|
|
4098
|
+
registerCleanCommand(program);
|
|
3270
4099
|
registerCompletionCommand(program);
|
|
3271
|
-
registerUpdateCommand(program,
|
|
4100
|
+
registerUpdateCommand(program, VERSION2);
|
|
3272
4101
|
return program;
|
|
3273
4102
|
}
|
|
3274
4103
|
|