loadam 0.1.1 → 0.2.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/bin.js CHANGED
@@ -2346,8 +2346,8 @@ async function runMcp(spec, opts) {
2346
2346
  }
2347
2347
 
2348
2348
  // src/commands/test.ts
2349
- import { mkdir as mkdir3, writeFile as writeFile5 } from "fs/promises";
2350
- import { dirname as dirname3, resolve as resolve6 } from "path";
2349
+ import { mkdir as mkdir3, writeFile as writeFile6 } from "fs/promises";
2350
+ import { dirname as dirname3, resolve as resolve7 } from "path";
2351
2351
 
2352
2352
  // ../data/dist/index.js
2353
2353
  import { faker } from "@faker-js/faker";
@@ -2400,20 +2400,20 @@ function inferKindFromFieldName(field) {
2400
2400
  const root = m[1];
2401
2401
  return root.charAt(0).toUpperCase() + root.slice(1);
2402
2402
  }
2403
- function expandRefs(node, resolve7, seen) {
2404
- if (Array.isArray(node)) return node.map((n) => expandRefs(n, resolve7, seen));
2403
+ function expandRefs(node, resolve8, seen) {
2404
+ if (Array.isArray(node)) return node.map((n) => expandRefs(n, resolve8, seen));
2405
2405
  if (!node || typeof node !== "object") return node;
2406
2406
  const obj = node;
2407
2407
  if (typeof obj.$ref === "string") {
2408
2408
  if (seen.has(obj.$ref)) return {};
2409
- const target = resolve7(obj.$ref);
2409
+ const target = resolve8(obj.$ref);
2410
2410
  if (target === void 0) return obj;
2411
2411
  const next = new Set(seen);
2412
2412
  next.add(obj.$ref);
2413
- return expandRefs(target, resolve7, next);
2413
+ return expandRefs(target, resolve8, next);
2414
2414
  }
2415
2415
  const out = {};
2416
- for (const [k, v] of Object.entries(obj)) out[k] = expandRefs(v, resolve7, seen);
2416
+ for (const [k, v] of Object.entries(obj)) out[k] = expandRefs(v, resolve8, seen);
2417
2417
  return out;
2418
2418
  }
2419
2419
 
@@ -2905,19 +2905,144 @@ function compileK6(ir, opts = {}) {
2905
2905
  ".env.example": emitEnvExample3(ctx),
2906
2906
  "README.md": emitReadme3(ctx)
2907
2907
  };
2908
- return { files, fixtures };
2908
+ return { files, fixtures, auth, baseUrl };
2909
+ }
2910
+
2911
+ // src/util/interactive.ts
2912
+ import { chmod, readFile as readFile4, writeFile as writeFile5 } from "fs/promises";
2913
+ import { resolve as resolve6 } from "path";
2914
+ import { confirm, password, select } from "@inquirer/prompts";
2915
+ function isInteractive(noInteractiveFlag) {
2916
+ if (noInteractiveFlag) return false;
2917
+ if (process.env.CI && process.env.CI !== "false") return false;
2918
+ if (!process.stdin.isTTY) return false;
2919
+ if (!process.stdout.isTTY) return false;
2920
+ return true;
2921
+ }
2922
+ async function promptForAuthEnv(envVarNames, parentEnv) {
2923
+ const collected = {};
2924
+ for (const name of envVarNames) {
2925
+ if (parentEnv[name]) continue;
2926
+ const value = await password({
2927
+ message: `${name}:`,
2928
+ mask: "*"
2929
+ });
2930
+ if (value) collected[name] = value;
2931
+ }
2932
+ return collected;
2933
+ }
2934
+ async function promptForMode() {
2935
+ return await select({
2936
+ message: "What would you like to run?",
2937
+ choices: [
2938
+ { name: "Smoke test (1 VU, fast)", value: "smoke" },
2939
+ { name: "Load test (multiple VUs, longer)", value: "load" },
2940
+ { name: "Both (smoke first, then load)", value: "both" },
2941
+ { name: "Skip \u2014 I'll run it later", value: "skip" }
2942
+ ],
2943
+ default: "smoke"
2944
+ });
2945
+ }
2946
+ async function confirmContinue(message) {
2947
+ return await confirm({ message, default: true });
2948
+ }
2949
+ async function writeEnvFile(dir, baseUrl, vars, opts = {}) {
2950
+ const path = resolve6(dir, ".env");
2951
+ const existing = await readEnvFile(path);
2952
+ const written = [];
2953
+ const preserved = [];
2954
+ if (!existing.BASE_URL || opts.overwrite) {
2955
+ existing.BASE_URL = baseUrl;
2956
+ written.push("BASE_URL");
2957
+ } else {
2958
+ preserved.push("BASE_URL");
2959
+ }
2960
+ for (const [k, v] of Object.entries(vars)) {
2961
+ if (existing[k] && !opts.overwrite) {
2962
+ preserved.push(k);
2963
+ } else {
2964
+ existing[k] = v;
2965
+ written.push(k);
2966
+ }
2967
+ }
2968
+ const body = `${Object.entries(existing).map(([k, v]) => `${k}=${v}`).join("\n")}
2969
+ `;
2970
+ await writeFile5(path, body, "utf8");
2971
+ await chmod(path, 384).catch(() => {
2972
+ });
2973
+ return { path, written, preserved };
2974
+ }
2975
+ async function readEnvFile(path) {
2976
+ try {
2977
+ const text = await readFile4(path, "utf8");
2978
+ const out = {};
2979
+ for (const raw of text.split(/\r?\n/)) {
2980
+ const line = raw.trim();
2981
+ if (!line || line.startsWith("#")) continue;
2982
+ const eq = line.indexOf("=");
2983
+ if (eq < 0) continue;
2984
+ const k = line.slice(0, eq).trim();
2985
+ const v = line.slice(eq + 1);
2986
+ if (k) out[k] = v;
2987
+ }
2988
+ return out;
2989
+ } catch {
2990
+ return {};
2991
+ }
2992
+ }
2993
+
2994
+ // src/util/k6.ts
2995
+ import { spawn } from "child_process";
2996
+ import { constants, access } from "fs/promises";
2997
+ import { delimiter, join } from "path";
2998
+ async function findK6Binary() {
2999
+ const path = process.env.PATH ?? "";
3000
+ const exts = process.platform === "win32" ? [".exe", ".cmd", ".bat", ""] : [""];
3001
+ for (const dir of path.split(delimiter)) {
3002
+ if (!dir) continue;
3003
+ for (const ext of exts) {
3004
+ const candidate = join(dir, `k6${ext}`);
3005
+ try {
3006
+ await access(candidate, constants.X_OK);
3007
+ return candidate;
3008
+ } catch {
3009
+ }
3010
+ }
3011
+ }
3012
+ return null;
3013
+ }
3014
+ async function runK6(opts) {
3015
+ const bin = await findK6Binary();
3016
+ if (!bin) {
3017
+ throw new Error(
3018
+ "k6 not found on PATH. Install it from https://grafana.com/docs/k6/latest/set-up/install-k6/ and try again."
3019
+ );
3020
+ }
3021
+ return await new Promise((resolveExit, reject) => {
3022
+ const child = spawn(bin, ["run", ...opts.args ?? [], opts.script], {
3023
+ cwd: opts.cwd,
3024
+ env: { ...process.env, ...opts.env },
3025
+ stdio: "inherit"
3026
+ });
3027
+ child.on("error", reject);
3028
+ child.on("close", (code) => resolveExit(code ?? 0));
3029
+ });
2909
3030
  }
