Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

104 KB
Raw Blame
'use strict';
// `businessRules[]` — the App Spec surface over the SDK's business-rule compiler.
//
// Three layers, because a failure in any one of them is silent in a different way:
//   1. VALIDATION — the spec surface mirrors the SDK's supported slice exactly. Anything outside it
//      throws a ValidationError deep inside the push, so catching it here names the field instead.
//   2. MAPPING — `businessRuleDef` produces the SDK's node shape. This is the dangerous one: the
//      obvious shape (operator/lhs/rhs/thenActions) is silently MERGED and ignored, producing a rule
//      that deploys, activates, and never fires. See sdk-uptake-contract.test.js.
//   3. REAL BUNDLE — the mapped def is pushed through the shipped SDK and the compiled XAML is
//      inspected. Only this proves the author's columns actually reach the platform.
const test = require('node:test');
const assert = require('node:assert');
const os = require('node:os');
const path = require('node:path');
const fs = require('node:fs');

const { validateAppSpec } = require('../lib/app-spec.js');
const { businessRuleDef } = require('../lib/sdk-build.js');

const BUNDLE = path.resolve(__dirname, '..', 'vendor', 'cds-maker-sdk.cjs');
const dirs = [];
test.after(() => { for (const d of dirs) fs.rmSync(d, { recursive: true, force: true }); });

// Minimal spec that is valid on its own, so a validation failure below is always about the rule.
function specWith(rules) {
  return {
    solution: { uniqueName: 'BR', displayName: 'BR', publisherPrefix: 'new' },
    app: { name: 'BR App', description: '' },
    entities: [{
      schemaName: 'new_ticket', displayName: 'Ticket', pluralName: 'Tickets',
      primaryAttribute: { schemaName: 'new_subject', displayName: 'Subject' },
      columns: [
        { schemaName: 'new_status', displayName: 'Status', type: 'Choice', options: ['New', 'Closed'] },
        { schemaName: 'new_notes', displayName: 'Notes', type: 'Memo' },
        { schemaName: 'new_owner', displayName: 'Owner', type: 'Text' },
      ],
    }],
    appShell: { areas: [{ label: 'Main', groups: [{ label: 'Records', subAreas: [{ entity: 'new_ticket', title: 'Tickets' }] }] }] },
    businessRules: rules,
  };
}
const RULE = {
  entity: 'new_ticket', name: 'Hide notes when closed',
  conditions: [{ field: 'new_status', operator: 'Equals', value: '100000001', dataType: 'Picklist' }],
  actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }],
};
const errorsFor = (rules) => (validateAppSpec(specWith(rules), { profile: 'plan' }).errors || []);

// --- 1. validation ----------------------------------------------------------------------------

test('a well-formed business rule validates', () => {
  const v = validateAppSpec(specWith([RULE]), { profile: 'plan' });
  assert.strictEqual(v.ok, true, JSON.stringify(v.errors));
});

test('a rule must name a known entity and its own columns', () => {
  assert.ok(errorsFor([{ ...RULE, entity: 'new_nope' }]).some((e) => /unknown entity/.test(e)));
  // A column that does not exist on the table is the silent case that matters: the platform accepts
  // such a rule and it simply never fires, so nothing downstream would catch it.
  assert.ok(errorsFor([{ ...RULE, conditions: [{ field: 'new_ghost', operator: 'Equals', value: '1' }] }])
    .some((e) => /'new_ghost', which is not a column on new_ticket/.test(e)));
  assert.ok(errorsFor([{ ...RULE, actions: [{ type: 'SetVisibility', field: 'new_ghost', visible: false }] }])
    .some((e) => /not a column on new_ticket/.test(e)));
});

test('operators and actions are constrained to the slice the SDK supports', () => {
  // Outside the slice the SDK does something WORSE than throwing: an unknown operator resolves to
  // Equals, so the point of validating here is to stop a wrong rule rather than to name a field.
  assert.ok(errorsFor([{ ...RULE, conditions: [{ field: 'new_status', operator: 'NotARealOperator', value: 'x' }] }])
    .some((e) => /condition operator must be one of/.test(e)));
  // `ShowErrorMessage` is modelled by the SDK but deliberately not exposed yet — it needs mapping in
  // businessRuleDef that cannot be live-verified on an environment without the bound member.
  assert.ok(errorsFor([{ ...RULE, actions: [{ type: 'ShowErrorMessage', field: 'new_notes', message: 'x' }] }])
    .some((e) => /action type must be one of/.test(e)));
  for (const operator of ['Equals', 'DoesNotEqual', 'BeginsWith', 'IsGreaterThan']) {
    assert.deepStrictEqual(errorsFor([{ ...RULE, conditions: [{ field: 'new_status', operator, value: '1' }] }]), []);
  }
});

test('presence operators are ACCEPTED now that the compiler emits a null parameter list', () => {
  // These were blocked while the compiler emitted an EMPTY parameter array for a valueless operator
  // (`[New Object() { }]`) where every server-authored rule writes `<x:Null x:Key="Parameters" />`.
  // An empty Object[] is not null, and the UiData generator answered HTTP 500 on it (#481).
  //
  // Fixed upstream and re-verified LIVE on a real org with the vendored bundle: all four operators
  // push successfully in one run, where previously the two presence operators failed 500 while
  // `Equals` succeeded alongside them.
  for (const operator of ['ContainsData', 'DoesNotContainData']) {
    const errs = errorsFor([{ ...RULE, conditions: [{ field: 'new_notes', operator }] }]);
    assert.deepStrictEqual(errs, [], `${operator} must now validate: ${JSON.stringify(errs)}`);
  }
});

test('comparison operators must carry a value', () => {
  assert.ok(errorsFor([{ ...RULE, conditions: [{ field: 'new_status', operator: 'Equals' }] }])
    .some((e) => /needs a value/.test(e)));
});

test('a boolean action payload must be a real boolean', () => {
  // "false" is truthy, so coercing would invert the author's intent silently — the same trap
  // `app.newLook` validation closes.
  for (const bad of ['false', 'true', 0, 1]) {
    assert.ok(errorsFor([{ ...RULE, actions: [{ type: 'SetVisibility', field: 'new_notes', visible: bad }] }])
      .some((e) => /must be a boolean/.test(e)), `visible=${JSON.stringify(bad)} must be rejected`);
  }
  assert.deepStrictEqual(errorsFor([{ ...RULE, actions: [{ type: 'LockUnlock', field: 'new_notes', lock: true }] }]), []);
  assert.ok(errorsFor([{ ...RULE, actions: [{ type: 'LockUnlock', field: 'new_notes' }] }])
    .some((e) => /needs 'lock'/.test(e)));
});

test('a rule with no conditions or no actions is rejected', () => {
  // Both compile to a rule that deploys and does nothing — the failure mode this whole surface exists
  // to make impossible.
  assert.ok(errorsFor([{ ...RULE, conditions: [] }]).some((e) => /conditions\[\] is required/.test(e)));
  assert.ok(errorsFor([{ ...RULE, actions: [] }]).some((e) => /actions\[\] is required/.test(e)));
});

// --- 2. mapping -------------------------------------------------------------------------------

test('businessRuleDef maps onto the SDK node shape (clauses + trueBranch), not the obvious one', () => {
  const def = businessRuleDef({
    ...RULE,
    actions: [
      { type: 'SetVisibility', field: 'new_notes', visible: false },
      { type: 'LockUnlock', field: 'new_owner', lock: true },
      { type: 'SetBusinessRequired', field: 'new_owner', required: true },
      { type: 'SetFieldValue', field: 'new_owner', value: 'unassigned' },
    ],
  });
  assert.strictEqual(def.entityLogicalName, 'new_ticket');
  assert.strictEqual(def.scope, 'Entity');
  assert.strictEqual(def.status, 'Active', 'a rule is inert until activated, so Active is the default');

  const rc = def.rootCondition;
  assert.strictEqual(rc.logic, 'AND');
  // The keys that matter. `thenActions`/`lhs`/`rhs` would be merged and ignored.
  assert.deepStrictEqual(Object.keys(rc).sort(), ['clauses', 'displayName', 'falseBranch', 'id', 'logic', 'trueBranch']);
  // `valueWorkflowType` is the SDK's WorkflowAttributeType ENUM VALUE, not the App Spec's token:
  // 'Picklist' -> '10'. The SDK forwards this field verbatim into the serialized expression, and the
  // enum has no 'Picklist' member, so emitting the token would put an out-of-domain value on the
  // wire. See workflowAttributeType() in sdk-build.js.
  assert.deepStrictEqual(rc.clauses, [{
    id: 'c1', field: 'new_status', operator: 'Equals', valueType: 'Value', value: '100000001', valueWorkflowType: '10',
  }]);
  assert.deepStrictEqual(rc.trueBranch.map((a) => [a.type, a.field, a.visible ?? a.lock ?? a.required ?? a.value]), [
    ['SetVisibility', 'new_notes', false],
    ['LockUnlock', 'new_owner', true],
    ['SetBusinessRequired', 'new_owner', true],
    ['SetFieldValue', 'new_owner', 'unassigned'],
  ]);
  assert.ok(rc.trueBranch.every((a) => a.id && a.displayName), 'every action carries an id and a label');
});

test('a presence operator still MAPS correctly (the mapper is fine; the platform is not)', () => {
  // Kept because the block is a platform limitation, not a mapping bug — when #481 is fixed the
  // operator can be re-enabled in validation without touching businessRuleDef. Emitting
  // `value: undefined` would still add the key, and the compiler treats present-but-empty
  // differently from absent.
  const def = businessRuleDef({ ...RULE, conditions: [{ field: 'new_notes', operator: 'ContainsData' }] });
  assert.deepStrictEqual(Object.keys(def.rootCondition.clauses[0]).sort(), ['field', 'id', 'operator', 'valueType']);
});

// --- 3. real bundle ---------------------------------------------------------------------------

test('REAL BUNDLE: the mapped rule reaches the wire as WfomJson naming the authored columns', async () => {
  const { createMakerSdk, createNodeWorkspaceStorage } = require(BUNDLE);
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'br-spec-'));
  dirs.push(dir);
  const calls = [];
  const sdk = createMakerSdk({
    workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: 'https://contoso.crm.dynamics.com',
    httpClient: {
      get: async () => ({ status: 200, headers: {}, body: { value: [] } }),
      post: async (url, body) => {
        calls.push({ url, body });
        return { status: 204, headers: { 'odata-entityid': 'https://x/workflows(55555555-5555-5555-5555-555555555555)' }, body: {} };
      },
      patch: async () => ({ status: 204, headers: {}, body: {} }),
      put: async () => ({ status: 204, headers: {}, body: {} }),
      delete: async () => ({ status: 204, headers: {}, body: {} }),
    },
  });
  await sdk.initWorkspace();

  const def = businessRuleDef(RULE);
  const art = await sdk.createArtifact('businessRule', def);
  await sdk.updateElement('businessRule', art.id, '/rootCondition', def.rootCondition);
  const pushed = await sdk.pushArtifact('businessRule', art.id);
  assert.strictEqual(pushed.saved, true);

  const bound = calls.find((c) => /CreateProcessWithWfomJson/i.test(String(c.url)));
  assert.ok(bound, 'the bound member is the ONLY write path; urls: ' + JSON.stringify(calls.map((c) => c.url)));
  // There is no longer a classic `workflows` row to fall back to — the SDK deleted that compiler.
  assert.strictEqual(calls.some((c) => /\/workflows$/.test(String(c.url))), false,
    'no classic XAML row is written any more');
  assert.deepStrictEqual(Object.keys(bound.body).sort(), ['Entity', 'WfomJson']);

  // The assertion that matters: a well-formed but EMPTY rule would satisfy everything above.
  const wfom = String(bound.body.WfomJson);
  assert.match(wfom, /new_status/, 'the condition column reaches the workflow object model');
  assert.match(wfom, /new_notes/, 'the action column reaches the workflow object model');
  assert.match(wfom, /new_ticket/, 'the rule is bound to the authored table');
});

