@fabricorg/experiments 0.6.1 → 0.6.3

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/fx.js CHANGED
@@ -7086,7 +7086,8 @@ async function cmdImportMojito(inputDir, options = {}) {
7086
7086
  results.push({
7087
7087
  source: waveDir,
7088
7088
  target: "",
7089
- warnings: [`failed to convert: ${e.message}`]
7089
+ warnings: [`failed to convert: ${e.message}`],
7090
+ trigger: "manual"
7090
7091
  });
7091
7092
  }
7092
7093
  }
@@ -7103,8 +7104,36 @@ async function cmdImportMojito(inputDir, options = {}) {
7103
7104
  for (const w of r.warnings) process.stdout.write(` ! ${w}
7104
7105
  `);
7105
7106
  }
7107
+ const reportFile = resolve(options.reportFile ?? join(outDir, "import-report.json"));
7108
+ await writeFile(
7109
+ reportFile,
7110
+ `${JSON.stringify(
7111
+ {
7112
+ schemaVersion: 1,
7113
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
7114
+ input: root,
7115
+ output: outDir,
7116
+ summary: {
7117
+ waves: results.length,
7118
+ converted: results.filter((result) => result.target).length,
7119
+ failed: results.filter((result) => !result.target).length,
7120
+ manualReview: results.filter((result) => result.warnings.length > 0).length
7121
+ },
7122
+ waves: results
7123
+ },
7124
+ null,
7125
+ 2
7126
+ )}
7127
+ `,
7128
+ "utf8"
7129
+ );
7130
+ process.stdout.write(`
7131
+ Compatibility report: ${reportFile}
7132
+ `);
7106
7133
  const failed = results.filter((r) => !r.target).length;
7107
- return failed === 0 ? 0 : 2;
7134
+ if (failed > 0) return 2;
7135
+ if (options.strict && results.some((result) => result.warnings.length > 0)) return 3;
7136
+ return 0;
7108
7137
  }