2910
3031
 
2911
3032
  // src/commands/test.ts
3033
+ var VALID_MODES = ["smoke", "load", "both", "skip"];
2912
3034
  function registerTestCommand(program) {
2913
3035
  program.command("test").description("Compile an OpenAPI spec into a runnable k6 test rig (smoke + load).").argument("<spec>", "path to OpenAPI 3.x spec (YAML or JSON)").option("-o, --output <dir>", "output directory for the generated rig", "./loadam-out/k6").option(
2914
3036
  "--target <url>",
2915
3037
  "override base URL baked into config.js (env BASE_URL still wins at runtime)"
2916
- ).option("--fixture-size <n>", "number of fixtures per slot", "10").option("--seed <n>", "deterministic seed for fixture generation", "1").option("--json", "emit a single JSON summary on stdout (CI mode)", false).action(withFriendlyErrors(runTest));
3038
+ ).option("--fixture-size <n>", "number of fixtures per slot", "10").option("--seed <n>", "deterministic seed for fixture generation", "1").option(
3039
+ "--mode <mode>",
3040
+ "run after generating: smoke | load | both | skip (default: prompt if interactive, skip otherwise)"
3041
+ ).option("--no-interactive", "disable all prompts (CI mode)", false).option("--json", "emit a single JSON summary on stdout (CI mode)", false).action(withFriendlyErrors(runTest));
2917
3042
  }