// REAL BUNDLES: a rule the PREVIOUS release's SDK created leaves its copy, stamped with its projection, in the workspace. A rule
// that exists is never fetched or pushed again by a build (sdk-build.js: it is reused, and only its state is set, by a record update),
// so that copy is never refused; what this release's SDK does on that same workspace is create the next rule — and it does so exactly
// as on a workspace it wrote, sending nothing for the old rule.
const { skipWithoutPrevious, requirePrevious } = require('./helpers/previous-bundle.js');
test('REAL BUNDLES: on a workspace holding a rule the previous release created, this release creates the next rule as on its own', { skip: skipWithoutPrevious() }, async () => {
  const run = async (creatorBundle) => {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'br-upgrade-'));
    dirs.push(dir);
    const calls = [];
    let n = 0;
    const httpClient = {
      get: async () => ({ status: 200, headers: {}, body: { value: [] } }),
      post: async (url, body) => {
        calls.push({ url: String(url), body });
        n += 1;
        return { status: 204, headers: { 'odata-entityid': `https://x/workflows(5555555${n}-5555-5555-5555-555555555555)` }, body: {} };
      },
      patch: async (url, body) => { calls.push({ url: String(url), body }); return { status: 204, headers: {}, body: {} }; },
      put: async () => ({ status: 204, headers: {}, body: {} }),
      delete: async () => ({ status: 204, headers: {}, body: {} }),
    };
    const open = async (bundle) => {
      const sdk = bundle.createMakerSdk({ workspaceStorage: bundle.createNodeWorkspaceStorage(dir), instanceUrl: 'https://contoso.crm.dynamics.com', httpClient });
      await sdk.initWorkspace();
      return sdk;
    };
    const author = async (sdk, rule) => {
      const def = businessRuleDef(rule);
      const art = await sdk.createArtifact('businessRule', def);
      await sdk.updateElement('businessRule', art.id, '/rootCondition', def.rootCondition);
      return sdk.pushArtifact('businessRule', art.id);
    };
    const first = await author(await open(creatorBundle), RULE);
    assert.strictEqual(first.saved, true);
    calls.length = 0;
    const next = await author(await open(require(BUNDLE)), { ...RULE, name: 'Hide notes when closed' });
    return { saved: next.saved, wire: calls.map((c) => ({ url: c.url, body: c.body })) };
  };
  const upgrade = await run(requirePrevious());
  const control = await run(require(BUNDLE));
  assert.strictEqual(upgrade.saved, true);
  // The first rule was answered the id 55555551-…: nothing this release sends names it.
  assert.ok(upgrade.wire.every((w) => !w.url.includes('55555551-')), JSON.stringify(upgrade.wire.map((w) => w.url)));
  assert.match(upgrade.wire[0].url, /CreateProcessWithWfomJson/i);
  // The WfomJson carries ids the SDK mints at random; the rest is compared as it is.
  const norm = (wire) => JSON.stringify(wire).replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '<id>');
  assert.strictEqual(norm(upgrade.wire), norm(control.wire), 'the same request as on a workspace this release wrote');
});

test('REAL BUNDLE: a valueless operator emits a unary condition with the IsNull/NotNull opcode', async () => {
  // A presence operator has nothing to compare against, so the SDK must emit NO right-hand operand
  // and the dedicated opcode — not `Equals` against an empty string, which is a different question
  // and would silently answer false for a populated column.
  //
  // Measured opcodes (WorkflowConditionOperator in the bundle): NotNull "1", IsNull "0".
  //
  // The SDK used to emit these as a BinaryExpression with an EMPTY `right` list. The platform's
  // BinaryExpression rejects a unary operator ("Operator in a binary expression must be a binary
  // operator"), so the vendored SDK now emits the UnaryExpression the platform's own designer does:
  //   {"__class":"UnaryExpression:#Microsoft.Crm.Workflow.Expressions","conditionOperatoroperator":"1",
  //    "operand":{"__class":"EntityAttributeExpression:#...","attributeName":"new_notes",...}}
  const { createMakerSdk, createNodeWorkspaceStorage } = require(BUNDLE);
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'br-null-'));
  dirs.push(dir);

  for (const [operator, expectedOpcode] of [['ContainsData', '1'], ['DoesNotContainData', '0']]) {
    const calls = [];
    const sdk = createMakerSdk({
      workspaceStorage: createNodeWorkspaceStorage(fs.mkdtempSync(path.join(dir, 'w'))), instanceUrl: 'https://contoso.crm.dynamics.com',
      httpClient: {
        get: async () => ({ status: 200, headers: {}, body: { value: [] } }),
        post: async (url, body) => {
          calls.push({ url, body });
          return { status: 204, headers: { 'odata-entityid': 'https://x/workflows(55555555-5555-5555-5555-555555555555)' }, body: {} };
        },
        patch: async () => ({ status: 204, headers: {}, body: {} }),
        put: async () => ({ status: 204, headers: {}, body: {} }),
        delete: async () => ({ status: 204, headers: {}, body: {} }),
      },
    });
    await sdk.initWorkspace();

    const def = businessRuleDef({
      name: 'Presence Rule', entity: 'new_ticket',
      conditions: [{ field: 'new_notes', operator }],
      actions: [{ type: 'SetVisibility', field: 'new_owner', visible: false }],
    });
    const art = await sdk.createArtifact('businessRule', def);
    await sdk.updateElement('businessRule', art.id, '/rootCondition', def.rootCondition);
    await sdk.pushArtifact('businessRule', art.id);

    const bound = calls.find((c) => /CreateProcessWithWfomJson/i.test(String(c.url)));
    assert.ok(bound, `${operator}: the bound member must be called`);
    const wfom = JSON.parse(bound.body.WfomJson);
    const expr = wfom.steps.list[0].steps.list[0].conditionExpression;
    assert.strictEqual(expr.conditionOperatoroperator, expectedOpcode,
      `${operator} must emit opcode ${expectedOpcode}, got ${expr.conditionOperatoroperator}`);
    assert.match(String(expr.__class), /^UnaryExpression:/, `${operator} must be a unary condition`);
    assert.strictEqual(expr.right, undefined, `${operator} must carry NO right-hand operand`);
    assert.strictEqual(expr.operand.attributeName, 'new_notes');
  }
});

// --- peer-review findings: verified and fixed ---------------------------------------------------

test('REAL BUNDLE: dataType maps to a WorkflowAttributeType on CONDITIONS and is ignored on SetFieldValue ACTIONS', async () => {
  // ⚠ THIS TEST WAS REWRITTEN TWICE BY MEASUREMENT, which is precisely why it existed.
  //
  // Originally it asserted `dataType` was DECORATIVE everywhere, because the JSON path that replaced
  // the deleted XAML compiler typed every literal as WorkflowAttributeType String ("14"). Its own
  // comment said: "if the SDK begins emitting a real type, this fails and whoever sees it must
  // re-read the surface rather than discover the change in production." That happened on the
  // injected-storage re-vendor.
  //
  // The FIRST rewrite pinned what it saw — the authored token reaching the wire verbatim — and read
  // that as "dataType is honoured". It was not: `WorkflowAttributeType` is a NUMERIC-STRING enum
  // (`{ Boolean:'0' … Money:'7', Picklist:'10', String:'14' … }`), so `"String"` and even
  // `"NotAWorkflowType"` were OUT-OF-DOMAIN values, and the SDK's own fallback for the same field is
  // `'14'`. Pinning pass-through of a non-member value is the opposite of honoured; it froze a
  // silent wrong write. `businessRuleDef` now maps the spec token through the enum.
  //
  // MEASURED against this bundle after the mapping:
  //
  //   CONDITION path  — `conditionExpression.right[0].type` is the ENUM VALUE for the authored
  //                     token ('Money' -> '7', 'Picklist' -> '10'), and '14' when none is authored,
  //                     which is what the previous bundle hard-coded for every literal.
  //   ACTION path     — the SetFieldValue step still emits "14" for EVERY token, so dataType really
  //                     is ignored there.
  //
  // The two paths therefore DISAGREE, and that asymmetry is the thing worth pinning: an author who
  // sets `dataType: 'Integer'` on a SetFieldValue action gets a String literal on the wire, while
  // the same token on a condition is typed. Do not "simplify" this test by asserting one rule for
  // both paths — that would re-hide the difference.
  const { createMakerSdk, createNodeWorkspaceStorage } = require(BUNDLE);
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'br-dt-'));
  dirs.push(dir);

  // The metadata-derived EXPRESSION type. 0 because this client serves no field metadata.
  const UNTYPED_TOKEN = '0';
  // What the SetFieldValue action path emits for every token: WorkflowAttributeType.String.
  const ACTION_STRING_TOKEN = '14';
  const { BUSINESS_RULE_DATA_TYPES } = require('../lib/app-spec.js');
  // The enum the SDK serializes into, restated here INDEPENDENTLY of the engine's own table so this
  // is an oracle rather than a restatement of `workflowAttributeType`.
  const EXPECTED_ENUM = {
    String: '14', Memo: '14', Boolean: '0', DateTime: '2', Decimal: '3', Double: '4', Float: '4',
    Integer: '5', Money: '7', Picklist: '10', State: '12', Status: '13', Lookup: '6', Customer: '1',
    Owner: '8', UniqueIdentifier: '15', MultiSelectPicklist: '23',
  };
  const TOKENS = [...BUSINESS_RULE_DATA_TYPES];

  const push = async (def) => {
    const calls = [];
    const sdk = createMakerSdk({
      workspaceStorage: createNodeWorkspaceStorage(fs.mkdtempSync(path.join(dir, 'w'))), instanceUrl: 'https://contoso.crm.dynamics.com',
      httpClient: {
        get: async () => ({ status: 200, headers: {}, body: { value: [] } }),
        post: async (url, body) => {
          calls.push({ url, body });
          return { status: 204, headers: { 'odata-entityid': 'https://x/workflows(55555555-5555-5555-5555-555555555555)' }, body: {} };
        },
        patch: async () => ({ status: 204, headers: {}, body: {} }),
        put: async () => ({ status: 204, headers: {}, body: {} }),
        delete: async () => ({ status: 204, headers: {}, body: {} }),
      },
    });
    await sdk.initWorkspace();
    const art = await sdk.createArtifact('businessRule', def);
    await sdk.updateElement('businessRule', art.id, '/rootCondition', def.rootCondition);
    await sdk.pushArtifact('businessRule', art.id);
    return JSON.parse(calls.find((c) => /CreateProcessWithWfomJson/i.test(String(c.url))).body.WfomJson);
  };

  for (const dataType of TOKENS) {
    // 1. the CONDITION path
    const condWfom = await push(businessRuleDef({
      ...RULE, conditions: [{ field: 'new_status', operator: 'Equals', value: '1', dataType }],
    }));
    const expr = condWfom.steps.list[0].steps.list[0].conditionExpression;
    // The EXPRESSION type is metadata-derived and stays untyped here — this client serves no field
    // metadata. It is deliberately asserted so a future change that starts deriving it from the
    // authored token (rather than from the field) shows up as a failure.
    assert.strictEqual(expr.type, UNTYPED_TOKEN,
      `condition dataType '${dataType}': the EXPRESSION type should stay metadata-derived, got ${expr.type}`);
    // The LITERAL operand type is the ENUM VALUE for the authored token, never the token itself.
    // `WorkflowAttributeType` has no `String`/`Money` members — only '14'/'7' — so a raw token here
    // would be an out-of-domain value that Dataverse either rejects or coerces.
    const want = EXPECTED_ENUM[dataType];
    assert.ok(want, `test gap: no expected enum value recorded for accepted token '${dataType}'`);
    assert.strictEqual(expr.right[0].type, want,
      `condition dataType '${dataType}' should serialize as WorkflowAttributeType ${want}, got ${expr.right[0].type}`);

    // 2. the SetFieldValue ACTION path, a DIFFERENT field on a different code path — and the one
    //    that does NOT honour the token. Asserted explicitly so the asymmetry is pinned rather
    //    than assumed away.
    const actWfom = await push(businessRuleDef({
      ...RULE,
      conditions: [{ field: 'new_notes', operator: 'ContainsData' }],
      actions: [{ type: 'SetFieldValue', field: 'new_owner', value: 'x', dataType }],
    }));
    const serialized = JSON.stringify(actWfom.steps.list[0].steps.list[0].steps.list[0]);
    const types = [...serialized.matchAll(/"type":"?([A-Za-z0-9]+)"?/g)].map((m) => m[1]);
    assert.ok(types.length > 0, `action dataType '${dataType}': no type token found on the wire`);
    assert.deepStrictEqual([...new Set(types)], [ACTION_STRING_TOKEN],
      `action dataType '${dataType}' should still be IGNORED (String), got ${JSON.stringify([...new Set(types)])}`);
    assert.ok(!serialized.includes(`"${dataType}"`) || dataType === 'String',
      `action dataType '${dataType}' must NOT reach the wire verbatim — that would make the two paths agree, and this test would need rewriting`);
  }

  // The DEFAULT, which is the case that actually hit every rule. `businessRuleDef` defaults the
  // token to 'String', and before the enum mapping that put the literal "String" on the wire for
  // every business rule this plugin has ever written — not just ones that authored a dataType.
  // '14' is what the PREVIOUS bundle hard-coded, so this pins the restoration of that behaviour.
  const defaultWfom = await push(businessRuleDef({
    ...RULE, conditions: [{ field: 'new_status', operator: 'Equals', value: '1' }],
  }));
  assert.strictEqual(defaultWfom.steps.list[0].steps.list[0].conditionExpression.right[0].type, '14',
    'a condition with NO authored dataType must serialize as String (14), the value the previous bundle hard-coded');

  // An unmapped token must FALL BACK to String rather than be forwarded. The spec gate makes this
  // unreachable from a valid spec, but forwarding would reopen the out-of-domain write, so the
  // fallback is asserted rather than assumed.
  const bogusWfom = await push(businessRuleDef({
    ...RULE, conditions: [{ field: 'new_status', operator: 'Equals', value: '1', dataType: 'NotAWorkflowType' }],
  }));
  const bogusType = bogusWfom.steps.list[0].steps.list[0].conditionExpression.right[0].type;
  assert.strictEqual(bogusType, '14',
    `an unmapped dataType must fall back to String (14), not reach the wire as '${bogusType}'`);

  // The spec-level list being closed is load-bearing: it is what keeps a typo from reaching the
  // mapping at all, and every accepted token must have an enum value to map to.
  assert.ok(Array.isArray(BUSINESS_RULE_DATA_TYPES) && BUSINESS_RULE_DATA_TYPES.length > 0);
  for (const t of BUSINESS_RULE_DATA_TYPES) {
    assert.ok(EXPECTED_ENUM[t], `accepted dataType '${t}' has no WorkflowAttributeType mapping`);
  }
  assert.ok(!BUSINESS_RULE_DATA_TYPES.includes('DateTime'),
    'DateTime stays out: it was never exercised on this path');
});