7109
7138
  async function findWaves(root) {
7110
7139
  const out = [];
@@ -7159,12 +7188,23 @@ async function convertWave(waveDir, outDir) {
7159
7188
  variants.push(variant);
7160
7189
  }
7161
7190
  let triggerNote = null;
7191
+ let convertedTrigger;
7192
+ let triggerMigration = "none";
7162
7193
  if (cfg.trigger) {
7163
7194
  triggerNote = await readFile(join(waveDir, cfg.trigger), "utf8").catch(() => "");
7164
7195
  if (triggerNote) {
7165
- warnings.push(
7166
- "trigger.js was not auto-converted \u2014 review and translate to a Trigger DSL or host-supplied trigger override."
7167
- );
7196
+ const translated = translateCommonTrigger(triggerNote);
7197
+ convertedTrigger = translated.trigger;
7198
+ triggerMigration = translated.kind;
7199
+ if (!translated.trigger) {
7200
+ warnings.push(
7201
+ "trigger.js was not auto-converted \u2014 review and translate to a Trigger DSL or host-supplied trigger override."
7202
+ );
7203
+ triggerMigration = "manual";
7204
+ }
7205
+ } else {
7206
+ warnings.push(`trigger file not found: ${cfg.trigger}`);
7207
+ triggerMigration = "manual";
7168
7208
  }
7169
7209
  }
7170
7210
  let sharedJs;
@@ -7191,20 +7231,47 @@ async function convertWave(waveDir, outDir) {
7191
7231
  if (cfg.manualExposure) spec.manualExposure = true;
7192
7232
  if (sharedJs) spec.sharedJs = sharedJs;
7193
7233
  if (sharedCss) spec.sharedCss = sharedCss;
7194
- if (triggerNote) spec.trigger = { kind: "auto" };
7234
+ if (triggerNote) spec.trigger = convertedTrigger ?? { kind: "auto" };
7195
7235
  const header = [
7196
7236
  "# Imported from Mojito wave",
7197
7237
  `# Source: ${waveDir}`,
7198
7238
  cfg.state ? `# Original state: ${cfg.state}` : null,
7199
7239
  triggerNote ? "#" : null,
7200
- triggerNote ? "# Original trigger.js (NOT auto-applied):" : null,
7240
+ triggerNote ? convertedTrigger ? `# Original trigger.js (auto-converted to ${String(convertedTrigger.kind)}):` : "# Original trigger.js (NOT auto-applied):" : null,
7201
7241
  triggerNote ? triggerNote.split("\n").map((l) => `# ${l}`).join("\n") : null
7202
7242
  ].filter((l) => l !== null).join("\n");
7203
7243
  const yamlBody = stringify(spec);
7204
7244
  const target = join(outDir, `${spec.id}.yaml`);
7205
7245
  await writeFile(target, `${header}
7206
7246
  ${yamlBody}`, "utf8");
7207
- return { source: waveDir, target, warnings };
7247
+ return { source: waveDir, target, warnings, trigger: triggerMigration };
7248
+ }
7249
+ function translateCommonTrigger(source) {
7250
+ const selector = source.match(/(?:waitForElement|waitForSelector)\s*\(\s*(['"])([^'"]+)\1/);
7251
+ if (selector?.[2]) {
7252
+ return {
7253
+ trigger: { kind: "waitForSelector", selector: selector[2] },
7254
+ kind: "waitForSelector"
7255
+ };
7256
+ }
7257
+ const pathname = source.match(/(?:document\.)?location\.pathname\s*={2,3}\s*(['"])([^'"]+)\1/);
7258
+ if (pathname?.[2]) {
7259
+ const escaped = pathname[2].replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
7260
+ return {
7261
+ trigger: { kind: "urlMatch", pattern: `https?://[^/]+${escaped}(?:[?#]|$)`, regex: true },
7262
+ kind: "urlMatch"
7263
+ };
7264
+ }
7265
+ const hrefIncludes = source.match(
7266
+ /(?:document\.)?location\.href\.(?:includes|indexOf)\s*\(\s*(['"])([^'"]+)\1/
7267
+ );
7268
+ if (hrefIncludes?.[2]) {
7269
+ return { trigger: { kind: "urlMatch", pattern: hrefIncludes[2] }, kind: "urlMatch" };
7270
+ }
7271
+ if (/\bdomReady\s*\(/.test(source) && !/\bif\s*\(/.test(source)) {
7272
+ return { trigger: { kind: "auto" }, kind: "auto" };
7273
+ }
7274
+ return { kind: "manual" };
7208
7275
  }
7209
7276
  function normalizeKey(k) {
7210
7277
  return k.toLowerCase().replace(/[^a-z0-9_-]/g, "-");
@@ -7824,14 +7891,17 @@ var ApiClient = class {
7824
7891
  orgId: this.opts.defaultOrgId,
7825
7892
  ...pathParams ?? {}
7826
7893
  };
7827
- const url = new URL(buildPath(def.path, merged), this.opts.baseUrl);
7894
+ const baseUrl = this.opts.baseUrl.endsWith("/") ? this.opts.baseUrl : `${this.opts.baseUrl}/`;
7895
+ const endpointPath = buildPath(def.path, merged).replace(/^\//, "");
7896
+ const url = new URL(endpointPath, baseUrl);
7828
7897
  const init = {
7829
7898
  method: def.method,
7830
7899
  headers: {
7831
7900
  accept: "application/json",
7832
7901
  // Hosted Fabric API keys use their own header so upstream auth
7833
7902
  // middleware cannot mistake them for OAuth bearer credentials.
7834
- ...this.opts.apiKey ? { "x-api-key": this.opts.apiKey } : {}
7903
+ ...this.opts.apiKey ? { "x-api-key": this.opts.apiKey } : {},
7904
+ ...this.opts.bearerToken ? { authorization: `Bearer ${this.opts.bearerToken}` } : {}
7835
7905
  },
7836
7906
  credentials: "include"
7837
7907
  };
@@ -8133,7 +8203,7 @@ var FILES = {
8133
8203
  devDependencies: {
8134
8204
  "@cucumber/cucumber": "^13.0.0",
8135
8205
  "@duckdb/node-api": "^1.5.0",
8136
- "@fabricorg/databricks-bdd": "^0.3.1",
8206
+ "@fabricorg/databricks-bdd": "^0.3.2",
8137
8207
  tsx: "^4.19.2",
8138
8208
  typescript: "^5.8.3"
8139
8209
  }
@@ -8466,7 +8536,13 @@ async function cmdTestPublish(evidencePath, options = {}) {
8466
8536
  }
8467
8537
  const evidence = parseEvidenceText(await readFile(resolve(evidencePath), "utf8"));
8468
8538
  const input = normalizeTestEvidence(evidence, { ...options, env });
8469
- const api = new ApiClient({ baseUrl: apiUrl, apiKey, defaultOrgId: tenantId });
8539
+ const bearerToken = options.bearerToken ?? await resolveGatewayToken(apiUrl, env);
8540
+ const api = new ApiClient({
8541
+ baseUrl: apiUrl,
8542
+ apiKey,
8543
+ ...bearerToken ? { bearerToken } : {},
8544
+ defaultOrgId: tenantId
8545
+ });
8470
8546
  const created = await api.testRuns.create(input);
8471
8547
  process.stdout.write(
8472
8548
  `Published ${created.source} run ${created.id}: ${created.status} (${created.summary.passed}/${created.summary.total} passed)
@@ -8474,6 +8550,32 @@ async function cmdTestPublish(evidencePath, options = {}) {
8474
8550
  );
8475
8551
  return 0;
8476
8552
  }
8553
+ async function resolveGatewayToken(apiUrl, env) {
8554
+ if (env.FX_GATEWAY_BEARER_TOKEN) return env.FX_GATEWAY_BEARER_TOKEN;
8555
+ if (!new URL(apiUrl).hostname.endsWith(".databricksapps.com")) return void 0;
8556
+ const host = env.DATABRICKS_HOST?.replace(/\/$/, "");
8557
+ const clientId = env.FX_QUALITY_DATABRICKS_CLIENT_ID ?? env.DATABRICKS_CLIENT_ID;
8558
+ const clientSecret = env.FX_QUALITY_DATABRICKS_CLIENT_SECRET ?? env.DATABRICKS_CLIENT_SECRET;
8559
+ if (!host || !clientId || !clientSecret) {
8560
+ throw new Error("Databricks Apps publication requires DATABRICKS_HOST and client credentials");
8561
+ }
8562
+ const response = await fetch(`${host}/oidc/v1/token`, {
8563
+ method: "POST",
8564
+ headers: {
8565
+ authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`,
8566
+ "content-type": "application/x-www-form-urlencoded"
8567
+ },
8568
+ body: new URLSearchParams({ grant_type: "client_credentials", scope: "all-apis" })
8569
+ });
8570
+ if (!response.ok) {
8571
+ throw new Error(`Databricks OAuth failed (${response.status} ${response.statusText})`);
8572
+ }
8573
+ const body = await response.json();
8574
+ if (typeof body.access_token !== "string" || !body.access_token) {
8575
+ throw new Error("Databricks OAuth response did not include an access token");
8576
+ }
8577
+ return body.access_token;
8578
+ }
8477
8579
  function normalizeTestEvidence(value, options = {}) {
8478
8580
  if (!value || typeof value !== "object") throw new Error("Evidence must be a JSON object");
8479
8581
  const evidence = value;
@@ -8716,10 +8818,13 @@ GLOBAL FLAGS
8716
8818
  --external-id <id> test publish: idempotent CI run identifier
8717
8819
  --run-url <url> test publish: CI artifact/run link
8718
8820
  --key-id <id> publish: keyId to sign with (default: dev-default)
8821
+ --out <dir> import-mojito: output directory
8822
+ --report <file> import-mojito: compatibility report path
8823
+ --strict import-mojito: fail while manual-review warnings remain
8719
8824
  -h, --help print this help
8720
8825
  -v, --version print version
8721
8826
  `;
8722
- var VERSION2 = "0.6.1";
8827
+ var VERSION2 = "0.6.3";
8723
8828
  function parse2(argv) {
8724
8829
  const positional = [];
8725
8830
  const flags = {};
@@ -8833,11 +8938,15 @@ async function main() {
8833
8938
  });
8834
8939
  case "import-mojito":
8835
8940
  if (!args.positional[0]) {
8836
- process.stderr.write("Usage: fx import-mojito <dir> [--out <out-dir>]\n");
8941
+ process.stderr.write(
8942
+ "Usage: fx import-mojito <dir> [--out <out-dir>] [--report <file>] [--strict]\n"
8943
+ );
8837
8944
  return 1;
8838
8945
  }
8839
8946
  return cmdImportMojito(args.positional[0], {
8840
- outputDir: args.flags.out});
8947
+ outputDir: args.flags.out,
8948
+ reportFile: args.flags.report,
8949
+ strict: args.flags.strict === true});
8841
8950
  case "preview":
8842
8951
  if (!args.positional[0]) {
8843
8952
  process.stderr.write(