2918
3043
  async function runTest(spec, opts) {
2919
3044
  const out = makeOutput(!!opts.json);
2920
- const specPath = resolve6(spec);
3045
+ const specPath = resolve7(spec);
2921
3046
  out.start(`Parsing OpenAPI spec: ${specPath}`);
2922
3047
  const ir = await fromOpenApiFile(specPath);
2923
3048
  inferResourceGraph(ir);
@@ -2929,12 +3054,12 @@ async function runTest(spec, opts) {
2929
3054
  fixtureSize: Number.isFinite(fixtureSize) ? fixtureSize : 10,
2930
3055
  seed: Number.isFinite(seed) ? seed : 1
2931
3056
  });
2932
- const outDir = resolve6(opts.output);
3057
+ const outDir = resolve7(opts.output);
2933
3058
  await mkdir3(outDir, { recursive: true });
2934
3059
  for (const [rel, contents] of Object.entries(result.files)) {
2935
- const full = resolve6(outDir, rel);
3060
+ const full = resolve7(outDir, rel);
2936
3061
  await mkdir3(dirname3(full), { recursive: true });
2937
- await writeFile5(full, contents, "utf8");
3062
+ await writeFile6(full, contents, "utf8");
2938
3063
  }
2939
3064
  const fileCount = Object.keys(result.files).length;
2940
3065
  const fixtureCount = Object.keys(result.fixtures.byOperationId).length;