test('a DateTime dataType is rejected at the spec gate, not mid-build', () => {
  const res = validateAppSpec(specWith([{
    name: 'R', entity: 'new_ticket',
    conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'DateTime' }],
    actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }],
  }]));
  assert.ok(res.errors.some((e) => /dataType/i.test(e) && /DateTime/.test(e)), `expected a dataType error, got ${JSON.stringify(res.errors)}`);
});

// --- verify reconciles business rules (previously: no checks at all) ----------------------------

const { verifySpec } = require('../lib/verify-spec.js');

function ruleSpec(status) {
  const s = specWith([{
    name: 'Lock when closed', entity: 'new_ticket',
    conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' }],
    actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }],
    ...(status ? { status } : {}),
  }]);
  return s;
}
const baseReader = (workflows) => ({
  findTable: async () => ({ logicalName: 'new_ticket' }),
  findColumns: async () => [{ logicalName: 'new_status' }, { logicalName: 'new_notes' }, { logicalName: 'new_owner' }],
  queryRecords: async (entity) => (entity === 'workflow' ? workflows : []),
  sitemapXml: async () => '',
});
const ruleCheck = (r) => r.checks.find((c) => c.kind === 'business-rule');

test('verify PASSES an Active rule the spec wants Active', async () => {
  const r = await verifySpec(ruleSpec(), baseReader([{ workflowid: 'w1', statecode: 1 }]));
  assert.strictEqual(ruleCheck(r).present, true);
});

test('verify FAILS a rule that never deployed', async () => {
  const c = ruleCheck(await verifySpec(ruleSpec(), baseReader([])));
  assert.strictEqual(c.present, false);
  assert.match(c.detail, /not deployed/);
});

test('verify FAILS a rule deployed as Draft — "exists" is not "runs"', async () => {
  const c = ruleCheck(await verifySpec(ruleSpec(), baseReader([{ workflowid: 'w1', statecode: 0 }])));
  assert.strictEqual(c.present, false);
  assert.match(c.detail, /DRAFT/);
});

test('verify FAILS duplicates — the #482 residue the build cannot always remove', async () => {
  const c = ruleCheck(await verifySpec(ruleSpec(), baseReader([{ workflowid: 'w1', statecode: 1 }, { workflowid: 'w2', statecode: 1 }])));
  assert.strictEqual(c.present, false);
  assert.match(c.detail, /2 rules share this name/);
});

test('verify counts a rule the server repeats once, and never reads a read cut short as "deployed once"', async () => {
  const same = { workflowid: 'w1', statecode: 1 };
  assert.strictEqual(ruleCheck(await verifySpec(ruleSpec(), baseReader([same, { ...same }]))).present, true, 'one rule read twice is one rule');
  // The SDK marks an answer that is not every row with a non-enumerable `hasMore`.
  const partial = [same];
  Object.defineProperty(partial, 'hasMore', { value: true, enumerable: false });
  const r = await verifySpec(ruleSpec(), baseReader(partial));
  assert.strictEqual(ruleCheck(r), undefined, 'neither a pass nor a failure');
  assert.match((r.unverified || []).join('\n'), /^business-rule:new_ticket\..* \(the read answered 1 definition row\(s\) and said more exist/m);
});
test('verify FAILS an ACTIVE rule the spec asks to be Draft', async () => {
  const c = ruleCheck(await verifySpec(ruleSpec('Draft'), baseReader([{ workflowid: 'w1', statecode: 1 }])));
  assert.strictEqual(c.present, false);
  assert.match(c.detail, /running/);
});

test('verify fails CLOSED when the workflow read itself errors', async () => {
  // "Could not look" must never read as "present and correct".
  const read = Object.assign(baseReader([]), {
    queryRecords: async (entity) => { if (entity === 'workflow') throw new Error('HTTP 401'); return []; },
  });
  const c = ruleCheck(await verifySpec(ruleSpec(), read));
  assert.strictEqual(c.present, false);
  assert.match(c.detail, /could not be read/);
});

// --- rebuild keeps pre-existing same-name definitions -----------------------------------------
//
// An ordered, complete read makes reuse deterministic and reports additional matches. A name
// does not prove another definition was created by this run, so rebuild must not delete it.
// An apply that names no app folder and no workspace gets an app folder of its own: it holds its build lease in its workspace (helpers/own-app-folder.js).
const { inOwnFolder } = require('./helpers/own-app-folder.js');
const { runSdkBuild: runBuildInPlace } = require('../lib/sdk-build.js');
const runBuild = inOwnFolder(runBuildInPlace);

function ruleOnlySpec() {
  const s = specWith([{
    name: 'Lock when closed', entity: 'new_ticket',
    conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' }],
    actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }],
  }]);
  return s;
}

// Minimal provision double: only the workflow surface the business-rules phase touches.
function provisionWithRules(rows) {
  const calls = [];
  return {
    calls,
    provision: {
      queryRecords: async (entity, opts) => {
        if (entity === 'workflow') { calls.push(['queryRecords', entity, opts]); return rows; }
        return entity === 'solution' ? [] : [{ publisherid: 'pub-1' }];
      },
      updateRecord: async (e, id, patch) => { calls.push(['updateRecord', e, id, patch]); },
      deleteRecord: async (e, id) => { calls.push(['deleteRecord', e, id]); },
      createArtifact: () => ({ id: 'br-new' }),
      updateElement: async () => undefined,
      pushArtifact: async () => ({ id: 'br-new', saved: true, publish: { kind: 'notRequested' } }),
      addSolutionComponent: async () => undefined,
    },
  };
}

// --- the designer's own completeness validator, run BEFORE the push -----------------------------
//
// The trap it closes is pinned in sdk-uptake-contract.test.js: a condition tree in the wrong shape is
// MERGED onto the node, ignored by the serializer, and written as a rule with no clauses and no
// actions — 204, activated, and it never fires. Nothing on the push path detects that, and nothing
// afterwards can either. This SDK exposes the validator the designer gates Save on; the build runs it.

test('a rule the designer calls incomplete HALTS before anything is written', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionWithRules([]);
  const pushes = [];
  // ⚠ The refusal moved. This test used to stub `provision.validateBusinessRule` so the BUILD would
  // run it and halt. That method no longer exists — the SDK validates internally on every save — so
  // the refusal now arrives as a REJECTED `pushArtifact`, and what must still hold is that the build
  // surfaces it and writes nothing.
  provision.pushArtifact = async (...a) => {
    pushes.push(a);
    const err = new Error(
      'Validation failed for businessRule/x:\n  • /rootCondition (root): A business rule must include ' +
        'at least one action step. [business-rule-NO_ACTION]'
    );
    err.code = 'VALIDATION_FAILED';
    throw err;
  };

  const err = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {},
  }).then(() => null, (e) => e);

  assert.ok(err, 'an incomplete rule must not be pushed');
  assert.match(err.message, /NO_ACTION/, "the designer's own finding is the actionable part");
  assert.strictEqual(pushes.length, 1, 'exactly one push was attempted, and it was refused');
  // The rule must not be recorded as created off the back of a refused push.
  assert.doesNotMatch(String(err.message), /^\s*$/, 'the halt carries the reason');
});

// ⚠ REMOVED: 'the validator is best-effort: an older bundle without it, or one that throws, still
// builds'.
//
// Its entire premise is gone. It asserted that the plugin's OPT-IN call to
// `provision.validateBusinessRule` degraded gracefully when the vendored bundle predated the method
// or the validator itself faulted — correct while validation was a best-effort diagnostic the
// PLUGIN chose to run.
//
// Validation is no longer optional or plugin-driven: the SDK runs it internally on every
// business-rule save, so there is no "absent" or "throws" state left to tolerate. A replacement
// test would have to stub the SDK into not validating, which asserts the opposite of the guarantee
// this change bought. The coverage that matters now lives in the two tests above: an incomplete
// rule halts the build, and every shape the spec surface can author is accepted by the real push.

test('REAL BUNDLE: the validator accepts EVERY shape this spec surface can author', async () => {
  // The one way wiring the validator could regress a working build is if it rejected something the
  // App Spec already allows. So the assertion is over the spec surface's OWN declared sets rather
  // than a hand-picked sample — a future operator or action type added to app-spec.js is covered the
  // day it is added, without anyone remembering to extend this test.
  const { BUSINESS_RULE_OPERATORS, BUSINESS_RULE_VALUELESS_OPERATORS, BUSINESS_RULE_ACTION_TYPES, BUSINESS_RULE_SCOPES, BUSINESS_RULE_DATA_TYPES } = require('../lib/app-spec.js');
  const { createMakerSdk, createNodeWorkspaceStorage } = require(BUNDLE);
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'brval-'));
  dirs.push(dir);
  const noop = async () => ({ status: 200, headers: {}, body: { value: [] } });
  const sdk = createMakerSdk({
    workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: 'https://contoso.crm.dynamics.com',
    httpClient: { get: noop, post: noop, patch: noop, put: noop, delete: noop },
  });
  await sdk.initWorkspace();

  // The payload each action type carries — mirrors BUSINESS_RULE_ACTIONS in app-spec.js.
  const ACTION_PAYLOAD = { SetVisibility: { visible: false }, LockUnlock: { lock: true }, SetBusinessRequired: { required: true }, SetFieldValue: { value: 'x', dataType: 'String' } };
  const shapes = [];
  for (const op of BUSINESS_RULE_OPERATORS) {
    const valueless = BUSINESS_RULE_VALUELESS_OPERATORS.has(op);
    shapes.push([`operator ${op}`, { entity: 'new_ticket', name: `Op ${op}`, conditions: [Object.assign({ field: 'new_owner', operator: op }, valueless ? {} : { value: 'x', dataType: 'String' })], actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }] }]);
  }
  for (const type of BUSINESS_RULE_ACTION_TYPES) {
    assert.ok(ACTION_PAYLOAD[type], `this test must be taught the payload for a new action type '${type}'`);
    shapes.push([`action ${type}`, { entity: 'new_ticket', name: `Act ${type}`, conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' }], actions: [Object.assign({ type, field: 'new_notes' }, ACTION_PAYLOAD[type])] }]);
  }
  for (const scope of BUSINESS_RULE_SCOPES) {
    shapes.push([`scope ${scope}`, { entity: 'new_ticket', name: `Sc ${scope}`, scope, conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' }], actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }] }]);
  }
  // `status` and `dataType` are separately-accepted axes, and the build's comment claims BOTH are
  // covered — so iterate them here rather than leave the claim resting on a hand-picked sample. A
  // future validator that started rejecting `Draft`, or one literal type, would otherwise stay green.
  for (const status of ['Active', 'Draft']) {
    shapes.push([`status ${status}`, { entity: 'new_ticket', name: `St ${status}`, status, conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' }], actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }] }]);
  }
  for (const dataType of BUSINESS_RULE_DATA_TYPES) {
    shapes.push([`dataType ${dataType} (condition)`, { entity: 'new_ticket', name: `Dt ${dataType}`, conditions: [{ field: 'new_owner', operator: 'Equals', value: '1', dataType }], actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }] }]);
    shapes.push([`dataType ${dataType} (SetFieldValue)`, { entity: 'new_ticket', name: `Dv ${dataType}`, conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' }], actions: [{ type: 'SetFieldValue', field: 'new_notes', value: '1', dataType }] }]);
  }
  shapes.push(['multi-condition + multi-action', { entity: 'new_ticket', name: 'Multi', conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' }, { field: 'new_notes', operator: 'ContainsData' }], actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }, { type: 'LockUnlock', field: 'new_owner', lock: true }] }]);

  const rejected = [];
  for (const [label, rule] of shapes) {
    const def = businessRuleDef(rule);
    const art = await sdk.createArtifact('businessRule', def);
    await sdk.updateElement('businessRule', art.id, '/rootCondition', def.rootCondition);
    // ⚠ This used to call `sdk.validateBusinessRule(...)` directly. That method is gone: the SDK now
    // runs the designer's validator INTERNALLY on every save, so the equivalent — and stronger —
    // check is that the real push path accepts the shape. A rejection surfaces as a throw.
    try {
      await sdk.pushArtifact('businessRule', art.id);
    } catch (e) {
      rejected.push(`${label}: ${e && e.message}`);
    }
  }
  assert.deepStrictEqual(rejected, [],
    `the push REFUSES on findings, so a rejected authorable shape is a build the spec gate said was fine:\n${rejected.join('\n')}`);
  assert.ok(shapes.length >= 20, `the matrix must be broad enough to mean something; got ${shapes.length}`);

  // Negative control: without this the assertion above is satisfied by a validator that finds
  // nothing at all, and the gate would be inert while looking healthy.
  const empty = await sdk.createArtifact('businessRule', { name: 'Empty', entityLogicalName: 'new_ticket', scope: 'Entity', status: 'Draft' });
  let controlErr = null;
  try {
    await sdk.pushArtifact('businessRule', empty.id);
  } catch (e) {
    controlErr = e;
  }
  assert.ok(controlErr, 'the validator must actually refuse a rule with no clauses and no actions');
  assert.match(String(controlErr.message), /NO_ACTION/, 'and name the missing action step');
});

test('a rebuild KEEPS and reports pre-existing same-name definitions', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionWithRules([
    { workflowid: 'keep-oldest', statecode: 1, createdon: '2026-01-01T00:00:00Z' },
    { workflowid: 'dupe-1', statecode: 1, createdon: '2026-01-01T00:00:05Z' },
  ]);
  const warnings = [];
  await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true,
    phases: ['business-rules'], warn: (m) => warnings.push(m),
  });

  const deletes = calls.filter((c) => c[0] === 'deleteRecord').map((c) => c[2]);
  assert.deepStrictEqual(deletes, []);
  assert.strictEqual(calls.filter((c) => c[0] === 'updateRecord').length, 0);
  assert.ok(warnings.some((w) => /kept pre-existing same-name rule dupe-1/.test(w)), `expected a warning; got ${JSON.stringify(warnings)}`);
});

test('the reuse query is ORDERED and reads every matching definition', async () => {
  // `top: 1` unordered is what made the survivor arbitrary and hid the duplicates entirely.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionWithRules([{ workflowid: 'only', statecode: 1, createdon: '2026-01-01T00:00:00Z' }]);
  await runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'] });
  const q = calls.find((c) => c[0] === 'queryRecords');
  assert.ok(q, 'the reuse query must run');
  assert.strictEqual(q[2].paginate, true);
  assert.strictEqual(q[2].top, undefined, 'the matching set is not truncated');
  assert.match(String(q[2].orderBy), /createdon asc/, 'oldest-first makes the surviving row deterministic');
});

test('a single existing rule is left completely alone', async () => {
  // The repair must not become a delete-happy sweep: one row is the healthy case.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionWithRules([{ workflowid: 'only', statecode: 1, createdon: '2026-01-01T00:00:00Z' }]);
  const warnings = [];
  await runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m) });
  assert.strictEqual(calls.filter((c) => c[0] === 'deleteRecord').length, 0);
  assert.strictEqual(warnings.length, 0);
});

// --- the activated copy is NOT a duplicate (live-measured) --------------------------------------
//
// Activating a business rule makes Dataverse create a SECOND workflows row:
//   type=1, parentworkflowid=(none)  -> the definition
//   type=2, parentworkflowid=<def>   -> the platform's activated copy
// Measured directly: a Draft create yields 1 row; a plain PATCH to statecode 1 yields 2. A PATCH
// cannot create anything, so the platform made it, and the pair is normal for any activated process.
//
// Every business-rule query must therefore filter `type eq 1`. Without it the build tried to delete
// the activated copy (405), warned about a "duplicate" that did not exist, and verify would have
// failed EVERY active rule. The vendored SDK's own orphan probe filters the same way.
const { businessRuleFilter } = require('../lib/sdk-build.js');

test('businessRuleFilter selects the definition only', () => {
  const f = businessRuleFilter("O'Brien rule", 'CFO_WorkOrder');
  assert.match(f, /category eq 2/);
  assert.match(f, /type eq 1/, 'without `type eq 1` the activated copy is counted as a duplicate');
  assert.match(f, /primaryentity eq 'cfo_workorder'/, 'entity is lower-cased for the logical name');
  assert.match(f, /name eq 'O''Brien rule'/, "a quote in the name must be OData-escaped, not left to break the filter");
});

test('every business-rule query constrains workflow type intentionally', () => {
  // A guard on the SOURCE, because the call sites are in different modules and it is the omission
  // — not a wrong value — that caused the defect. Build/verify must select definitions only; teardown
  // is the deliberate exception after #493, because it owns the activated copy before table deletion.
  const fs = require('node:fs');
  const path = require('node:path');
  for (const file of ['sdk-build.js', 'verify-spec.js']) {
    const src = fs.readFileSync(path.resolve(__dirname, '..', 'lib', file), 'utf8');
    const raw = src.match(/category eq 2(?! and type eq 1)/g) || [];
    // The only permitted mention of `category eq 2` without `type eq 1` is inside businessRuleFilter
    // itself, which immediately follows it with the type clause — hence the negative lookahead above
    // matching nothing.
    assert.deepStrictEqual(raw, [], `${file} has a business-rule query that does not constrain type: ${raw.join(' | ')}`);
  }
  const teardown = fs.readFileSync(path.resolve(__dirname, '..', 'lib', 'sdk-teardown.js'), 'utf8');
  assert.match(teardown, /type eq 1 or type eq 2/, 'teardown must include the activated-copy row it is responsible for deleting');
});

test('a rule with an activated copy present is NOT reported as duplicated', async () => {
  // The reader is filtered server-side in production; this pins that verify treats a single
  // definition row as healthy rather than counting anything extra it might receive.
  const spec = specWith([{
    name: 'Lock when closed', entity: 'new_ticket',
    conditions: [{ field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' }],
    actions: [{ type: 'SetVisibility', field: 'new_notes', visible: false }],
  }]);
  const read = {
    findTable: async () => ({ logicalName: 'new_ticket' }),
    findColumns: async () => [{ logicalName: 'new_status' }, { logicalName: 'new_notes' }, { logicalName: 'new_owner' }],
    queryRecords: async (entity, opts) => {
      if (entity !== 'workflow') return [];
      assert.match(String(opts.filter), /type eq 1/, 'verify must ask the server for definitions only');
      return [{ workflowid: 'def-1', statecode: 1 }];
    },
    sitemapXml: async () => '',
  };
  const r = await verifySpec(spec, read);
  const c = r.checks.find((x) => x.kind === 'business-rule');
  assert.strictEqual(c.present, true, `an active rule with its activated copy must PASS; detail: ${c.detail}`);
});

// --- #488 follow-ups -----------------------------------------------------------------------------

test('a multi-condition rule is now ACCEPTED — the single-clause limit was the deleted compiler', () => {
  // This gate existed because `businessRuleXaml.ts` threw
  // "this increment supports a single clause (got N); multi-clause AND/OR is a follow-up", and
  // `bodyXml` only ever read clauses[0]. That compiler has been deleted upstream: the JSON path
  // folds N clauses with LogicalAnd, MEASURED through the real bundle (see the wire test below).
  //
  // Kept as a test rather than deleted, because re-introducing the gate would silently reject specs
  // that now deploy correctly.
  const errs = errorsFor([{
    ...RULE,
    conditions: [
      { field: 'new_status', operator: 'Equals', value: 'Closed', dataType: 'Picklist' },
      { field: 'new_owner', operator: 'Equals', value: 'x', dataType: 'String' },
    ],
  }]);
  assert.deepStrictEqual(errs, [], `a two-clause rule must validate, got ${JSON.stringify(errs)}`);
});

test('one condition still validates', () => {
  assert.deepStrictEqual(errorsFor([RULE]), []);
});

test('an object-valued businessRules is a validation error, not a TypeError', () => {
  // spec-shape normalises collections so a malformed spec fails at the gate with a message naming
  // the field. `businessRules`/`conditions`/`actions` were missing from that map, so an object where
  // an array belongs reached a raw `for...of` and threw.
  const { validateAppSpec: validate } = require('../lib/app-spec.js');
  const spec = specWith([]);
  spec.businessRules = { name: 'not an array' };
  const res = validate(spec); // must not throw
  assert.ok(Array.isArray(res.errors));
  assert.ok(res.errors.length > 0, 'a malformed businessRules must be reported');
});

test('object-valued conditions/actions are reported, not thrown', () => {
  const { validateAppSpec: validate } = require('../lib/app-spec.js');
  for (const field of ['conditions', 'actions']) {
    const spec = specWith([{ ...RULE, [field]: { bad: true } }]);
    const res = validate(spec); // must not throw
    assert.ok(Array.isArray(res.errors) && res.errors.length > 0, `${field} must be reported`);
  }
});

// --- an environment that cannot host business rules at all --------------------------------------
//
// The vendored SDK writes rules ONLY through the bound `CreateProcessWithWfomJson` member; the
// client-side XAML fallback it used to compile was deleted upstream because it covered 4 of the 7
// action types and one clause, so it silently narrowed a rule into something that did not say what
// the author wrote.
//
// The consequence is environment-visible. MEASURED live on a real environment: `$metadata` does not
// declare the member and every POST to it answers 404, and the SDK's own message reports 18 of 20
// environments in the same state. So this is the COMMON case, not an edge case.
//
// The build degrades the way `app.newLook` already does for its tenant-gated setting: skip, say so
// once and specifically, and let the rest of the app build. Halting would abandon a run that has
// already created everything else.
function provisionThatCannotAuthorRules(rows = []) {
  const calls = [];
  const err = Object.assign(new Error(
    "This environment does not expose 'Microsoft.Dynamics.CRM.CreateProcessWithWfomJson', so business rules cannot be authored here."),
  { code: 'BUSINESS_RULE_API_UNAVAILABLE' });
  return {
    calls,
    provision: {
      queryRecords: async (entity, opts) => {
        if (entity === 'workflow') { calls.push(['queryRecords', entity, opts]); return rows; }
        return entity === 'solution' ? [] : [{ publisherid: 'pub-1' }];
      },
      updateRecord: async (e, id, patch) => { calls.push(['updateRecord', e, id, patch]); },
      deleteRecord: async (e, id) => { calls.push(['deleteRecord', e, id]); },
      createArtifact: () => ({ id: 'br-new' }),
      updateElement: async () => undefined,
      pushArtifact: async () => { calls.push(['pushArtifact']); throw err; },
      addSolutionComponent: async () => { calls.push(['addSolutionComponent']); },
    },
  };
}

test('an environment without the bound member SKIPS the rule instead of halting the build', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionThatCannotAuthorRules();
  const warnings = [];
  const events = [];

  // The whole point: this resolves. Before the fix it rejected with a BuildHalt, and every phase
  // after business-rules never ran.
  const res = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true,
    phases: ['business-rules'], warn: (m) => warnings.push(m), emit: (e) => events.push(e),
  });
  assert.strictEqual(res.ok, true, 'the build must complete');

  // Nothing was written in the rule's place.
  assert.strictEqual(calls.some((c) => c[0] === 'addSolutionComponent'), false,
    'a rule that was never created must not be added to the solution');

  // The operator is told exactly what happened and what to do, ONCE.
  assert.strictEqual(warnings.length, 1, `expected exactly one warning, got ${JSON.stringify(warnings)}`);
  assert.match(warnings[0], /business rules were NOT created/);
  assert.match(warnings[0], /CreateProcessWithWfomJson/, 'name the member so it can be searched for');
  assert.match(warnings[0], /Everything else in the app was built normally/);

  // The skip is visible in the event stream, and labelled for what it IS — not "(exists)".
  const skip = events.find((e) => e.phase === 'business-rules' && e.status === 'skip');
  assert.ok(skip, `expected a skip event; got ${JSON.stringify(events.map((e) => [e.phase, e.status, e.label]))}`);
  assert.match(skip.label, /unsupported in this environment/);
  assert.doesNotMatch(skip.label, /exists/, 'labelling this "(exists)" states the opposite of the truth');

  // And it is recorded on the result, so --verify and the summary can report it rather than
  // implying the rules are there. Keyed off the spec so a rename of the fixture cannot make this
  // assertion vacuous.
  const only = ruleOnlySpec().businessRules[0];
  assert.deepStrictEqual(res.skipped.businessRules, [`${only.entity.toLowerCase()}|${only.name}`]);
  assert.strictEqual(Object.keys(res.created.businessRules).length, 0);
});

test('the unsupported-environment warning is emitted ONCE, not once per rule', async () => {
  // On such an environment EVERY rule skips, so a per-rule paragraph would bury the rest of the
  // build output under copies of the same text.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionThatCannotAuthorRules();
  const warnings = [];
  const spec = ruleOnlySpec();
  spec.businessRules = [
    { ...RULE, name: 'Rule A' },
    { ...RULE, name: 'Rule B' },
    { ...RULE, name: 'Rule C' },
  ];
  const res = await runBuild(spec, {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m),
  });
  assert.strictEqual(res.ok, true);
  assert.strictEqual(warnings.length, 1, `expected one warning for three rules, got ${warnings.length}`);
  assert.strictEqual(res.skipped.businessRules.length, 3, 'but every skipped rule is still recorded');
});

test('any OTHER business-rule failure still HALTS — the skip is not a blanket catch', async () => {
  // The dangerous over-correction: swallowing every push failure would turn a real breakage (403,
  // 429, a malformed rule) into a silently ruleless app that reports success.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  for (const code of ['CONNECTION_ERROR', 'VALIDATION_ERROR', undefined]) {
    const { provision } = provisionThatCannotAuthorRules();
    provision.pushArtifact = async () => { throw Object.assign(new Error('boom'), code ? { code } : {}); };
    await assert.rejects(
      () => runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {} }),
      `code ${String(code)} must still halt the build`);
  }
});