@@ -2942,12 +3067,46 @@ async function runTest(spec, opts) {
2942
3067
  out.info(
2943
3068
  ` ${ir.operations.length} operations \xB7 ${fixtureCount} ops with fixtures \xB7 ${result.fixtures.size} samples each`
2944
3069
  );
2945
- if (!out.json) {
2946
- out.step("");
2947
- out.step("Next steps:");
2948
- out.step(` cd ${opts.output}`);
2949
- out.step(" cp .env.example .env # fill in credentials");
2950
- out.step(" k6 run smoke.js");
3070
+ const interactive = !opts.json && isInteractive(!!opts.noInteractive);
3071
+ const requestedMode = parseModeFlag(opts.mode);
3072
+ let envAction = "not-needed";
3073
+ let runResult = null;
3074
+ if (interactive) {
3075
+ if (result.auth.envVars.length > 0) {
3076
+ out.step("");
3077
+ out.info(
3078
+ `Detected ${result.auth.envVars.length} required env var(s): ${result.auth.envVars.join(", ")}`
3079
+ );
3080
+ for (const note of result.auth.notes) out.info(` ${note}`);
3081
+ const collected = await promptForAuthEnv(result.auth.envVars, process.env);
3082
+ const usedFromShell = result.auth.envVars.filter((v) => process.env[v] && !(v in collected));
3083
+ const written = await writeEnvFile(outDir, result.baseUrl, collected);
3084
+ envAction = "wrote";
3085
+ out.success(`Wrote ${written.path} (mode 0600)`);
3086
+ if (usedFromShell.length > 0) {
3087
+ out.info(` Using from shell: ${usedFromShell.join(", ")}`);
3088
+ }
3089
+ if (written.preserved.length > 0) {
3090
+ out.info(` Preserved existing keys: ${written.preserved.join(", ")}`);
3091
+ }
3092
+ } else {
3093
+ const written = await writeEnvFile(outDir, result.baseUrl, {});
3094
+ out.success(`Wrote ${written.path}`);
3095
+ envAction = "wrote";
3096
+ }
3097
+ const mode = requestedMode ?? await promptForMode();
3098
+ runResult = await maybeRun(mode, outDir, out);
3099
+ } else {
3100
+ const mode = requestedMode ?? "skip";
3101
+ if (mode !== "skip") {
3102
+ runResult = await maybeRun(mode, outDir, out);
3103
+ } else if (!out.json) {
3104
+ out.step("");
3105
+ out.step("Next steps:");
3106
+ out.step(` cd ${opts.output}`);
3107
+ out.step(" cp .env.example .env # fill in credentials");
3108
+ out.step(" k6 run smoke.js");
3109
+ }
2951
3110
  }
2952
3111
  out.result({
2953
3112
  command: "test",
@@ -2955,8 +3114,54 @@ async function runTest(spec, opts) {
2955
3114
  files: fileCount,
2956
3115
  operations: ir.operations.length,
2957
3116
  fixtureOps: fixtureCount,
2958
- fixtureSize: result.fixtures.size
3117
+ fixtureSize: result.fixtures.size,
3118
+ envVars: result.auth.envVars,
3119
+ envAction,
3120
+ interactive,
3121
+ run: runResult
2959
3122
  });
3123
+ if (runResult) {
3124
+ if (runResult.smokeExit && runResult.smokeExit !== 0 || runResult.loadExit && runResult.loadExit !== 0) {
3125
+ process.exitCode = 1;
3126
+ }
3127
+ }
3128
+ }
3129
+ function parseModeFlag(raw) {
3130
+ if (!raw) return null;
3131
+ if (!VALID_MODES.includes(raw)) {
3132
+ throw new Error(`Invalid --mode value "${raw}". Expected one of: ${VALID_MODES.join(", ")}.`);
3133
+ }
3134
+ return raw;
3135
+ }
3136
+ async function maybeRun(mode, cwd, out) {
3137
+ if (mode === "skip") return { mode };
3138
+ const bin = await findK6Binary();
3139
+ if (!bin) {
3140
+ out.info("");
3141
+ out.info("k6 binary not found on PATH. Skipping run.");
3142
+ out.info("Install: https://grafana.com/docs/k6/latest/set-up/install-k6/");
3143
+ return { mode: "skip" };
3144
+ }
3145
+ const result = { mode };
3146
+ if (mode === "smoke" || mode === "both") {
3147
+ if (!out.json) {
3148
+ out.step("");
3149
+ out.step("Running k6 smoke test...");
3150
+ }
3151
+ result.smokeExit = await runK6({ cwd, script: "smoke.js" });
3152
+ }
3153
+ if (mode === "load" || mode === "both") {
3154
+ if (mode === "both") {
3155
+ const cont = await confirmContinue("Smoke complete. Continue with load test?");
3156
+ if (!cont) return result;
3157
+ }
3158
+ if (!out.json) {
3159
+ out.step("");
3160
+ out.step("Running k6 load test...");
3161
+ }
3162
+ result.loadExit = await runK6({ cwd, script: "load.js" });
3163
+ }
3164
+ return result;
2960
3165
  }
2961
3166
 
2962
3167
  // src/util/update.ts
@@ -3050,7 +3255,7 @@ ${dim}${TAGLINE}${reset}
3050
3255
  }
3051
3256
 
3052
3257
  // src/program.ts
3053
- var VERSION = "0.0.0";
3258
+ var VERSION = true ? "0.2.0" : "0.0.0";
3054
3259
  function buildProgram() {
3055
3260
  const program = new Command();
3056
3261
  program.name("loadam").description(