// --- the SDK moved this signal from a THROW to a RETURN (SDK uptake) -----------------------------
//
// The bundle used to throw `BUSINESS_RULE_API_UNAVAILABLE` out of `pushArtifact`. It now treats the
// preview rollout as a reported no-op and RESOLVES with `{ saved: false, error }` instead — measured
// against the vendored bundle, a 404 on the bound member resolves while 400/401/403/429/500 still
// throw.
//
// That is invisible to a mock that keeps throwing, which is exactly why the whole plugin suite
// stayed green through the uptake while the real bundle would have HALTED every build on an
// environment without the member — i.e. most of them. `requireSuccessfulPush` turns any by-value
// failure into a BuildHalt, so the error the skip predicate sees is the halt, not the SdkError.
//
// These two tests pin the RESULT-shaped contract so it cannot regress back.
function byValuePushFailure(code, message) {
  const { provision, calls } = provisionThatCannotAuthorRules();
  provision.pushArtifact = async () => {
    calls.push(['pushArtifact']);
    // The exact shape the bundle returns: a PushResult carrying the SdkError, not a throw.
    return { type: 'businessRule', id: 'br-new', saved: false, shipped: false, publish: { kind: 'notRequested' }, error: Object.assign(new Error(message), { code }) };
  };
  return { provision, calls };
}

test('an unavailable bound member reported BY VALUE still skips, and does not halt the build', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = byValuePushFailure('BUSINESS_RULE_API_UNAVAILABLE',
    "Business-rule authoring (preview) is not enabled on this environment yet: it does not expose 'Microsoft.Dynamics.CRM.CreateProcessWithWfomJson'.");
  const warnings = [];
  const events = [];
  const res = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true,
    phases: ['business-rules'], warn: (m) => warnings.push(m), emit: (e) => events.push(e),
  });

  assert.strictEqual(res.ok, true, 'the build must complete, exactly as it does for the thrown form');
  const only = ruleOnlySpec().businessRules[0];
  assert.deepStrictEqual(res.skipped.businessRules, [`${only.entity.toLowerCase()}|${only.name}`]);
  assert.strictEqual(Object.keys(res.created.businessRules).length, 0);
  assert.strictEqual(calls.some((c) => c[0] === 'addSolutionComponent'), false,
    'a rule that was never created must not be added to the solution');

  assert.strictEqual(warnings.length, 1, `expected exactly one warning, got ${JSON.stringify(warnings)}`);
  assert.match(warnings[0], /business rules were NOT created/);
  const skip = events.find((e) => e.phase === 'business-rules' && e.status === 'skip');
  assert.ok(skip, `expected a skip event; got ${JSON.stringify(events.map((e) => [e.phase, e.status, e.label]))}`);
  assert.match(skip.label, /unsupported in this environment/);
});

test('BUSINESS_RULE_LEFT_DEACTIVATED is NOT swallowed by the preview skip — it halts and says why', async () => {
  // The SDK raises this DISTINCT code when a write failed AND it could not put the rule back into
  // the Activated state it found it in, so a live rule is sitting Draft on the server. Collapsing it
  // into the preview no-op would tell the operator "not enabled here" while their rule is disabled —
  // the SDK's own comment says it must not collapse, so the plugin must not collapse it either.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = byValuePushFailure('BUSINESS_RULE_LEFT_DEACTIVATED',
    "The business-rule write failed AND the rule could not be re-activated, so 'br-9' is left DEACTIVATED (Draft) on the server.");
  const err = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {},
  }).then(() => null, (e) => e);

  assert.ok(err, 'a rule left deactivated must not be reported as a benign skip');
  assert.match(err.message, /DEACTIVATED/,
    `the halt must carry the SDK's own diagnosis; got: ${err && err.message}`);
  assert.doesNotMatch(err.message, /changed in Maker since it was fetched/,
    'reporting this as a concurrent Maker edit sends the operator to re-download over an unrelated cause');
});

test('a LEFT_DEACTIVATED that WRAPS the preview failure still halts — broad matching must not swallow it', async () => {
  // This is the shape the exclusion actually defends, and it exists BECAUSE the predicate matches on
  // the whole cause chain rather than the top-level code.
  //
  // The two conditions are causally linked in the SDK: the rule is deactivated, the write fails, and
  // the re-activation fails — so the write failure is the natural `cause` of the LEFT_DEACTIVATED
  // error, and the SDK already embeds its text in the message. If that write failure is the preview
  // gate, a chain walk sees BOTH codes. Taking the first match it recognises would report "this
  // environment cannot host business rules" for a rule that is sitting DISABLED on a server that
  // plainly can — the operator is told to stop worrying about the exact thing they must go fix.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const preview = Object.assign(new Error("does not expose 'Microsoft.Dynamics.CRM.CreateProcessWithWfomJson'"), { code: 'BUSINESS_RULE_API_UNAVAILABLE' });
  const { provision } = byValuePushFailure('BUSINESS_RULE_LEFT_DEACTIVATED',
    "The business-rule write failed AND the rule could not be re-activated, so 'br-9' is left DEACTIVATED (Draft) on the server.");
  const inner = provision.pushArtifact;
  provision.pushArtifact = async (...args) => {
    const res = await inner(...args);
    res.error.cause = preview;   // the linkage the SDK's own message already describes
    return res;
  };

  const events = [];
  const err = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {}, emit: (e) => events.push(e),
  }).then(() => null, (e) => e);

  assert.ok(err, 'the deactivated rule outranks the preview code anywhere else in the chain');
  assert.match(err.message, /DEACTIVATED/);
  const skip = events.find((e) => e.phase === 'business-rules' && e.status === 'skip');
  assert.strictEqual(skip, undefined,
    `this must not be recorded as a skip; got ${JSON.stringify(skip && skip.label)}`);
});

// --- the operator table, and why it must mirror the SDK exactly ---------------------------------

test('REAL BUNDLE: every spec operator EXISTS in the SDK table, and none silently becomes Equals', () => {
  // The sharp edge. The serializer resolves an operator with
  //   `Uf[operator] ?? WorkflowConditionOperator.Equal`
  // so an operator the SDK does not know is not rejected — it becomes EQUALS. A spec written with
  // `GreaterThan` (which is NOT the table's spelling; it is `IsGreaterThan`) would deploy, activate,
  // and quietly test equality instead.
  //
  // Pinned against the bundle's own table so a re-vendor that renames or drops an operator fails
  // here rather than in production.
  //
  // The module alias in front of `WorkflowConditionOperator` is MINIFIER-ASSIGNED and changes on
  // every re-vendor — it was `z` when this was written and became `G` on the next bundle, which made
  // this test fail with "got 0" for a table that was fully intact. Match the alias as a wildcard, not
  // a literal: hardcoding a minified identifier is the same rebuild-unstable-string trap the build
  // engine documents for `err.name`, and here it costs a false failure rather than a silent pass.
  const bundle = fs.readFileSync(BUNDLE, 'utf8');
  const m = bundle.match(/\b[A-Za-z_$][\w$]*=\{Equals:[^}]*\}/);
  assert.ok(m, 'the operator table must be present in the vendored bundle');
  const sdkOperators = [...m[0].matchAll(/(\w+):\s*[A-Za-z_$][\w$]*\.WorkflowConditionOperator\./g)].map((x) => x[1]);
  assert.ok(sdkOperators.length >= 16, `expected the full table, got ${sdkOperators.length}: ${sdkOperators}`);

  const { BUSINESS_RULE_OPERATORS } = require('../lib/app-spec.js');
  const notInSdk = BUSINESS_RULE_OPERATORS.filter((o) => !sdkOperators.includes(o));
  assert.deepStrictEqual(notInSdk, [],
    `these spec operators are NOT in the SDK table, so they would silently deploy as Equals: ${notInSdk}`);

  // And the reverse, so an operator the SDK gains is noticed rather than quietly unavailable.
  const notInSpec = sdkOperators.filter((o) => !BUSINESS_RULE_OPERATORS.includes(o));
  assert.deepStrictEqual(notInSpec, [],
    `the SDK supports these operators the spec does not expose: ${notInSpec}`);
});

test('a near-miss operator spelling is rejected AND corrected', () => {
  // `GreaterThan` is the natural thing to write and is the exact input that would deploy as Equals.
  const errs = errorsFor([{ ...RULE, conditions: [{ field: 'new_status', operator: 'GreaterThan', value: '1' }] }]);
  assert.ok(errs.some((e) => /GreaterThan/.test(e)), `expected rejection: ${JSON.stringify(errs)}`);
  assert.ok(errs.some((e) => /did you mean 'IsGreaterThan'/.test(e)),
    `the message must name the legal spelling; got ${JSON.stringify(errs)}`);
});

test('the newly-unlocked operators all validate', () => {
  // These were blocked only because the deleted XAML compiler could not express them.
  for (const operator of ['IsGreaterThan', 'IsLessThanEqualTo', 'Contains', 'BeginsWith', 'DoesNotEndWith', 'On']) {
    const errs = errorsFor([{ ...RULE, conditions: [{ field: 'new_status', operator, value: '1' }] }]);
    assert.deepStrictEqual(errs, [], `${operator} must validate: ${JSON.stringify(errs)}`);
  }
});

// --- multi-clause AND ---------------------------------------------------------------------------

test('a multi-condition rule now validates', () => {
  // Previously rejected with "only ONE condition is supported", which was never a platform limit —
  // the deleted XAML compiler read `clauses[0]` and ignored the rest.
  const errs = errorsFor([{
    ...RULE,
    conditions: [
      { field: 'new_status', operator: 'Equals', value: '1' },
      { field: 'new_owner', operator: 'ContainsData' },
    ],
  }]);
  assert.deepStrictEqual(errs, [], JSON.stringify(errs));
});

test('REAL BUNDLE: BOTH conditions of a multi-clause rule reach the wire, joined by LogicalAnd', async () => {
  // The failure that matters is silent: a serializer that kept only the first clause would deploy a
  // rule that fires under half the intended circumstances, and every structural check would pass.
  //
  // WorkflowConditionOperator.LogicalAnd is "2" in the bundle's own enum.
  const { createMakerSdk, createNodeWorkspaceStorage } = require(BUNDLE);
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'br-multi-'));
  dirs.push(dir);
  const calls = [];
  const sdk = createMakerSdk({
    workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: 'https://contoso.crm.dynamics.com',
    httpClient: {
      get: async () => ({ status: 200, headers: {}, body: { value: [] } }),
      post: async (url, body) => { calls.push({ url, body }); return { status: 204, headers: { 'odata-entityid': 'https://x/workflows(55555555-5555-5555-5555-555555555555)' }, body: {} }; },
      patch: async () => ({ status: 204, headers: {}, body: {} }),
      put: async () => ({ status: 204, headers: {}, body: {} }),
      delete: async () => ({ status: 204, headers: {}, body: {} }),
    },
  });
  await sdk.initWorkspace();

  const def = businessRuleDef({
    ...RULE,
    conditions: [
      { field: 'new_status', operator: 'Equals', value: '1' },
      { field: 'new_owner', operator: 'ContainsData' },
    ],
  });
  assert.strictEqual(def.rootCondition.clauses.length, 2, 'the mapper must carry both clauses');

  const art = await sdk.createArtifact('businessRule', def);
  await sdk.updateElement('businessRule', art.id, '/rootCondition', def.rootCondition);
  await sdk.pushArtifact('businessRule', art.id);

  const bound = calls.find((c) => /CreateProcessWithWfomJson/i.test(String(c.url)));
  const expr = JSON.parse(bound.body.WfomJson).steps.list[0].steps.list[0].conditionExpression;
  assert.strictEqual(expr.conditionOperatoroperator, '2', 'two clauses must be folded with LogicalAnd');

  // Both operands, and both authored columns, must survive the fold.
  const flat = JSON.stringify(expr);
  assert.match(flat, /new_status/, 'the FIRST condition column must reach the wire');
  assert.match(flat, /new_owner/, 'the SECOND condition column must reach the wire — dropping it is the silent failure');
});

// --- verify must not punish an environment that cannot host business rules -----------------------
//
// Found in review, and the point is not tidiness. `verify.ok` gates THREE things: the process exit
// code, whether `.last-applied.json` is written, and whether the `--changed-only` snapshot is
// persisted. A spec with implemented pages makes verify MANDATORY. So on the 18-of-20 environments
// that cannot host business rules, a permanently-false `ok` would permanently withhold the
// changed-only baseline and force a full build on every subsequent run, forever — while the build
// itself reported success. Those gates are built for TRANSIENT failures; this one never clears.

test('verify SKIPS a rule the build reported as unsupported on this environment', async () => {
  const r = await verifySpec(ruleSpec(), baseReader([]), {
    environmentSkipped: { businessRules: ['new_ticket|Lock when closed'] },
  });
  // Asserted on the business-rule outcome specifically rather than on `r.ok`: this fixture's reader
  // also reports an unrelated missing subarea, so `ok` would be false either way and an `ok` check
  // would pass for the wrong reason.
  assert.strictEqual(ruleCheck(r), undefined,
    'the rule must not be counted as a check at all — passing it would claim it exists');
  assert.strictEqual(r.missing.some((m) => m.kind === 'business-rule'), false,
    'and it must not count toward `missing`, which is what drives verify.ok');
  assert.deepStrictEqual(r.environmentSkipped, ['business-rule:new_ticket.Lock when closed'],
    'it is REPORTED, so a green verify never silently means "everything the spec asked for is deployed"');
});

test('verify still FAILS a missing rule when the build did NOT skip it', async () => {
  // The dangerous over-correction: treating every absent rule as environment-gated would turn a real
  // deployment failure into a pass.
  const r = await verifySpec(ruleSpec(), baseReader([]), { environmentSkipped: { businessRules: [] } });
  assert.strictEqual(ruleCheck(r).present, false);
  assert.ok(r.missing.some((m) => m.kind === 'business-rule'));
  assert.strictEqual(r.environmentSkipped, undefined, 'absent when nothing was skipped, so existing callers are unaffected');
});

test('verify run STANDALONE (no build result) checks every rule', async () => {
  // `verify-model-app.js` has no build result to consult. Absent one, "not deployed" is the honest
  // verdict — the alternative would be a verify that quietly stops checking business rules.
  const r = await verifySpec(ruleSpec(), baseReader([]));
  assert.strictEqual(ruleCheck(r).present, false);
  assert.ok(r.missing.some((m) => m.kind === 'business-rule'));
});

test('the skip key matches the shape the BUILD records', async () => {
  // The two sides must agree on `entity|name` or the wiring is silently inert: verify would keep
  // failing and nobody would notice the option is being ignored.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionThatCannotAuthorRules();
  const built = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {},
  });
  const r = await verifySpec(ruleSpec(), baseReader([]), { environmentSkipped: built.skipped });
  assert.strictEqual(ruleCheck(r), undefined,
    `the build recorded ${JSON.stringify(built.skipped.businessRules)}, which verify must recognise`);
});

test('a PHASE-LIMITED verify does not demand a rule whose phase never ran', async () => {
  // The `--changed-only` FAST path applies `phases: ['pages']`. The business-rules loop is gated on
  // `has('business-rules')`, so it never executes and `skipped.businessRules` comes back EMPTY —
  // which made the skip list useless on exactly the runs that need it most.
  //
  // The consequence was not "verify is noisy". A fast apply is only chosen when every change is a
  // page, which requires implemented .tsx pages, which makes verify MANDATORY. So on a gated
  // environment every fast apply failed verify, refused to re-bless the snapshot, and exited
  // non-zero — an alternating full / failing-fast cycle forever, with the log line blaming PAGES for
  // a business-rule gate. The page upload itself had succeeded.
  const r = await verifySpec(ruleSpec(), baseReader([]), { phases: ['pages'] });
  assert.strictEqual(ruleCheck(r), undefined, 'a phase that did not run must not be demanded');
  assert.strictEqual(r.missing.some((m) => m.kind === 'business-rule'), false);
  assert.deepStrictEqual(r.phaseSkipped, ['business-rule:new_ticket.Lock when closed'],
    'still reported, so it is visible rather than silently dropped');
});

test('the two skip REASONS are reported separately — they warrant opposite messages', async () => {
  // Merging them was wrong in a way the live run could not surface, because it happened to read
  // correctly on a gated environment. On a HEALTHY one, every `--changed-only` fast apply runs
  // `phases: ['pages']`, so every business rule would be reported as "not applicable on this
  // environment" — telling an operator whose environment works perfectly well that it does not.
  // Wrong 100% of the time on the normal fast-apply path.
  const gated = await verifySpec(ruleSpec(), baseReader([]), {
    environmentSkipped: { businessRules: ['new_ticket|Lock when closed'] },
  });
  assert.deepStrictEqual(gated.environmentSkipped, ['business-rule:new_ticket.Lock when closed']);
  assert.strictEqual(gated.phaseSkipped, undefined, 'a gated environment is NOT a phase that did not run');

  const phased = await verifySpec(ruleSpec(), baseReader([]), { phases: ['pages'] });
  assert.deepStrictEqual(phased.phaseSkipped, ['business-rule:new_ticket.Lock when closed']);
  assert.strictEqual(phased.environmentSkipped, undefined, 'a skipped phase says NOTHING about the environment');
});

test('a FULL-phase verify still demands the rule', async () => {
  // The over-correction to avoid: treating every phase list as permission to stop checking. A full
  // build lists every phase, so nothing is relaxed there.
  const { PHASES } = require('../lib/stages.js');
  const r = await verifySpec(ruleSpec(), baseReader([]), { phases: PHASES });
  assert.strictEqual(ruleCheck(r).present, false);
  assert.ok(r.missing.some((m) => m.kind === 'business-rule'));
});

test('verify with NO phases supplied checks everything — standalone must not be relaxed', async () => {
  const r = await verifySpec(ruleSpec(), baseReader([]));
  assert.strictEqual(ruleCheck(r).present, false);
});

test('a phase list that INCLUDES business-rules still demands the rule', async () => {
  // The gate is "did this phase run", not "was a phase list supplied".
  const r = await verifySpec(ruleSpec(), baseReader([]), { phases: ['business-rules'] });
  assert.strictEqual(ruleCheck(r).present, false);
  assert.ok(r.missing.some((m) => m.kind === 'business-rule'));
});

// --- a REUSED business rule must still join the solution ----------------------------------------
//
// `addSolutionComponent` used to be reached only by the create branch, so a rule that already
// existed was recorded and skipped. Two ordinary situations then left it permanently outside the
// solution, with nothing in the output saying so: a run where the rule was written but the
// component add failed (the retry reuses and reports success), and a rule created by an earlier
// build of a DIFFERENT solution. Neither is visible afterwards — the rule works, it just never
// travels on export/import.
test('a reused business rule is re-added to the solution on every run', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionWithRules([{ workflowid: 'existing-1', statecode: 1, createdon: '2026-01-01T00:00:00Z' }]);
  const adds = [];
  provision.addSolutionComponent = async (a) => { adds.push(a); };
  const res = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {},
  });
  assert.strictEqual(res.ok, true);
  assert.strictEqual(adds.length, 1, `the reused rule must be re-added; got ${JSON.stringify(adds)}`);
  assert.strictEqual(adds[0].componentId, 'existing-1', 'and it must be the EXISTING id, not a new one');
  assert.strictEqual(calls.some((c) => c[0] === 'deleteRecord'), false, 'reuse must not delete anything');
});

test('a failed solution add on the reuse path WARNS — it never fails an otherwise-good build', async () => {
  // The rule itself is correct and running; blocking the build over solution bookkeeping is worse
  // than reporting it. Mirrors the business-process-flows policy exactly.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionWithRules([{ workflowid: 'existing-1', statecode: 1, createdon: '2026-01-01T00:00:00Z' }]);
  provision.addSolutionComponent = async () => { throw new Error('component add refused'); };
  const warnings = [];
  const res = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m),
  });
  assert.strictEqual(res.ok, true, 'the build must still complete');
  assert.ok(warnings.some((w) => /could not be added to solution/.test(w)),
    `the operator must be told; got ${JSON.stringify(warnings)}`);
});

// --- an environment whose bound member exists but is switched off ------------------------------------
//
// Measured on production-geo environments: the platform declares `CreateProcessWithWfomJson` and answers EVERY create, whatever the
// rule says (garbage content included), with
//   HTTP 400  {"error":{"code":"0x80040216","message":"An unexpected error occurred."}}
// and nothing is created. The member-is-missing gate cannot see it (the member IS declared), so a build used to halt at this phase and
// leave an app built only up to it. The SDK throws that answer as
//   Error 'HTTP 400 from <env>/api/data/v9.0/workflows(<null guid>)/Microsoft.Dynamics.CRM.CreateProcessWithWfomJson: <message>'
//   { code: 'CONNECTION_ERROR', statusCode: 400, url, cause: <the response body> }
// and nothing in it names the gate. The FIRST create of a build answering exactly this is read as "not available here"; the same answer
// after a rule of this build was created, or any other failure, is not.
const MEMBER_URL = 'https://contoso.crm.dynamics.com/api/data/v9.0/workflows(00000000-0000-0000-0000-000000000000)/Microsoft.Dynamics.CRM.CreateProcessWithWfomJson';
const { HTTP_SEND_HISTORY } = require('../lib/sdk-http-client.js');
// `history` is what the plugin's HTTP client says of the sends that led to the answer (lib/sdk-http-client.js): the one send of a first attempt by default;
// null is an answer from a transport that says nothing.
function memberRefusal({ status = 400, code = '0x80040216', url = MEMBER_URL, message = 'An unexpected error occurred.', history = { sends: 1, earlierAmbiguous: false } } = {}) {
  const body = { error: { code, message } };
  if (history) Object.defineProperty(body, HTTP_SEND_HISTORY, { value: history, enumerable: false });
  return Object.assign(new Error(`HTTP ${status} from ${url}: ${message}`),
    { code: 'CONNECTION_ERROR', statusCode: status, url, cause: body });
}

// `answers[i]` is what the i-th push does: an Error is thrown, `'ok'` saves, anything else is returned as the push result.
function provisionPushing(answers) {
  const { provision, calls } = provisionThatCannotAuthorRules();
  let pushes = 0;
  provision.pushArtifact = async () => {
    const answer = answers[Math.min(pushes, answers.length - 1)];
    pushes += 1;
    calls.push(['pushArtifact']);
    if (answer instanceof Error) throw answer;
    if (answer === 'ok') return { id: `br-${pushes}`, saved: true, publish: { kind: 'notRequested' } };
    return answer;
  };
  return { provision, calls };
}
const threeRules = () => {
  const spec = ruleOnlySpec();
  spec.businessRules = [{ ...RULE, name: 'Rule A' }, { ...RULE, name: 'Rule B' }, { ...RULE, name: 'Rule C' }];
  return spec;
};

test('the first rule refused with HTTP 400 0x80040216 on the bound member SKIPS every rule and the build goes on', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionPushing([memberRefusal()]);
  const warnings = [];
  const events = [];
  const spec = threeRules();
  const res = await runBuild(spec, {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m), emit: (e) => events.push(e),
  });
  assert.strictEqual(res.ok, true, 'the build must complete, exactly as for a missing member');

  // The refused create is the probe: the remaining rules are not sent.
  assert.strictEqual(calls.filter((c) => c[0] === 'pushArtifact').length, 1, 'only the first rule is sent');
  assert.strictEqual(calls.some((c) => c[0] === 'addSolutionComponent'), false);

  assert.strictEqual(warnings.length, 1, `exactly one warning for three rules; got ${JSON.stringify(warnings)}`);
  assert.match(warnings[0], /business rules were NOT created/);
  assert.match(warnings[0], /0x80040216/, 'name the code so it can be searched for');
  assert.match(warnings[0], /platform limit/);
  assert.match(warnings[0], /production environments/);
  assert.match(warnings[0], /Everything else in the app was built normally/);

  const skips = events.filter((e) => e.phase === 'business-rules' && e.status === 'skip');
  assert.strictEqual(skips.length, 3, `every rule is a skip; got ${JSON.stringify(events.map((e) => [e.status, e.label]))}`);
  for (const s of skips) assert.match(s.label, /unsupported in this environment/);
  assert.strictEqual(events.some((e) => e.phase === 'business-rules' && e.status === 'error'), false);

  assert.deepStrictEqual(res.skipped.businessRules, spec.businessRules.map((r) => `${r.entity.toLowerCase()}|${r.name}`));
  assert.strictEqual(Object.keys(res.created.businessRules).length, 0);
});

test('the same refusal reported BY VALUE (a saved:false result) skips too', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionPushing([
    { type: 'businessRule', id: 'br-new', saved: false, shipped: false, publish: { kind: 'notRequested' }, error: memberRefusal() },
  ]);
  const warnings = [];
  const res = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m),
  });
  assert.strictEqual(res.ok, true);
  assert.strictEqual(res.skipped.businessRules.length, 1);
  assert.strictEqual(warnings.length, 1);
});

test('REAL BUNDLE: the refusal the bundle throws for HTTP 400 0x80040216 skips the rules, and verify treats them as the missing-member gate does', async () => {
  const { createMakerSdk, createNodeWorkspaceStorage } = require(BUNDLE);
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'br-gate-'));
  dirs.push(dir);
  const memberPosts = [];
  // The plugin's own HTTP client over a fake transport: it is the one that tells the SDK's error what the sends before the answer were.
  const { createAzHttpClient } = require('../lib/sdk-http-client.js');
  const httpClient = createAzHttpClient('https://contoso.crm.dynamics.com', {
    getToken: () => 'TOK',
    sleep: async () => {},
    request: async ({ method, url }) => {
      if (method === 'POST' && /CreateProcessWithWfomJson/.test(String(url))) {
        memberPosts.push(String(url));
        return { statusCode: 400, headers: {}, body: JSON.stringify({ error: { code: '0x80040216', message: 'An unexpected error occurred.' } }) };
      }
      return { statusCode: method === 'GET' ? 200 : 204, headers: {}, body: JSON.stringify({ value: [] }) };
    },
  });
  const real = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: 'https://contoso.crm.dynamics.com', httpClient });
  await real.initWorkspace();
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  // The reads (the rows already there) are the double's; what is under test is the real create, update and push of a rule.
  const { provision } = provisionWithRules([]);
  for (const method of ['createArtifact', 'updateElement', 'pushArtifact']) provision[method] = (...args) => real[method](...args);
  const warnings = [];
  const res = await runBuild(threeRules(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m),
  });
  assert.strictEqual(res.ok, true);
  assert.strictEqual(memberPosts.length, 1, 'the member is asked once; the other two rules are not sent');
  assert.strictEqual(res.skipped.businessRules.length, 3);
  assert.strictEqual(warnings.length, 1);
  assert.match(warnings[0], /0x80040216/);

  const r = await verifySpec(ruleSpec(), baseReader([]), { environmentSkipped: { businessRules: [`new_ticket|Lock when closed`] } });
  assert.strictEqual(ruleCheck(r), undefined, 'a skipped rule is not demanded by verify');
  assert.deepStrictEqual(r.environmentSkipped, ['business-rule:new_ticket.Lock when closed']);
});

test('a refusal that is NOT this one still HALTS: another code, another status, another member', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const refusals = [
    memberRefusal({ code: '0x80040265' }),
    memberRefusal({ status: 500 }),
    memberRefusal({ status: 403, code: '0x80040220' }),
    memberRefusal({ url: 'https://contoso.crm.dynamics.com/api/data/v9.0/workflows' }),
    Object.assign(new Error('HTTP 400 from the bound member'), { code: 'CONNECTION_ERROR', statusCode: 400, url: MEMBER_URL }),
  ];
  for (const refusal of refusals) {
    const { provision } = provisionPushing([refusal]);
    const warnings = [];
    await assert.rejects(
      () => runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m) }),
      `${refusal.message} (${refusal.statusCode}) must still halt the build`);
    assert.strictEqual(warnings.length, 0, 'a halt is not announced as an environment limit');
  }
});

test('the same refusal AFTER a rule of this build was created still HALTS', async () => {
  // The environment hosted the first rule, so the refusal of the second is not "rules are switched off here".
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionPushing(['ok', memberRefusal()]);
  const warnings = [];
  const events = [];
  const err = await runBuild(threeRules(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m), emit: (e) => events.push(e),
  }).then(() => null, (e) => e);
  assert.ok(err, 'the build halts');
  assert.match(err.message, /business-rules failed/);
  assert.strictEqual(calls.filter((c) => c[0] === 'pushArtifact').length, 2, 'the third rule is not tried');
  assert.strictEqual(warnings.length, 0);
  assert.strictEqual(events.some((e) => e.phase === 'business-rules' && e.status === 'skip'), false);
});

test('a rule that already exists is reused, not counted as a create: a refusal of the next rule is still the first create of the build', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionPushing([memberRefusal()]);
  const queryRecords = provision.queryRecords;
  provision.queryRecords = async (entity, opts) => (entity === 'workflow' && /Rule A/.test(JSON.stringify(opts.filter))
    ? [{ workflowid: 'existing-a', statecode: 1, createdon: '2026-01-01T00:00:00Z' }]
    : queryRecords(entity, opts));
  const res = await runBuild(threeRules(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {},
  });
  assert.strictEqual(res.ok, true);
  assert.strictEqual(res.created.businessRules['new_ticket|Rule A'], 'existing-a');
  assert.deepStrictEqual(res.skipped.businessRules, ['new_ticket|Rule B', 'new_ticket|Rule C']);
});

// --- the same 400 from a create the platform SAVED and then failed to finish (#482) ------------------------------------
//
// The workflow row commits, then generating its UiData faults, and the member answers the very same HTTP 400 0x80040216 (measured as live,
// firing rows; pinned in sdk-uptake-contract.test.js). So the answer alone never proves nothing was written: only a fresh, complete read of
// the rule's definitions that finds none the before-create snapshot lacked lets the build call rules unavailable here.
function provisionRefusedThenRead({ before = async () => [], readAfter }) {
  const { provision, calls } = provisionPushing([memberRefusal()]);
  let pushed = false;
  const push = provision.pushArtifact;
  provision.pushArtifact = async (...args) => { pushed = true; return push(...args); };
  provision.queryRecords = async (entity, opts) => {
    if (entity !== 'workflow') return entity === 'solution' ? [] : [{ publisherid: 'pub-1' }];
    calls.push([pushed ? 'readAfter' : 'readBefore', opts.select]);
    return pushed ? readAfter() : before(opts);
  };
  return { provision, calls };
}

test('the switched-off answer to a create the platform SAVED anyway is not the gate: the build halts and names the new rule row', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionRefusedThenRead({ readAfter: async () => [{ workflowid: 'saved-row-1' }] });
  const warnings = [];
  const events = [];
  const err = await runBuild(threeRules(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m), emit: (e) => events.push(e),
  }).then(() => null, (e) => e);
  assert.ok(err, 'the build halts, as it did before the gate existed');
  assert.match(err.message, /business-rules failed/);
  assert.strictEqual(calls.filter((c) => c[0] === 'pushArtifact').length, 1, 'no other rule is sent');
  assert.strictEqual(calls.filter((c) => c[0] === 'readAfter').length, 1, 'one read after the refusal');
  assert.strictEqual(warnings.some((w) => /NOT created|platform limit/.test(w)), false, 'nothing is called a platform limit');
  assert.strictEqual(warnings.length, 1, `one warning naming the row; got ${JSON.stringify(warnings)}`);
  assert.match(warnings[0], /saved-row-1/);
  assert.match(warnings[0], /Either the platform saved this create and then failed to finish it, or something else added that rule meanwhile/);
  assert.doesNotMatch(warnings[0], /business rules ARE available/i, 'the new row is not attributed to this create: another writer can add it meanwhile');
  assert.strictEqual(events.some((e) => e.phase === 'business-rules' && e.status === 'skip'), false);
});

test('a read after the switched-off answer that fails or is unreadable proves nothing: the build halts', async () => {
  const reads = [async () => { throw new Error('HTTP 503 from workflows'); }, async () => [{ workflowid: '' }], async () => null];
  for (const readAfter of reads) {
    const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
    const { provision } = provisionRefusedThenRead({ readAfter });
    const warnings = [];
    const err = await runBuild(ruleOnlySpec(), {
      sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m),
    }).then(() => null, (e) => e);
    assert.ok(err, 'the build halts');
    assert.strictEqual(warnings.some((w) => /NOT created/.test(w)), false);
    assert.match(warnings.join('\n'), /failed|unreadable id set/);
  }
});

test('the switched-off answer with the rule\'s definitions unchanged skips: a row already in the snapshot is not counted as created', async () => {
  // The first workflow read (does the rule exist?) finds nothing; a row then appears before the push (another writer), so the
  // before-create snapshot holds it, and the read after the refusal finds the same row and nothing else.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  let beforeReads = 0;
  const { provision, calls } = provisionRefusedThenRead({
    before: async () => { beforeReads += 1; return beforeReads === 1 ? [] : [{ workflowid: 'Concurrent-1' }]; },
    readAfter: async () => [{ workflowid: 'concurrent-1' }],
  });
  const warnings = [];
  const res = await runBuild(ruleOnlySpec(), {
    sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m),
  });
  assert.strictEqual(res.ok, true);
  assert.strictEqual(res.skipped.businessRules.length, 1);
  assert.strictEqual(calls.filter((c) => c[0] === 'readAfter').length, 1);
  assert.strictEqual(warnings.filter((w) => /business rules were NOT created/.test(w)).length, 1);
});

test('only the answer the gate could accept is followed by a read: another refusal halts without one', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision, calls } = provisionPushing([memberRefusal({ code: '0x80040265' })]);
  await assert.rejects(() => runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {} }));
  assert.strictEqual(calls.filter((c) => c[0] === 'queryRecords').length, 2, 'the existence read and the before-create snapshot, no read after the refusal');
});

// --- a refusal is the gate only when it is the definitive answer to the first send of the create ------------------------------------
//
// The first member create answers 503 from a gateway that may sit in front of a create that ran; the plugin's HTTP client sends it again; the second answer
// is the 400. That 400 says nothing of the first send, so "rules are switched off here" would hide a rule that may exist and say none was created.
// The history of the sends comes from the plugin's own HTTP client (lib/sdk-http-client.js), and is checked WITHOUT relying on anything else sending the create once.
const LATER_PHASES = ['business-rules', 'dashboards', 'app-shell'];
async function buildThroughAdapter(answerMember, spec = threeRules(), workflowRows = () => []) {
  const { createMakerSdk, createNodeWorkspaceStorage } = require(BUNDLE);
  const { createAzHttpClient } = require('../lib/sdk-http-client.js');
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'br-hist-'));
  dirs.push(dir);
  const memberPosts = [];
  const httpClient = createAzHttpClient('https://contoso.crm.dynamics.com', {
    getToken: () => 'TOK',
    sleep: async () => {},
    request: async ({ method, url }) => {
      if (method === 'POST' && /\/Microsoft\.Dynamics\.CRM\.CreateProcessWithWfomJson$/.test(String(url))) {
        memberPosts.push(String(url));
        return answerMember(memberPosts.length);
      }
      return { statusCode: method === 'GET' ? 200 : 204, headers: {}, body: JSON.stringify({ value: [] }) };
    },
  });
  const real = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: 'https://contoso.crm.dynamics.com', httpClient });
  await real.initWorkspace();
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  // The mock SDK serves every other phase; the rows already there and the create, update and push of a rule are the ones under test.
  const initialQuery = sdk.queryRecords;
  const provision = { ...sdk, queryRecords: async (entity, opts) => (entity === 'workflow' ? workflowRows(memberPosts.length) : initialQuery(entity, opts)) };
  for (const method of ['createArtifact', 'updateElement', 'pushArtifact']) {
    const original = provision[method];
    provision[method] = (...args) => (args[0] === 'businessRule' ? real[method](...args) : original(...args));
  }
  const warnings = [];
  const events = [];
  const outcome = await runBuild({ ...spec, dashboards: [{ name: 'Later dashboard', tiles: [] }] }, {
    sdk, provisionSdk: provision, apply: true, phases: LATER_PHASES, warn: (m) => warnings.push(m), emit: (e) => events.push(e),
  }).then((res) => ({ res }), (err) => ({ err }));
  const phasesRun = [...new Set(events.filter((e) => e.status === 'ok').map((e) => e.phase))];
  return { ...outcome, memberPosts, warnings, events, phasesRun };
}
const REFUSED_ANSWER = { statusCode: 400, headers: {}, body: JSON.stringify({ error: { code: '0x80040216', message: 'An unexpected error occurred.' } }) };
const GATEWAY_ANSWER = { statusCode: 503, headers: {}, body: JSON.stringify({ error: { code: 'upstream', message: 'Gateway failed after dispatch' } }) };

test('REAL BUNDLE + plugin HTTP client: a definitive refusal of the first send skips the rules and the later phases run', async () => {
  const r = await buildThroughAdapter(() => REFUSED_ANSWER);
  assert.ok(r.res, `the build completes; got ${r.err && r.err.message}`);
  assert.strictEqual(r.memberPosts.length, 1);
  assert.strictEqual(r.res.skipped.businessRules.length, 3);
  assert.strictEqual(r.warnings.filter((w) => /business rules were NOT created/.test(w)).length, 1);
  assert.ok(r.phasesRun.includes('dashboards') && r.phasesRun.includes('app-shell'), `the phases after business-rules run; got ${JSON.stringify(r.phasesRun)}`);
});

test('REAL BUNDLE + plugin HTTP client: the switched-off answer to a create the platform saved anyway halts and names the row', async () => {
  const r = await buildThroughAdapter(() => REFUSED_ANSWER, threeRules(), (posts) => (posts > 0 ? [{ workflowid: 'saved-by-the-platform' }] : []));
  assert.ok(r.err, 'the build halts');
  assert.strictEqual(r.memberPosts.length, 1);
  assert.strictEqual(r.warnings.some((w) => /NOT created|platform limit/.test(w)), false);
  assert.match(r.warnings.join('\n'), /saved-by-the-platform/);
  assert.deepStrictEqual(r.phasesRun.filter((p) => p !== 'business-rules'), [], 'no later phase runs');
});

test('REAL BUNDLE + plugin HTTP client: a 503 sent again and then refused is NOT the gate — the build halts and nothing is called a limit', async () => {
  const r = await buildThroughAdapter((n) => (n === 1 ? GATEWAY_ANSWER : REFUSED_ANSWER));
  assert.ok(r.err, 'the build halts, as it did before the gate existed');
  assert.match(r.err.message, /business-rules failed/);
  assert.strictEqual(r.memberPosts.length, 2, 'the client sent it twice: the first send may have run');
  assert.strictEqual(r.warnings.some((w) => /NOT created|platform limit/.test(w)), false, 'no claim that nothing was created');
  assert.strictEqual(r.events.some((e) => e.phase === 'business-rules' && e.status === 'skip'), false);
  assert.deepStrictEqual(r.phasesRun.filter((p) => p !== 'business-rules'), [], 'no later phase runs');
});

test('REAL BUNDLE + plugin HTTP client: a send that got no answer, sent again and then refused, is NOT the gate either', async () => {
  const r = await buildThroughAdapter((n) => (n === 1 ? { error: 'socket hang up', code: 'ECONNRESET' } : REFUSED_ANSWER));
  assert.ok(r.err);
  assert.strictEqual(r.warnings.some((w) => /NOT created/.test(w)), false);
});

test('an unknown or partial outcome anywhere in the chain wins over a refusal inside it: thrown and by-value, and it still says it is unknown', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const wrappers = [
    ['an outcome flag', () => Object.assign(new Error('The create outcome is unknown; do not assume nothing was written.'), { code: 'create-outcome-unknown', transient: false, outcome: 'unknown', cause: memberRefusal() })],
    ['an outcome flag alone', () => Object.assign(new Error('not sure what the create did'), { outcome: 'unknown', cause: memberRefusal() })],
    ['a code naming an unknown outcome', () => Object.assign(new Error('the row MAY have been written'), { code: 'UPSERT_OUTCOME_UNKNOWN', cause: memberRefusal() })],
    ['an earlier gateway answer in the chain', () => Object.assign(new Error('gateway'), { code: 'CONNECTION_ERROR', statusCode: 503, cause: memberRefusal() })],
    ['a send that got no answer in the chain', () => Object.assign(new Error('Request failed'), { code: 'CONNECTION_ERROR', statusCode: 0, cause: memberRefusal() })],
  ];
  for (const [what, make] of wrappers) {
    for (const byValue of [false, true]) {
      const { provision } = provisionPushing([byValue
        ? { type: 'businessRule', id: 'br-new', saved: false, shipped: false, publish: { kind: 'notRequested' }, error: make() }
        : make()]);
      const warnings = [];
      const events = [];
      const err = await runBuild(threeRules(), {
        sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m), emit: (e) => events.push(e),
      }).then(() => null, (e) => e);
      assert.ok(err, `${what}${byValue ? ' (by value)' : ''} must halt`);
      assert.strictEqual(warnings.length, 0, `${what}: no environment warning`);
      assert.strictEqual(events.some((e) => e.status === 'skip'), false, `${what}: no skip`);
    }
  }
  const { provision } = provisionPushing([wrappers[0][1]()]);
  const err = await runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {} }).then(() => null, (e) => e);
  assert.match(err.message, /outcome is unknown/, 'the halt keeps the words that say the outcome is unknown');
});

test('a plain wrapper around the confirmed refusal — nothing that says unknown — still skips', async () => {
  // requireSuccessfulPush's BuildHalt is the by-value case above; a plain Error around the SDK's error is the same kind of wrapper.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionPushing([Object.assign(new Error('push business rule Rule A failed'), { cause: memberRefusal() })]);
  const res = await runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {} });
  assert.strictEqual(res.skipped.businessRules.length, 1);
});

test('a refusal from a transport that reports no send history is not confirmed, so it halts', async () => {
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionPushing([memberRefusal({ history: null })]);
  await assert.rejects(() => runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {} }));
});

test('a refusal whose earlier send was ambiguous halts even when it is the only send of its own call', async () => {
  // The SDK can call a request again itself: the history then comes from the client's ledger, not from this call's loop.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const { provision } = provisionPushing([memberRefusal({ history: { sends: 1, earlierAmbiguous: true } })]);
  await assert.rejects(() => runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: () => {} }));
});

test('only the member itself is the gate: a failed read whose URL merely QUOTES the member is not', async () => {
  // A rule named like the member puts the member's name into the query of the read before its create. Judged by the request, that read is not the create.
  const { sdk } = require('./helpers/mock-sdk.js').makeSimpleMockSdk();
  const quoted = `https://contoso.crm.dynamics.com/api/data/v9.0/workflows?$filter=name%20eq%20'Microsoft.Dynamics.CRM.CreateProcessWithWfomJson'`;
  const lookalikes = [
    quoted,
    `${MEMBER_URL}2`,
    'https://contoso.crm.dynamics.com/api/data/v9.0/workflows(00000000-0000-0000-0000-000000000000)/Xmicrosoft.Dynamics.CRM.CreateProcessWithWfomJson',
    'https://contoso.crm.dynamics.com/api/data/v9.0/workflows(00000000-0000-0000-0000-000000000000)/Microsoft.Dynamics.CRM.UpdateProcessWithWfomJson?x=CreateProcessWithWfomJson',
  ];
  for (const url of lookalikes) {
    const { provision } = provisionPushing([memberRefusal({ url })]);
    const warnings = [];
    await assert.rejects(
      () => runBuild(ruleOnlySpec(), { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m) }),
      `${url} is not the member`);
    assert.strictEqual(warnings.length, 0);
  }
  // The same refusal raised by the read before the create (the one that really carries the quoted name) halts as well.
  const { provision } = provisionPushing(['ok']);
  const queryRecords = provision.queryRecords;
  let reads = 0;
  provision.queryRecords = async (entity, opts) => {
    if (entity === 'workflow') { reads += 1; if (reads === 2) throw memberRefusal({ url: quoted }); }
    return queryRecords(entity, opts);
  };
  const spec = ruleOnlySpec();
  spec.businessRules[0].name = 'Microsoft.Dynamics.CRM.CreateProcessWithWfomJson';
  const warnings = [];
  const err = await runBuild(spec, { sdk, provisionSdk: provision, apply: true, phases: ['business-rules'], warn: (m) => warnings.push(m) }).then(() => null, (e) => e);
  assert.ok(err, 'a failed read is not a refused create');
  assert.strictEqual(warnings.length, 0);
});