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.

268 KB
Raw Blame
'use strict';
// scripts/solution-settings.js — deployment settings for a solution: the CLI contract, the checks made before any request, the SDK calls and
// pac's defaults, which refusals fall back to pac and which do not, the long-running-action client, and what the sanitizer keeps out of what is printed (and what it leaves).
// The SDK and pac are stood in for here; tests/solution-settings-real-bundle.test.js drives the real bundle and the real script.
// This file checks where lib/temp-workspace.js makes a workspace, so it works in a temp folder of its own (helpers/own-temp-folder.js): in the
// suite's shared one, other test processes make and remove the plugin's workspace folder. Required first, so every path below is inside it.
require('./helpers/own-temp-folder.js');
const test = require('node:test');
const assert = require('node:assert');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const crypto = require('node:crypto');
const { loadCli } = require('./helpers/cli-harness.js');
const realAuth = require('../lib/dataverse-auth.js');
// The copy of the settings that pac is given is made the plain way here (helpers/plain-private-file.js: the real one asks Windows for an access list, half a second a copy); before the script is loaded.
const plainPrivateFile = require('./helpers/plain-private-file.js');
plainPrivateFile.install();
const { WORK_FOLDER, PLAIN_WORK_FOLDER, ownerRecordOf, refusingWorkFolders, assertMadeByTheMaker } = require('./helpers/solution-settings-work-folder.js');

const SCRIPT = path.resolve(__dirname, '..', 'solution-settings.js');
const ss = require(SCRIPT);

const ENV = 'https://contoso.crm.dynamics.com';
const SECRET = 'kv-secret-value-0123456789';
// A value of a variable that is not secret (a text variable), beside it.
const PLAIN = 'plain-value-0123456789';
const CONNECTION_ID = 'shared-commondataser-00000000-0000-0000-0000-00000000abcd';
const JOB = '44444444-4444-4444-8444-444444444444';

const tempDirs = [];
const tempDir = () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'solution-settings-test-'));
  tempDirs.push(dir);
  return dir;
};
test.after(() => { for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); });

const ZIP = Buffer.concat([Buffer.from([0x50, 0x4b, 0x03, 0x04]), Buffer.from(' a solution package')]);
function files(dir) {
  const pkg = path.join(dir, 'solution.zip');
  fs.writeFileSync(pkg, ZIP);
  return { pkg, out: path.join(dir, 'settings.json'), settings: path.join(dir, 'filled.json') };
}

const template = (extra = {}) => ({
  EnvironmentVariables: [{ SchemaName: 'new_Url', Value: '', DefaultValue: 'https://default.example', TypeId: 100000000, IsRequired: false }],
  ConnectionReferences: [{ LogicalName: 'new_Dataverse', ConnectionId: '', ConnectorId: '/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps' }],
  ...extra,
});
const filled = () => template({
  EnvironmentVariables: [{ SchemaName: 'new_Url', Value: SECRET, DefaultValue: 'https://default.example', TypeId: 100000005, IsRequired: false }],
  ConnectionReferences: [{ LogicalName: 'new_Dataverse', ConnectionId: CONNECTION_ID, ConnectorId: '/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps' }],
});
// The same file with a variable that is not secret: a message that quotes its value, or the connection id, is shown with them replaced.
const filledPlain = () => template({
  EnvironmentVariables: [{ SchemaName: 'new_Url', Value: PLAIN, DefaultValue: 'https://default.example', TypeId: 100000000, IsRequired: false }],
  ConnectionReferences: [{ LogicalName: 'new_Dataverse', ConnectionId: CONNECTION_ID, ConnectorId: '/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps' }],
});

// A stand-in for lib/sdk-first.js's sdkFirst that applies the rule with the plan's OWN refusal: the SDK's answer, or a refusal → pac, or the failure.
const stubSdkFirst = (sdk, record = []) => async (env, plan) => {
  record.push({ env, plan });
  try {
    return { source: 'sdk', value: await plan.fromSdk(sdk) };
  } catch (error) {
    const refusal = plan.refusal(error);
    if (!refusal) throw error;
    return { source: 'pac', value: await plan.fromPac(refusal), fallback: refusal };
  }
};
const refusal = (code, message = `${code} words`) => Object.assign(new Error(message), { code });

// A pac that records its calls. `onCall(args)` may write the file pac would write and answers the result.
function fakePac(onCall) {
  const calls = [];
  return { calls, pac: { spawn: (name, args, options) => { calls.push({ name, args, options }); return onCall(args, options); } } };
}
const flagValue = (args, flag) => args[args.indexOf(flag) + 1];

// --- arguments ----------------------------------------------------------------------------------------------------------------------

test('template: three forms — a solution to export, a package to stage, a package to read with pac — and import needs all three of its flags', () => {
  const a = ss.readRequest(['template', '--env', ENV, '--solution', 'ContosoDemo', '--out', 'out.json']).request;
  assert.deepStrictEqual({ ...a, outPath: path.basename(a.outPath) }, { command: 'template', env: ENV, solution: 'ContosoDemo', packagePath: undefined, outPath: 'out.json', force: false });
  const b = ss.readRequest(['template', '--env', `${ENV}/`, '--package', 'p.zip', '--out', 'out.json', '--force']).request;
  assert.strictEqual(b.env, ENV, 'the origin');
  assert.strictEqual(b.force, true);
  assert.strictEqual(path.basename(b.packagePath), 'p.zip');
  assert.ok(path.isAbsolute(b.packagePath) && path.isAbsolute(b.outPath), 'paths are resolved once, here');
  const c = ss.readRequest(['template', '--package', 'p.zip', '--out', 'out.json']).request;
  assert.strictEqual(c.env, undefined);
  const d = ss.readRequest(['import', '--env', ENV, '--package', 'p.zip', '--settings', 's.json']).request;
  assert.deepStrictEqual([d.command, d.env, path.basename(d.packagePath), path.basename(d.settingsPath)], ['import', ENV, 'p.zip', 's.json']);
});

test('a command is required, and is template or import', () => {
  assert.match(ss.readRequest([]).error, /a command is required: template or import/);
  assert.match(ss.readRequest(['export']).error, /unknown command 'export': use template or import/);
});

test('template: name a solution or a package — one of the two — and a solution needs the environment it is exported from', () => {
  assert.match(ss.readRequest(['template', '--env', ENV, '--out', 'o.json']).error, /name the solution to export with --solution, or a package with --package — one of the two/);
  assert.match(ss.readRequest(['template', '--env', ENV, '--solution', 'Demo', '--package', 'p.zip', '--out', 'o.json']).error, /one of the two/);
  assert.match(ss.readRequest(['template', '--solution', 'Demo', '--out', 'o.json']).error, /--solution needs --env/);
  assert.match(ss.readRequest(['template', '--env', ENV, '--solution', 'Demo']).error, /--out is required/);
  for (const bad of ['1Demo', 'Demo-App', 'Demo App', 'Demo;x', '', 'x'.repeat(66), '../x']) {
    assert.ok(ss.readRequest(['template', '--env', ENV, '--solution', bad || ' ', '--out', 'o.json']).error, JSON.stringify(bad));
  }
  assert.match(ss.readRequest(['template', '--env', ENV, '--solution', 'Demo-App', '--out', 'o.json']).error, /--solution must be a solution's unique name/);
});

test('import: every flag is required', () => {
  assert.match(ss.readRequest(['import', '--package', 'p.zip', '--settings', 's.json']).error, /--env is required/);
  assert.match(ss.readRequest(['import', '--env', ENV, '--settings', 's.json']).error, /--package is required/);
  assert.match(ss.readRequest(['import', '--env', ENV, '--package', 'p.zip']).error, /--settings is required/);
});

test('an environment that is not a Dataverse origin is refused before any file is read or request sent — in both commands', () => {
  for (const bad of ['https://contoso.example.com', 'http://contoso.crm.dynamics.com', `${ENV}/api/data/v9.2`, 'contoso']) {
    assert.match(ss.readRequest(['import', '--env', bad, '--package', 'p.zip', '--settings', 's.json']).error, /is not a Dataverse environment URL/, bad);
    assert.match(ss.readRequest(['template', '--env', bad, '--package', 'p.zip', '--out', 'o.json']).error, /is not a Dataverse environment URL/, bad);
  }
});

test('flags are validated up front: an unknown one (with the nearest name), a missing value, the other command\'s flag, extra arguments', () => {
  assert.match(ss.readRequest(['template', '--env', ENV, '--solutionn', 'x', '--out', 'o.json']).error, /unknown flag\(s\): --solutionn \(did you mean --solution\?\)/);
  assert.match(ss.readRequest(['template', '--env', ENV, '--solution', 'Demo', '--out']).error, /--out requires a value/);
  assert.match(ss.readRequest(['template', '--env', ENV, '--solution', 'Demo', '--out', 'o.json', '--settings', 's.json']).error, /unknown flag\(s\): --settings/);
  assert.match(ss.readRequest(['import', '--env', ENV, '--package', 'p.zip', '--settings', 's.json', '--force']).error, /unknown flag\(s\): --force/);
  assert.match(ss.readRequest(['import', '--env', ENV, '--package', 'p.zip', '--settings', 's.json', '--solution', 'x']).error, /unknown flag\(s\): --solution/);
  assert.match(ss.readRequest(['template', 'extra', '--env', ENV, '--solution', 'Demo', '--out', 'o.json']).error, /unexpected argument 'extra'/);
});

test('--force is a switch: it takes no value, and a value after it is an error rather than swallowed', () => {
  assert.match(ss.readRequest(['template', '--env', ENV, '--solution', 'Demo', '--force', 'o.json']).error, /--force takes no value \(got 'o\.json'\)/);
  assert.match(ss.readRequest(['template', '--env', ENV, '--solution', 'Demo', '--out', 'o.json', '--force=yes']).error, /--force takes no value/);
  assert.strictEqual(ss.readRequest(['template', '--env', ENV, '--solution', 'Demo', '--out', 'o.json', '--force']).request.force, true);
  assert.strictEqual(ss.readRequest(['template', '--force', '--env', ENV, '--solution', 'Demo', '--out', 'o.json']).request.force, true);
});

// --- the settings file --------------------------------------------------------------------------------------------------------------

test('a settings file is what pac writes: lists of entries with a name and a value; pac\'s own file and this script\'s both pass', () => {
  assert.strictEqual(ss.settingsProblem(template()), null);
  assert.strictEqual(ss.settingsProblem(filled()), null);
  // What `pac solution create-settings` 2.12 writes — display names, an empty CopilotAgents list — is accepted.
  assert.strictEqual(ss.settingsProblem({
    EnvironmentVariables: [{ SchemaName: 'x_a', Value: '', DefaultValue: 'd', Name: { Default: 'A', ByLcid: { 1033: 'A' } }, TypeId: 100000000, IsRequired: false }],
    ConnectionReferences: [{ LogicalName: 'x_cr', ConnectionId: '', ConnectorId: '/providers/Microsoft.PowerApps/apis/shared_sql' }],
    CopilotAgents: [],
  }), null);
  assert.strictEqual(ss.settingsProblem({ EnvironmentVariables: [] }), null, 'one list is enough');
  assert.strictEqual(ss.settingsProblem({ ConnectionReferences: [] }), null);
});

test('a file that is not that shape is named, entry and field, in words that never quote a value', () => {
  const cases = [
    [null, /one JSON object/],
    [[], /one JSON object/],
    ['text', /one JSON object/],
    [{}, /neither an "EnvironmentVariables" nor a "ConnectionReferences" list/],
    [{ Other: [] }, /neither/],
    [{ EnvironmentVariables: {} }, /"EnvironmentVariables" must be a list/],
    [{ ConnectionReferences: 'x' }, /"ConnectionReferences" must be a list/],
    [{ EnvironmentVariables: ['x'] }, /EnvironmentVariables\[0\] must be an object/],
    [{ EnvironmentVariables: [{ Value: 'x' }] }, /EnvironmentVariables\[0\]\.SchemaName must be a non-empty string/],
    [{ EnvironmentVariables: [{ SchemaName: '', Value: '' }] }, /SchemaName must be a non-empty string/],
    [{ EnvironmentVariables: [{ SchemaName: ' a ', Value: '' }] }, /SchemaName must not begin or end with white space/],
    [{ EnvironmentVariables: [{ SchemaName: 'a' }] }, /EnvironmentVariables\[0\]\.Value must be a string — empty to leave the entry unset/],
    [{ EnvironmentVariables: [{ SchemaName: 'a', Value: 5 }] }, /Value must be a string/],
    [{ EnvironmentVariables: [{ SchemaName: 'a', Value: { x: 1 } }] }, /Value must be a string/],
    [{ ConnectionReferences: [{ LogicalName: 'a' }] }, /ConnectionReferences\[0\]\.ConnectionId must be a string/],
    [{ ConnectionReferences: [{ LogicalName: 'a', ConnectionId: ' x' }] }, /ConnectionId must not begin or end with white space/],
    [{ ConnectionReferences: [{ ConnectionId: '' }] }, /ConnectionReferences\[0\]\.LogicalName must be a non-empty string/],
    [{ EnvironmentVariables: [{ SchemaName: 'ok', Value: '' }, { SchemaName: 'b', Value: 12 }] }, /EnvironmentVariables\[1\]\.Value/],
  ];
  for (const [settings, message] of cases) {
    const problem = ss.settingsProblem(settings);
    assert.match(problem, message, JSON.stringify(settings));
  }
  // The problem text never holds what was in the field: a settings file can hold a secret.
  const secret = { EnvironmentVariables: [{ SchemaName: 'new_Secret', Value: { vault: SECRET } }, { SchemaName: 'x', Value: '' }] };
  assert.ok(!ss.settingsProblem(secret).includes(SECRET), ss.settingsProblem(secret));
  const padded = { ConnectionReferences: [{ LogicalName: 'a', ConnectionId: ` ${CONNECTION_ID}` }] };
  assert.ok(!ss.settingsProblem(padded).includes(CONNECTION_ID));
});

test('an environment variable\'s value is TEXT and passes through byte for byte — leading and trailing white space, a JSON document that ends in a newline, a value made of white space alone — while identifiers keep their checks', () => {
  const values = [' leading', 'trailing ', '  both  ', ' {\n  "feature": true\n}\n', '{"a":1}\n', '   ', '\t', '\n', ' \u00a0 ', 'line one\nline two\r\n'];
  for (const Value of values) {
    assert.strictEqual(ss.settingsProblem({ EnvironmentVariables: [{ SchemaName: 'new_Text', Value }] }), null, JSON.stringify(Value));
    // Through the whole parse as well, and nothing in the entry changes on the way: what the file holds is what the environment gets.
    const parsed = ss.parseSettings(JSON.stringify({ EnvironmentVariables: [{ SchemaName: 'new_Text', Value }] }), 'f');
    assert.strictEqual(parsed.EnvironmentVariables[0].Value, Value, JSON.stringify(Value));
  }
  // A value of white space alone is FILLED (the SDK sends it: it only skips an empty string), so it is named among the entries the file fills.
  assert.deepStrictEqual(ss.filledNames({ EnvironmentVariables: [{ SchemaName: 'new_Text', Value: '   ' }, { SchemaName: 'new_Empty', Value: '' }] }), { environmentVariables: ['new_Text'], connectionReferences: [] });
  // The identifiers keep their checks: a name or a connection id with white space around it is a mistake the SDK refuses too.
  assert.match(ss.settingsProblem({ EnvironmentVariables: [{ SchemaName: ' new_Text', Value: 'x' }] }), /SchemaName must not begin or end with white space/);
  assert.match(ss.settingsProblem({ ConnectionReferences: [{ LogicalName: 'new_ref ', ConnectionId: '' }] }), /LogicalName must not begin or end with white space/);
  assert.match(ss.settingsProblem({ ConnectionReferences: [{ LogicalName: 'new_ref', ConnectionId: `${CONNECTION_ID}\n` }] }), /ConnectionId must not begin or end with white space/);
  assert.match(ss.settingsProblem({ ConnectionReferences: [{ LogicalName: 'new_ref', ConnectionId: '   ' }] }), /ConnectionId must not begin or end with white space/);
  // A value that is not a string is still refused, in words that never quote it.
  assert.match(ss.settingsProblem({ EnvironmentVariables: [{ SchemaName: 'new_Text', Value: null }] }), /Value must be a string — empty to leave the entry unset/);
});

test('a settings file is read as UTF-8 with or without the byte order mark a Windows tool adds, and invalid JSON is said without quoting it', () => {
  const text = JSON.stringify(template());
  assert.deepStrictEqual(ss.parseSettings(`\uFEFF${text}`, 'f'), template());
  assert.deepStrictEqual(ss.parseSettings(text, 'f'), template());
  assert.throws(() => ss.parseSettings(`{"EnvironmentVariables": [{"SchemaName": "a", "Value": "${SECRET}`, 'the file'), (err) => err.code === 'SETTINGS_INVALID' && /the file is not valid JSON\./.test(err.message) && !err.message.includes(SECRET));
  assert.throws(() => ss.parseSettings('[]', 'the file'), (err) => err.code === 'SETTINGS_INVALID' && /the file is not a deployment-settings file: the file must hold one JSON object/.test(err.message));
});

test('what a file fills in is named, never shown: the entries with a value or a connection id', () => {
  assert.deepStrictEqual(ss.filledNames(template()), { environmentVariables: [], connectionReferences: [] });
  assert.deepStrictEqual(ss.filledNames(filled()), { environmentVariables: ['new_Url'], connectionReferences: ['new_Dataverse'] });
  assert.deepStrictEqual(ss.filledNames({ EnvironmentVariables: [{ SchemaName: 'a', Value: 'x' }] }), { environmentVariables: ['a'], connectionReferences: [] });
});

// The secret of the file above (TypeId 100000005) beside a text variable and a connection id: what a message may quote, and how it is shown.
const mixedSettings = () => ({
  EnvironmentVariables: [
    { SchemaName: 'new_Secret', Value: SECRET, TypeId: 100000005 },
    { SchemaName: 'new_Text', Value: PLAIN, TypeId: 100000000 },
  ],
  ConnectionReferences: [{ LogicalName: 'new_Dataverse', ConnectionId: CONNECTION_ID }],
});
// What the package's own definitions say each variable is (a `type` option value), by lower-cased schema name — what the StageSolution reply gives, as the import records it.
const TEXT_TYPE = 100000000;
const SECRET_TYPE = 100000005;
const packageTypes = (types) => new Map(Object.entries(types).map(([name, type]) => [name.toLowerCase(), type]));
// What takes the place of a text that repeated a value of the file.
const WITHHELD = (what = 'text') => `<${what} withheld: it repeated a value from the settings file>`;

test('the sanitizer takes every filled connection id and every value that is not a secret out of a text, longest first, in one pass — and leaves short non-secret words alone', () => {
  const sanitize = ss.sanitizer({
    EnvironmentVariables: [{ SchemaName: 'a', Value: PLAIN }, { SchemaName: 'b', Value: `${PLAIN}-suffix` }, { SchemaName: 'c', Value: 'true' }, { SchemaName: 'd', Value: '' }],
    ConnectionReferences: [{ LogicalName: 'e', ConnectionId: CONNECTION_ID }],
  }, packageTypes({ a: TEXT_TYPE, b: TEXT_TYPE, c: TEXT_TYPE, d: TEXT_TYPE }));
  assert.strictEqual(sanitize(`bad value ${PLAIN}-suffix and ${PLAIN} and ${CONNECTION_ID}`), 'bad value <redacted> and <redacted> and <redacted>');
  assert.strictEqual(sanitize('the value true is fine'), 'the value true is fine', 'a short value would be taken out of ordinary words');
  assert.strictEqual(sanitize(`${PLAIN}${PLAIN}`), '<redacted><redacted>');
  assert.strictEqual(ss.sanitizer({})('nothing to hide'), 'nothing to hide');
  assert.strictEqual(ss.sanitizer(null)('x'), 'x');
  assert.strictEqual(sanitize(null), '');
  // One pass: what replaces a value is never searched for another (a value that is a piece of "<redacted>"), and a metacharacter in a value is only itself.
  const onePass = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'a', Value: 'abcdef' }, { SchemaName: 'b', Value: 'redact' }] }, packageTypes({ a: TEXT_TYPE, b: TEXT_TYPE }));
  assert.strictEqual(onePass('abcdef redact'), '<redacted> <redacted>');
  // ...also when the text has an escape in it, which makes the decoded views of what is left be looked at: the marker is not what they are searched in.
  assert.strictEqual(onePass('abcdef \\u0041 redact'), '<redacted> \\u0041 <redacted>');
  const meta = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'a', Value: 'a.b*c(d)[e]|f+g' }] }, packageTypes({ a: TEXT_TYPE }));
  assert.strictEqual(meta('x a.b*c(d)[e]|f+g y aXbbc(d)e'), 'x <redacted> y aXbbc(d)e');
});

test('what is KNOWN of a variable\'s secrecy is the PACKAGE\'s to say — its definition\'s type — and never the file\'s optional TypeId: the file can only add secrecy, and a variable the package was not read for is not known to be anything', () => {
  assert.strictEqual(ss.MIN_HIDDEN_LENGTH, 6);
  assert.strictEqual(ss.SECRET_TYPE_ID, 100000005);
  // [the package's type, the file's TypeId, what is known: 'secret', 'plain' (the package says another type) or 'unknown' (no definition was read)]
  const NUMBER_TYPE = 100000001;
  const table = [
    [SECRET_TYPE, undefined, 'secret'], // the package says Secret and the file says nothing: the case an optional field must not decide
    [SECRET_TYPE, null, 'secret'],
    [SECRET_TYPE, TEXT_TYPE, 'secret'], // the file says Text, the package says Secret: the package's word is the one that counts
    [SECRET_TYPE, '100000000', 'secret'],
    [SECRET_TYPE, 'secret', 'secret'],
    [SECRET_TYPE, SECRET_TYPE, 'secret'],
    [TEXT_TYPE, undefined, 'plain'],
    [TEXT_TYPE, TEXT_TYPE, 'plain'],
    [NUMBER_TYPE, undefined, 'plain'],
    [NUMBER_TYPE, 'nonsense', 'plain'],
    [TEXT_TYPE, SECRET_TYPE, 'secret'], // the file adds secrecy: a maker who marks a value secret is believed
    [TEXT_TYPE, '100000005', 'secret'],
    [undefined, undefined, 'unknown'], // nothing read, nothing said: not known to be anything
    [undefined, null, 'unknown'],
    [undefined, TEXT_TYPE, 'unknown'], // the file's word is not evidence: nothing checks it
    [undefined, '100000000', 'unknown'],
    [undefined, 'secret', 'unknown'],
    [undefined, SECRET_TYPE, 'secret'], // ...except that a file's word that a value is a secret is believed: evidence only ever raises secrecy
    [undefined, '100000005', 'secret'],
  ];
  for (const [packageType, TypeId, expected] of table) {
    const entry = TypeId === undefined ? {} : { TypeId };
    assert.strictEqual(ss.secrecyOf(entry, packageType), expected, `package ${packageType}, file ${JSON.stringify(TypeId)}`);
  }
});

test('a secret is hidden at any length whatever the file says of its type; a value that is not one is hidden from six characters on, by its trimmed length', () => {
  const as = (packageType, Value, TypeId) => ss.sanitizer(
    { EnvironmentVariables: [{ SchemaName: 'new_A', Value, ...(TypeId === undefined ? {} : { TypeId }) }] },
    packageType === undefined ? new Map() : packageTypes({ new_A: packageType })
  );
  assert.strictEqual(as(TEXT_TYPE, 'abcdef')('x abcdef y'), 'x <redacted> y', 'six characters, not a secret: replaced');
  assert.strictEqual(as(TEXT_TYPE, 'abcde')('x abcde y'), 'x abcde y', 'five characters, not a secret: shown — it is a word as much as a value, and a message with every digit blanked says nothing');
  assert.strictEqual(as(TEXT_TYPE, '  abcde  ')('x abcde y'), 'x abcde y', 'the length is that of the value without its padding');
  assert.strictEqual(as(TEXT_TYPE, ' abcdef ')('x abcdef y'), 'x<redacted>y', 'the padded form is the longest, so it is the one taken out');
  assert.strictEqual(as(TEXT_TYPE, ' abcdef ')('x abcdef, y'), 'x <redacted>, y', 'the trimmed form is taken out where the padding is not there (a service may trim what it quotes)');
  // The package says Secret: every length, and whatever the file says (the file's TypeId is optional, and nothing checks it).
  for (const TypeId of [undefined, null, TEXT_TYPE, '100000000', 'secret', SECRET_TYPE, '100000005']) {
    assert.strictEqual(as(SECRET_TYPE, 'PIN42', TypeId)('Value PIN42 was refused'), WITHHELD(), `package Secret, file ${JSON.stringify(TypeId)}`);
  }
  assert.strictEqual(as(SECRET_TYPE, 'x')('an x in it'), WITHHELD(), 'a one-character secret is hidden too');
  // The package says Text and the file says Secret: the file adds secrecy.
  assert.strictEqual(as(TEXT_TYPE, 'PIN42', SECRET_TYPE)('Value PIN42 was refused'), WITHHELD());
  // The package was not read: a variable is not known to be anything else, whatever the file says. (Five characters is below MIN_HIDDEN_LENGTH, so the value is looked for as a whole word: here it is one.)
  for (const TypeId of [undefined, null, TEXT_TYPE, '100000000', 'secret', SECRET_TYPE]) {
    assert.strictEqual(as(undefined, 'PIN42', TypeId)('Value PIN42 was refused'), WITHHELD(), `no definition, file ${JSON.stringify(TypeId)}`);
  }
  // A text that holds none of it is shown as it is, whichever way the variable is read.
  assert.strictEqual(as(undefined, 'PIN42')('nothing of it here'), 'nothing of it here');
  // The definitions are looked at when a text is sanitized, not when the sanitizer is made: what the import learns of the package later is used.
  const definitions = new Map();
  const later = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'New_A', Value: 'abcdefgh' }] }, definitions);
  assert.strictEqual(later('x abcdefgh y'), WITHHELD(), 'before the package was read: not known to be anything, and six characters or more are looked for as a substring');
  definitions.set('new_a', TEXT_TYPE);
  assert.strictEqual(later('x abcdefgh y'), 'x <redacted> y', 'after: the package says Text, and the schema name is matched without regard to case');
});

// The escapes of a double quote that the decoders do not read, and the value glued to words: only a SUBSTRING test finds a value in them. (`\042` is an octal escape, `%u0022` the legacy
// percent form, and `&#x22` has no `;`.) The other texts quote the value in a form the decoders DO read, or plainly.
const UNDECODED_TEXTS = [
  ['an octal escape', 'Value \\042PIN42\\042 was refused'],
  ['a legacy %u escape', 'Value %u0022PIN42%u0022 was refused'],
  ['an XML reference with no semicolon', 'Value &#x22PIN42&#x22 was refused'],
  ['glued to words', 'ValuePIN42wasRefused'],
  ['after an underscore', 'key_PIN42 was refused'],
];
const DECODED_TEXTS = [
  ['plainly', "Value 'PIN42' was refused"],
  ['a JSON \\u0022', 'Value \\u0022PIN42\\u0022 was refused'],
  ['a percent-encoded quote', 'value=%22PIN42%22'],
  ['an XML entity', 'Value &quot;PIN42&quot; was refused'],
];

test('a KNOWN secret — the package says Secret, or the file does — is found as a substring at ANY length, whatever the text spells around it: glued to words, inside an escape the decoders do not read', () => {
  const sanitizeAs = (value, packageType, TypeId) => ss.sanitizer(
    { EnvironmentVariables: [{ SchemaName: 'new_A', Value: value, ...(TypeId === undefined ? {} : { TypeId }) }] },
    packageType === undefined ? new Map() : packageTypes({ new_A: packageType })
  );
  const known = [
    ['the package says Secret', SECRET_TYPE, undefined],
    ['the package says Secret, the file says Text', SECRET_TYPE, TEXT_TYPE],
    ['the file says Secret, the package says Text', TEXT_TYPE, SECRET_TYPE],
    ['the file says Secret, the package was not read', undefined, SECRET_TYPE],
    ['both say Secret', SECRET_TYPE, SECRET_TYPE],
  ];
  for (const [how, packageType, TypeId] of known) {
    const sanitize = sanitizeAs('PIN42', packageType, TypeId);
    for (const [name, text] of [...UNDECODED_TEXTS, ...DECODED_TEXTS]) assert.strictEqual(sanitize(text, 'message'), WITHHELD('message'), `${how}: ${name}: ${text}`);
    // Even one character: a substring of a word is found, which is what hiding a secret of any length costs.
    assert.strictEqual(sanitizeAs('x', packageType, TypeId)('new_Text', 'message'), WITHHELD('message'), `${how}: one character, inside a word`);
  }
});

test('a value whose type is NOT known is looked for as a substring from six characters, as before — a Secret-type value is a Key Vault reference, always long — in every text it can be spelled in', () => {
  const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: 'PIN420' }] }, new Map());
  for (const [name, text] of [...UNDECODED_TEXTS, ...DECODED_TEXTS]) assert.strictEqual(sanitize(text.replace(/PIN42/g, 'PIN420'), 'message'), WITHHELD('message'), `${name}: ${text}`);
  // A Key Vault reference, as a settings file holds one for a Secret-type variable, is found in any of them.
  const reference = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.KeyVault/vaults/contoso/secrets/token';
  const vault = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: reference }] }, new Map());
  for (const text of [`the secret ${reference} was refused`, `{"path":"${reference.replace(/\//g, '\\/')}"}`, `value%3D${encodeURIComponent(reference)}`, `the vault is ${reference.toUpperCase()}`]) assert.strictEqual(vault(text, 'message'), WITHHELD('message'), text);
  // The length is that of the trimmed value: six characters are looked for as a substring, five as a whole word.
  const at = (value) => ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: value }] }, new Map());
  assert.strictEqual(at('abcdef')('xabcdefy'), WITHHELD(), 'six: a substring');
  assert.strictEqual(at(' abcdef ')('xabcdefy'), WITHHELD(), 'six once trimmed: a substring');
  assert.strictEqual(at('abcde')('xabcdey'), 'xabcdey', 'five: not a whole word in it');
  assert.strictEqual(at('  abcde  ')('xabcdey'), 'xabcdey', 'five once trimmed: not a whole word in it');
  assert.strictEqual(at('abcde')('x abcde y'), WITHHELD(), 'five: a whole word in it');
});

// The SDK's own account of an entry the package does not define (api/SolutionApi.ts of the vendored SDK, measured): it names the entry and the package's variables, and never a value.
const MISMATCH = "importSolution: deploymentSettings does not match the package — EnvironmentVariables: 'new_NotInPackage' is not a variable of the package. The package's variables: new_Text; its connection references: none. Nothing was imported.";

test('a value whose type is NOT known and is shorter than six characters is looked for as a WHOLE WORD — so "x" does not withhold "new_Text" — in every decoded view, every occurrence, either case', () => {
  const FEATURE = listSentence('Workflows');
  const unknown = (value) => ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: value }] }, new Map());
  // [value, text]: a text that does not have the value as a word is shown as it is.
  const shown = [
    ['x', 'new_Text'], // the case seen on a real organization: the x of "Text"
    ['x', MISMATCH],
    ['Text', MISMATCH], // the second half of a name: the underscore before it is part of the word
    ['new', 'new_NotInPackage'], // an underscore is part of a word
    ['en', 'deployment'],
    ['en', FEATURE],
    ['x', 'new_Text, boxes, x1, x_y, and 2x'], // a letter, a digit or an underscore on either side
    ['  x ', 'new_Text'], // the white space around a value is not part of what is looked for
    ['1', 'a1b, 11, 1a, _1'],
    ['_', 'a_b'],
    ['x', 'x\u203Fy'], // U+203F, another connector punctuation (Pc)
    ['e', 'x e\u0301 y'], // a combining mark glued to it
    ['e\u0301', 'cafe\u0301'], // a value that ends in a mark has a word's edge there, and a letter before it
    ['\u00e9', 'caf\u00e9'], // a letter that is not ASCII
    ['\u00c9', 'caf\u00e9'], // ...in the other case
    ['\u4e16', '\u4e16\u754c'], // ideographs are letters
    ['\ud835\udc9c', 'x\ud835\udc9cy'], // a letter outside the Basic Multilingual Plane is one character
    ['-x', 'a-xb'], // an edge that is a word character is glued to the next letter, whatever the other edge is
    ['x-', 'ax-b'],
    ['PIN42', 'ValuePIN42wasRefused'], // what the rule leaves (stated in AGENTS.md): a short value glued to words…
    ['PIN42', 'key_PIN42 was refused'],
    ['PIN42', 'Value \\042PIN42\\042 was refused'], // …or inside an escape the decoders do not read
    ['PIN42', 'Value %u0022PIN42%u0022 was refused'],
    ['PIN42', 'Value &#x22PIN42&#x22 was refused'],
  ];
  for (const [value, text] of shown) assert.strictEqual(unknown(value)(text, 'message'), text, `${JSON.stringify(value)} in ${JSON.stringify(text)}`);
  // A text that has it as a word is withheld whole.
  const withheld = [
    ['x', "'x'"],
    ['x', 'a x b'],
    ['X', "'x'"], // either case
    ['x', 'X'],
    ['x', 'x'],
    ['  x ', "'x'"], // a value is looked for without the white space around it: a service may trim it
    ['\tPIN4 ', 'Value "PIN4" was refused'],
    ['x', 'x y'],
    ['x', 'y x'],
    ['x', 'x.'], // punctuation and symbols are not word characters
    ['x', 'x-y'],
    ['x', 'value=x;'],
    ['x', "new_Text and 'x'"], // every occurrence is looked at, not the first
    ['x', "'x' and new_Text"],
    ['x', 'new_Text box x1 and, last of all, x'],
    ['en', 'the en value'],
    ['1', '`pac solution import` failed (exit 1)'],
    ['none', MISMATCH], // a whole word of the SDK's own words: what the rule costs
    ['is', MISMATCH], // …and so is any other word of that sentence, whatever the value is: a word as much as a value
    ['the', MISMATCH],
    ['_', 'a _ b'],
    ['-x-', 'the token a-x-b was refused'], // edges that are not word characters need nothing around them
    ['-x', 'a-x b'], // …and a side whose edge IS a word character needs its neighbour not to be one: here the x is followed by a space
    ['x-', 'a x-b'], // …here the x is preceded by one
    ['e\u0301', 'a (e\u0301) b'],
    ['\u00e9', 'a (\u00e9) b'],
    ['\u00c9', 'a (\u00e9) b'],
    ['\u4e16', 'a \u4e16 b'],
    ['\ud835\udc9c', 'x \ud835\udc9c y'],
    ['PIN42', "Value 'PIN42' was refused"],
    ['PIN42', '[PIN42]'],
    ['PIN42', 'Value \\u0022PIN42\\u0022 was refused'], // a form the decoders read puts the quote beside it
    ['PIN42', 'value=%22PIN42%22'],
    ['PIN42', 'Value &quot;PIN42&quot; was refused'],
    ['PIN42', 'Value &#x22;PIN42&#x22; was refused'],
    ['PIN42', 'a %2522PIN42%2522 b'], // two levels
    ['a  b', 'the pair a b was refused'], // white space collapsed, as for a secret
    ['a  b', 'the pair a\n\nb was refused'],
  ];
  for (const [value, text] of withheld) assert.strictEqual(unknown(value)(text, 'message'), WITHHELD('message'), `${JSON.stringify(value)} in ${JSON.stringify(text)}`);
  // The caller's status line (`lead`) is still kept in front of what is withheld, and a text that does not have the value is not touched.
  const lead = `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: `;
  assert.strictEqual(unknown('x')(`${lead}the value 'x' is not a number`, 'message', lead), `${lead}${WITHHELD('message')}`);
  assert.strictEqual(unknown('x')(`${lead}the value is not a number`, 'message', lead), `${lead}the value is not a number`);
});

test('a short value of an entry the package does not define does not withhold the SDK\'s account of which entry is wrong — the case seen on a real organization — which names the entry, never a value', () => {
  const definitions = packageTypes({ new_Text: TEXT_TYPE });
  for (const value of ['x', 'true', '1', 'en', 'Text', 'qqq', 'new']) {
    const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_Text', Value: '' }, { SchemaName: 'new_NotInPackage', Value: value }] }, definitions);
    assert.strictEqual(sanitize(MISMATCH, 'message'), MISMATCH, `a value of ${JSON.stringify(value)}`);
  }
});

// What the documents say the sanitizer leaves: the skill, AGENTS.md and the CHANGELOG each list it. Every limit they name is checked here against the sanitizer itself, so a document that promises more than
// the rule does — or a rule that changes under a document — is a failing test, and each document is checked to name each limit it should.
test('the limits the skill, AGENTS.md and the CHANGELOG state are the limits the sanitizer has — each is a text that prints a value — and each document names them', () => {
  const sanitizerOf = (Value, types) => ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value }] }, types);
  const unknownOf = (Value) => sanitizerOf(Value, new Map());
  const printed = (sanitize, text, what) => assert.strictEqual(sanitize(text, 'message'), text, `${what}: ${text}`);
  // A value of fewer than six characters that is known not to be a secret: printed wherever a text quotes it.
  printed(sanitizerOf('PIN42', packageTypes({ new_A: TEXT_TYPE })), "Value 'PIN42' was refused", 'a short value the package says is not a secret');
  // A value of fewer than six characters whose type is not known: found only as a whole word, so printed when glued to letters, digits, marks or underscores or inside an escape the decoders do not read.
  for (const [what, text] of [...UNDECODED_TEXTS, ['a digit', 'PIN421 was refused'], ['a mark', 'Value PIN42\u0301 was refused']]) printed(unknownOf('PIN42'), text, `no type, ${what}`);
  // What the decoders do not know, for a secret of any length.
  const secret = 'caf\u00e9-secret-0123';
  const nested = (levels) => Array.from({ length: levels }).reduce((text) => encodeURIComponent(text), secret);
  const known = sanitizerOf(secret, packageTypes({ new_A: SECRET_TYPE }));
  for (const [what, text] of [
    ['a value quoted in part', 'the value caf\u00e9-secret was refused'],
    ['a value hashed', `the digest ${crypto.createHash('sha256').update(secret).digest('hex')} was refused`],
    ['a value base64-encoded', `the value ${Buffer.from(secret).toString('base64')} was refused`],
    ["a value written as an HTML entity beyond XML's five", 'the value caf&eacute;-secret-0123 was refused'],
    ['a value whose Unicode a service normalizes', `the value ${secret.normalize('NFD')} was refused`],
    ['an encoding nested more than three levels deep', `the value ${nested(4)} was refused`],
  ]) printed(known, text, what);
  assert.strictEqual(known(`the value ${nested(3)} was refused`, 'message'), WITHHELD('message'), 'three levels of encoding are within what the decoders read');
  // What the whole-word rule costs: a short value of unknown type that is a whole word of the SDK's own sentence withholds it.
  for (const value of ['none', 'is', 'the']) assert.strictEqual(unknownOf(value)(MISMATCH, 'message'), WITHHELD('message'), value);

  const read = (...parts) => fs.readFileSync(path.join(__dirname, '..', '..', ...parts), 'utf8').replace(/\r\n/g, '\n').replace(/\s+/g, ' ');
  const documents = [
    ['skills/genpage/SKILL.md', read('skills', 'genpage', 'SKILL.md'), [
      /a value of fewer than six characters that is known not to be a secret is not hidden/,
      /is found only as a whole word, so it is printed when glued to letters, digits, marks or underscores or written inside an escape the sanitizer does not decode/,
      /quoted in part, hashed, base64-encoded, written as an HTML entity beyond XML's five, Unicode-normalized or nested in more than three levels of encoding is not recognised/,
    ]],
    // The CHANGELOG names the same limits in fewer words: an entry there holds at most 300 (changelog-brevity.test.js).
    ['CHANGELOG.md', read('CHANGELOG.md'), [
      /values partly quoted, hashed, base64-encoded, Unicode-normalized, nested over three encodings or written with HTML entities beyond XML's five/,
      /non-secret values under six characters/,
      /untyped ones under six glued to letters, digits, marks or underscores or inside an undecoded escape/,
    ]],
    ['AGENTS.md', read('AGENTS.md'), [
      /the SDK's account of which entry is wrong \(it names entries, never values\) stays readable unless the value is itself a whole word of that sentence \(see below\)/,
      /printed when a text quotes it glued to letters, digits, marks or underscores \(`ValuePIN42was`, `key_PIN42`\), or inside an escape the decoders do not read/,
      /a value that is known not to be one is printed wherever it is quoted/,
      /a service that quotes only part of a value, hashes or base64-encodes it, writes it as a named HTML entity beyond XML's five, normalizes its Unicode, or nests the encodings more than three deep/,
      /`none`, `is` or `the` as a value — still withholds that sentence/,
    ]],
  ];
  for (const [name, text, patterns] of documents) for (const pattern of patterns) assert.match(text, pattern, `${name} says ${pattern}`);
});

test('a text that quotes a SECRET is withheld whole — what takes its place says so — and the only thing kept in front of it is the status line the CALLER built from the SDK\'s own fields', () => {
  const sanitize = ss.sanitizer(mixedSettings(), packageTypes({ new_Secret: SECRET_TYPE, new_Text: TEXT_TYPE }));
  const lead = `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: `;
  assert.strictEqual(sanitize(`${lead}value ${SECRET} is not a number; ${PLAIN} too`, 'message', lead), `${lead}${WITHHELD('message')}`);
  assert.strictEqual(sanitize(`the platform said ${SECRET}`, 'warning'), WITHHELD('warning'));
  assert.strictEqual(sanitize(`pac says ${SECRET}`), WITHHELD('text'));
  assert.strictEqual(sanitize(SECRET), WITHHELD('text'));
  // A text that only LOOKS like the SDK's status line is just text: without the caller's lead nothing is kept, and nothing in it is exempt — not the "URL", not the "status".
  assert.strictEqual(sanitize(`${lead}value ${SECRET}`, 'message'), WITHHELD('message'));
  assert.strictEqual(sanitize(`HTTP 400 from https://diagnostic.example/${SECRET}: rejected`, "the job log's text"), WITHHELD("the job log's text"));
  assert.strictEqual(sanitize(`HTTP 400 from ${ENV}/x: ${SECRET}`, 'warning'), WITHHELD('warning'));
  // A lead the text does not begin with is not used (the caller's claim is checked against the text).
  assert.strictEqual(sanitize(`value ${SECRET}`, 'message', lead), WITHHELD('message'));
  assert.strictEqual(sanitize(`${lead.slice(0, -2)}x ${SECRET}`, 'message', lead), WITHHELD('message'));
  // A value that straddles the lead and what follows it is found: the whole text is read, the lead included.
  const straddling = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_S', Value: 'ImportSolution: kv-tail-value' }] }, packageTypes({ new_S: SECRET_TYPE }));
  assert.strictEqual(straddling(`${lead}kv-tail-value was refused`, 'message', lead), `${lead}${WITHHELD('message')}`);
  // A text that quotes only a connection id or a text variable is shown, with each replaced — a lead too, which is shown as the SDK wrote it unless it holds one.
  assert.strictEqual(sanitize(`${lead}no connection ${CONNECTION_ID} and no value ${PLAIN}`, 'message', lead), `${lead}no connection <redacted> and no value <redacted>`);
  assert.strictEqual(sanitize('a text that quotes nothing'), 'a text that quotes nothing');
});

test('a text longer than the sanitizer reads is withheld, not cut — a value that straddled the cut would leave its first half in what is shown — unless there is nothing to hide', () => {
  const sanitize = ss.sanitizer(mixedSettings(), packageTypes({ new_Secret: SECRET_TYPE, new_Text: TEXT_TYPE }));
  const fits = 'x'.repeat(ss.MAX_SANITIZED_CHARS);
  assert.strictEqual(sanitize(fits), fits, 'exactly the limit is read');
  assert.strictEqual(sanitize(`${fits}y`, 'text'), '<text withheld: it is too long to check against the settings file>');
  assert.strictEqual(sanitize(`${fits}${SECRET}`, 'warning'), '<warning withheld: it is too long to check against the settings file>');
  const lead = `HTTP 502 from ${ENV}/api/data/v9.0/ImportSolution: `;
  assert.strictEqual(sanitize(`${lead}${fits}`, 'message', lead), `${lead}<message withheld: it is too long to check against the settings file>`);
  assert.strictEqual(ss.sanitizer({})(`${fits}y`), `${fits}y`, 'a file that fills nothing in has nothing to hide from any text');
});

// The text of a spelling is looked at by DECODING it (JSON string escapes, XML character references, percent-encoding, a form's `+`), never by listing the spellings a value can have.
// The spellings themselves are written out in helpers/value-spellings.js, independently of the code under test, and each is put in a message of its own — one that is recognised
// must not hide one that is not.
const { SPELLINGS, VALUES, spellingsOfValue } = require('./helpers/value-spellings.js');

test('a SECRET is withheld whatever way its text is spelled — JSON, \\uXXXX in either case, XML references, percent-encoding, nested in one another — each spelling in a message of its own', () => {
  let tried = 0;
  for (const [valueName, value] of Object.entries(VALUES)) {
    const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: value }] }, packageTypes({ new_A: SECRET_TYPE }));
    for (const [name, spelled] of spellingsOfValue(value)) {
      tried += 1;
      assert.strictEqual(sanitize(`Value ${spelled} was refused`, 'message'), WITHHELD('message'), `${valueName} as ${name}: ${spelled}`);
    }
  }
  assert.ok(tried >= 40, `the table is not empty (${tried})`);
});

test('a value that is NOT a secret is taken out where a text spells it as it is, and a text that spells it only in another form is withheld — it cannot be taken out of the text it was decoded from', () => {
  for (const [valueName, value] of Object.entries(VALUES)) {
    const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: value }] }, packageTypes({ new_A: TEXT_TYPE }));
    assert.strictEqual(sanitize(`Value ${value} was refused`, 'message'), 'Value <redacted> was refused', valueName);
    for (const [name, spelled] of spellingsOfValue(value)) {
      if (name === 'raw') continue;
      const shown = sanitize(`Value ${spelled} was refused`, 'message');
      assert.ok(shown === WITHHELD('message') || shown === 'Value <redacted> was refused', `${valueName} as ${name}: ${shown}`);
      assert.ok(!shown.includes(spelled), `${valueName} as ${name}: ${shown}`);
    }
  }
  // Raw and encoded in one text: the raw one is replaced, the encoded one cannot be, so the text is withheld.
  const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: VALUES.quote }] }, packageTypes({ new_A: TEXT_TYPE }));
  assert.strictEqual(sanitize(`${VALUES.quote} and ${SPELLINGS.json(VALUES.quote)}`, 'message'), WITHHELD('message'));
  // The same text as one that quotes it only raw: replaced, and the words around it kept.
  assert.strictEqual(sanitize(`${VALUES.quote} and ${VALUES.quote}`, 'message'), '<redacted> and <redacted>');
});

test('\\u0022, the JSON escape of a double quote that a list of spellings is likeliest to miss, and the other legal escapes of one character are each recognised alone', () => {
  const secret = 'cedar-"pine"-token';
  const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: secret }] }, packageTypes({ new_A: SECRET_TYPE }));
  for (const spelled of [
    'cedar-\\u0022pine\\u0022-token', 'cedar-\\u0022pine"-token', 'cedar-"pine\\u0022-token', 'cedar-\\"pine\\"-token', 'cedar-\\x22pine\\x22-token',
    'cedar-&#34;pine&#34;-token', 'cedar-&#x22;pine&#x22;-token', 'cedar-%22pine%22-token', 'cedar-%5C%22pine%5C%22-token',
    '\\u0063edar-"pine"-token', 'cedar\\u002d"pine"-token', // letters and hyphens may be escaped too
  ]) {
    assert.strictEqual(sanitize(`Value "${spelled}" was refused`, 'message'), WITHHELD('message'), spelled);
  }
  assert.strictEqual(sanitize('Value "cedar-pine-token" was refused', 'message'), 'Value "cedar-pine-token" was refused', 'a text that does not repeat the value is not touched');
});

test('what looks like an escape but is not one is left alone, and a text is never changed by being looked at in its decoded forms', () => {
  const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: 'abcdefgh' }] }, packageTypes({ new_A: SECRET_TYPE }));
  const innocent = 'see \\u0041 and %20 and &amp; and C:\\keys\\vault and %zz and &bogus; and &#99999999999; and 100%';
  assert.strictEqual(sanitize(innocent), innocent);
  // A value is not found in a text that only has pieces of it in different forms.
  assert.strictEqual(sanitize('abcd and \\u0065fgh'), 'abcd and \\u0065fgh');
  // A decoded view can bring a value out of an escape that is not a whole one's: `\u0065` is `e`.
  assert.strictEqual(sanitize('abcd\\u0065fgh'), WITHHELD());
});

test('decodedViews: a text, then each form the decoders make of it and of what they made — JSON string escapes, XML references, percent-encoding, `+` as a space — a few levels deep, each once', () => {
  const views = ss.decodedViews;
  assert.deepStrictEqual(views('plain text'), ['plain text']);
  assert.deepStrictEqual(views(''), ['']);
  const decodes = (encoded, decoded) => assert.ok(views(encoded).includes(decoded), `${encoded} -> ${decoded}: ${JSON.stringify(views(encoded))}`);
  // JSON: the two-character escapes (RFC 8259 §7), \uXXXX in either case, a surrogate pair, and the non-JSON \' and \xHH.
  for (const [encoded, decoded] of [
    ['\\"', '"'], ['\\\\', '\\'], ['\\/', '/'], ['\\b', '\b'], ['\\f', '\f'], ['\\n', '\n'], ['\\r', '\r'], ['\\t', '\t'], ["\\'", "'"],
    ['\\u0022', '"'], ['\\u003c', '<'], ['\\u003C', '<'], ['\\u00e9', '\u00e9'], ['\\u00E9', '\u00e9'], ['\\ud83d\\ude80', '\ud83d\ude80'], ['\\x41', 'A'],
  ]) decodes(`a${encoded}b`, `a${decoded}b`);
  // XML: the five named references, and decimal and hexadecimal numeric ones.
  for (const [encoded, decoded] of [['&quot;', '"'], ['&lt;', '<'], ['&gt;', '>'], ['&amp;', '&'], ['&apos;', "'"], ['&#34;', '"'], ['&#x22;', '"'], ['&#X22;', '"'], ['&#233;', '\u00e9'], ['&#xE9;', '\u00e9'], ['&#128640;', '\ud83d\ude80']]) decodes(`a${encoded}b`, `a${decoded}b`);
  // Percent-encoding: either hex case, multi-byte UTF-8, a run that is not valid UTF-8 decoded as far as it is valid, and a form's `+`.
  for (const [encoded, decoded] of [['%22', '"'], ['%2f', '/'], ['%2F', '/'], ['%C3%A9', '\u00e9'], ['%e4%b8%96', '\u4e16'], ['%F0%9F%9A%80', '\ud83d\ude80'], ['%FF%22', '\ufffd"'], ['+', ' ']]) decodes(`a${encoded}b`, `a${decoded}b`);
  // One inside another: two and three levels.
  decodes('a%2522b', 'a"b');
  decodes('a\\\\u0022b', 'a"b');
  decodes('a&amp;quot;b', 'a"b');
  decodes('a%5C%22b', 'a"b');
  decodes('a\\u0025\\u0032\\u0032b', 'a"b');
  decodes('a%252522b', 'a"b'); // three levels of percent
  decodes('a%26amp%3Bquot%3Bb', 'a"b'); // percent of XML of XML
  decodes('a\\u0022b%22c&quot;d', 'a"b"c"d'); // different spellings in one text
  // What is not an escape stays as it is — a path's backslashes, a stray `%`, a made-up entity — and an escape that is not a character is not made into one.
  for (const text of ['C:\\keys\\vault', '%zz', '%2', '100%', '&bogus;', '&#99999999999;', '&#xZZ;', '\\u12', '\\q']) assert.ok(views(text).includes(text), text);
  assert.ok(!views('a&#99999999999;b').some((view) => view.includes('\ufffd')), 'a number that is no character is not replaced');
  // Each view once, and few: every decoder at every level is at most 1 + 4 + 16 + 64 views, and a pathological text cannot make more.
  const heap = `${'\\u0025'.repeat(50)}25%2525&amp;amp;\\\\\\\\\\\\ ${'+%'.repeat(100)}`;
  const many = views(heap);
  assert.strictEqual(new Set(many).size, many.length);
  assert.ok(many.length <= 100, String(many.length));
  assert.strictEqual(many[0], heap, 'the text itself is the first view');
});

test('a secret with white space in it is found whatever white space the text holds — and a text that has collapsed it', () => {
  const secret = 'Cedar  Pine\tSecret42\nline';
  const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: secret }] }, packageTypes({ new_A: SECRET_TYPE }));
  for (const text of [
    `Value '${secret}' was refused`,
    "Value 'Cedar Pine Secret42 line' was refused", // runs of white space made one by a service that joins the lines of what it quotes
    "Value 'Cedar    Pine   Secret42   line' was refused",
    "Value 'Cedar \t Pine\r\nSecret42\r\nline' was refused",
  ]) assert.strictEqual(sanitize(text, 'message'), WITHHELD('message'), JSON.stringify(text));
  assert.strictEqual(sanitize("Value 'Cedar Pine' was refused", 'message'), "Value 'Cedar Pine' was refused", 'a piece of it is not it');
  // The same, in an encoded text.
  assert.strictEqual(sanitize('Value Cedar%20%20Pine%09Secret42%0Aline', 'message'), WITHHELD('message'));
  // A value that is not a secret, with white space in it: replaced where it is spelled as it is; a text that collapsed it is withheld.
  const plain = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_A', Value: 'Cedar  Pine Secret42' }] }, packageTypes({ new_A: TEXT_TYPE }));
  assert.strictEqual(plain("Value 'Cedar  Pine Secret42' was refused", 'message'), "Value '<redacted>' was refused");
  assert.strictEqual(plain("Value 'Cedar Pine Secret42' was refused", 'message'), WITHHELD('message'));
});

test('the status line the SDK writes is built from the error\'s own statusCode and url — never found by looking at how a text begins', () => {
  const { ConnectionError } = require('../lib/maker-sdk.js');
  const url = `${ENV}/api/data/v9.0/ImportSolution`;
  assert.strictEqual(ss.sdkLead(new ConnectionError(url, 400, 'bad')), `HTTP 400 from ${url}: `);
  assert.strictEqual(ss.sdkLead(new ConnectionError(url, 504, 'bad')), `HTTP 504 from ${url}: `);
  assert.strictEqual(ss.sdkLead(new ConnectionError(url, 0, 'no token')), `Transport failure reaching ${url}: `);
  // The message must really begin with it. One that does not (another error, a rebuilt message) has no lead: nothing is kept.
  assert.strictEqual(ss.sdkLead(Object.assign(new Error('something else'), { url, statusCode: 400 })), '');
  assert.strictEqual(ss.sdkLead(Object.assign(new Error(`HTTP 400 from ${url}x: bad`), { url, statusCode: 400 })), '');
  assert.strictEqual(ss.sdkLead(Object.assign(new Error(`HTTP 401 from ${url}: bad`), { url, statusCode: 400 })), '', 'the status of the error, not the one the text claims');
  assert.strictEqual(ss.sdkLead(Object.assign(new Error(`HTTP 400 from ${ENV}/other: bad`), { url, statusCode: 400 })), '', 'the url of the error, not the one the text claims');
  // An error without its fields — a plain one, one from pac, a string — has none, whatever its text says; nor has anything that is not an Error.
  const lookalike = `HTTP 400 from ${url}: bad`;
  for (const error of [null, undefined, lookalike, new Error(lookalike), Object.assign(new Error(lookalike), { statusCode: 400 }), Object.assign(new Error(lookalike), { url }),
    Object.assign(new Error(lookalike), { url, statusCode: '400' }), Object.assign(new Error(lookalike), { url: 42, statusCode: 400 }), { message: lookalike, url, statusCode: 400 }]) {
    assert.strictEqual(ss.sdkLead(error), '', String(error));
  }
});

test('case does not matter: a secret, a value that is not one and a connection id are found in a text that normalises their case — also inside an encoding', () => {
  const guid = '00000000-AAAA-BBBB-CCCC-0123456789AB';
  const sanitize = ss.sanitizer({
    EnvironmentVariables: [{ SchemaName: 'new_S', Value: 'AbCdEf-Secret-123' }, { SchemaName: 'new_T', Value: 'Production-Key-001' }],
    ConnectionReferences: [{ LogicalName: 'new_C', ConnectionId: `shared-sql-${guid}` }],
  }, packageTypes({ new_S: SECRET_TYPE, new_T: TEXT_TYPE }));
  assert.strictEqual(sanitize('Value abcdef-secret-123 refused', 'message'), WITHHELD('message'));
  assert.strictEqual(sanitize('Value ABCDEF-SECRET-123 refused', 'message'), WITHHELD('message'));
  assert.strictEqual(sanitize('Value production-key-001 and PRODUCTION-KEY-001 and Production-Key-001', 'message'), 'Value <redacted> and <redacted> and <redacted>');
  assert.strictEqual(sanitize(`no connection shared-sql-${guid.toLowerCase()} and SHARED-SQL-${guid}`, 'message'), 'no connection <redacted> and <redacted>');
  // Inside an encoding: a secret withholds the text, a value that is not one cannot be taken out of the text it was decoded from.
  assert.strictEqual(sanitize('Value abcdef%2dsecret%2d123 refused', 'message'), WITHHELD('message'));
  assert.strictEqual(sanitize('Value production%2Dkey%2D001 refused', 'message'), WITHHELD('message'));
  // A text that has none of them, in any case, is left alone.
  assert.strictEqual(sanitize('Value abcdef-secret refused', 'message'), 'Value abcdef-secret refused');
});

test('a value of white space alone, or empty, has nothing in it to hide — whatever its type — so the sanitizer leaves every text as it is', () => {
  for (const Value of ['', ' ', '   ', '\n', '\t \n']) {
    const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'a', Value, TypeId: 100000005 }], ConnectionReferences: [{ LogicalName: 'r', ConnectionId: '' }] });
    assert.strictEqual(sanitize('HTTP 400 from https://contoso.crm.dynamics.com/x: a message with spaces\nand a line break'), 'HTTP 400 from https://contoso.crm.dynamics.com/x: a message with spaces\nand a line break', JSON.stringify(Value));
  }
});

// --- the SDK's refusals --------------------------------------------------------------------------------------------------------------

test('template falls back to pac only for the two staging refusals', () => {
  for (const code of ['SOLUTION_STAGING_FAILED', 'SOLUTION_STAGING_UNREADABLE']) {
    assert.deepStrictEqual(ss.templateRefusal(refusal(code, 'words')), { code, message: 'words' }, code);
  }
  for (const code of ['INVALID_ARGUMENT', 'CONNECTION_ERROR', 'SOLUTION_NOT_FOUND', 'CONNECTIVITY_ROUTE_DECOMMISSIONED', 'VERSION_CONFLICT']) {
    assert.strictEqual(ss.templateRefusal(refusal(code)), null, code);
  }
  assert.strictEqual(ss.templateRefusal(new Error('plain')), null);
  assert.strictEqual(ss.templateRefusal(null), null);
});

const featureSettings = (extra = {}) => template(extra);
// The SDK's own sentence for a feature of pac's that the file holds (api/SolutionApi.ts readOverrides) — what the real bundle throws, pinned in solution-settings-real-bundle.test.js.
const listSentence = (key) => `importSolution: deploymentSettings is not a deployment-settings file this SDK applies — deploymentSettings.${key} is a pac feature this SDK does not apply: only an empty list is accepted. Nothing was imported.`;
const instanceSentence = (index) => `importSolution: deploymentSettings is not a deployment-settings file this SDK applies — deploymentSettings.ConnectionReferences[${index}].ConnectionInstance is pac's connection-instance feature, which this SDK does not apply. Nothing was imported.`;

test('import falls back to pac for an unreadable staging and for a settings feature the FILE holds and the SDK does not apply — never for a package that did not stage, nor any other failure', () => {
  assert.deepStrictEqual(ss.importRefusal(refusal('SOLUTION_STAGING_UNREADABLE', 'w'), template()), { code: 'SOLUTION_STAGING_UNREADABLE', message: 'w' });
  const withWorkflows = featureSettings({ Workflows: [{ WorkflowId: 'x' }] });
  const feature = refusal('INVALID_ARGUMENT', listSentence('Workflows'));
  assert.deepStrictEqual(ss.importRefusal(feature, withWorkflows), { code: 'INVALID_ARGUMENT', message: feature.message });
  assert.ok(ss.importRefusal(refusal('INVALID_ARGUMENT', listSentence('CanvasApps')), featureSettings({ CanvasApps: [{ Name: 'x' }] })));
  assert.ok(ss.importRefusal(refusal('INVALID_ARGUMENT', listSentence('CopilotAgents')), featureSettings({ CopilotAgents: [{ Name: 'x' }] })));
  const instance = template({ ConnectionReferences: [{ LogicalName: 'new_a', ConnectionId: '' }, { LogicalName: 'new_b', ConnectionId: '', ConnectionInstance: { x: 1 } }] });
  assert.ok(ss.importRefusal(refusal('INVALID_ARGUMENT', instanceSentence(1)), instance));
  // The platform said the package cannot go in here: pac would only try the same import.
  assert.strictEqual(ss.importRefusal(refusal('SOLUTION_STAGING_FAILED'), template()), null);
  // An argument error that is the caller's to fix is not a feature the SDK lacks.
  assert.strictEqual(ss.importRefusal(refusal('INVALID_ARGUMENT', 'importSolution: deploymentSettings does not match the package — EnvironmentVariables: \'x\' is not a variable of the package.'), withWorkflows), null);
  assert.strictEqual(ss.importRefusal(refusal('INVALID_ARGUMENT', '…deploymentSettings.Other is not part of the format (EnvironmentVariables, ConnectionReferences; Workflows, CanvasApps and CopilotAgents only empty; $schema)'), withWorkflows), null);
  for (const code of ['CONNECTION_ERROR', 'VERSION_CONFLICT', 'SOLUTION_STAGING_X']) assert.strictEqual(ss.importRefusal(refusal(code, listSentence('Workflows')), withWorkflows), null, code);
  assert.strictEqual(ss.importRefusal(null, template()), null);
});

test('whether pac is asked is decided from the FILE: its feature, and the SDK\'s sentence for exactly that feature — never from a phrase input can put in a message', () => {
  // The sentences the SDK refuses a file with, one per feature the file holds: a non-empty list, a ConnectionInstance.
  assert.deepStrictEqual(ss.pacOnlyFeatureMessages(template()), [], 'pac\'s own template — empty lists, no instance — holds none');
  assert.deepStrictEqual(ss.pacOnlyFeatureMessages(template({ Workflows: [], CanvasApps: [], CopilotAgents: [] })), [], 'an EMPTY list is not a feature: pac writes CopilotAgents: [] into a template');
  assert.deepStrictEqual(ss.pacOnlyFeatureMessages(template({ Workflows: [{ a: 1 }], CopilotAgents: [1] })), [listSentence('Workflows'), listSentence('CopilotAgents')]);
  assert.deepStrictEqual(ss.pacOnlyFeatureMessages(template({ ConnectionReferences: [{ LogicalName: 'a', ConnectionId: '', ConnectionInstance: 1 }] })), [instanceSentence(0)]);
  // Not a list at all (the SDK's sentence says "only an empty list is accepted" for it too) is a faulty file, not a feature pac has.
  for (const bad of [5, null, 'x', {}, true]) assert.deepStrictEqual(ss.pacOnlyFeatureMessages(template({ Workflows: bad })), [], JSON.stringify(bad));
  assert.deepStrictEqual(ss.pacOnlyFeatureMessages(null), []);
  assert.deepStrictEqual(ss.pacOnlyFeatureMessages({ ConnectionReferences: [null, 5, 'x'] }), []);

  // The phrase "this SDK does not apply" in a key or a name of an ORDINARY invalid file — the SDK quotes it in its normal complaint — is not a feature, and pac is not asked.
  const phrase = 'this SDK does not apply';
  const quoting = [
    [template({ [phrase]: true }), `importSolution: deploymentSettings is not a deployment-settings file this SDK applies — deploymentSettings.${phrase} is not part of the format (EnvironmentVariables, ConnectionReferences; Workflows, CanvasApps and CopilotAgents only empty; $schema). Nothing was imported.`],
    [template({ EnvironmentVariables: [{ SchemaName: 'new_a', Value: '', [phrase]: 1 }] }), `importSolution: deploymentSettings is not a deployment-settings file this SDK applies — deploymentSettings.EnvironmentVariables[0].${phrase} is not part of a variable (SchemaName, Value; DefaultValue, TypeId, IsRequired, Name). Nothing was imported.`],
    [template({ ConnectionReferences: [{ LogicalName: 'a', ConnectionId: '', [`ConnectionInstance is pac's connection-instance feature, which ${phrase}`]: 1 }] }), `…deploymentSettings.ConnectionReferences[0].x is pac's connection-instance feature, which ${phrase}. Nothing was imported.`],
    [template({ Other: 1 }), listSentence('Workflows')], // the SDK's sentence, but the file holds no such feature
    [template({ Workflows: [{ a: 1 }] }), `${listSentence('Workflows')} (and more)`], // a feature, but not the SDK's sentence for it
    [template({ Workflows: [{ a: 1 }] }), listSentence('CanvasApps')], // a feature, but another one's sentence
  ];
  for (const [settings, message] of quoting) assert.strictEqual(ss.importRefusal(refusal('INVALID_ARGUMENT', message), settings), null, message);
});

// --- the client for the long-running actions ------------------------------------------------------------------------------------------

function clientWith(answers) {
  const sent = [];
  const client = ss.solutionHttpClient(ENV, {
    getToken: () => 'dataverse-token',
    sleep: async () => {},
    request: async (options) => { sent.push(options); const next = answers.shift(); return typeof next === 'function' ? next(options) : next; },
  });
  return { client, sent };
}
const ok = (body = {}) => ({ statusCode: 200, headers: {}, body: JSON.stringify(body) });

test('ExportSolution, StageSolution and ImportSolution are given an hour to answer; any other request keeps the client\'s own limits', async () => {
  const { client, sent } = clientWith([ok(), ok(), ok(), ok(), ok()]);
  await client.post(`${ENV}/api/data/v9.0/ExportSolution`, { SolutionName: 'x' });
  await client.post(`${ENV}/api/data/v9.0/StageSolution`, { CustomizationFile: 'x' });
  await client.post(`${ENV}/api/data/v9.0/ImportSolution`, { ImportJobId: JOB });
  await client.post(`${ENV}/api/data/v9.0/appmodules`, {});
  await client.get(`${ENV}/api/data/v9.0/RetrieveCurrentOrganization()`);
  assert.deepStrictEqual(sent.map((s) => s.timeout), [3600000, 3600000, 3600000, 300000, 60000]);
});

test('a long action that gets no answer is never sent again, and an import names its job so the outcome can be looked up', async () => {
  const noAnswer = { error: 'Request timed out' };
  for (const [action, body] of [['ExportSolution', { SolutionName: 'x' }], ['StageSolution', { CustomizationFile: 'x' }], ['ImportSolution', { ImportJobId: JOB, CustomizationFile: 'x' }]]) {
    const { client, sent } = clientWith([noAnswer, ok(), ok(), ok(), ok(), ok(), ok()]);
    await assert.rejects(client.post(`${ENV}/api/data/v9.0/${action}`, body), (err) => {
      assert.match(err.message, new RegExp(`^${action} got no answer \\(Request timed out\\)\\. It was not sent again`));
      if (action === 'ImportSolution') assert.match(err.message, new RegExp(`import job ${JOB} \\(importjobs\\(${JOB}\\)`));
      else assert.doesNotMatch(err.message, /import job/);
      return true;
    }, action);
    assert.strictEqual(sent.length, 1, `${action}: one request, not the six attempts the plain client makes for a POST that got no answer`);
  }
});

test('another POST that gets no answer is still sent again by the plain client — the policy is the three actions\'', async () => {
  const { client, sent } = clientWith([{ error: 'Request timed out' }, ok({ done: true })]);
  const res = await client.post(`${ENV}/api/data/v9.0/appmodules`, {});
  assert.strictEqual(res.status, 200);
  assert.strictEqual(sent.length, 2);
});

test('a long action that is answered — even with an error status — is returned as the SDK reads it', async () => {
  const { client } = clientWith([{ statusCode: 400, headers: {}, body: JSON.stringify({ error: { message: 'bad' } }) }]);
  const res = await client.post(`${ENV}/api/data/v9.0/ImportSolution`, { ImportJobId: JOB });
  assert.deepStrictEqual([res.status, res.body.error.message], [400, 'bad']);
});

test('a long action is sent EXACTLY ONCE whatever answers it — 500, 502, 503, 504, 429 and 401 each come back as the first answer, with its status and its typed code, and nothing is sent again or refreshed', async () => {
  const actions = [['ExportSolution', { SolutionName: 'x' }], ['StageSolution', { CustomizationFile: 'x' }], ['ImportSolution', { ImportJobId: JOB, CustomizationFile: 'x' }]];
  for (const status of [500, 502, 503, 504, 429, 401]) {
    for (const [action, body] of actions) {
      const tokenReads = [];
      const sent = [];
      const client = ss.solutionHttpClient(ENV, {
        getToken: (env, options) => { tokenReads.push(options); return 'dataverse-token'; },
        sleep: async () => {},
        // The platform ACCEPTED the request (it is in `sent`) and a gateway answered that it failed; a second send would be answered 200.
        request: async (options) => {
          sent.push(options);
          return sent.length === 1
            ? { statusCode: status, headers: { 'retry-after': '1' }, body: JSON.stringify({ error: { code: '0x80048d19', message: `status ${status}` } }) }
            : { statusCode: 200, headers: {}, body: '{}' };
        },
      });
      const res = await client.post(`${ENV}/api/data/v9.0/${action}`, body);
      assert.strictEqual(sent.length, 1, `${action} after ${status}: one send, not the client's usual six`);
      assert.deepStrictEqual([res.status, res.body.error.code, res.body.error.message], [status, '0x80048d19', `status ${status}`], `${action} after ${status}: the answer reaches the SDK as it came`);
      assert.strictEqual(tokenReads.length, 1, `${action} after ${status}: the token is read once — a 401 is not answered by a refresh and a second send`);
    }
  }
});

test('the policy is the three actions\': any other POST that is answered with a transient status is still sent again, and a 401 on one refreshes the token', async () => {
  const sent = [];
  const reads = [];
  const client = ss.solutionHttpClient(ENV, {
    getToken: (env, options) => { reads.push(options); return 'dataverse-token'; },
    sleep: async () => {},
    request: async (options) => { sent.push(options); return sent.length < 3 ? { statusCode: sent.length === 1 ? 503 : 401, headers: {}, body: '' } : { statusCode: 200, headers: {}, body: '{"done":true}' }; },
  });
  const res = await client.post(`${ENV}/api/data/v9.0/appmodules`, {});
  assert.strictEqual(res.status, 200);
  assert.strictEqual(sent.length, 3, 'a 503, then a 401, then the answer');
  assert.deepStrictEqual(reads.map((options) => Boolean(options && options.fresh)), [false, true], 'the 401 asked for a fresh token');
  // A GET of the same path as an action is not one of them either: the policy is a POST of one of the three.
  const read = [];
  const getter = ss.solutionHttpClient(ENV, { getToken: () => 't', sleep: async () => {}, request: async (options) => { read.push(options); return read.length === 1 ? { statusCode: 503, headers: {}, body: '' } : { statusCode: 200, headers: {}, body: '{}' }; } });
  await getter.get(`${ENV}/api/data/v9.0/ImportSolution`);
  assert.strictEqual(read.length, 2);
});

// --- what the package says its variables are --------------------------------------------------------------------------------------------

// StageSolution's reply, as the platform shapes it (measured): each component's `Attributes` is { Count, Keys, Values }.
const stageReply = (components) => ({ StageSolutionResults: { StageSolutionStatus: 'Passed', StageSolutionUploadId: JOB, SolutionComponentsDetails: components } });
const definitionOf = (attributes, ComponentTypeName = 'EnvironmentVariableDefinition') => {
  const Keys = Object.keys(attributes);
  return { ComponentTypeName, Attributes: { Count: Keys.length, Keys, Values: Keys.map((key) => attributes[key]) } };
};
// What the SDK's staging of a package leaves in an import — the type of each variable its definitions give (`types`: schema name -> type) — for a test that stands the SDK in:
// `http` answers StageSolution with that reply, and `sdkFirst` does what the real one does with the plan's `httpClient` — the SDK stages the package through it before it imports.
function stagedAs(types, sdk) {
  const body = JSON.stringify(stageReply(Object.entries(types).map(([schemaname, type]) => definitionOf({ schemaname, type: String(type) }))));
  return {
    http: { getToken: () => 'dataverse-token', sleep: async () => {}, request: async () => ({ statusCode: 200, headers: {}, body }) },
    sdkFirst: async (env, plan) => {
      await plan.httpClient(env).post(`${env}/api/data/v9.0/StageSolution`, { CustomizationFile: 'x' });
      return stubSdkFirst(sdk)(env, plan);
    },
  };
}

test('the types of the package\'s own variable definitions are read from the StageSolution reply — by lower-cased schema name, a number or a decimal string — and what cannot be read is not recorded', () => {
  const definitions = new Map();
  ss.recordStagedTypes(definitions, JSON.stringify(stageReply([
    definitionOf({ schemaname: 'New_Secret', type: '100000005', defaultvalue: '' }),
    definitionOf({ schemaname: 'new_Text', type: 100000000 }),
    definitionOf({ SchemaName: 'new_Cased', Type: '100000002' }), // the case of a key is not relied on
    definitionOf({ schemaname: 'new_NoType' }), // no type: not recorded
    definitionOf({ schemaname: 'new_BadType', type: 'text' }),
    definitionOf({ schemaname: 'new_Float', type: 1.5 }),
    definitionOf({ connectionreferencelogicalname: 'new_Reference', schemaname: 'new_FromAReference', type: '100000005' }, 'ConnectionReference'), // another kind of component
    definitionOf({ schemaname: 'new_Other', type: '100000005' }, 'Entity'),
    { ComponentTypeName: 'EnvironmentVariableDefinition' }, // no attributes
    { ComponentTypeName: 'EnvironmentVariableDefinition', Attributes: { Keys: ['schemaname', 'type'], Values: ['new_Short'] } }, // keys and values differ in number
    { ComponentTypeName: 'EnvironmentVariableDefinition', Attributes: { Keys: ['schemaname', 'type'], Values: ['new_Long', '100000005', 'extra'] } }, // ...either way
    { ComponentTypeName: 7, Attributes: {} },
    null,
    definitionOf({ schemaname: '', type: '100000005' }),
    definitionOf({ schemaname: 42, type: '100000005' }),
  ])));
  assert.deepStrictEqual([...definitions], [['new_secret', 100000005], ['new_text', 100000000], ['new_cased', 100000002]]);
  // A status that is not "Passed" still lists what the package holds.
  const failed = new Map();
  ss.recordStagedTypes(failed, { StageSolutionResults: { StageSolutionStatus: 'Failed', SolutionComponentsDetails: [definitionOf({ schemaname: 'a', type: '100000005' })] } });
  assert.deepStrictEqual([...failed], [['a', 100000005]]);
  // Anything else is nothing: no throw, nothing recorded.
  for (const body of [undefined, null, '', 'not json', '[]', '{}', '{"StageSolutionResults":null}', '{"StageSolutionResults":{"SolutionComponentsDetails":"x"}}', 42, [], { StageSolutionResults: 'x' }]) {
    const none = new Map();
    assert.doesNotThrow(() => ss.recordStagedTypes(none, body), String(body));
    assert.strictEqual(none.size, 0, String(body));
  }
});

test('evidence only RAISES secrecy: a name the reply lists twice keeps the most secret type, and when the types differ and none is Secret nothing is known of it', () => {
  const stage = (...definitions) => stageReply(definitions.map(([schemaname, type]) => definitionOf(type === undefined ? { schemaname } : { schemaname, type: String(type) })));
  const NUMBER_TYPE = 100000001;
  const recorded = (...replies) => {
    const definitions = new Map();
    for (const reply of replies) ss.recordStagedTypes(definitions, JSON.stringify(reply));
    return [...definitions];
  };
  // Secret wins, in either order, however many times the name is listed, and in any case of the name.
  assert.deepStrictEqual(recorded(stage(['new_Secret', SECRET_TYPE], ['new_Secret', TEXT_TYPE])), [['new_secret', SECRET_TYPE]]);
  assert.deepStrictEqual(recorded(stage(['new_Secret', TEXT_TYPE], ['new_Secret', SECRET_TYPE])), [['new_secret', SECRET_TYPE]]);
  assert.deepStrictEqual(recorded(stage(['new_Secret', TEXT_TYPE], ['NEW_SECRET', NUMBER_TYPE], ['new_secret', SECRET_TYPE])), [['new_secret', SECRET_TYPE]]);
  assert.deepStrictEqual(recorded(stage(['new_Secret', SECRET_TYPE], ['new_Secret', TEXT_TYPE], ['new_Secret', NUMBER_TYPE])), [['new_secret', SECRET_TYPE]]);
  // The same type twice is that type; types that differ with no Secret among them are no type — kept in the map as "not known", which reads as no evidence.
  assert.deepStrictEqual(recorded(stage(['new_Text', TEXT_TYPE], ['new_Text', TEXT_TYPE])), [['new_text', TEXT_TYPE]]);
  assert.deepStrictEqual(recorded(stage(['new_Text', TEXT_TYPE], ['new_Text', NUMBER_TYPE])), [['new_text', undefined]]);
  assert.deepStrictEqual(recorded(stage(['new_Text', TEXT_TYPE], ['new_Text', NUMBER_TYPE], ['new_Text', TEXT_TYPE])), [['new_text', undefined]], 'a third listing does not undo the disagreement');
  // A listing whose type cannot be read disagrees with one that can; one listing that cannot be read records nothing, as before.
  assert.deepStrictEqual(recorded(stage(['new_Text', TEXT_TYPE], ['new_Text', undefined])), [['new_text', undefined]]);
  assert.deepStrictEqual(recorded(stage(['new_Text', undefined], ['new_Text', TEXT_TYPE])), [['new_text', undefined]]);
  assert.deepStrictEqual(recorded(stage(['new_Text', undefined])), []);
  assert.deepStrictEqual(recorded(stage(['new_Text', undefined], ['new_Text', SECRET_TYPE])), [['new_text', SECRET_TYPE]]);
  // The same holds across replies: what an earlier one said is evidence too.
  assert.deepStrictEqual(recorded(stage(['new_Text', TEXT_TYPE]), stage(['new_Text', NUMBER_TYPE])), [['new_text', undefined]]);
  assert.deepStrictEqual(recorded(stage(['new_Text', TEXT_TYPE], ['new_Text', NUMBER_TYPE]), stage(['new_Text', TEXT_TYPE])), [['new_text', undefined]], 'a later, clean listing does not undo it');
  assert.deepStrictEqual(recorded(stage(['new_Text', TEXT_TYPE], ['new_Text', NUMBER_TYPE]), stage(['new_Text', SECRET_TYPE])), [['new_text', SECRET_TYPE]]);
  assert.deepStrictEqual(recorded(stage(['new_Text', TEXT_TYPE]), stage(['new_Text', TEXT_TYPE])), [['new_text', TEXT_TYPE]]);
  // Names that are listed once are untouched by another's listings.
  assert.deepStrictEqual(recorded(stage(['new_A', TEXT_TYPE], ['new_B', SECRET_TYPE], ['new_B', TEXT_TYPE], ['new_C', NUMBER_TYPE])), [['new_a', TEXT_TYPE], ['new_b', SECRET_TYPE], ['new_c', NUMBER_TYPE]]);
  // What that does to a value: Secret wins, so a five-character value is found glued to words, as a secret's is; not known is a whole-word match, as for a name the package does not define.
  const settings = { EnvironmentVariables: [{ SchemaName: 'new_Secret', Value: 'PIN42' }, { SchemaName: 'new_Text', Value: 'en' }] };
  const definitions = new Map();
  ss.recordStagedTypes(definitions, JSON.stringify(stage(['new_Secret', SECRET_TYPE], ['new_Secret', TEXT_TYPE], ['new_Text', TEXT_TYPE], ['new_Text', NUMBER_TYPE])));
  const sanitize = ss.sanitizer(settings, definitions);
  assert.strictEqual(sanitize('ValuePIN42wasRefused', 'message'), WITHHELD('message'), 'Secret won');
  assert.strictEqual(sanitize('deployment', 'message'), 'deployment', 'no type: a whole-word match, so "en" in "deployment" is shown');
  assert.strictEqual(sanitize('the en value', 'message'), WITHHELD('message'), '...and "en" as a word is withheld');
});

test('the client tells what StageSolution answered — only that, and only an answer — before the SDK reads it, and passes the answer on untouched', async () => {
  const body = JSON.stringify(stageReply([definitionOf({ schemaname: 'new_Secret', type: '100000005' })]));
  const heard = [];
  const answers = [ok(), { statusCode: 200, headers: {}, body }, ok(), { error: 'Request timed out' }, { statusCode: 503, headers: {}, body: '{"error":{"message":"x"}}' }];
  const client = ss.solutionHttpClient(ENV, {
    getToken: () => 'dataverse-token',
    sleep: async () => {},
    request: async () => answers.shift(),
  }, { onStageReply: (text) => heard.push(text) });
  await client.post(`${ENV}/api/data/v9.0/ExportSolution`, {});
  const res = await client.post(`${ENV}/api/data/v9.0/StageSolution`, { CustomizationFile: 'x' });
  await client.post(`${ENV}/api/data/v9.0/ImportSolution`, { ImportJobId: JOB });
  await assert.rejects(client.post(`${ENV}/api/data/v9.0/StageSolution`, {}), /StageSolution got no answer/);
  const refused = await client.post(`${ENV}/api/data/v9.0/StageSolution`, {});
  assert.deepStrictEqual(heard, [body, '{"error":{"message":"x"}}'], 'the two StageSolution answers that arrived: not the export, not the import, not the one that never came');
  assert.deepStrictEqual(res.body, JSON.parse(body), 'the SDK gets the answer as it came');
  assert.strictEqual(refused.status, 503);
  // Without a listener — every other caller — the client is as it was.
  const plain = ss.solutionHttpClient(ENV, { getToken: () => 't', sleep: async () => {}, request: async () => ({ statusCode: 200, headers: {}, body }) });
  assert.deepStrictEqual((await plain.post(`${ENV}/api/data/v9.0/StageSolution`, {})).body, JSON.parse(body));
});

// --- template -----------------------------------------------------------------------------------------------------------------------

function sdkStub({ exported = 'RVhQT1JURUQ=', result = { settings: template() } } = {}) {
  const calls = [];
  return {
    calls,
    sdk: {
      exportSolution: async (options) => { calls.push(['exportSolution', options]); return { base64: exported }; },
      getDeploymentSettingsTemplate: async (options) => { calls.push(['getDeploymentSettingsTemplate', options]); return result; },
      importSolution: async (options) => { calls.push(['importSolution', options]); return { appliedSettings: { environmentVariables: ['new_Url'], connectionReferences: ['new_Dataverse'] } }; },
    },
  };
}
const templateRequest = (extra) => ({ command: 'template', env: ENV, force: false, ...extra });

test('template --solution: the solution is exported unmanaged from --env, its package staged for the template, and the file written as pac\'s format', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  const { sdk, calls } = sdkStub();
  const record = [];
  const result = await ss.runTemplate(templateRequest({ solution: 'ContosoDemo', outPath: out }), { sdkFirst: stubSdkFirst(sdk, record) });
  assert.deepStrictEqual(result, { source: 'sdk', out, environmentVariables: 1, connectionReferences: 1 });
  assert.deepStrictEqual(calls, [
    ['exportSolution', { solutionName: 'ContosoDemo', managed: false }],
    ['getDeploymentSettingsTemplate', { base64: 'RVhQT1JURUQ=' }],
  ]);
  assert.strictEqual(record[0].env, ENV);
  const text = fs.readFileSync(out, 'utf8');
  assert.strictEqual(text, `${JSON.stringify(template(), null, 2)}\n`);
  assert.deepStrictEqual(JSON.parse(text), template());
  assert.strictEqual(typeof record[0].plan.httpClient, 'function', 'the org client is the long-action one');
});

test('template --package --env: the zip is staged as it is on disk (base64 of its bytes), and nothing is exported', async () => {
  const dir = tempDir();
  const { pkg, out } = files(dir);
  const { sdk, calls } = sdkStub();
  const result = await ss.runTemplate(templateRequest({ packagePath: pkg, outPath: out }), { sdkFirst: stubSdkFirst(sdk) });
  assert.strictEqual(result.source, 'sdk');
  assert.deepStrictEqual(calls, [['getDeploymentSettingsTemplate', { base64: ZIP.toString('base64') }]]);
});

test('warnings the SDK gives (a staged copy it could not delete) are carried to the output', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  const { sdk } = sdkStub({ result: { settings: template(), warnings: ['The staged copy of the package (stagesolutionupload x) could not be deleted'] } });
  const result = await ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out }), { sdkFirst: stubSdkFirst(sdk) });
  assert.deepStrictEqual(result.warnings, ['The staged copy of the package (stagesolutionupload x) could not be deleted']);
});

test('an existing --out is not replaced unless --force is given — and the refusal comes before anything is asked of the environment', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  fs.writeFileSync(out, '{"mine":"filled in"}');
  const { sdk, calls } = sdkStub();
  const record = [];
  await assert.rejects(ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out }), { sdkFirst: stubSdkFirst(sdk, record) }), (err) => err.code === 'OUT_EXISTS' && /already exists[\s\S]*--force/.test(err.message));
  assert.deepStrictEqual(calls, []);
  assert.deepStrictEqual(record, [], 'the SDK was not even made');
  assert.strictEqual(fs.readFileSync(out, 'utf8'), '{"mine":"filled in"}', 'the file is untouched');
  await ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out, force: true }), { sdkFirst: stubSdkFirst(sdk) });
  assert.deepStrictEqual(JSON.parse(fs.readFileSync(out, 'utf8')), template());
});

test('--out must be a plain file in a folder that exists: a directory, a missing folder and a link are refused before anything is read', async () => {
  const dir = tempDir();
  const { sdk, calls } = sdkStub();
  const run = (outPath) => ss.runTemplate(templateRequest({ solution: 'Demo', outPath }), { sdkFirst: stubSdkFirst(sdk) });
  await assert.rejects(run(dir), (err) => /not a regular file|could not be inspected|refusing/.test(err.message));
  await assert.rejects(run(path.join(dir, 'missing', 'settings.json')), (err) => err.code === 'UNSAFE_OUTPUT');
  const target = path.join(dir, 'target.json');
  fs.writeFileSync(target, 'x');
  const link = path.join(dir, 'link.json');
  let linked = true;
  try {
    fs.symlinkSync(target, link);
  } catch {
    linked = false; // creating a link needs a privilege some Windows machines do not grant
  }
  if (linked) {
    await assert.rejects(run(link), (err) => err.code === 'UNSAFE_OUTPUT');
    assert.strictEqual(fs.readFileSync(target, 'utf8'), 'x');
  }
  assert.deepStrictEqual(calls, []);
});

test('without --force a file that APPEARS while the export, the staging or pac is awaited is never replaced — it is created, not overwritten, at the moment it is installed — and with --force it is', async () => {
  const mine = '{"EnvironmentVariables":[{"SchemaName":"new_Url","Value":"filled in meanwhile"}]}';
  // 1. through the SDK: the file is written while StageSolution's answer is awaited
  const dir = tempDir();
  const { out } = files(dir);
  const { sdk } = sdkStub();
  const meanwhile = { ...sdk, getDeploymentSettingsTemplate: async (options) => { fs.writeFileSync(out, mine); return sdk.getDeploymentSettingsTemplate(options); } };
  assert.strictEqual(fs.existsSync(out), false, 'absent when the run begins: the early check passes');
  await assert.rejects(ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out }), { sdkFirst: stubSdkFirst(meanwhile) }), (err) => err.code === 'OUT_EXISTS' && /already exists[\s\S]*--force/.test(err.message));
  assert.strictEqual(fs.readFileSync(out, 'utf8'), mine, 'the file a maker started is untouched');
  assert.deepStrictEqual(fs.readdirSync(dir).filter((name) => name.endsWith('.tmp')), [], 'and nothing is left beside it');
  await ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out, force: true }), { sdkFirst: stubSdkFirst(meanwhile) });
  assert.deepStrictEqual(JSON.parse(fs.readFileSync(out, 'utf8')), template(), 'with --force replacing it is what was asked');

  // 2. through pac, offline: the file is written while pac runs
  const dir2 = tempDir();
  const { pkg, out: out2 } = files(dir2);
  const { pac } = fakePac((args) => {
    fs.writeFileSync(out2, mine);
    fs.writeFileSync(flagValue(args, '--settings-file'), JSON.stringify(PAC_TEMPLATE));
    return { status: 0, stdout: 'created', stderr: '' };
  });
  await assert.rejects(ss.runTemplate({ command: 'template', packagePath: pkg, outPath: out2, force: false }, { sdkFirst: stubSdkFirst({}), pac }), (err) => err.code === 'OUT_EXISTS');
  assert.strictEqual(fs.readFileSync(out2, 'utf8'), mine);
});

test('a name that is taken at the very moment the file is installed is OUT_EXISTS, and a temporary file that cannot be removed is said there — and the SDK\'s warnings and the write\'s come out together', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  // What a file system does that a test cannot ask of the real one: another writer takes the name between the look and the link, and a scanner holds the temporary name. Both are patched on the
  // module object that lib/safe-fs.js looks its functions up on at the time of the call, and put back in the `finally`.
  const realLink = fs.linkSync;
  const realUnlink = fs.unlinkSync;
  const sibling = /[\\/]\.[^\\/]+\.\d+\.[0-9a-f]{8}\.tmp$/;
  fs.unlinkSync = (target, ...rest) => {
    if (sibling.test(String(target))) throw Object.assign(new Error('EIO: i/o error, unlink'), { code: 'EIO' });
    return realUnlink(target, ...rest);
  };
  try {
    fs.linkSync = (from, to) => {
      fs.writeFileSync(to, 'theirs');
      return realLink(from, to);
    };
    const { sdk } = sdkStub();
    await assert.rejects(ss.runTemplate(templateRequest({ solution: 'ContosoDemo', outPath: out }), { sdkFirst: stubSdkFirst(sdk) }), (err) => {
      assert.strictEqual(err.code, 'OUT_EXISTS');
      assert.match(err.message, /^--out: .* already exists\. Choose another file, or pass --force to replace it \(a replaced file loses every value already filled in\)\. Its temporary file .*\.tmp could not be removed \(EIO\): delete it\.$/);
      return true;
    });
    assert.strictEqual(fs.readFileSync(out, 'utf8'), 'theirs', 'the file that appeared is not replaced');
    const [left] = fs.readdirSync(dir).filter((name) => /^\..*\.tmp$/.test(name));
    assert.ok(left, 'the temporary file is where the message says');
    fs.rmSync(path.join(dir, left));
    fs.rmSync(out);

    // The write is a success whose temporary name stays: its warning comes after the SDK's own, in one list.
    fs.linkSync = realLink;
    const staged = sdkStub({ result: { settings: template(), warnings: ['The staged copy of the package could not be deleted.'] } });
    const result = await ss.runTemplate(templateRequest({ solution: 'ContosoDemo', outPath: out }), { sdkFirst: stubSdkFirst(staged.sdk) });
    assert.strictEqual(result.warnings.length, 2);
    assert.strictEqual(result.warnings[0], 'The staged copy of the package could not be deleted.');
    assert.match(result.warnings[1], /was written in full, but its temporary name .*\.tmp could not be removed \(EIO\): delete it/);
    assert.deepStrictEqual(JSON.parse(fs.readFileSync(out, 'utf8')), template(), 'the file is complete');
    // And without a leftover the warnings are the SDK's alone.
    for (const name of fs.readdirSync(dir).filter((entry) => /^\..*\.tmp$/.test(entry))) fs.rmSync(path.join(dir, name));
    fs.unlinkSync = realUnlink;
    const clean = await ss.runTemplate(templateRequest({ solution: 'ContosoDemo', outPath: out, force: true }), { sdkFirst: stubSdkFirst(staged.sdk) });
    assert.deepStrictEqual(clean.warnings, ['The staged copy of the package could not be deleted.']);
  } finally {
    fs.linkSync = realLink;
    fs.unlinkSync = realUnlink;
  }
});

// The run knows things by the time it installs the file — what the SDK could not clean up before it refused (a staged copy of the package, with its upload id), and a folder of pac's it could not remove —
// and a failure of the last step must not make it forget them: they are what the maker has left in the environment and on the disk. They are carried on the error as they were written, once each: nothing
// of this command is sanitized (it reads no settings file), and the plugin's own note is a path and a code.
test('a failure to install the file keeps every warning the run already knew — the SDK\'s cleanup warnings and the note of a pac folder that stayed — once each, in the order a success prints them', async () => {
  const sdkWarning = 'The staged copy of the package (stagesolutionupload 33333333-3333-4333-8333-333333333333) could not be deleted: HTTP 403. It stays until it is deleted.';
  // 1. through the SDK: the name is taken while the SDK stages (OUT_EXISTS from the exclusive writer)
  const dir = tempDir();
  const { out } = files(dir);
  const { sdk } = sdkStub({ result: { settings: template(), warnings: [sdkWarning] } });
  const meanwhile = { ...sdk, getDeploymentSettingsTemplate: async (options) => { fs.writeFileSync(out, 'theirs'); return sdk.getDeploymentSettingsTemplate(options); } };
  await assert.rejects(ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out }), { sdkFirst: stubSdkFirst(meanwhile) }), (err) => {
    assert.strictEqual(err.code, 'OUT_EXISTS');
    assert.deepStrictEqual(ss.describeFailure(err, () => { throw new Error('they are carried, not read again'); }).warnings, [sdkWarning]);
    return true;
  });
  assert.strictEqual(fs.readFileSync(out, 'utf8'), 'theirs');
  // 2. through the SDK: the install fails for another reason (--force renames a finished copy over the name, and the file system refuses)
  const realRename = fs.renameSync;
  fs.renameSync = (from, to, ...rest) => {
    if (/[\\/]\.[^\\/]+\.\d+\.[0-9a-f]{8}\.tmp$/.test(String(from))) throw Object.assign(new Error('EIO: i/o error, rename'), { code: 'EIO' });
    return realRename(from, to, ...rest);
  };
  try {
    await assert.rejects(ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out, force: true }), { sdkFirst: stubSdkFirst(sdkStub({ result: { settings: template(), warnings: [sdkWarning] } }).sdk) }), (err) => {
      assert.strictEqual(err.code, 'IO_ERROR', 'the write\'s own failure, as lib/safe-fs.js words it');
      assert.match(err.message, /EIO/);
      assert.deepStrictEqual(ss.describeFailure(err, () => { throw new Error('they are carried, not read again'); }).warnings, [sdkWarning]);
      return true;
    });
  } finally {
    fs.renameSync = realRename;
  }
  // 3. through pac: the SDK's warnings came across the fallback, the folder pac worked in cannot be removed, and the name is taken while pac runs
  const dir3 = tempDir();
  const { pkg, out: out3 } = files(dir3);
  const realRemove = fs.rmSync;
  let left;
  fs.rmSync = (target, options) => {
    if (WORK_FOLDER.test(path.basename(String(target)))) {
      left = String(target);
      throw Object.assign(new Error(`EBUSY: resource busy or locked, rmdir '${target}'`), { code: 'EBUSY' });
    }
    return realRemove(target, options);
  };
  const { pac } = fakePac((args) => {
    fs.writeFileSync(out3, 'theirs');
    fs.writeFileSync(flagValue(args, '--settings-file'), JSON.stringify(PAC_TEMPLATE));
    return { status: 0, stdout: 'created', stderr: '' };
  });
  const carrying = async (env, plan) => ({ source: 'pac', value: await plan.fromPac({ code: 'SOLUTION_STAGING_UNREADABLE', message: 'unreadable' }), fallback: { code: 'SOLUTION_STAGING_UNREADABLE', message: 'unreadable' }, warnings: [sdkWarning] });
  try {
    await assert.rejects(ss.runTemplate({ command: 'template', env: ENV, packagePath: pkg, outPath: out3, force: false }, { sdkFirst: carrying, pac }), (err) => {
      assert.strictEqual(err.code, 'OUT_EXISTS');
      const note = `The folder ${left} could not be removed (EBUSY); it holds the settings template pac wrote (emptied, so it keeps no value). Delete it.`;
      assert.deepStrictEqual(ss.describeFailure(err, () => { throw new Error('they are carried, not read again'); }).warnings, [sdkWarning, note]);
      return true;
    });
    assert.strictEqual(fs.readFileSync(out3, 'utf8'), 'theirs', 'the file that appeared is not replaced');
  } finally {
    fs.rmSync = realRemove;
    if (left) realRemove(left, { recursive: true, force: true, maxRetries: 8, retryDelay: 100 });
  }
});

test('a failure to install the file with no warning to keep is the failure it always was — no `warnings` on the error, none printed', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  const { sdk } = sdkStub();
  const meanwhile = { ...sdk, getDeploymentSettingsTemplate: async (options) => { fs.writeFileSync(out, 'theirs'); return sdk.getDeploymentSettingsTemplate(options); } };
  await assert.rejects(ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out }), { sdkFirst: stubSdkFirst(meanwhile) }), (err) => {
    assert.ok(!('warnings' in err) && !('leftoverWarnings' in err));
    assert.ok(!('warnings' in ss.describeFailure(err, () => undefined)));
    return true;
  });
});

test('--package is checked as far as a file can be before anything is requested: missing, a folder, empty, not a zip', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  const empty = path.join(dir, 'empty.zip');
  fs.writeFileSync(empty, '');
  const notZip = path.join(dir, 'notzip.zip');
  fs.writeFileSync(notZip, 'this is text, not a package');
  const short = path.join(dir, 'short.zip');
  fs.writeFileSync(short, 'P');
  const { sdk, calls } = sdkStub();
  const record = [];
  const cases = [
    [path.join(dir, 'missing.zip'), /could not be read \(ENOENT\)/],
    [dir, /is not a file/],
    [empty, /is empty/],
    [notZip, /not a solution package: it is not a \.zip file \(it does not begin with PK\)/],
    [short, /not a solution package/],
  ];
  for (const [packagePath, message] of cases) {
    await assert.rejects(ss.runTemplate(templateRequest({ packagePath, outPath: out }), { sdkFirst: stubSdkFirst(sdk, record) }), (err) => err.code === 'PACKAGE_UNREADABLE' && message.test(err.message), packagePath);
  }
  assert.deepStrictEqual(calls, []);
  assert.deepStrictEqual(record, []);
  assert.strictEqual(fs.existsSync(out), false);
});

test('a package over the size limit is refused before it is read, a settings file over its limit likewise — and exactly the limit is allowed', async () => {
  // The limits are guards against reading an unexpectedly large file into memory (the package is held, then held again as base64).
  assert.strictEqual(ss.MAX_PACKAGE_BYTES, 128 * 1024 * 1024);
  assert.strictEqual(ss.MAX_SETTINGS_BYTES, 5 * 1024 * 1024);
  const dir = tempDir();
  const pkg = path.join(dir, 'big.zip');
  fs.writeFileSync(pkg, Buffer.concat([Buffer.from([0x50, 0x4b, 0x03, 0x04]), Buffer.alloc(8)]));
  for (const withBytes of [false, true]) {
    assert.throws(() => ss.inspectPackage(pkg, { withBytes, maxBytes: 11 }), (err) => err.code === 'PACKAGE_UNREADABLE' && /is larger than/.test(err.message), `withBytes ${withBytes}`);
    assert.doesNotThrow(() => ss.inspectPackage(pkg, { withBytes, maxBytes: 12 }), `a package of exactly the limit, withBytes ${withBytes}`);
  }
  const settings = path.join(dir, 'big.json');
  const run = () => ss.runImport({ command: 'import', env: ENV, packagePath: pkg, settingsPath: settings }, { sdkFirst: async () => { throw new Error('nothing may be requested'); } });
  fs.writeFileSync(settings, ' '.repeat(ss.MAX_SETTINGS_BYTES + 1));
  await assert.rejects(run(), (err) => err.code === 'SETTINGS_UNREADABLE' && /is larger than 5 MiB/.test(err.message));
  fs.writeFileSync(settings, ' '.repeat(ss.MAX_SETTINGS_BYTES));
  await assert.rejects(run(), (err) => err.code === 'SETTINGS_INVALID', 'a file of exactly the limit is read (and is not JSON)');
});

// the template, through pac

const PAC_TEMPLATE = {
  EnvironmentVariables: [{ SchemaName: 'new_Url', Value: '', DefaultValue: 'https://default.example', Name: { Default: 'Url', ByLcid: { 1033: 'Url' } }, TypeId: 100000000, IsRequired: false }],
  ConnectionReferences: [{ LogicalName: 'new_Dataverse', ConnectionId: '', ConnectorId: '/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps' }],
  CopilotAgents: [],
};
const pacWritesTemplate = (content = JSON.stringify(PAC_TEMPLATE, null, 2)) => fakePac((args) => {
  fs.writeFileSync(flagValue(args, '--settings-file'), content);
  return { status: 0, stdout: 'Deployment settings file created.\n', stderr: '' };
});

test('when the SDK refuses to stage the package, pac reads the zip the SDK exported — offline — and its file is what is written, with the refusal as the fallback', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  const exported = Buffer.concat([ZIP, Buffer.from(' exported')]).toString('base64');
  const { sdk } = sdkStub({ exported });
  sdk.getDeploymentSettingsTemplate = async () => { throw refusal('SOLUTION_STAGING_FAILED', 'the package did not pass StageSolution in this environment'); };
  const { pac, calls } = pacWritesTemplate();
  let zipSeen;
  const spawn = pac.spawn;
  pac.spawn = (name, args, options) => {
    zipSeen = { path: flagValue(args, '--solution-zip'), bytes: fs.readFileSync(flagValue(args, '--solution-zip')), settingsDir: path.dirname(flagValue(args, '--settings-file')) };
    return spawn(name, args, options);
  };
  const result = await ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out }), { sdkFirst: stubSdkFirst(sdk), pac });
  assert.deepStrictEqual(result, {
    source: 'pac', out, environmentVariables: 1, connectionReferences: 1,
    fallback: { code: 'SOLUTION_STAGING_FAILED', message: 'the package did not pass StageSolution in this environment' },
  });
  assert.deepStrictEqual(calls[0].args.slice(0, 3), ['solution', 'create-settings', '--solution-zip']);
  assert.deepStrictEqual(zipSeen.bytes, Buffer.from(exported, 'base64'), 'pac was given the package the SDK had exported');
  assert.strictEqual(fs.readFileSync(out, 'utf8'), JSON.stringify(PAC_TEMPLATE, null, 2), 'pac\'s own file, byte for byte');
  assert.strictEqual(fs.existsSync(zipSeen.settingsDir), false, 'the scratch folder holding a copy of the package is removed');
  assert.notStrictEqual(path.dirname(flagValue(calls[0].args, '--settings-file')), path.dirname(out), 'pac never writes at --out itself');
});

test('for a package on disk pac is given that file', async () => {
  const dir = tempDir();
  const { pkg, out } = files(dir);
  const { sdk } = sdkStub();
  sdk.getDeploymentSettingsTemplate = async () => { throw refusal('SOLUTION_STAGING_UNREADABLE'); };
  const { pac, calls } = pacWritesTemplate();
  const result = await ss.runTemplate(templateRequest({ packagePath: pkg, outPath: out }), { sdkFirst: stubSdkFirst(sdk), pac });
  assert.strictEqual(flagValue(calls[0].args, '--solution-zip'), pkg);
  assert.strictEqual(result.fallback.code, 'SOLUTION_STAGING_UNREADABLE');
});

test('the scratch folder pac writes in is made LENT, once, when it is made: its record says so before pac starts, is not written again while pac runs, and names no process', async () => {
  const dir = tempDir();
  const { pkg, out } = files(dir);
  const { sdk } = sdkStub();
  sdk.getDeploymentSettingsTemplate = async () => { throw refusal('SOLUTION_STAGING_UNREADABLE'); };
  const seen = {};
  const pac = {
    spawn: (name, args, options) => {
      const file = flagValue(args, '--settings-file');
      const marker = path.join(path.dirname(file), '.owner.json');
      seen.before = fs.readFileSync(marker, 'utf8');
      seen.beforeId = fs.statSync(marker).ino;
      assert.ok(!('onSpawn' in options), 'pac is not followed: nothing is recorded about its process');
      fs.writeFileSync(file, JSON.stringify(PAC_TEMPLATE, null, 2));
      seen.after = fs.readFileSync(marker, 'utf8');
      seen.afterId = fs.statSync(marker).ino;
      seen.entries = fs.readdirSync(path.dirname(file)).sort();
      return { status: 0, stdout: '', stderr: '' };
    },
  };
  const result = await ss.runTemplate(templateRequest({ packagePath: pkg, outPath: out }), { sdkFirst: stubSdkFirst(sdk), pac });
  assert.strictEqual(result.source, 'pac');
  const record = JSON.parse(seen.before);
  assert.strictEqual(record.lent, true, 'the folder pac is given was made lent');
  assert.strictEqual(record.pid, process.pid);
  assert.deepStrictEqual(Object.keys(record).sort(), ['at', 'host', 'lent', 'pid', 'platform', 'v', ...(record.ns ? ['ns'] : []), ...(record.start ? ['start'] : [])].sort(), 'the maker\'s record and the one word, nothing about pac');
  assert.strictEqual(seen.after, seen.before, 'the record was not written again while pac ran');
  assert.strictEqual(seen.afterId, seen.beforeId, 'nor replaced by another file');
  assert.deepStrictEqual(seen.entries, ['.owner.json', 'settings.json'], 'and no temporary copy of it is left in the folder');
});

test('without --env there is nothing to stage a package in: pac reads the zip, offline, and the SDK is never made — no fallback to report', async () => {
  const dir = tempDir();
  const { pkg, out } = files(dir);
  const record = [];
  const { pac, calls } = pacWritesTemplate();
  const result = await ss.runTemplate({ command: 'template', packagePath: pkg, outPath: out, force: false }, { sdkFirst: stubSdkFirst({}, record), pac });
  assert.deepStrictEqual(result, { source: 'pac', out, environmentVariables: 1, connectionReferences: 1 });
  assert.deepStrictEqual(record, []);
  assert.deepStrictEqual(calls[0].args.slice(0, 4), ['solution', 'create-settings', '--solution-zip', pkg]);
  assert.strictEqual(calls[0].name, 'pac');
});

test('--out is replaced the safe way: written to a temp file in its own folder and renamed over the target, never written in place — and no temp file is left', async (t) => {
  const dir = tempDir();
  const { pkg, out } = files(dir);
  fs.writeFileSync(out, '{"old":true}');
  const renames = [];
  const realRename = fs.renameSync;
  t.mock.method(fs, 'renameSync', (from, to) => {
    renames.push({ fromDir: path.dirname(from), fromName: path.basename(from), toDir: path.dirname(to), toName: path.basename(to) });
    return realRename(from, to);
  });
  const { pac } = pacWritesTemplate();
  await ss.runTemplate({ command: 'template', packagePath: pkg, outPath: out, force: true }, { sdkFirst: stubSdkFirst({}), pac });
  const onto = renames.filter((r) => r.toName === 'settings.json');
  assert.strictEqual(onto.length, 1, 'one rename onto --out');
  assert.strictEqual(fs.realpathSync.native(onto[0].toDir), fs.realpathSync.native(dir));
  assert.strictEqual(fs.realpathSync.native(onto[0].fromDir), fs.realpathSync.native(dir), 'the temp file is beside the target, so the rename replaces the entry');
  assert.match(onto[0].fromName, /^\.settings\.json\..+\.tmp$/);
  assert.deepStrictEqual(fs.readdirSync(dir).filter((name) => name.endsWith('.tmp')), []);
  assert.deepStrictEqual(JSON.parse(fs.readFileSync(out, 'utf8')), PAC_TEMPLATE, 'the old file is replaced');
});

test('a failure that is not a staging refusal is thrown — pac is not asked, and --out is not written', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  const { sdk } = sdkStub();
  const failures = [
    Object.assign(new Error('HTTP 400 from https://contoso.crm.dynamics.com/api/data/v9.0/ExportSolution: Solution not found'), { code: 'CONNECTION_ERROR' }),
    refusal('INVALID_ARGUMENT', 'getDeploymentSettingsTemplate: bad input'),
    refusal('CONNECTIVITY_ROUTE_DECOMMISSIONED'),
  ];
  for (const failure of failures) {
    sdk.exportSolution = async () => { throw failure; };
    const { pac, calls } = pacWritesTemplate();
    await assert.rejects(ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out }), { sdkFirst: stubSdkFirst(sdk), pac }), (err) => err === failure);
    assert.deepStrictEqual(calls, []);
    assert.strictEqual(fs.existsSync(out), false);
  }
});

test('pac\'s failures leave --out unwritten, each with a code and the reason pac was asked: not run, timed out, refused, failed, wrote nothing, wrote what is no settings file', async () => {
  const dir = tempDir();
  const { out } = files(dir);
  const { sdk } = sdkStub();
  sdk.getDeploymentSettingsTemplate = async () => { throw refusal('SOLUTION_STAGING_FAILED', 'staging said no'); };
  const cases = [
    ['PAC_MISSING', { spawn: () => ({ status: null, stdout: '', stderr: '', error: Object.assign(new Error('spawn pac ENOENT: not found on PATH'), { code: 'ENOENT' }) }) }, /could not run/],
    ['PAC_TIMEOUT', { spawn: () => ({ status: null, stdout: '', stderr: '', error: Object.assign(new Error('t'), { code: 'ETIMEDOUT' }) }) }, /did not finish within/],
    ['PAC_REFUSED', { spawn: () => ({ status: null, stdout: '', stderr: '', error: Object.assign(new Error('cannot pass "a%b" to pac.cmd: a Windows batch file cannot receive a double quote, %, a control character unchanged'), { code: 'EARGUMENT' }) }) }, /was not run, and nothing was sent to pac: cannot pass/],
    ['PAC_FAILED', { spawn: () => ({ status: 1, stdout: '', stderr: 'Error: the zip is corrupt' }) }, /failed \(exit 1\): Error: the zip is corrupt/],
    ['PAC_OUTPUT_UNREADABLE', { spawn: () => ({ status: 0, stdout: 'created', stderr: '' }) }, /exited 0 but wrote no settings file/],
    ['PAC_OUTPUT_UNREADABLE', pacWritesTemplate('not json').pac, /is not valid JSON/],
    ['PAC_OUTPUT_UNREADABLE', pacWritesTemplate('[1]').pac, /is not a deployment-settings file/],
  ];
  for (const [code, pac, message] of cases) {
    await assert.rejects(ss.runTemplate(templateRequest({ solution: 'Demo', outPath: out }), { sdkFirst: stubSdkFirst(sdk), pac }), (err) => {
      assert.strictEqual(err.code, code);
      assert.match(err.message, message);
      assert.deepStrictEqual(err.fallback, { code: 'SOLUTION_STAGING_FAILED', message: 'staging said no' });
      return true;
    }, code);
    assert.strictEqual(fs.existsSync(out), false, `${code}: nothing is left at --out`);
  }
});

// --- import -------------------------------------------------------------------------------------------------------------------------

function importFiles(settings = filled()) {
  const dir = tempDir();
  const f = files(dir);
  fs.writeFileSync(f.settings, JSON.stringify(settings, null, 2));
  return { ...f, request: { command: 'import', env: ENV, packagePath: f.pkg, settingsPath: f.settings } };
}

test('import: the package and the parsed settings go to the SDK with pac\'s defaults — nothing overwritten, no workflows activated — and the job id is ours', async () => {
  const { request } = importFiles();
  const { sdk, calls } = sdkStub();
  const record = [];
  const result = await ss.runImport(request, { sdkFirst: stubSdkFirst(sdk, record), importJobId: JOB });
  assert.deepStrictEqual(calls, [['importSolution', {
    base64: ZIP.toString('base64'),
    overwriteUnmanagedCustomizations: false,
    publishWorkflows: false,
    importJobId: JOB,
    deploymentSettings: filled(),
  }]]);
  assert.deepStrictEqual(result, { source: 'sdk', importJobId: JOB, applied: { environmentVariables: ['new_Url'], connectionReferences: ['new_Dataverse'] } });
  assert.strictEqual(record[0].env, ENV);
  assert.ok(!JSON.stringify(result).includes(SECRET) && !JSON.stringify(result).includes(CONNECTION_ID), 'the output names entries, never their values');
});

test('import mints a job id when none is given: a GUID, so an unknown outcome can always be looked up', async () => {
  const { request } = importFiles();
  const { sdk } = sdkStub();
  const result = await ss.runImport(request, { sdkFirst: stubSdkFirst(sdk) });
  assert.match(result.importJobId, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
});

test('import refuses a missing, unreadable or mis-shaped file before anything is requested — and says nothing of what the file holds', async () => {
  const { request, pkg, settings } = importFiles();
  const { sdk, calls } = sdkStub();
  const record = [];
  const run = (patch) => ss.runImport({ ...request, ...patch }, { sdkFirst: stubSdkFirst(sdk, record) });
  const dir = path.dirname(pkg);
  await assert.rejects(run({ settingsPath: path.join(dir, 'missing.json') }), (err) => err.code === 'SETTINGS_UNREADABLE' && /could not be read \(ENOENT\)/.test(err.message));
  await assert.rejects(run({ settingsPath: dir }), (err) => err.code === 'SETTINGS_UNREADABLE' && /is not a file/.test(err.message));
  for (const [text, code] of [['{ nope', 'SETTINGS_INVALID'], ['[]', 'SETTINGS_INVALID'], [JSON.stringify({ Other: SECRET }), 'SETTINGS_INVALID'], [JSON.stringify({ EnvironmentVariables: [{ SchemaName: 'a', Value: { v: SECRET } }] }), 'SETTINGS_INVALID']]) {
    fs.writeFileSync(settings, text);
    await assert.rejects(run({}), (err) => err.code === code && !err.message.includes(SECRET), text);
  }
  fs.writeFileSync(settings, JSON.stringify(filled()));
  await assert.rejects(run({ packagePath: path.join(dir, 'missing.zip') }), (err) => err.code === 'PACKAGE_UNREADABLE');
  fs.writeFileSync(path.join(dir, 'text.zip'), 'plain text');
  await assert.rejects(run({ packagePath: path.join(dir, 'text.zip') }), (err) => err.code === 'PACKAGE_UNREADABLE' && /does not begin with PK/.test(err.message));
  assert.deepStrictEqual(calls, []);
  assert.deepStrictEqual(record, [], 'no SDK was made, so no token was read and no request sent');
});

test('import: a settings feature the SDK does not apply goes to pac with the same package and file, pinned to the environment, with the long budget; the refusal says why', async () => {
  const { request, pkg, settings } = importFiles(template({ Workflows: [{ WorkflowId: 'x' }] }));
  const original = fs.readFileSync(settings);
  const { sdk } = sdkStub();
  sdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', listSentence('Workflows')); };
  let given;
  const { pac, calls } = fakePac((args) => { given = { file: flagValue(args, '--settings-file'), bytes: fs.readFileSync(flagValue(args, '--settings-file')) }; return { status: 0, stdout: 'Solution Imported successfully.\n', stderr: '' }; });
  const result = await ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac });
  // The package is the maker's file and the environment is pinned; the settings are a private copy of the bytes the run read (see "the settings pac is given").
  assert.deepStrictEqual(calls[0].args, ['solution', 'import', '--path', pkg, '--environment', ENV, '--settings-file', given.file]);
  assert.ok(given.file !== settings && given.bytes.equals(original), 'the same settings, as a copy');
  assert.strictEqual(calls[0].options.timeout, 3600000, 'an import is given pac\'s own longest wait');
  assert.strictEqual(result.source, 'pac');
  assert.strictEqual(result.fallback.code, 'INVALID_ARGUMENT');
  assert.deepStrictEqual(result.applied, { environmentVariables: [], connectionReferences: [] }, 'the entries the file filled — here none');
  assert.ok(!('importJobId' in result), 'pac has its own job');
});

// --- the settings pac is given ---------------------------------------------------------------------------------------------------------------------
// The run reads the settings file ONCE: the sanitizer is built from it, the SDK is given what it parsed, and `applied` names its entries. The staging that comes before an import takes as long as the platform takes,
// and a file a maker (or a deployment) saves in that time is a file the run never looked at — if pac read the file by its path, its values would reach pac, and pac's own words about them, with no sanitizer that knows them.
// So pac is given a private copy of the BYTES the run read, in a folder of the run's own that is removed afterwards. The folder is made by lib/temp-workspace.js — in the plugin's own folder of the temp directory, named for the
// run's pid and recorded, so that a later run's sweep can find the folder of a run that was killed — or, where that folder cannot be used, in the temp directory itself (the name tests below accept both: what the run
// guarantees about the folder does not depend on which).
const PRIVATE_FOLDER = WORK_FOLDER;
// A stand-in sdkFirst that does what the real one does when the SDK refuses — after `meanwhile()`: the staging is where the file gets saved.
const refusedAfter = (meanwhile, sdk) => async (env, plan) => { meanwhile(); return stubSdkFirst(sdk)(env, plan); };
const withFeature = (settings) => ({ ...settings, Workflows: [{ WorkflowId: 'x' }] });
const toPac = () => {
  const { sdk } = sdkStub();
  sdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', listSentence('Workflows')); };
  return sdk;
};
// A pac that reads the settings file it is given at the time of the call, and says where it found it — and the owner record the folder held then (a folder that is removed has none to read afterwards).
function readingPac(answer = () => ({ status: 0, stdout: 'Solution Imported successfully.\n', stderr: '' })) {
  const seen = [];
  return {
    seen,
    pac: {
      spawn: (name, args) => {
        const file = flagValue(args, '--settings-file');
        seen.push({ file, dir: path.dirname(file), bytes: fs.readFileSync(file), record: ownerRecordOf(path.dirname(file)) });
        return answer(args, seen[seen.length - 1]);
      },
    },
  };
}

test('pac is given a private copy of the bytes the run read — never the file\'s path — so a file saved before pac starts is neither applied, nor quoted, nor reported', async () => {
  const { request, settings } = importFiles(withFeature(filled()));
  // The bytes as a Windows editor saves them: a byte order mark and CRLF line ends. They reach pac exactly.
  const original = Buffer.from(`\uFEFF${JSON.stringify(withFeature(filled()), null, 2).replace(/\n/g, '\r\n')}`, 'utf8');
  fs.writeFileSync(settings, original);
  const edited = JSON.stringify(template({ EnvironmentVariables: [{ SchemaName: 'new_Url', Value: 'saved-meanwhile-0123456789', TypeId: 100000005 }] }));
  const { seen, pac } = readingPac();
  const result = await ss.runImport(request, { sdkFirst: refusedAfter(() => fs.writeFileSync(settings, edited), toPac()), pac });
  assert.strictEqual(seen.length, 1);
  assert.notStrictEqual(seen[0].file, settings, 'not the maker\'s file');
  assert.strictEqual(path.basename(seen[0].file), 'settings.json');
  assertMadeByTheMaker(seen[0].dir, { pid: process.pid, record: seen[0].record }); // in a folder of the run's own: the plugin's folder, the run's pid, recorded and lent
  assert.ok(seen[0].bytes.equals(original), 'the bytes the run read, byte order mark and line ends included');
  assert.strictEqual(fs.readFileSync(settings, 'utf8'), edited, 'the maker\'s file is as they saved it');
  assert.strictEqual(fs.existsSync(seen[0].dir), false, 'and the copy is gone');
  assert.deepStrictEqual(result.applied, { environmentVariables: ['new_Url'], connectionReferences: ['new_Dataverse'] }, 'what is reported is what pac was given');
});

test('the sanitizer knows what pac was given: a value pac quotes from its copy is withheld — and the value a file was saved with meanwhile is never given to pac, so it is quoted by no one', async () => {
  const { request, settings } = importFiles(withFeature(filled()));
  const edited = JSON.stringify(template({ EnvironmentVariables: [{ SchemaName: 'new_Url', Value: 'saved-meanwhile-0123456789', TypeId: 100000005 }] }));
  const { seen, pac } = readingPac((args, call) => ({ status: 1, stdout: '', stderr: `Error: rejected '${JSON.parse(call.bytes.toString('utf8')).EnvironmentVariables[0].Value}'` }));
  await assert.rejects(ss.runImport(request, { sdkFirst: refusedAfter(() => fs.writeFileSync(settings, edited), toPac()), pac }), (err) => {
    assert.strictEqual(err.code, 'PAC_FAILED');
    assert.strictEqual(err.message, `\`pac solution import\` failed (exit 1): ${WITHHELD('text')}`);
    assert.ok(!JSON.stringify(ss.describeFailure(err)).includes('saved-meanwhile-0123456789'));
    return true;
  });
  assert.strictEqual(JSON.parse(seen[0].bytes.toString('utf8')).EnvironmentVariables[0].Value, SECRET, 'pac read the value the sanitizer was built from');
});

test('the private copy is removed whichever way pac ends — it succeeded, failed, was not found, ran out of time or was refused — and a failure of pac is pac\'s, with its code and the reason pac was asked', async () => {
  const lost = (code, message) => () => ({ status: null, stdout: '', stderr: '', error: Object.assign(new Error(message), { code }) });
  const outcomes = [
    ['succeeded', () => ({ status: 0, stdout: 'ok', stderr: '' }), null],
    ['PAC_FAILED', () => ({ status: 1, stdout: '', stderr: 'Error: nope' }), 'PAC_FAILED'],
    ['PAC_MISSING', lost('ENOENT', 'spawn pac ENOENT: not found on PATH'), 'PAC_MISSING'],
    ['PAC_TIMEOUT', lost('ETIMEDOUT', 't'), 'PAC_TIMEOUT'],
    ['PAC_REFUSED', lost('EARGUMENT', 'cannot pass "a%b" to pac.cmd'), 'PAC_REFUSED'],
  ];
  for (const [name, answer, code] of outcomes) {
    const { request } = importFiles(withFeature(filled()));
    const { seen, pac } = readingPac(answer);
    const run = ss.runImport(request, { sdkFirst: stubSdkFirst(toPac()), pac });
    if (code) {
      await assert.rejects(run, (err) => err.code === code && err.fallback.code === 'INVALID_ARGUMENT' && !('warnings' in err), name);
    } else {
      assert.ok(!('warnings' in (await run)), name);
    }
    assert.strictEqual(seen.length, 1, name);
    assert.strictEqual(fs.existsSync(seen[0].dir), false, `${name}: the folder the copy was in is gone`);
  }
});

// The copy is made in ONE guarded step — the folder, its access list, the file, its bytes — and a failure of any part of it is the same failure: the pac step could not be started. The reason pac was asked is kept
// (`fallback`), the message holds the system's code and no value, pac is not run, the folder is removed, and no SDK import job is named — the SDK never sent an import, and pac mints its own.
const COPY_FAILED = (reason) => `\`pac solution import\` was not run: a private copy of the settings file could not be made (${reason}).`;
// Fails the first call that creates the copy's file, as a file system that refuses it would; `seen.folder` is where that was.
function refusingTheCopyFile(seen, code = 'EACCES') {
  const real = fs.openSync;
  fs.openSync = (file, ...rest) => {
    if (path.basename(String(file)) === 'settings.json' && PRIVATE_FOLDER.test(path.basename(path.dirname(String(file))))) {
      seen.folder = path.dirname(String(file));
      throw Object.assign(new Error(`${code}: permission denied, open '${file}'`), { code });
    }
    return real(file, ...rest);
  };
  return () => { fs.openSync = real; };
}

test('a copy that cannot be made is a failure of the pac step — nothing is sent to pac, the folder is removed, and the reason pac was asked is kept', async () => {
  const { request } = importFiles(withFeature(filled()));
  const { pac, calls } = fakePac(() => ({ status: 0, stdout: 'ok', stderr: '' }));
  const seen = {};
  const restore = refusingTheCopyFile(seen);
  try {
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(toPac()), pac }), (err) => {
      assert.strictEqual(err.code, 'PAC_SETTINGS_COPY_FAILED');
      assert.strictEqual(err.message, COPY_FAILED('EACCES'));
      assert.strictEqual(err.fallback.code, 'INVALID_ARGUMENT');
      return true;
    });
  } finally {
    restore();
  }
  assert.deepStrictEqual(calls, [], 'pac was never started');
  assert.ok(seen.folder && !fs.existsSync(seen.folder), 'the folder was removed');
});

test('a folder for the copy that cannot be made — in the plugin\'s own folder and in the temp folder it falls back to — is the same failure: PAC_SETTINGS_COPY_FAILED with the reason pac was asked, no SDK import job named, pac never started', async (t) => {
  const { request } = importFiles(withFeature(filled()));
  const { pac, calls } = fakePac(() => ({ status: 0, stdout: 'ok', stderr: '' }));
  // Each way of making the folder fails, with a code of its own: an error is reported only when the plain call fails too, and then it is that call's (lib/temp-workspace.js), which the reason shows.
  const refused = refusingWorkFolders(t, { plugin: 'ENOSPC', plain: 'EACCES' });
  let failure;
  await ss.runImport(request, { sdkFirst: stubSdkFirst(toPac()), pac, importJobId: JOB }).catch((err) => { failure = err; });
  assert.deepStrictEqual(refused.map(({ way, code }) => [way, code]), [['plugin', 'ENOSPC'], ['plain', 'EACCES']], 'the folder was asked for in the plugin\'s own folder, and then in the temp folder');
  assert.ok(failure, 'the import failed');
  assert.strictEqual(failure.code, 'PAC_SETTINGS_COPY_FAILED');
  assert.strictEqual(failure.message, COPY_FAILED('EACCES'));
  assert.strictEqual(failure.fallback.code, 'INVALID_ARGUMENT', 'pac was asked because the SDK refused a feature of the file, and the failure says so');
  assert.deepStrictEqual(ss.describeFailure(failure), { ok: false, error: COPY_FAILED('EACCES'), code: 'PAC_SETTINGS_COPY_FAILED', fallback: failure.fallback });
  assert.ok(!('importJobId' in failure), 'the SDK sent no import, and pac mints its own job: there is none to look up');
  assert.deepStrictEqual(calls, [], 'pac was never started');
});

// The plugin's own folder is a courtesy to the next run, never a reason for this one to fail (lib/temp-workspace.js): where it will not take a folder, the run makes its own in the temp folder itself, as it always did —
// unrecorded, so that nothing sweeps it — and everything else about the folder is as it is anywhere: the copy is made in it, and the folder is removed.
test('a plugin folder that will not take the work folder does not fail the import — the run goes on in the temp folder itself: pac is given its copy there, and the folder is removed', async (t) => {
  const { request, settings } = importFiles(withFeature(filled()));
  const original = fs.readFileSync(settings);
  const refused = refusingWorkFolders(t, { plugin: 'ENOSPC', plain: null });
  const { seen, pac } = readingPac();
  const result = await ss.runImport(request, { sdkFirst: stubSdkFirst(toPac()), pac });
  assert.deepStrictEqual(refused.map(({ way, code }) => [way, code]), [['plugin', 'ENOSPC']], 'only the plugin\'s folder refused');
  assert.strictEqual(result.source, 'pac');
  assert.strictEqual(seen.length, 1, 'pac ran');
  assert.ok(seen[0].bytes.equals(original), 'with the bytes the run read');
  assert.strictEqual(path.dirname(seen[0].dir), os.tmpdir(), 'in a folder of the temp folder itself');
  assert.match(path.basename(seen[0].dir), PLAIN_WORK_FOLDER, 'named as it always was, with no pid');
  assert.strictEqual(seen[0].record, null, 'and unrecorded');
  assert.strictEqual(fs.existsSync(seen[0].dir), false, 'and the folder is gone');
});

test('a copy whose protection cannot be set or verified — the private-copy step refuses — is the same failure, however it words the reason: pac is not run and the folder is removed', async () => {
  for (const [what, refused, reason] of [
    ['an error with a system code', Object.assign(new Error('EPERM: operation not permitted'), { code: 'EPERM' }), 'EPERM'],
    ['an error with a reason of the plugin\'s own words', Object.assign(new Error('icacls failed'), { code: 'PRIVATE_FOLDER', reason: 'icacls exited 5 restricting the folder to the current user' }), 'icacls exited 5 restricting the folder to the current user'],
    ['an error with neither', new TypeError('x'), 'TypeError'],
  ]) {
    const { request } = importFiles(withFeature(filled()));
    const { pac, calls } = fakePac(() => ({ status: 0, stdout: 'ok', stderr: '' }));
    let folder;
    const privateFile = (dir) => { folder = dir; throw refused; };
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(toPac()), pac, privateFile }), (err) => {
      assert.strictEqual(err.code, 'PAC_SETTINGS_COPY_FAILED', what);
      assert.strictEqual(err.message, COPY_FAILED(reason), what);
      assert.strictEqual(err.fallback.code, 'INVALID_ARGUMENT', what);
      assert.ok(!('importJobId' in ss.describeFailure(err)), what);
      return true;
    });
    assert.deepStrictEqual(calls, [], `${what}: pac was never started`);
    assert.ok(folder && PRIVATE_FOLDER.test(path.basename(folder)) && !fs.existsSync(folder), `${what}: the folder the copy was to be in is removed`);
  }
});

test('the private-copy step is given the folder, the file name and the BYTES the run read — and what it answers is the file pac is given', async () => {
  const { request, settings } = importFiles(withFeature(filled()));
  const original = fs.readFileSync(settings);
  let given;
  const privateFile = (dir, name, bytes) => {
    given = { dir, name, bytes: Buffer.from(bytes) };
    const file = path.join(dir, 'chosen.json');
    fs.writeFileSync(file, bytes);
    return file;
  };
  const { seen, pac } = readingPac();
  await ss.runImport(request, { sdkFirst: stubSdkFirst(toPac()), pac, privateFile });
  assert.strictEqual(given.name, 'settings.json');
  assert.ok(given.bytes.equals(original), 'the bytes of the one reading');
  assert.match(path.basename(given.dir), PRIVATE_FOLDER);
  assert.strictEqual(seen[0].file, path.join(given.dir, 'chosen.json'), 'pac was given what the step answered');
  assert.strictEqual(fs.existsSync(given.dir), false);
});

test('the sentence for a copy that cannot be made is the plugin\'s own — a value of the file that is a word of it ("copy", "made", "pac", "file") does not withhold it', async () => {
  // The pac route reads no package, so a short value of a variable is looked for as a WHOLE WORD (secrecyOf): a sanitizer that looked at this sentence would find them and withhold it.
  const wordy = withFeature(template({ EnvironmentVariables: ['copy', 'made', 'pac', 'file'].map((Value, i) => ({ SchemaName: `new_V${i}`, Value, DefaultValue: '', TypeId: 100000000, IsRequired: false })), ConnectionReferences: [] }));
  const { request } = importFiles(wordy);
  const { pac, calls } = fakePac(() => ({ status: 0, stdout: 'ok', stderr: '' }));
  const restore = refusingTheCopyFile({});
  try {
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(toPac()), pac }), (err) => {
      assert.strictEqual(err.code, 'PAC_SETTINGS_COPY_FAILED');
      assert.strictEqual(err.message, COPY_FAILED('EACCES'));
      return true;
    });
  } finally {
    restore();
  }
  assert.deepStrictEqual(calls, []);
});

test('an ordinary invalid file whose key or name holds "this SDK does not apply" is NOT sent to pac — the SDK\'s normal complaint quotes it, and the import fails with the SDK\'s code', async () => {
  const phrase = 'this SDK does not apply';
  const cases = [
    [template({ [phrase]: true }), `importSolution: deploymentSettings is not a deployment-settings file this SDK applies — deploymentSettings.${phrase} is not part of the format (EnvironmentVariables, ConnectionReferences; Workflows, CanvasApps and CopilotAgents only empty; $schema). Nothing was imported.`],
    [template({ EnvironmentVariables: [{ SchemaName: phrase, Value: '' }, { SchemaName: phrase, Value: '' }] }), `importSolution: deploymentSettings is not a deployment-settings file this SDK applies — deploymentSettings.EnvironmentVariables[1].SchemaName names '${phrase}' a second time (names are case-insensitive). Nothing was imported.`],
  ];
  for (const [settings, message] of cases) {
    const { request } = importFiles(settings);
    const { sdk } = sdkStub();
    sdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', message); };
    const { pac, calls } = fakePac(() => ({ status: 0, stdout: 'imported', stderr: '' }));
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac, importJobId: JOB }), (err) => {
      assert.strictEqual(err.code, 'INVALID_ARGUMENT');
      assert.ok(err.message.includes(phrase));
      assert.strictEqual(err.importJobId, JOB);
      assert.ok(!('fallback' in err), 'pac was not asked');
      return true;
    }, message);
    assert.deepStrictEqual(calls, [], 'pac is never started for it');
  }
});

test('import through pac names the entries the file filled, and never what they hold — a secret the SDK quoted withholds its words, a plain value or a connection id is replaced', async () => {
  const { request } = importFiles();
  const { sdk } = sdkStub();
  sdk.importSolution = async () => { throw refusal('SOLUTION_STAGING_UNREADABLE', `StageSolution's reply could not be read — detail mentions ${SECRET}`); };
  const { pac } = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
  const result = await ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac });
  assert.deepStrictEqual(result.applied, { environmentVariables: ['new_Url'], connectionReferences: ['new_Dataverse'] });
  assert.ok(!JSON.stringify(result).includes(SECRET), 'the SDK\'s words in the fallback are cleaned too');
  assert.strictEqual(result.fallback.message, WITHHELD('message'));

  // A variable the package's own definition says is Text: its value is replaced where the words quote it, as a connection id is.
  const plain = importFiles(filledPlain());
  const plainSdk = sdkStub().sdk;
  plainSdk.importSolution = async () => { throw refusal('SOLUTION_STAGING_UNREADABLE', `StageSolution's reply could not be read — detail mentions ${PLAIN} and ${CONNECTION_ID}`); };
  const viaPac = await ss.runImport(plain.request, { ...stagedAs({ new_Url: TEXT_TYPE }, plainSdk), pac });
  assert.strictEqual(viaPac.fallback.message, 'StageSolution\'s reply could not be read — detail mentions <redacted> and <redacted>');
  // The package was not read: nothing says the variable is Text, so it is treated as a secret and the words are withheld — whatever TypeId the file gives it. A connection id alone is still replaced.
  const unread = await ss.runImport(plain.request, { sdkFirst: stubSdkFirst(plainSdk), pac });
  assert.strictEqual(unread.fallback.message, WITHHELD('message'));
  const idOnly = sdkStub().sdk;
  idOnly.importSolution = async () => { throw refusal('SOLUTION_STAGING_UNREADABLE', `StageSolution's reply could not be read — detail mentions ${CONNECTION_ID}`); };
  assert.strictEqual((await ss.runImport(plain.request, { sdkFirst: stubSdkFirst(idOnly), pac })).fallback.message, 'StageSolution\'s reply could not be read — detail mentions <redacted>');
});

test('a pac import that fails names no import job — pac mints its own, and the SDK\'s job was never sent — and says how long it was given and that pac ran out of it', async () => {
  const viaPac = importFiles({ ...filled(), CanvasApps: [{ Name: 'x' }] });
  const sdk = sdkStub().sdk;
  sdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', listSentence('CanvasApps')); };
  const failed = fakePac(() => ({ status: 1, stdout: '', stderr: 'Error: the import failed' }));
  await assert.rejects(ss.runImport(viaPac.request, { sdkFirst: stubSdkFirst(sdk), pac: failed.pac, importJobId: JOB }), (err) => {
    assert.strictEqual(err.code, 'PAC_FAILED');
    assert.strictEqual(err.fallback.code, 'INVALID_ARGUMENT');
    assert.ok(!('importJobId' in err), 'no SDK job to look up');
    assert.deepStrictEqual(ss.describeFailure(err), { ok: false, error: err.message, code: 'PAC_FAILED', fallback: err.fallback });
    return true;
  });
  const slow = fakePac(() => ({ status: null, stdout: '', stderr: '', error: Object.assign(new Error('spawn pac ETIMEDOUT'), { code: 'ETIMEDOUT' }) }));
  await assert.rejects(ss.runImport(viaPac.request, { sdkFirst: stubSdkFirst(sdk), pac: slow.pac, importJobId: JOB }), (err) => {
    assert.strictEqual(err.code, 'PAC_TIMEOUT');
    assert.match(err.message, /^`pac solution import` did not finish within 3600 s; pac and every process that held its output were stopped\. The environment may still be importing the package: look at its import history/);
    assert.ok(!('importJobId' in err));
    return true;
  });
});

// What pac's own text is, the plugin's words around it, and the sentence that sends a file to pac, are three different things, and only the first is another party's. pac's text is given to the
// sanitizer where it is READ (runPac's `redact`); the label and the exit status the plugin writes around it, and the feature sentence it builds from the file's keys, hold no value and are not.
const FEATURE_FILE = (value, name = 'new_Flag') => template({ EnvironmentVariables: [{ SchemaName: name, Value: value }], Workflows: [{ WorkflowId: 'x' }] });
function viaPacRequest(settings) {
  const files = importFiles(settings);
  const sdk = sdkStub().sdk;
  sdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', listSentence('Workflows')); };
  return { ...files, sdk };
}

test('a pac failure is sanitized ONCE, where pac\'s text is read: the plugin\'s own label and exit status stay whatever the values are — "1" for the status, "pac" for the label — and pac\'s own words are still cleaned', async () => {
  // Nothing was read of the package (the pac fallback), so each variable's type is unknown: below six characters a value is looked for as a whole word, from six as a substring.
  for (const value of ['1', 'pac', 'exit', 'solution', 'failed', 'import', 'import`']) {
    const { request, sdk } = viaPacRequest(FEATURE_FILE(value));
    const { pac } = fakePac(() => ({ status: 1, stdout: '', stderr: 'Error: the platform said no' }));
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac, importJobId: JOB }), (err) => {
      assert.strictEqual(err.message, '`pac solution import` failed (exit 1): Error: the platform said no', JSON.stringify(value));
      assert.strictEqual(err.code, 'PAC_FAILED');
      assert.strictEqual(err.fallback.message, listSentence('Workflows'), `the reason pac was asked, for ${JSON.stringify(value)}`);
      return true;
    }, JSON.stringify(value));
  }
  // pac's own words are looked at where they are read: a value they quote takes them out and leaves the plugin's words.
  for (const [value, stderr] of [['1', 'Error: value 1 was refused'], ['pac', 'Error: pac said no'], ['PIN420', 'Error: the value PIN420 was refused'], ['PIN42', "Error: the value 'PIN42' was refused"]]) {
    const { request, sdk } = viaPacRequest(FEATURE_FILE(value));
    const { pac } = fakePac(() => ({ status: 1, stdout: '', stderr }));
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac, importJobId: JOB }), (err) => {
      assert.strictEqual(err.message, `\`pac solution import\` failed (exit 1): ${WITHHELD('text')}`, stderr);
      return true;
    }, stderr);
  }
  // A call that ran out of time says so in the plugin's own words, "3600" and "within" among them.
  for (const value of ['3600', 'within', 'finish']) {
    const { request, sdk } = viaPacRequest(FEATURE_FILE(value));
    const { pac } = fakePac(() => ({ status: null, stdout: '', stderr: '', error: Object.assign(new Error('spawn pac ETIMEDOUT'), { code: 'ETIMEDOUT' }) }));
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac, importJobId: JOB }), (err) => {
      assert.strictEqual(err.code, 'PAC_TIMEOUT');
      assert.match(err.message, /^`pac solution import` did not finish within 3600 s/, JSON.stringify(value));
      assert.doesNotMatch(err.message, /withheld/, JSON.stringify(value));
      return true;
    }, JSON.stringify(value));
  }
});

test('the sentence that sends a file to pac is the plugin\'s own and is shown as it is — failing and succeeding — whatever the values are: "en" is in "deployment", "Workflows" is the key it names', async () => {
  for (const value of ['en', 'pac', 'only', 'file', 'list', 'Workflows', 'a pac feature', 'deploymentSettings', 'Nothing was imported']) {
    const failing = viaPacRequest(FEATURE_FILE(value));
    const failed = fakePac(() => ({ status: 1, stdout: '', stderr: 'Error: the platform said no' }));
    await assert.rejects(ss.runImport(failing.request, { sdkFirst: stubSdkFirst(failing.sdk), pac: failed.pac }), (err) => {
      assert.deepStrictEqual(err.fallback, { code: 'INVALID_ARGUMENT', message: listSentence('Workflows') }, JSON.stringify(value));
      return true;
    }, JSON.stringify(value));
    const succeeding = viaPacRequest(FEATURE_FILE(value));
    const imported = fakePac(() => ({ status: 0, stdout: 'Solution Imported successfully.\n', stderr: '' }));
    const result = await ss.runImport(succeeding.request, { sdkFirst: stubSdkFirst(succeeding.sdk), pac: imported.pac });
    assert.deepStrictEqual(result.fallback, { code: 'INVALID_ARGUMENT', message: listSentence('Workflows') }, JSON.stringify(value));
    assert.deepStrictEqual(result.applied, { environmentVariables: ['new_Flag'], connectionReferences: [] });
  }
  // The other feature sentences are the plugin's own too.
  const instance = importFiles(template({ EnvironmentVariables: [{ SchemaName: 'new_Flag', Value: 'en' }], ConnectionReferences: [{ LogicalName: 'new_a', ConnectionId: '', ConnectionInstance: { x: 1 } }] }));
  const instanceSdk = sdkStub().sdk;
  instanceSdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', instanceSentence(0)); };
  const done = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
  assert.strictEqual((await ss.runImport(instance.request, { sdkFirst: stubSdkFirst(instanceSdk), pac: done.pac })).fallback.message, instanceSentence(0));
});

test('what the SDK says of a staging reply it could not read is ANOTHER party\'s text — it can quote what it was given — so it is still sanitized, and a short value that stands alone in it withholds it', async () => {
  const cases = [
    // [value, the SDK's words, shown as they are?]
    ['x', 'StageSolution\'s reply could not be read: the entry \'x\' has no type', false],
    ['x', 'StageSolution\'s reply could not be read: a box was not a list', true],
    ['en', 'StageSolution\'s reply could not be read: deployment of the entry has no type', true],
    ['en', 'StageSolution\'s reply could not be read: the en entry has no type', false],
    ['PIN420', 'StageSolution\'s reply could not be read: it held PIN420', false],
    ['PIN420', 'StageSolution\'s reply could not be read: it held ValuePIN420wasRefused', false],
  ];
  for (const [value, message, shown] of cases) {
    const { request } = importFiles(template({ EnvironmentVariables: [{ SchemaName: 'new_Flag', Value: value }] }));
    const sdk = sdkStub().sdk;
    sdk.importSolution = async () => { throw refusal('SOLUTION_STAGING_UNREADABLE', message); };
    const { pac } = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
    const result = await ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac });
    assert.strictEqual(result.fallback.message, shown ? message : WITHHELD('message'), `${JSON.stringify(value)}: ${message}`);
    const failed = fakePac(() => ({ status: 1, stdout: '', stderr: 'Error: the platform said no' }));
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac: failed.pac }), (err) => {
      assert.strictEqual(err.fallback.message, shown ? message : WITHHELD('message'));
      assert.strictEqual(err.message, '`pac solution import` failed (exit 1): Error: the platform said no');
      return true;
    });
  }
  // The feature sentence is accepted by EXACT equality: a complaint about a reply that merely begins with it is the SDK's words about a reply — it can quote a value — and is sanitized.
  const feature = importFiles(template({ EnvironmentVariables: [{ SchemaName: 'new_Flag', Value: 'PIN420' }], Workflows: [{ WorkflowId: 'x' }] }));
  const lookalike = sdkStub().sdk;
  lookalike.importSolution = async () => { throw refusal('SOLUTION_STAGING_UNREADABLE', `${listSentence('Workflows')} The reply also held PIN420.`); };
  const imported = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
  assert.strictEqual((await ss.runImport(feature.request, { sdkFirst: stubSdkFirst(lookalike), pac: imported.pac })).fallback.message, WITHHELD('message'));
});

test('a package that did not stage is NOT imported by pac: the failure is the SDK\'s, with its words and its code', async () => {
  const { request } = importFiles();
  const { sdk } = sdkStub();
  const failure = refusal('SOLUTION_STAGING_FAILED', 'importSolution: the package did not pass StageSolution in this environment (status "Failed"). Missing dependencies: x. Nothing was imported.');
  sdk.importSolution = async () => { throw failure; };
  const { pac, calls } = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
  await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), pac, importJobId: JOB }), (err) => {
    assert.strictEqual(err.code, 'SOLUTION_STAGING_FAILED');
    assert.match(err.message, /Missing dependencies: x/);
    assert.strictEqual(err.importJobId, JOB);
    return true;
  });
  assert.deepStrictEqual(calls, []);
});

// --- what an import's failure tells, and what it never does ----------------------------------------------------------------------------------

// What the SDK throws for an answer that is not a success: a ConnectionError of the real bundle, naming the URL it reached.
const { ConnectionError } = require('../lib/maker-sdk.js');
const importFailureOf = (status, message, url = `${ENV}/api/data/v9.0/ImportSolution`) => new ConnectionError(url, status, message);
// The SDK attaches the cleanup warnings of a failed staging to its error under a symbol (errors/index.ts withCleanupWarnings), where readCleanupWarnings finds them.
const withWarnings = (error, warnings) => Object.defineProperty(error, Symbol.for('cds-maker-sdk.cleanupWarnings'), { value: warnings, enumerable: false });
const failAs = (error) => async () => { throw error; };

test('no message leaves an import with a filled value or connection id in it — the SDK\'s, the platform\'s or pac\'s — and the job is named', async () => {
  // A SECRET the platform quoted: its text is withheld, its status line and the job stay.
  const { request } = importFiles();
  const { sdk } = sdkStub();
  sdk.importSolution = failAs(importFailureOf(400, `the connection ${CONNECTION_ID} is not valid; value ${SECRET} is not a number`));
  await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.ok(!err.message.includes(SECRET) && !err.message.includes(CONNECTION_ID), err.message);
    assert.strictEqual(err.message, `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: ${WITHHELD('message')}`);
    assert.strictEqual(err.code, 'CONNECTION_ERROR');
    assert.strictEqual(err.statusCode, 400);
    assert.strictEqual(err.importJobId, JOB);
    return true;
  });

  // A connection id and a value the package says is Text, alone: replaced where the platform spelled them, the rest of its words kept.
  const plain = importFiles(filledPlain());
  const plainSdk = sdkStub().sdk;
  plainSdk.importSolution = failAs(importFailureOf(400, `the connection ${CONNECTION_ID} is not valid; value ${PLAIN} is not a number`));
  await assert.rejects(ss.runImport(plain.request, { ...stagedAs({ new_Url: TEXT_TYPE }, plainSdk), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: the connection <redacted> is not valid; value <redacted> is not a number`);
    return true;
  });

  const viaPac = importFiles({ ...filled(), CanvasApps: [{ Name: 'x' }] });
  const pacSdk = sdkStub().sdk;
  pacSdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', listSentence('CanvasApps')); };
  const { pac } = fakePac(() => ({ status: 1, stdout: `the value ${SECRET} was refused`, stderr: '' }));
  await assert.rejects(ss.runImport({ ...viaPac.request }, { sdkFirst: stubSdkFirst(pacSdk), pac }), (err) => err.code === 'PAC_FAILED' && !err.message.includes(SECRET) && /withheld/.test(err.message));

  const withSecret = importFiles();
  const secretSdk = sdkStub().sdk;
  secretSdk.importSolution = async () => { throw refusal('SOLUTION_STAGING_UNREADABLE', `reply held ${SECRET}`); };
  const { pac: failingPac } = fakePac(() => ({ status: 1, stdout: '', stderr: `bad ${CONNECTION_ID}` }));
  await assert.rejects(ss.runImport(withSecret.request, { sdkFirst: stubSdkFirst(secretSdk), pac: failingPac }), (err) => {
    assert.ok(!JSON.stringify({ message: err.message, fallback: err.fallback }).includes(SECRET));
    assert.ok(!err.message.includes(CONNECTION_ID));
    assert.strictEqual(err.fallback.code, 'SOLUTION_STAGING_UNREADABLE');
    return true;
  });
});

test('a rejection that is not an Error — a string — is still cleaned of values and names the job, as an Error with the same words', async () => {
  const { request } = importFiles();
  const { sdk } = sdkStub();
  sdk.importSolution = async () => { throw `the platform quoted ${SECRET}`; };
  await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB }), (err) => {
    assert.ok(err instanceof Error);
    assert.strictEqual(err.message, WITHHELD('message'));
    assert.strictEqual(err.importJobId, JOB);
    return true;
  });
  const plain = importFiles(filledPlain());
  const plainSdk = sdkStub().sdk;
  plainSdk.importSolution = async () => { throw `the platform quoted ${PLAIN}`; };
  await assert.rejects(ss.runImport(plain.request, { ...stagedAs({ new_Url: TEXT_TYPE }, plainSdk), importJobId: JOB }), (err) => err.message === 'the platform quoted <redacted>' && err.importJobId === JOB);
});

test('the error an import leaves with is a NEW one: the SDK\'s own error, which holds the service\'s raw answer as its cause and its warnings unfiltered, is not passed on', async () => {
  const { request } = importFiles();
  const { sdk } = sdkStub();
  const original = withWarnings(new ConnectionError(`${ENV}/api/data/v9.0/ImportSolution`, 400, `bad ${SECRET}`, { error: { message: `raw ${SECRET}` } }), [`left behind: ${SECRET}`]);
  sdk.importSolution = failAs(original);
  await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.notStrictEqual(err, original);
    assert.ok(!('cause' in err) || err.cause === undefined, 'the raw answer does not travel with it');
    assert.ok(!JSON.stringify({ ...err, message: err.message, stack: err.stack }).includes(SECRET), 'nothing of it — message, stack, properties — holds the secret');
    return true;
  });
});

test('the SDK\'s cleanup warnings are cleaned on a FAILURE as they are on a success: a secret withholds the warning, a plain value or an id is replaced, and the failure carries them', async () => {
  const { request } = importFiles();
  const { sdk } = sdkStub();
  sdk.importSolution = failAs(withWarnings(importFailureOf(400, 'the import was refused'), [
    `The staged copy of the package (stagesolutionupload 33333333-3333-4333-8333-333333333333) could not be deleted: HTTP 400 from ${ENV}/api/data/v9.0/stagesolutionuploads(33333333-3333-4333-8333-333333333333): cleanup diagnosis contains ${SECRET}. It stays until it is deleted.`,
    `another warning with ${CONNECTION_ID}`,
  ]));
  await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.deepStrictEqual(err.warnings, [WITHHELD('warning'), 'another warning with <redacted>']);
    const described = ss.describeFailure(err, () => { throw new Error('the raw warnings must not be read again'); });
    assert.deepStrictEqual(described.warnings, err.warnings);
    assert.ok(!JSON.stringify(described).includes(SECRET) && !JSON.stringify(described).includes(CONNECTION_ID));
    return true;
  });
});

test('warnings the SDK gives on an import reach the output, cleaned of values', async () => {
  const { request } = importFiles();
  const { sdk } = sdkStub();
  sdk.importSolution = async () => ({ appliedSettings: { environmentVariables: [], connectionReferences: [] }, warnings: [`The staged copy could not be deleted (${SECRET})`, `and (${CONNECTION_ID})`] });
  const result = await ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB });
  assert.deepStrictEqual(result.warnings, [WITHHELD('warning'), 'and (<redacted>)']);
});

// The ones the platform's own answer says nothing about: a gateway's 504, a 500, a 401, 408 and no answer at all are NOT "refused" — the work may be running.
test('an import whose answer does not say it was refused says it was sent once and was not repeated, and names the job to look up; a refusal does not', async () => {
  for (const status of [500, 502, 503, 504, 401, 408]) {
    const { request } = importFiles();
    const { sdk } = sdkStub();
    sdk.importSolution = failAs(importFailureOf(status, 'the gateway gave up'));
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog: async () => null }), (err) => {
      assert.strictEqual(err.statusCode, status);
      assert.strictEqual(err.message, `HTTP ${status} from ${ENV}/api/data/v9.0/ImportSolution: the gateway gave up The request was sent once and was not sent again: the environment may have accepted it.${ss.lookUpJob(JOB)}`, String(status));
      assert.strictEqual(err.importJobId, JOB);
      return true;
    }, String(status));
  }
  // Answers that say the platform looked at the request and refused it: nothing to look up.
  for (const status of [400, 403, 404, 409, 412, 429]) {
    const { request } = importFiles();
    const { sdk } = sdkStub();
    sdk.importSolution = failAs(importFailureOf(status, 'refused'));
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog: async () => null }), (err) => {
      assert.strictEqual(err.message, `HTTP ${status} from ${ENV}/api/data/v9.0/ImportSolution: refused`, String(status));
      return true;
    }, String(status));
  }
  // No answer at all: the client's own message already names the job — it is not said twice.
  const { request } = importFiles();
  const { sdk } = sdkStub();
  sdk.importSolution = failAs(importFailureOf(0, `ImportSolution got no answer (Request timed out). It was not sent again: the environment may still be working on it, or may have finished.${ss.lookUpJob(JOB)}`));
  await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message.split('Look up import job').length, 2, err.message);
    return true;
  });
  // A failure with no status that did not come from the no-answer wrapper — no token could be had — happened BEFORE the request left: nothing is said of what the environment may have accepted.
  const { request: unsent } = importFiles();
  const { sdk: tokenless } = sdkStub();
  tokenless.importSolution = failAs(importFailureOf(0, "Failed to acquire an Azure CLI token for the environment. Run 'az login' first."));
  await assert.rejects(ss.runImport(unsent, { sdkFirst: stubSdkFirst(tokenless), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `Transport failure reaching ${ENV}/api/data/v9.0/ImportSolution: Failed to acquire an Azure CLI token for the environment. Run 'az login' first.`);
    assert.strictEqual(err.importJobId, JOB);
    return true;
  });
});

// What the SDK does with the error a client throws: its message is the status line followed by the client's own message, and the client's error is kept as its `cause` (measured on the real bundle in
// solution-settings-real-bundle.test.js). The plugin's own sentence for a request that got no answer is recognised by the identity of that cause — never by how a message reads.
test('the plugin\'s own "got no answer" sentence is shown whole, whatever short value the file fills in; a message that only reads like it, or has anything added to it, is sanitized like any other', async () => {
  const settings = { EnvironmentVariables: [{ SchemaName: 'new_Text', Value: 'not' }], ConnectionReferences: [] }; // "not" is a word of the plugin's sentence, and nothing is known of the variable's type here
  // The error the plugin's client throws for a request that got no answer: the one object the plugin marks as its own composition.
  let composed;
  const client = ss.solutionHttpClient(ENV, { getToken: () => 'dataverse-token', sleep: async () => {}, request: async () => ({ error: 'Request timed out' }) });
  await assert.rejects(client.post(`${ENV}/api/data/v9.0/StageSolution`, { CustomizationFile: 'x' }), (err) => { composed = err; return true; });
  const lead = `Transport failure reaching ${ENV}/api/data/v9.0/ImportSolution: `;
  const asTheSdkWraps = (message, cause) => new ConnectionError(`${ENV}/api/data/v9.0/ImportSolution`, 0, message, cause);
  const run = async (error) => {
    const { request } = importFiles(settings);
    const { sdk } = sdkStub();
    sdk.importSolution = failAs(error);
    return ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog: async () => null }).then(
      () => assert.fail('the import was expected to fail'),
      (err) => err.message
    );
  };
  // The status line and the plugin's own sentence, exactly: not looked at again.
  assert.strictEqual(await run(asTheSdkWraps(composed.message, composed)), `${lead}${composed.message}`);
  // The same words with a cause the plugin never made (nothing marks it), or with no cause: another party's text, so sanitized — and "not" is a whole word in it.
  assert.strictEqual(await run(asTheSdkWraps(composed.message, new Error(composed.message))), `${lead}${WITHHELD('message')}`);
  assert.strictEqual(await run(importFailureOf(0, composed.message)), `${lead}${WITHHELD('message')}`);
  // The plugin's error as the cause, with something else in the message — the SDK's or the platform's words, before or after: sanitized.
  assert.strictEqual(await run(asTheSdkWraps(`${composed.message} The platform added a word.`, composed)), `${lead}${WITHHELD('message')}`);
  assert.strictEqual(await run(asTheSdkWraps(`Added first. ${composed.message}`, composed)), `${lead}${WITHHELD('message')}`);
  // The transport's own words are cleaned where the plugin reads them: a value they quote is withheld there, and the sentence around it stays.
  const sanitize = ss.sanitizer({ EnvironmentVariables: [{ SchemaName: 'new_Text', Value: 'timed' }] });
  const cleaning = ss.solutionHttpClient(ENV, { getToken: () => 'dataverse-token', sleep: async () => {}, request: async () => ({ error: 'Request timed out' }) }, { sanitize });
  await assert.rejects(cleaning.post(`${ENV}/api/data/v9.0/StageSolution`, { CustomizationFile: 'x' }), (err) => {
    assert.strictEqual(err.message, `StageSolution got no answer (${WITHHELD('transport text')}). It was not sent again: the environment may still be working on it, or may have finished.`);
    return true;
  });
});

// --- an import the platform refused before it started ----------------------------------------------------------------------------------------
// ImportSolution is sent ONCE, whatever answers it — with ONE exception: the platform's refusal to START it because another operation (a publish, another import) holds the
// environment's customization lock, HTTP 429 with the lock's code, when the import job the refused send named does not exist (importjobs(<id>) answers 404). Then the run waits
// and sends the import again under a NEW job id, at most five sends in all. Each test below pins one side of that rule: what is sent again, and — as much — everything that is not.
const { LOCK_REFUSAL } = require('./helpers/fake-power-platform.js');

const GUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
const lockAnswer = ({ code = LOCK_REFUSAL.code, message = LOCK_REFUSAL.message, headers = {}, statusCode = LOCK_REFUSAL.status } = {}) => ({ statusCode, headers, body: JSON.stringify({ error: { code, message } }) });
const NO_SUCH_JOB = { statusCode: 404, headers: {}, body: JSON.stringify({ error: { code: '0x80040217', message: `importjob With Id = ${JOB} Does Not Exist` } }) };
const isJobRead = (options) => /\/importjobs\(/.test(new URL(options.url).pathname);
const isImportSend = (options) => /\/ImportSolution$/.test(new URL(options.url).pathname);
const jobIdOf = (options) => JSON.parse(options.body).ImportJobId;
// The request the SDK sends (api/SolutionApi.ts importSolution): the package, then the job id, then — for a file that fills something in — the parameters.
const importBody = (jobId = JOB) => ({
  OverwriteUnmanagedCustomizations: false,
  PublishWorkflows: false,
  CustomizationFile: 'UEsDBA==',
  ImportJobId: jobId,
  ComponentParameters: [{ '@odata.type': 'Microsoft.Dynamics.CRM.environmentvariablevalue', schemaname: 'new_Url', value: 'x' }],
});

// A platform that answers the ImportSolution sends in turn (`sends`: a response, or a function of the request) and every read of an import job with `job`; `events` is what it saw
// and was asked to wait for, in order. The transport and the clock are the client's own seams, so the real client runs over it.
function platformFor({ sends, job = NO_SUCH_JOB }) {
  const requests = [];
  const waits = [];
  const events = [];
  const http = {
    getToken: () => 'dataverse-token',
    sleep: async (ms) => { waits.push(ms); events.push(`wait ${ms}`); },
    request: async (options) => {
      requests.push(options);
      if (isJobRead(options)) {
        events.push(`read ${decodeURIComponent(new URL(options.url).pathname.split('/').pop())}`);
        return typeof job === 'function' ? job(options) : job;
      }
      events.push(`send ${isImportSend(options) ? jobIdOf(options) : new URL(options.url).pathname.split('/').pop()}`);
      const next = sends.shift();
      return typeof next === 'function' ? next(options) : next;
    },
  };
  return { http, requests, waits, events, imports: () => requests.filter(isImportSend), jobReads: () => requests.filter(isJobRead) };
}
// The long-action client over such a platform; `told` is what the client says of each refusal it sends again, `stopped` what it says of a retry it ends for want of time, `unproven` what it says of a refusal
// it does not send again because the job does not prove that nothing was applied, and `observing: false` is a caller that gave it no listener.
function refusedImport({ sends, job, observing = true, http = {} }) {
  const platform = platformFor({ sends, job });
  const told = [];
  const stopped = [];
  const unproven = [];
  const exhausted = [];
  const listeners = { onImportRefused: (event) => told.push(event), onImportRetryStopped: (event) => stopped.push(event), onImportRetryUnproven: (event) => unproven.push(event), onImportRetryExhausted: (event) => exhausted.push(event) };
  const client = ss.solutionHttpClient(ENV, { ...platform.http, ...http }, observing ? listeners : {});
  return { ...platform, http: { ...platform.http, ...http }, client, told, stopped, unproven, exhausted, post: () => client.post(`${ENV}/api/data/v9.0/ImportSolution`, importBody()) };
}
// The same, over a clock that only the waits move: `now` is what the client reads, a wait moves it by its length, and a read of the job moves it by `readMs` — so what the client spends is measured, as
// it is when the waits are real. `sentAt` is when each send reached the platform, in milliseconds from the first. `lateMs` is how much later than asked each timer resumes (a timer that was delayed, a laptop
// that was suspended: the wait the client asked for is the least a real wait takes) and `job` is what every read of the job answers.
const CLOCK_ORIGIN = Date.UTC(2026, 9, 14, 12, 0, 0);
function clockedImport({ sends, readMs = 0, observing = true, lateMs = 0, job = NO_SUCH_JOB }) {
  const clock = { elapsed: 0, sentAt: [] };
  const answers = sends.map((answer) => (options) => {
    clock.sentAt.push(clock.elapsed);
    return typeof answer === 'function' ? answer(options, clock) : answer;
  });
  const waits = [];
  const world = refusedImport({
    sends: answers,
    job: () => { clock.elapsed += readMs; return job; },
    observing,
    http: { now: () => CLOCK_ORIGIN + clock.elapsed, sleep: async (ms) => { waits.push(ms); clock.elapsed += ms + lateMs; world.events.push(`wait ${ms}`); } },
  });
  return { ...world, clock, waits };
}
// A refusal that asks, in either form of Retry-After, to wait `seconds` from the moment it arrives.
const askingFor = (seconds) => ({ seconds: () => lockAnswer({ headers: { 'retry-after': String(seconds) } }), date: (options, clock) => lockAnswer({ headers: { 'retry-after': new Date(CLOCK_ORIGIN + clock.elapsed + seconds * 1000).toUTCString() } }) });

test('an import the platform refused before it started — HTTP 429 with the lock\'s code — is sent again under a NEW job id, once the job the refused send named is shown not to exist', async () => {
  const world = refusedImport({ sends: [lockAnswer(), ok()] });
  const res = await world.post();
  assert.strictEqual(res.status, 200, 'the second send\'s answer is the one the SDK reads');
  assert.strictEqual(world.imports().length, 2, 'the import was sent twice: the refused send, and one more');
  const [refusedId, newId] = world.imports().map(jobIdOf);
  assert.strictEqual(refusedId, JOB);
  assert.match(newId, GUID);
  assert.notStrictEqual(newId, JOB, 'a new job id — the refused one is never used again');
  // The same request in every other byte: the package, the parameters, the order of the keys — and the same hour to answer.
  assert.strictEqual(world.imports()[1].body, world.imports()[0].body.replace(JOB, newId));
  assert.deepStrictEqual(world.imports().map((r) => [r.method, r.timeout]), [['POST', 3600000], ['POST', 3600000]]);
  // In this order: the send, the wait, the one read of the refused send's job — the latest evidence before a second copy leaves — and the send again.
  assert.deepStrictEqual(world.events, [`send ${JOB}`, 'wait 10000', `read importjobs(${JOB})`, `send ${newId}`]);
  assert.deepStrictEqual(world.jobReads().map((r) => [r.method, new URL(r.url).search, new URL(r.url).origin]), [['GET', '?$select=importjobid', ENV]]);
  assert.deepStrictEqual(world.told, [{ importJobId: JOB, status: 429, code: '0x8004F064', waitedMs: 10000, nextImportJobId: newId }]);
});

test('the codes that make a refusal to start are exactly the five customization-lock codes whose documented text says "Cannot start" — pinned, each in either case of its hex digits — and no other code is', async () => {
  // The error reference's names: CustomizationLockEx_BothUnknown, _BlockedUnknown, _BothKnownSame, _BothKnownDifferent and _BlockingUnknown. Nothing else is in the set: SolutionConcurrencyFailure
  // (0x80071151) is left out because its documented text says the installation or removal FAILED, which does not say that nothing started (the next test).
  const family = ['0x8004F061', '0x8004F062', '0x8004F063', '0x8004F064', '0x8004F067'];
  assert.deepStrictEqual([...ss.LOCK_REFUSAL_CODES], family, 'the exact set');
  assert.ok(Object.isFrozen(ss.LOCK_REFUSAL_CODES));
  assert.ok(!ss.LOCK_REFUSAL_CODES.some((code) => code.toLowerCase() === '0x80071151'), '0x80071151 is not in it');
  for (const code of family) {
    for (const spelled of [code, `0x${code.slice(2).toLowerCase()}`, `0x${code.slice(2).toUpperCase()}`]) {
      const world = refusedImport({ sends: [lockAnswer({ code: spelled }), ok()] });
      assert.strictEqual((await world.post()).status, 200, spelled);
      assert.strictEqual(world.imports().length, 2, spelled);
      assert.strictEqual(world.told[0].code, code, `${spelled}: told as the reference spells it`);
    }
  }
});

// SolutionConcurrencyFailure (0x80071151) is documented as "The solution installation or removal failed due to the installation or removal of another solution at the same time. Please try again later." — an
// operation that FAILED, which does not say that nothing started, so the code alone does not make an import one to send again. What the platform was MEASURED to answer an import that meets another import or a
// publish is this code with a message that begins "Cannot start another [Import] because there is a previous [Import] running" ("Cannot start the requested operation [Import] because there is another [Publish]
// running" for a publish), and for that answer the import's own job decides, read once and at once (the tests below). An answer with this code whose message does not begin "Cannot start" is the first and the
// last send, as every answer to an import was before the resend existed.
const { CONCURRENCY_REFUSAL } = require('./helpers/fake-power-platform.js');
const { LOGS, jobRow, jobAnswer, CAPTURED_STATES, REFUSAL_STATUS } = require('./helpers/import-job-logs.js');
const concurrencyAnswer = (options = {}) => lockAnswer({ code: CONCURRENCY_REFUSAL.code, message: CONCURRENCY_REFUSAL.message, ...options });
// The read of a refused send's job asks for the three columns the rule looks at, and no other: `progress` is the same for a running import and a refused one, and `solutionid` is set for a solution never made.
const PROOF_READ = '?$select=importjobid,completedon,data';
const DOCUMENTED_CONCURRENCY_TEXT = 'The solution installation or removal failed due to the installation or removal of another solution at the same time. Please try again later.';

test('SolutionConcurrencyFailure (0x80071151) is a refusal to start only when its message says so — "Cannot start …" — and every other answer with the code is sent ONCE: no job read, no wait, nothing told, the answer back as it came', async () => {
  const plain = (value) => ({ statusCode: 429, headers: {}, body: typeof value === 'string' ? value : JSON.stringify(value) });
  const cases = [
    ['its documented text', lockAnswer({ code: '0x80071151', message: DOCUMENTED_CONCURRENCY_TEXT, headers: { 'retry-after': '5' } })],
    ['a message that says "Cannot start" further in', lockAnswer({ code: '0x80071151', message: `The import failed. Cannot start another one: ${DOCUMENTED_CONCURRENCY_TEXT}` })],
    ['a message that begins "Could not start"', lockAnswer({ code: '0x80071151', message: 'Could not start the requested operation [Import] because there is another [Publish] running at this moment.' })],
    ['a message in another language', lockAnswer({ code: '0x80071151', message: 'No se puede iniciar otro [Import] porque hay un [Import] anterior en ejecución.' })],
    ['an empty message', lockAnswer({ code: '0x80071151', message: '' })],
    ['a message that is null', plain({ error: { code: '0x80071151', message: null } })],
    ['a message that is not text', plain({ error: { code: '0x80071151', message: ['Cannot start another [Import]'] } })],
    ['no message at all', plain({ error: { code: '0x80071151' } })],
    ['a body that is not JSON', plain('Cannot start another [Import] because there is a previous [Import] running')],
    ['the words and the code outside "error"', plain({ code: '0x80071151', message: REFUSAL_STATUS })],
    ['the code spelled as a decimal', plain({ error: { code: -2147020463, message: REFUSAL_STATUS } })],
    ['the code in a text around it', lockAnswer({ code: 'error 0x80071151', message: REFUSAL_STATUS })],
  ];
  for (const status of [400, 401, 403, 404, 408, 409, 412, 500, 502, 503, 504]) cases.push([`the code and the words under HTTP ${status}`, lockAnswer({ code: '0x80071151', message: REFUSAL_STATUS, statusCode: status })]);
  for (const [what, answer] of cases) {
    const world = refusedImport({ sends: [answer, ok()] });
    const res = await world.post();
    assert.strictEqual(res.status, answer.statusCode, `${what}: the answer comes back as it came`);
    assert.strictEqual(world.imports().length, 1, `${what}: one send`);
    assert.deepStrictEqual([world.jobReads().length, world.waits.length, world.told.length, world.unproven.length, world.stopped.length], [0, 0, 0, 0, 0], `${what}: no job read, no wait, nothing told`);
  }
});

test('concurrencyRefusalOf and lockRefusalOf are two matchers that never answer the same refusal — 0x80071151 needs HTTP 429 and a message that begins "Cannot start", the lock codes need HTTP 429 and the code alone, each in either case of its hex digits', () => {
  const answer = (code, message, statusCode = 429) => lockAnswer({ code, message, statusCode });
  assert.deepStrictEqual(ss.concurrencyRefusalOf(answer('0x80071151', REFUSAL_STATUS)), { status: 429, code: '0x80071151' });
  assert.deepStrictEqual(ss.concurrencyRefusalOf(answer('0X80071151', '  Cannot start the requested operation')), { status: 429, code: '0x80071151' }, 'white space before the words, the other case of the code');
  assert.strictEqual(ss.concurrencyRefusalOf(answer('0x80071151', DOCUMENTED_CONCURRENCY_TEXT)), null);
  assert.strictEqual(ss.lockRefusalOf(answer('0x80071151', REFUSAL_STATUS)), null, 'the lock matcher is the five codes only');
  for (const code of ss.LOCK_REFUSAL_CODES) {
    assert.strictEqual(ss.concurrencyRefusalOf(answer(code, LOCK_REFUSAL.message)), null, code);
    assert.deepStrictEqual(ss.lockRefusalOf(answer(code, LOCK_REFUSAL.message)), { status: 429, code }, code);
  }
  for (const res of [null, undefined, {}, { error: 'Request timed out' }, { statusCode: 429 }, { statusCode: 429, body: '' }, { statusCode: 429, body: 'null' }, { statusCode: 429, body: '[]' }, { statusCode: 429, body: '{"error":"Cannot start"}' }]) {
    assert.strictEqual(ss.concurrencyRefusalOf(res), null, JSON.stringify(res));
    assert.strictEqual(ss.lockRefusalOf(res), null, JSON.stringify(res));
  }
  // A body the transport already parsed is read too.
  assert.deepStrictEqual(ss.concurrencyRefusalOf({ statusCode: 429, body: { error: { code: '0x80071151', message: 'Cannot start another [Import]' } } }), { status: 429, code: '0x80071151' });
});

test('an import refused with 0x80071151 ("Cannot start …") whose job does not exist is sent again under a NEW job id — the job is read ONCE and AT ONCE, before the wait, and the 404 is the proof', async () => {
  for (const [what, message] of [
    ['an import that met another import', REFUSAL_STATUS],
    ['the two-part message of an import that met a publish', CONCURRENCY_REFUSAL.message],
    ['the lock\'s own words', LOCK_REFUSAL.message],
  ]) {
    for (const spelled of ['0x80071151', '0X80071151']) {
      const world = refusedImport({ sends: [concurrencyAnswer({ code: spelled, message }), ok()] });
      const res = await world.post();
      assert.strictEqual(res.status, 200, `${what}: the second send's answer is the one the SDK reads`);
      assert.strictEqual(world.imports().length, 2, `${what}: the refused send, and one more`);
      const [refusedId, newId] = world.imports().map(jobIdOf);
      assert.strictEqual(refusedId, JOB);
      assert.match(newId, GUID);
      assert.notStrictEqual(newId, JOB, 'a new job id — the refused one is never used again');
      assert.strictEqual(world.imports()[1].body, world.imports()[0].body.replace(JOB, newId), 'the same request in every other byte');
      // In this order: the send, the ONE read of the refused send's job — at once — the wait, and the send again. The row of a refused send is final when the answer is in hand, so nothing is read again after the wait.
      assert.deepStrictEqual(world.events, [`send ${JOB}`, `read importjobs(${JOB})`, 'wait 10000', `send ${newId}`], what);
      assert.deepStrictEqual(world.jobReads().map((r) => [r.method, new URL(r.url).search, new URL(r.url).origin]), [['GET', PROOF_READ, ENV]], what);
      assert.deepStrictEqual(world.told, [{ importJobId: JOB, status: 429, code: '0x80071151', waitedMs: 10000, nextImportJobId: newId, proof: 'job-absent' }], `${what}: told as the reference spells it, with how it was proven`);
      assert.deepStrictEqual([world.stopped, world.unproven], [[], []], what);
    }
  }
});

test('an import refused with 0x80071151 whose job EXISTS is sent again when the job is finished and its log says the import failed with that error before it processed the solution manifest or any component — the log is the proof', async () => {
  for (const state of CAPTURED_STATES.filter((captured) => captured.group === 'refused-final')) {
    const world = refusedImport({ sends: [concurrencyAnswer(), ok()], job: jobAnswer(jobRow(state.data())) });
    const res = await world.post();
    assert.strictEqual(res.status, 200, state.name);
    assert.strictEqual(world.imports().length, 2, state.name);
    const newId = jobIdOf(world.imports()[1]);
    assert.deepStrictEqual(world.events, [`send ${JOB}`, `read importjobs(${JOB})`, 'wait 10000', `send ${newId}`], state.name);
    assert.deepStrictEqual(world.jobReads().map((r) => new URL(r.url).search), [PROOF_READ], state.name);
    assert.deepStrictEqual(world.told, [{ importJobId: JOB, status: 429, code: '0x80071151', waitedMs: 10000, nextImportJobId: newId, proof: 'job-refused-before-any-component' }], state.name);
    assert.deepStrictEqual([world.stopped, world.unproven], [[], []], state.name);
  }
});

test('a refusal with 0x80071151 whose job does NOT prove that nothing was applied is sent ONCE — one read at once, no wait, no second send — and the client says why it did not send again', async () => {
  const answer = (statusCode, body = {}) => ({ statusCode, headers: {}, body: JSON.stringify(body) });
  const cases = [
    // [what, the answer to the read of the job, why it is not proof]
    ['an import that is still running (completedon is empty)', jobAnswer(jobRow(LOGS.running(), { completed: false })), 'unfinished'],
    ['a refusal\'s row that is complete before its log is (completedon is set, the root has no verdict)', jobAnswer(jobRow(LOGS.unfinishedRoot())), 'not-a-failure'],
    ['an import that applied the package', jobAnswer(jobRow(LOGS.succeeded())), 'not-a-failure'],
    ['an import that failed AFTER it began (error 0x80048033, the manifest processed)', jobAnswer(jobRow(LOGS.failedAfterStart())), 'other-error'],
    ['the refusal\'s own log with the solution manifest processed', jobAnswer(jobRow(LOGS.refused({ manifest: 'true' }))), 'manifest-processed'],
    ['the refusal\'s own log with a component processed', jobAnswer(jobRow(LOGS.refused({ components: 'true' }))), 'component-processed'],
    ['a log that is not an XML document', jobAnswer(jobRow('<importexportxml succeeded="failure"')), 'log-unreadable'],
    ['a row with no log', jobAnswer(jobRow(null)), 'no-log'],
    ['an answer that is not a row', answer(200, 'plain'), 'no-log'],
    ['an empty 200', { statusCode: 200, headers: {}, body: '' }, 'no-log'],
    ['a 204', answer(204, ''), 'unreadable'],
    ['a 400', answer(400), 'unreadable'], ['a 401', answer(401), 'unreadable'], ['a 403', answer(403), 'unreadable'], ['a 408', answer(408), 'unreadable'], ['a 412', answer(412), 'unreadable'],
    ['a 429', answer(429), 'unreadable'], ['a 500', answer(500), 'unreadable'], ['a 502', answer(502), 'unreadable'], ['a 503', answer(503), 'unreadable'], ['a 504', answer(504), 'unreadable'],
    ['no answer', { error: 'Request timed out' }, 'no-answer'],
    ['an answer cut off', { error: 'Connection closed before the response completed', incompleteResponse: true, statusCode: 200 }, 'no-answer'],
    ['a transport that throws', () => { throw new Error('socket hang up'); }, 'no-answer'],
  ];
  for (const [what, job, why] of cases) {
    const world = refusedImport({ sends: [concurrencyAnswer(), ok()], job });
    const res = await world.post();
    assert.deepStrictEqual([res.status, res.body.error.code], [429, '0x80071151'], `${what}: the refusal comes back as it came`);
    assert.deepStrictEqual([world.imports().length, world.jobReads().length, world.waits.length, world.told.length, world.stopped.length], [1, 1, 0, 0, 0], `${what}: one send, one read, no wait, nothing sent again`);
    assert.deepStrictEqual(world.unproven.map((event) => [event.importJobId, event.status, event.code, event.why]), [[JOB, 429, '0x80071151', why]], what);
    assert.strictEqual(typeof world.unproven[0].reason, 'string', `${what}: and in words`);
  }
  // A read that cannot even have a token is no proof either.
  let tokens = 0;
  const tokenless = refusedImport({ sends: [concurrencyAnswer(), ok()], http: { getToken: () => { tokens += 1; return tokens === 1 ? 'dataverse-token' : null; } } });
  assert.strictEqual((await tokenless.post()).status, 429);
  assert.deepStrictEqual([tokenless.imports().length, tokenless.unproven.map((event) => event.why)], [1, ['no-answer']]);
});

test('the lock codes are read AFTER the wait, by the 404 alone, as before — and 0x80071151 is read AT ONCE, by the row — and a run that meets both proves each by its own rule', async () => {
  // The lock code first: wait, read (`$select=importjobid`), send. Then 0x80071151: read (the three columns), wait, send. Then the platform takes it.
  const world = refusedImport({ sends: [lockAnswer(), concurrencyAnswer(), ok()] });
  const res = await world.post();
  assert.strictEqual(res.status, 200);
  const [first, second, third] = world.imports().map(jobIdOf);
  assert.deepStrictEqual(world.events, [
    `send ${first}`, 'wait 10000', `read importjobs(${first})`,
    `send ${second}`, `read importjobs(${second})`, 'wait 20000',
    `send ${third}`,
  ]);
  assert.deepStrictEqual(world.jobReads().map((r) => new URL(r.url).search), ['?$select=importjobid', PROOF_READ]);
  assert.deepStrictEqual(world.told.map((event) => [event.code, 'proof' in event ? event.proof : '(none)']), [['0x8004F064', '(none)'], ['0x80071151', 'job-absent']], 'a lock code\'s refusal is told as it always was');
});

test('an import refused with 0x80071151 is sent FIVE times at most, on the lock codes\' schedule: each of the first four refusals is read at once and waited out (10, 20, 40, 60 s), the fifth is the failure — no read, no wait', async () => {
  const world = refusedImport({ sends: Array.from({ length: 6 }, () => concurrencyAnswer()) }); // a sixth would be answered, too: it must never be asked
  const res = await world.post();
  assert.deepStrictEqual([res.status, res.body.error.code], [429, '0x80071151']);
  assert.strictEqual(world.imports().length, 5);
  assert.strictEqual(world.jobReads().length, 4, 'one read for each of the first four refusals');
  assert.deepStrictEqual(world.waits, [10000, 20000, 40000, 60000]);
  const ids = world.imports().map(jobIdOf);
  assert.strictEqual(new Set(ids).size, 5, 'every send carried a job id of its own');
  assert.deepStrictEqual(world.events, [
    `send ${ids[0]}`, `read importjobs(${ids[0]})`, 'wait 10000',
    `send ${ids[1]}`, `read importjobs(${ids[1]})`, 'wait 20000',
    `send ${ids[2]}`, `read importjobs(${ids[2]})`, 'wait 40000',
    `send ${ids[3]}`, `read importjobs(${ids[3]})`, 'wait 60000',
    `send ${ids[4]}`,
  ]);
  assert.deepStrictEqual(world.told.map((event) => [event.importJobId, event.nextImportJobId, event.waitedMs, event.proof]), ids.slice(0, 4).map((id, i) => [id, ids[i + 1], [10000, 20000, 40000, 60000][i], 'job-absent']));
  assert.deepStrictEqual([world.stopped, world.unproven], [[], []], 'a refusal after the fifth send is the sends running out, not the time and not a job that proves nothing');
  assert.deepStrictEqual(world.exhausted, [{ importJobId: ids[4], status: 429, code: '0x80071151' }], 'the listener is told which refusal the sends ran out on — the job it carried, which was not looked at');
});

// A refusal with 0x80071151 that asks, with Retry-After, to wait `seconds`.
const askingConcurrency = (seconds) => () => concurrencyAnswer({ headers: { 'retry-after': String(seconds) } });

test('a Retry-After on a refusal with 0x80071151 is waited WHOLE, as for the lock codes — and one that does not fit the five minutes ends the retry AFTER the one read, which comes first for this refusal, with no wait and no send', async () => {
  const waited = clockedImport({ sends: [askingConcurrency(45), ok()] });
  assert.strictEqual((await waited.post()).status, 200);
  assert.deepStrictEqual(waited.waits, [45000]);
  assert.deepStrictEqual(waited.events.slice(0, 3), [`send ${JOB}`, `read importjobs(${JOB})`, 'wait 45000']);
  const world = clockedImport({ sends: [askingConcurrency(600), ok()] });
  const res = await world.post();
  assert.strictEqual(res.status, 429);
  assert.deepStrictEqual([world.imports().length, world.jobReads().length, world.waits.length, world.told.length], [1, 1, 0, 0], 'one send, the one read, nothing waited for');
  assert.deepStrictEqual(world.stopped, [{ importJobId: JOB, waitMs: 600000, spentMs: 0 }]);
  assert.deepStrictEqual(world.unproven, []);
});

test('the one read of the job counts in the five minutes: a refusal whose wait no longer fits once the read is done ends the retry with the platform\'s own refusal — the wait is never shortened to fit', async () => {
  // Each read takes 100 s. Send one is refused at 0 s, its read ends at 100 s, the platform asks for 120 s: 220 s are inside the five minutes, so the run waits and sends again at 220 s.
  // That send is refused too and its read ends at 320 s, which leaves nothing for a wait of 120 s — nor for the 20 s the schedule would give.
  const world = clockedImport({ sends: [askingConcurrency(120), askingConcurrency(120), ok()], readMs: 100000 });
  const res = await world.post();
  assert.strictEqual(res.status, 429);
  assert.deepStrictEqual(world.waits, [120000]);
  assert.deepStrictEqual(world.clock.sentAt, [0, 220000], 'a second send was made; a third was not');
  assert.strictEqual(world.jobReads().length, 2);
  assert.strictEqual(world.told.length, 1);
  assert.deepStrictEqual(world.stopped, [{ importJobId: world.imports().map(jobIdOf)[1], waitMs: 120000, spentMs: 320000 }]);
});

// The five minutes are asked for AFTER a wait as well as before it. What the client asks the timer for is the least a wait takes: a timer that is delayed, a busy machine, a laptop that is suspended resumes it
// later, so a wait that fitted when it began can end after the five minutes — and a send made then is a send the budget does not allow. `lateMs` is how much later than asked each timer resumes.
const LATE_PROOFS = [
  ['its job does not exist (a 404)', NO_SUCH_JOB],
  ['its job is finished and its log says the import was refused at the lock', jobAnswer(jobRow(LOGS.refused()))],
];

test('a wait that ends AFTER the five minutes — a timer that resumed late — ends the retry before the send, for a refusal with 0x80071151 proven by a 404 or by the log: nothing is sent, nothing is told as sent again, and the listener hears what was spent — to the millisecond', async () => {
  // 100 s late ends at 110 s and 290 s late at exactly 300 s: both are inside the five minutes and send. One millisecond more is past them, and does not.
  for (const [lateMs, expectedSends] of [[100000, 2], [290000, 2], [290001, 1]]) {
    const world = clockedImport({ sends: [() => concurrencyAnswer(), ok()], lateMs });
    await world.post();
    assert.strictEqual(world.imports().length, expectedSends, `a timer ${lateMs} ms late`);
    assert.deepStrictEqual(world.stopped, expectedSends === 1 ? [{ importJobId: JOB, waitMs: null, spentMs: 300001 }] : [], `a timer ${lateMs} ms late`);
  }
  for (const [what, job] of LATE_PROOFS) {
    // The first refusal: the wait is 10 s, which fits, and the timer resumes at 360 s.
    const first = clockedImport({ sends: [() => concurrencyAnswer(), ok()], lateMs: 350000, job });
    const res = await first.post();
    assert.strictEqual(res.status, 429, `${what}: the refusal comes back as it came`);
    assert.strictEqual(res.body.error.code, '0x80071151', what);
    assert.deepStrictEqual(first.events, [`send ${JOB}`, `read importjobs(${JOB})`, 'wait 10000'], `${what}: the read at once, the wait, and no send after it`);
    assert.deepStrictEqual([first.imports().length, first.clock.sentAt, first.told, first.unproven], [1, [0], [], []], what);
    assert.deepStrictEqual(first.stopped, [{ importJobId: JOB, waitMs: null, spentMs: 360000 }], `${what}: what was spent is what the clock says, and not what the wait asked for`);
    // A later refusal: the first wait is late but inside the five minutes (10 s asked, 150 s taken), the second (20 s asked, 160 s taken) ends at 310 s.
    const later = clockedImport({ sends: [() => concurrencyAnswer(), () => concurrencyAnswer(), ok()], lateMs: 140000, job });
    assert.strictEqual((await later.post()).status, 429, what);
    const [firstId, secondId] = later.imports().map(jobIdOf);
    assert.strictEqual(later.imports().length, 2, `${what}: a second send was made, a third was not`);
    assert.deepStrictEqual(later.clock.sentAt, [0, 150000], what);
    assert.deepStrictEqual(later.events, [`send ${firstId}`, `read importjobs(${firstId})`, 'wait 10000', `send ${secondId}`, `read importjobs(${secondId})`, 'wait 20000'], what);
    assert.strictEqual(later.told.length, 1, `${what}: the first refusal was sent again, the second was not`);
    assert.deepStrictEqual(later.stopped, [{ importJobId: secondId, waitMs: null, spentMs: 310000 }], what);
  }
});

test('the same late timer ends the retry for a lock code — after the read that follows its wait, as ever — and a timer that is late but ends inside the five minutes, or exactly at them, sends as it always did', async () => {
  const lock = clockedImport({ sends: [() => lockAnswer(), ok()], lateMs: 350000 });
  assert.strictEqual((await lock.post()).status, 429);
  assert.deepStrictEqual(lock.events, [`send ${JOB}`, 'wait 10000', `read importjobs(${JOB})`], 'the proof of a lock code is read after the wait');
  assert.deepStrictEqual([lock.imports().length, lock.told], [1, []]);
  assert.deepStrictEqual(lock.stopped, [{ importJobId: JOB, waitMs: null, spentMs: 360000 }]);
  // 100 s late ends at 110 s and 290 s late at exactly 300 s: both send. One millisecond more is past the five minutes, and does not.
  for (const [lateMs, expectedSends] of [[100000, 2], [290000, 2], [290001, 1]]) {
    const world = clockedImport({ sends: [() => lockAnswer(), ok()], lateMs });
    await world.post();
    assert.strictEqual(world.imports().length, expectedSends, `a timer ${lateMs} ms late`);
    assert.deepStrictEqual(world.events.slice(0, 3), [`send ${JOB}`, 'wait 10000', `read importjobs(${JOB})`], `a timer ${lateMs} ms late: the read after the wait, as ever`);
    assert.deepStrictEqual(world.stopped, expectedSends === 1 ? [{ importJobId: JOB, waitMs: null, spentMs: 300001 }] : [], `a timer ${lateMs} ms late`);
  }
});

test('a client with no listener never reads the job or waits for a refusal with 0x80071151 either: the retry belongs to the caller that asked for it', async () => {
  const world = refusedImport({ sends: [concurrencyAnswer(), ok()], observing: false });
  assert.strictEqual((await world.post()).status, 429);
  assert.deepStrictEqual([world.imports().length, world.jobReads().length, world.waits.length], [1, 0, 0]);
});

test('a refusal with 0x80071151 is ImportSolution\'s alone: a refused ExportSolution or StageSolution is sent once, job or no job', async () => {
  for (const [action, body] of [['ExportSolution', { SolutionName: 'x' }], ['StageSolution', { CustomizationFile: 'x' }]]) {
    const world = refusedImport({ sends: [concurrencyAnswer(), ok()] });
    const res = await world.client.post(`${ENV}/api/data/v9.0/${action}`, body);
    assert.strictEqual(res.status, 429, action);
    assert.deepStrictEqual([world.requests.length, world.waits.length], [1, 0], `${action}: one request, no job read, no wait`);
  }
});

test('import: a refusal with 0x80071151 that is sent again is reported — each refusal with how its job proved it — and a run that met only 404s says what it always said, with the code it was refused with', async () => {
  const { request } = importFiles();
  const platform = platformFor({ sends: [concurrencyAnswer(), concurrencyAnswer(), ok()] });
  const result = await ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB });
  const ids = platform.imports().map(jobIdOf);
  assert.deepStrictEqual(result, {
    source: 'sdk',
    importJobId: ids[2],
    applied: APPLIED,
    importAttempts: 3,
    importRefusals: [
      { importJobId: JOB, status: 429, code: '0x80071151', waitedMs: 10000, proof: 'job-absent' },
      { importJobId: ids[1], status: 429, code: '0x80071151', waitedMs: 20000, proof: 'job-absent' },
    ],
  });
  // The failure of a run that was refused at every send: the platform's own refusal, the count, and — for the last send, whose job was not looked at — what to look up before the import is run again.
  const refusedAlways = platformFor({ sends: Array.from({ length: 5 }, () => concurrencyAnswer()) });
  await assert.rejects(ss.runImport(importFiles().request, { http: refusedAlways.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${CONCURRENCY_REFUSAL.message} ${RESENT_AS('0x80071151')(5, '10 s, 20 s, 40 s, 60 s')} ${NOT_LOOKED_AT(refusedAlways.imports().map(jobIdOf)[4])}`);
    return true;
  });
});

// The sentences of a run that was refused with 0x80071151, by how the jobs of its refused sends proved it: all by their absence (the sentence of the lock codes), all by their logs, or some of each.
const RESENT_AS = (code) => (sends, waits) => `ImportSolution was sent ${sends} times. Each send before the last was refused before it started (HTTP 429, code ${code}: another operation held the environment's customization lock) and the job it named did not exist (importjobs(<id>) answered 404). The run waited ${waits} before sending again.`;
const RESENT_BY_LOG = (sends, waits, shown = 'importjobs(<id>) held the log of an import that failed with that error before it processed the solution or any component') => `ImportSolution was sent ${sends} times. Each send before the last was refused at the environment's customization lock (HTTP 429, code 0x80071151: another operation held it) before any of the package was applied, and its import job showed it: ${shown}. The run waited ${waits} before sending again.`;
const RESENT_BY_LOG_OR_404 = (sends, waits) => RESENT_BY_LOG(sends, waits, 'importjobs(<id>) answered 404, or held the log of an import that failed with that error before it processed the solution or any component');
// A job's answer that depends on which send's job is read: `proofs[i]` is the answer for the i-th job read.
const jobsAnsweringInOrder = (...answers) => { const left = [...answers]; return () => left.shift() || NO_SUCH_JOB; };

test('import: a refusal proven by the job\'s LOG is reported with the sentence that says how — and one proven by a 404 beside it says both', async () => {
  const refused = jobAnswer(jobRow(LOGS.refused()));
  const { request } = importFiles();
  const byLog = platformFor({ sends: [concurrencyAnswer(), ok()], job: refused });
  const result = await ss.runImport(request, { http: byLog.http, sdkFirst: importingOver(), importJobId: JOB });
  assert.deepStrictEqual(result.importRefusals, [{ importJobId: JOB, status: 429, code: '0x80071151', waitedMs: 10000, proof: 'job-refused-before-any-component' }]);
  assert.strictEqual(result.importAttempts, 2);
  // All five sends refused, each proven by its log: the platform's refusal, the count, and how each was proven.
  const allLogs = platformFor({ sends: Array.from({ length: 5 }, () => concurrencyAnswer()), job: refused });
  await assert.rejects(ss.runImport(importFiles().request, { http: allLogs.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${CONCURRENCY_REFUSAL.message} ${RESENT_BY_LOG(5, '10 s, 20 s, 40 s, 60 s')} ${NOT_LOOKED_AT(allLogs.imports().map(jobIdOf)[4])}`);
    assert.deepStrictEqual(err.importRefusals.map((refusal) => refusal.proof), Array(4).fill('job-refused-before-any-component'));
    return true;
  });
  // Some by a 404 (the first refusal is the commoner kind: no job at all), some by the log; the third is a job that is still running, which is no proof.
  const running = jobAnswer(jobRow(LOGS.running(), { completed: false }));
  const mixed = platformFor({ sends: [concurrencyAnswer(), concurrencyAnswer(), concurrencyAnswer()], job: jobsAnsweringInOrder(NO_SUCH_JOB, refused, running) });
  await assert.rejects(ss.runImport(importFiles().request, { http: mixed.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.statusCode, 429);
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${CONCURRENCY_REFUSAL.message} ${RESENT_BY_LOG_OR_404(3, '10 s, 20 s')} ${NOT_PROVEN(WHY.unfinished, mixed.imports().map(jobIdOf)[2])}`);
    return true;
  });
});

// What a refusal that is not sent again adds to its failure, word for word: why the job is no proof (`reason`), and what to do.
const NOT_PROVEN = (reason, id = JOB) => `ImportSolution was not sent again: the platform refused it (HTTP 429, code 0x80071151: another operation held the environment's customization lock), but ${reason}, so it is not proven that the refused send applied nothing of the package. Look up import job ${id} (importjobs(${id}) in the Web API, or the solution history in Maker) and run the import again only when that shows nothing was imported and the other operation has finished.`;
// What a refusal with 0x80071151 adds to the failure when it is the LAST send's — the sends ran out on it. Its job was not looked at (the four reads are for the refusals that can still be sent again, and their proofs
// are of four other jobs), so nothing shows that the send applied nothing: the failure says what to look up first, by the rule that decided the sends before it, and never "run the import again" without it.
const NOT_LOOKED_AT = (id = JOB) => `The last send was refused too (code 0x80071151), but this run sends the import 5 times at most and did not look at its import job, so it is not proven that it applied nothing of the package. Look up import job ${id} (importjobs(${id}) in the Web API, or the solution history in Maker) and run the import again only when that job does not exist (a 404), or is finished and its log says the import failed with that error before it processed the solution or any component, and the other operation has finished. Anything else means that the import may be running or may have applied the package: do not run it again.`;
// What the advice said before, and still says for a lock code: the documented text of those codes says the operation could not be started.
const RUN_AGAIN = 'It sends at most 5 times: run the import again when the other operation has finished.';
const WHY = {
  unfinished: 'its import job is not finished (completedon is empty)',
  noLog: 'its import job has no log to read',
  unreadable: 'the log of its import job is not an XML document this script can read',
  notFailure: 'the log of its import job does not say that the import failed',
  otherError: 'the log of its import job says the import failed with error 0x80048033, not with 0x80071151',
  manifestProcessed: 'the log of its import job shows that the import began to apply the package (its solution manifest is processed)',
  componentProcessed: 'the log of its import job shows that the import began to apply components of the package',
  http: (status) => `the read of its import job answered HTTP ${status}, which is neither the job nor a 404`,
  noAnswer: 'the read of its import job got no answer',
};

test('import: a refusal whose job does not prove that nothing was applied is the platform\'s own failure with a sentence that says why and what to do — one send, no count, no list — and pac is not asked', async () => {
  const cases = [
    [jobAnswer(jobRow(LOGS.running(), { completed: false })), WHY.unfinished],
    [jobAnswer(jobRow(LOGS.succeeded())), WHY.notFailure],
    [jobAnswer(jobRow(LOGS.unfinishedRoot())), WHY.notFailure],
    [jobAnswer(jobRow(LOGS.failedAfterStart())), WHY.otherError],
    [jobAnswer(jobRow(LOGS.refused({ manifest: 'true' }))), WHY.manifestProcessed],
    [jobAnswer(jobRow(LOGS.refused({ components: 'true' }))), WHY.componentProcessed],
    [jobAnswer(jobRow('<importexportxml>')), WHY.unreadable],
    [jobAnswer(jobRow(null)), WHY.noLog],
    [{ statusCode: 503, headers: {}, body: '{}' }, WHY.http(503)],
    [{ statusCode: 403, headers: {}, body: JSON.stringify({ error: { message: 'principal user is missing prvReadImportJob privilege' } }) }, WHY.http(403)],
    [{ error: 'Request timed out' }, WHY.noAnswer],
  ];
  for (const [job, reason] of cases) {
    const { request } = importFiles();
    const platform = platformFor({ sends: [concurrencyAnswer(), ok()], job });
    const { pac, calls } = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
    await assert.rejects(ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, pac, readJobLog: async () => null }), (err) => {
      assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${CONCURRENCY_REFUSAL.message} ${NOT_PROVEN(reason)}`);
      assert.deepStrictEqual(ss.describeFailure(err), { ok: false, error: err.message, code: 'CONNECTION_ERROR', status: 429, importJobId: JOB });
      return true;
    }, reason);
    assert.deepStrictEqual([platform.imports().length, platform.jobReads().length, platform.waits.length, calls.length], [1, 1, 0, 0], `${reason}: one send, one read, no wait, pac not asked`);
  }
});

test('import: a refusal that is not proven after one that was sent again keeps the count — both sentences, and the job to look up is the LAST send\'s', async () => {
  const { request } = importFiles();
  const platform = platformFor({ sends: [concurrencyAnswer(), concurrencyAnswer(), ok()], job: jobsAnsweringInOrder(NO_SUCH_JOB, jobAnswer(jobRow(LOGS.failedAfterStart()))) });
  await assert.rejects(ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    const [, second] = platform.imports().map(jobIdOf);
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${CONCURRENCY_REFUSAL.message} ${RESENT_AS('0x80071151')(2, '10 s')} ${NOT_PROVEN(WHY.otherError, second)}`);
    assert.deepStrictEqual([err.importJobId, err.importAttempts], [second, 2]);
    assert.deepStrictEqual(err.importRefusals, [{ importJobId: JOB, status: 429, code: '0x80071151', waitedMs: 10000, proof: 'job-absent' }]);
    assert.deepStrictEqual(ss.describeFailure(err), { ok: false, error: err.message, code: 'CONNECTION_ERROR', status: 429, importJobId: second, importAttempts: 2, importRefusals: err.importRefusals });
    return true;
  });
  assert.strictEqual(platform.imports().length, 2);
});

// A job whose read answers 404 for each of the first four refused sends — each is proven, so each is waited out and sent again — and `last` for every read after them: the failure's own read of the LAST send's job.
const lastJobAnswering = (last, proofs = 4) => {
  let reads = 0;
  return () => { reads += 1; return reads <= proofs ? NO_SUCH_JOB : last; };
};

test('import: when the sends run out on a refusal with 0x80071151 the failure never says "run the import again" outright, whatever the job of that last send is — it was not looked at, so the failure says what to look up first', async () => {
  const answer = (statusCode, body = {}) => ({ statusCode, headers: {}, body: JSON.stringify(body) });
  const cases = [
    ['a job that is still running', jobAnswer(jobRow(LOGS.running(), { completed: false }))],
    ['a job that applied the package', jobAnswer(jobRow(LOGS.succeeded()))],
    ['a job that does not exist', NO_SUCH_JOB],
    ['a read that is refused (403)', answer(403, { error: { message: 'principal user is missing prvReadImportJob privilege' } })],
    ['a read that fails (503)', answer(503)],
    ['a read that gets no answer', { error: 'Request timed out' }],
  ];
  for (const [what, last] of cases) {
    const platform = platformFor({ sends: Array.from({ length: 6 }, () => concurrencyAnswer()), job: lastJobAnswering(last) }); // a sixth would be answered, too: it must never be asked
    const { pac, calls } = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
    await assert.rejects(ss.runImport(importFiles().request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, pac }), (err) => {
      const ids = platform.imports().map(jobIdOf);
      assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${CONCURRENCY_REFUSAL.message} ${RESENT_AS('0x80071151')(5, '10 s, 20 s, 40 s, 60 s')} ${NOT_LOOKED_AT(ids[4])}`, what);
      assert.ok(!err.message.includes(RUN_AGAIN), `${what}: the advice that nothing has proven safe is not given`);
      assert.deepStrictEqual([err.code, err.statusCode, err.importJobId, err.importAttempts], ['CONNECTION_ERROR', 429, ids[4], 5], what);
      assert.deepStrictEqual(ss.describeFailure(err), { ok: false, error: err.message, code: 'CONNECTION_ERROR', status: 429, importJobId: ids[4], importAttempts: 5, importRefusals: err.importRefusals }, what);
      return true;
    }, what);
    assert.strictEqual(platform.imports().length, 5, `${what}: five sends, and no sixth`);
    assert.deepStrictEqual(platform.jobReads().map((r) => new URL(r.url).search), [...Array(4).fill(PROOF_READ), '?$select=data'], `${what}: the four proofs, then the one read of the last job's log that every failed import makes — never a fifth proof`);
    assert.deepStrictEqual(platform.waits, [10000, 20000, 40000, 60000], what);
    assert.strictEqual(calls.length, 0, `${what}: pac is not asked`);
  }
});

test('import: the log of the last send\'s job is still quoted when it says why the import failed — and the advice is still to look that job up first, since the quotation is not the rule that proves it', async () => {
  for (const [what, last, quoted] of [
    ['an import that failed after it began', jobAnswer(jobRow(LOGS.failedAfterStart())), 'its status is The import failed.'],
    ['an import refused at the lock, which is the shape the rule accepts', jobAnswer(jobRow(LOGS.refused())), `its status is ${REFUSAL_STATUS}`],
  ]) {
    const platform = platformFor({ sends: Array.from({ length: 5 }, () => concurrencyAnswer()), job: lastJobAnswering(last) });
    await assert.rejects(ss.runImport(importFiles().request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB }), (err) => {
      const ids = platform.imports().map(jobIdOf);
      assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${CONCURRENCY_REFUSAL.message} The import job's log (importjobs(${ids[4]})) says: ${quoted} ${RESENT_AS('0x80071151')(5, '10 s, 20 s, 40 s, 60 s')} ${NOT_LOOKED_AT(ids[4])}`, what);
      return true;
    }, what);
  }
});

test('import: the sentence about the last send is the one for the KIND of refusal it was — a lock code says what it always said, 0x80071151 says what to look up — whichever kind the sends before it were', async () => {
  const lockLast = platformFor({ sends: [...Array.from({ length: 4 }, () => concurrencyAnswer()), lockAnswer()] });
  await assert.rejects(ss.runImport(importFiles().request, { http: lockLast.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${LOCK_REFUSAL.message} ${RESENT_AS('0x80071151')(5, '10 s, 20 s, 40 s, 60 s')} ${RUN_AGAIN}`);
    return true;
  });
  const concurrencyLast = platformFor({ sends: [...Array.from({ length: 4 }, () => lockAnswer()), concurrencyAnswer()] });
  await assert.rejects(ss.runImport(importFiles().request, { http: concurrencyLast.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${CONCURRENCY_REFUSAL.message} ${RESENT(5, '10 s, 20 s, 40 s, 60 s')} ${NOT_LOOKED_AT(concurrencyLast.imports().map(jobIdOf)[4])}`);
    return true;
  });
});

test('import: when the sends run out on an answer that is not a refusal to start — a 429 of another kind, or 0x80071151 with other words — the failure is the platform\'s own, judged like a first send: what the sends before it were, and no advice to run the import again', async () => {
  const others = [
    ['a service protection limit', '0x80072322', 'Number of requests exceeded the limit of 6000 over time window of 300 seconds.'],
    ['0x80071151 with its documented words alone', '0x80071151', DOCUMENTED_CONCURRENCY_TEXT],
    ['an operation that failed because of another, which had started', '0x80071154', 'The current Import operation failed due to another concurrent operation running at the same time. Please try again later.'],
  ];
  for (const [what, code, message] of others) {
    const platform = platformFor({ sends: [...Array.from({ length: 4 }, () => lockAnswer()), lockAnswer({ code, message })] });
    await assert.rejects(ss.runImport(importFiles().request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
      assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${message} ${RESENT(5, '10 s, 20 s, 40 s, 60 s')}`, what);
      assert.deepStrictEqual([err.statusCode, err.importAttempts], [429, 5], what);
      return true;
    }, what);
    assert.deepStrictEqual([platform.imports().length, platform.jobReads().length], [5, 4], `${what}: five sends, the four proofs and no more`);
  }
});

test('the client tells the listener which refusal the sends ran out on — once, with the job it carried, its status and its code, for either kind of refusal — and of no earlier refusal and no other answer', async () => {
  const told = async (sends, extra = {}) => {
    const world = refusedImport({ sends, ...extra });
    await world.post();
    return world;
  };
  for (const [code, refusal] of [['0x8004F064', () => lockAnswer()], ['0x80071151', () => concurrencyAnswer()]]) {
    const world = await told(Array.from({ length: 6 }, () => refusal()));
    const ids = world.imports().map(jobIdOf);
    assert.deepStrictEqual(world.exhausted, [{ importJobId: ids[4], status: 429, code }], code);
    assert.strictEqual(world.jobReads().length, 4, `${code}: the job of the last send is not read`);
    assert.deepStrictEqual(world.told.map((event) => event.importJobId), ids.slice(0, 4), `${code}: the four before it were sent again, and are told as that`);
  }
  // The last send's own kind decides, not the kind of the others.
  const mixed = await told([...Array.from({ length: 4 }, () => concurrencyAnswer()), lockAnswer(), ok()]);
  assert.deepStrictEqual(mixed.exhausted.map((event) => event.code), ['0x8004F064']);
  // Not told: a refusal that is sent again, an answer that is not a refusal to start, a send the platform takes, a retry the time ended, and a refusal whose job proves nothing.
  const gateway = { statusCode: 504, headers: {}, body: JSON.stringify({ error: { code: '0x80048d19', message: 'gateway timeout' } }) };
  const quiet = [
    ['a refusal that is sent again and taken', await told([lockAnswer(), concurrencyAnswer(), ok()])],
    ['a gateway on the fifth send', await told([...Array.from({ length: 4 }, () => lockAnswer()), gateway, ok()])],
    ['a service protection limit on the fifth send', await told([...Array.from({ length: 4 }, () => lockAnswer()), lockAnswer({ code: '0x80072322', message: 'Number of requests exceeded the limit.' }), ok()])],
    ['0x80071151 with its documented words alone on the fifth send', await told([...Array.from({ length: 4 }, () => lockAnswer()), lockAnswer({ code: '0x80071151', message: DOCUMENTED_CONCURRENCY_TEXT }), ok()])],
    ['a refusal whose job proves nothing', await told([concurrencyAnswer(), ok()], { job: jobAnswer(jobRow(LOGS.running(), { completed: false })) })],
    ['a retry that the time ended', await told([askingConcurrency(600), ok()])],
  ];
  for (const [what, world] of quiet) assert.deepStrictEqual(world.exhausted, [], what);
  // A client that was given no listener for it is not harmed by the refusal the sends ran out on.
  const silent = ss.solutionHttpClient(ENV, platformFor({ sends: Array.from({ length: 6 }, () => lockAnswer()) }).http, { onImportRefused: () => {} });
  assert.strictEqual((await silent.post(`${ENV}/api/data/v9.0/ImportSolution`, importBody())).status, 429);
});

test('import: nothing the settings file holds is in a sentence a refusal with 0x80071151 adds — the platform\'s words that quote it are withheld, and the run\'s own sentences, which hold no value, are shown whole', async () => {
  const quoting = concurrencyAnswer({ message: `${REFUSAL_STATUS} (value ${SECRET}, connection ${CONNECTION_ID}, plain ${PLAIN})` });
  const quoted = importFiles(filled());
  const unproven = platformFor({ sends: [quoting, ok()], job: jobAnswer(jobRow(LOGS.running(), { completed: false })) });
  await assert.rejects(ss.runImport(quoted.request, { http: unproven.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${WITHHELD('message')} ${NOT_PROVEN(WHY.unfinished)}`);
    const printed = JSON.stringify(ss.describeFailure(err));
    for (const value of [SECRET, CONNECTION_ID, PLAIN]) assert.ok(!printed.includes(value), value);
    return true;
  });
  const exhausted = platformFor({ sends: Array.from({ length: 5 }, () => quoting) });
  await assert.rejects(ss.runImport(importFiles(filled()).request, { http: exhausted.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${WITHHELD('message')} ${RESENT_AS('0x80071151')(5, '10 s, 20 s, 40 s, 60 s')} ${NOT_LOOKED_AT(exhausted.imports().map(jobIdOf)[4])}`);
    return true;
  });
});

test('import: a refusal with 0x80071151 whose message does not begin "Cannot start" is reported exactly as an import refused for any other reason always was — the SDK\'s code and the status, the job that was sent, no count, no list — and pac is not asked', async () => {
  const { request } = importFiles();
  const platform = platformFor({ sends: [lockAnswer({ code: CONCURRENCY_REFUSAL.code, message: DOCUMENTED_CONCURRENCY_TEXT }), ok()] });
  const { pac, calls } = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
  await assert.rejects(ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, pac, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${DOCUMENTED_CONCURRENCY_TEXT}`);
    assert.deepStrictEqual(ss.describeFailure(err), { ok: false, error: err.message, code: 'CONNECTION_ERROR', status: 429, importJobId: JOB });
    return true;
  });
  assert.deepStrictEqual([platform.imports().length, platform.jobReads().length, platform.waits.length, calls.length], [1, 0, 0, 0]);
});

test('the job proves it: any answer to the read of the job but 404 — it exists, the read was refused, it failed, it got no answer — is NOT proof, and the import is not sent again', async () => {
  const answer = (statusCode, body = {}) => ({ statusCode, headers: {}, body: JSON.stringify(body) });
  const cases = [
    ['the job exists', answer(200, { importjobid: JOB })],
    ['the job exists and holds a log', answer(200, { importjobid: JOB, data: '<importexportxml succeeded="failure"/>' })],
    ['a 204', answer(204, '')],
    ['a 400', answer(400)], ['a 401', answer(401)], ['a 403', answer(403)], ['a 408', answer(408)], ['a 412', answer(412)], ['a 429', answer(429)],
    ['a 500', answer(500)], ['a 502', answer(502)], ['a 503', answer(503)], ['a 504', answer(504)],
    ['no answer', { error: 'Request timed out' }],
    ['an answer cut off', { error: 'Connection closed before the response completed', incompleteResponse: true, statusCode: 200 }],
    ['a transport that throws', () => { throw new Error('socket hang up'); }],
  ];
  for (const [what, job] of cases) {
    const world = refusedImport({ sends: [lockAnswer(), ok()], job });
    const res = await world.post();
    assert.deepStrictEqual([res.status, res.body.error.code], [429, '0x8004F064'], `${what}: the refusal comes back as it came`);
    assert.strictEqual(world.imports().length, 1, `${what}: one send`);
    assert.strictEqual(world.jobReads().length, 1, `${what}: one read, never repeated`);
    assert.deepStrictEqual(world.told, [], what);
  }
  // A read that cannot even have a token is no proof either.
  let tokens = 0;
  const tokenless = refusedImport({ sends: [lockAnswer(), ok()], http: { getToken: () => { tokens += 1; return tokens === 1 ? 'dataverse-token' : null; } } });
  assert.strictEqual((await tokenless.post()).status, 429);
  assert.strictEqual(tokenless.imports().length, 1);
});

test('anything that is not the lock\'s refusal is sent ONCE, as before: a 429 of another kind, the lock\'s words under another code, or its code under another status, a body that does not say what it is — no job is read, nothing waited for', async () => {
  const plain = (value) => ({ statusCode: 429, headers: {}, body: typeof value === 'string' ? value : JSON.stringify(value) });
  const cases = [
    ['service protection: the number of requests', lockAnswer({ code: '0x80072322', message: 'Number of requests exceeded the limit of 6000 over time window of 300 seconds.', headers: { 'retry-after': '20' } })],
    ['service protection: the execution time', lockAnswer({ code: '0x80072321', message: 'Combined execution time of incoming requests exceeded limit of 1,200,000 milliseconds over time window of 300 seconds.', headers: { 'retry-after': '20' } })],
    ['service protection: the concurrent requests', lockAnswer({ code: '0x80072326', message: 'Number of concurrent requests exceeded the limit of 52.', headers: { 'retry-after': '20' } })],
    ['an operation that FAILED for another that ran at the same time — it had started', lockAnswer({ code: '0x80071154', message: 'The current Import operation failed due to another concurrent operation running at the same time. Please try again later.' })],
    ['the customization lock of another service', lockAnswer({ code: '0x8004F077', message: 'RemoveActiveCustomizationService failed to acquire customization lock. Another metadata process is in progress. Please try again later.' })],
    ['an entity customization lock', lockAnswer({ code: '0x80090412', message: 'Cannot start the requested operation because there is an entity customization running at this moment.' })],
    ['the lock\'s own words under another code', lockAnswer({ code: '0x80048d19' })],
    ['the lock\'s own words and no code', plain({ error: { message: LOCK_REFUSAL.message } })],
    ['the lock\'s own words in a body that is not JSON', plain(LOCK_REFUSAL.message)],
    ['the lock\'s code in a text around it', lockAnswer({ code: `error ${LOCK_REFUSAL.code}` })],
    ['the lock\'s code in its decimal form', plain({ error: { code: -2147159964, message: LOCK_REFUSAL.message } })],
    ['the lock\'s code outside "error"', plain({ code: LOCK_REFUSAL.code, message: LOCK_REFUSAL.message })],
    ['an empty body', plain('')],
    ['a body that is not an object', plain('null')],
  ];
  for (const status of [400, 401, 403, 404, 408, 409, 412, 500, 502, 503, 504]) cases.push([`the lock's code under HTTP ${status}`, lockAnswer({ statusCode: status })]);
  for (const [what, answer] of cases) {
    const world = refusedImport({ sends: [answer, ok()] });
    const res = await world.post();
    assert.strictEqual(res.status, answer.statusCode, `${what}: the answer comes back as it came`);
    assert.strictEqual(world.imports().length, 1, `${what}: one send`);
    assert.deepStrictEqual([world.jobReads().length, world.waits.length, world.told.length], [0, 0, 0], `${what}: no job read, no wait, nothing told`);
  }
});

test('with the listener given, every answer that does not say the import was refused is still sent ONCE — a gateway\'s 500, 502, 503 and 504, a 401, a 408 — and so is a send that got no answer', async () => {
  for (const status of [500, 502, 503, 504, 401, 408]) {
    const world = refusedImport({ sends: [{ statusCode: status, headers: { 'retry-after': '1' }, body: JSON.stringify({ error: { code: '0x80048d19', message: `answered ${status}` } }) }, ok()] });
    const res = await world.post();
    assert.deepStrictEqual([res.status, res.body.error.message], [status, `answered ${status}`]);
    assert.deepStrictEqual([world.imports().length, world.jobReads().length, world.waits.length], [1, 0, 0], String(status));
  }
  for (const lost of [{ error: 'Request timed out' }, { error: 'Connection closed before the response completed', incompleteResponse: true, statusCode: 429 }]) {
    const world = refusedImport({ sends: [lost, ok()] });
    await assert.rejects(world.post(), /^Error: ImportSolution got no answer \(.*\)\. It was not sent again/);
    assert.deepStrictEqual([world.imports().length, world.jobReads().length, world.waits.length], [1, 0, 0]);
  }
});

test('the wait before a send again is 10 s, then 20, 40 and 60 — longer when the platform\'s Retry-After asks for more, in either form and WHOLE, and never shorter than that step — and a run waits four times at most', async () => {
  const waited = async (headers, refusals = 1) => {
    const world = clockedImport({ sends: [...Array.from({ length: refusals }, () => () => lockAnswer({ headers })), ok()] });
    await world.post();
    return world.waits;
  };
  const date = (seconds) => new Date(CLOCK_ORIGIN + seconds * 1000).toUTCString();
  assert.deepStrictEqual(await waited({}), [10000]);
  assert.deepStrictEqual(await waited({ 'retry-after': '45' }), [45000]);
  assert.deepStrictEqual(await waited({ 'Retry-After': '45' }), [45000], 'the header\'s case does not matter');
  assert.deepStrictEqual(await waited({ 'retry-after': ['45'] }), [45000], 'a header that arrives as a list');
  assert.deepStrictEqual(await waited({ 'retry-after': '5' }), [10000], 'a shorter Retry-After does not shorten the step');
  assert.deepStrictEqual(await waited({ 'retry-after': date(30) }), [30000], 'the date form, too');
  assert.deepStrictEqual(await waited({ 'retry-after': 'soon' }), [10000], 'what cannot be read is no request');
  assert.deepStrictEqual(await waited({ 'retry-after': date(-30) }), [10000], 'a date that has passed is no request either');
  assert.deepStrictEqual(await waited({}, 4), [10000, 20000, 40000, 60000]);
  assert.deepStrictEqual(await waited({ 'retry-after': '30' }, 4), [30000, 30000, 40000, 60000]);
  // The bound, in the constants the client reads: five sends, four waits at most, and five minutes in all for the whole of the retry — the wait the platform asks for and the proof reads included.
  assert.strictEqual(ss.IMPORT_MAX_SENDS, 5);
  assert.deepStrictEqual([...ss.IMPORT_RESEND_WAITS_MS], [10000, 20000, 40000, 60000]);
  assert.strictEqual(ss.IMPORT_RETRY_BUDGET_MS, 5 * 60 * 1000);
});

// A Retry-After the platform sends is the time it asks the caller to wait, and a send before it is a send the platform has said it will refuse: so it is waited out WHOLE — not cut to the minute the client's own
// retries cap it at — and, when it does not fit the budget of the retry, the import is not sent again at all: it is never sent early.
for (const [form, header] of [['delay-seconds', askingFor(180).seconds], ['HTTP-date', askingFor(180).date]]) {
  test(`a Retry-After of 180 s (${form}) is waited out whole — the platform is not sent the import again a minute into it — and the send comes after it`, async () => {
    const world = clockedImport({ sends: [header, ok()] });
    const res = await world.post();
    assert.strictEqual(res.status, 200);
    assert.deepStrictEqual(world.waits, [180000], 'the whole of it, not 60 s');
    assert.deepStrictEqual(world.clock.sentAt, [0, 180000], 'the second send is 180 s after the first, and not before');
    assert.deepStrictEqual(world.events.map((event) => event.replace(/ [0-9a-f-]{36}.*$/, '').replace(/\(.*\)$/, '')), ['send', 'wait 180000', 'read importjobs', 'send']);
    assert.deepStrictEqual(world.told.map((event) => event.waitedMs), [180000]);
    assert.deepStrictEqual(world.stopped, []);
  });
}

test('a wait that does not fit in what is left of the five minutes ends the retry — the refusal comes back as it came, with nothing sent early and nothing waited for — whatever the form of the Retry-After', async () => {
  const tooLong = [
    ['delay-seconds, beyond the budget', askingFor(301).seconds],
    ['HTTP-date, beyond the budget', askingFor(301).date],
    ['an hour', askingFor(3600).seconds],
    ['a day, as a date', askingFor(86400).date],
  ];
  for (const [what, refusal] of tooLong) {
    const world = clockedImport({ sends: [refusal, ok()] });
    const res = await world.post();
    assert.deepStrictEqual([res.status, res.body.error.code, res.body.error.message], [429, '0x8004F064', LOCK_REFUSAL.message], `${what}: the platform's refusal, as it came`);
    assert.deepStrictEqual([world.imports().length, world.jobReads().length, world.waits.length, world.told.length], [1, 0, 0, 0], `${what}: one send, no proof, no wait, nothing sent again`);
    assert.strictEqual(world.clock.elapsed, 0, `${what}: no time was spent`);
    assert.strictEqual(world.stopped.length, 1, `${what}: the listener is told the retry was ended`);
    assert.strictEqual(world.stopped[0].importJobId, JOB);
    assert.strictEqual(world.stopped[0].spentMs, 0);
    assert.ok(world.stopped[0].waitMs > 300000, what);
  }
  // The edge: a request for the whole five minutes fits, one second more does not.
  const fits = clockedImport({ sends: [askingFor(300).seconds, ok()] });
  assert.strictEqual((await fits.post()).status, 200);
  assert.deepStrictEqual([fits.waits, fits.stopped], [[300000], []]);
  const over = clockedImport({ sends: [askingFor(301).seconds, ok()] });
  assert.strictEqual((await over.post()).status, 429);
  assert.deepStrictEqual(over.waits, []);
});

test('the five minutes are one budget for the whole retry — what the first waits spent is not there for the next — and a refusal that asks for more than is left ends the retry with the attempts so far', async () => {
  // Every refusal asks for two minutes: the first two waits fit (two, then four minutes in all), the third would end at six.
  for (const [form, ask] of [['delay-seconds', askingFor(120).seconds], ['HTTP-date', askingFor(120).date]]) {
    const world = clockedImport({ sends: [ask, ask, ask, ok()] });
    const res = await world.post();
    assert.strictEqual(res.status, 429, form);
    assert.deepStrictEqual(world.waits, [120000, 120000], form);
    assert.deepStrictEqual(world.clock.sentAt, [0, 120000, 240000], `${form}: three sends, two minutes apart — never early`);
    assert.deepStrictEqual([world.imports().length, world.jobReads().length, world.told.length], [3, 2, 2], form);
    assert.deepStrictEqual(world.stopped, [{ importJobId: world.imports().map(jobIdOf)[2], waitMs: 120000, spentMs: 240000 }], `${form}: the third refusal is the last answer, and it is the one that could not be waited out`);
  }
  // A shorter request that comes after a long one still fits what is left: the budget is a sum, not a count.
  const mixed = clockedImport({ sends: [askingFor(200).seconds, askingFor(90).seconds, askingFor(30).seconds, ok()] });
  assert.strictEqual((await mixed.post()).status, 429);
  assert.deepStrictEqual(mixed.waits, [200000, 90000], '200 and 90 are 290 s, and the third wait — 30 s — would end at 320');
  assert.strictEqual(mixed.stopped.length, 1);
  // The default schedule never reaches the budget: 10, 20, 40 and 60 s are 130 s.
  const schedule = clockedImport({ sends: [...Array.from({ length: 5 }, () => () => lockAnswer())] });
  assert.strictEqual((await schedule.post()).status, 429);
  assert.deepStrictEqual(schedule.waits, [10000, 20000, 40000, 60000]);
  assert.deepStrictEqual(schedule.stopped, [], 'a refusal after the fifth send is the sends running out, not the time');
});

test('the proof reads count: time spent reading the refused send\'s job is time spent, and a read that leaves nothing of the budget ends the retry before the send, with the platform\'s refusal', async () => {
  // Each read of the job takes 100 s. Send one, refused at 0; wait 10, read: 110 s; send two refused; wait 20, read: 230 s; send three refused; wait 40 (230 + 40 fits), read: 370 s — over.
  const world = clockedImport({ sends: Array.from({ length: 5 }, () => () => lockAnswer()), readMs: 100000 });
  const res = await world.post();
  assert.strictEqual(res.status, 429);
  assert.deepStrictEqual(world.waits, [10000, 20000, 40000]);
  assert.deepStrictEqual(world.clock.sentAt, [0, 110000, 230000], 'a third send was made; a fourth was not');
  assert.strictEqual(world.jobReads().length, 3);
  assert.deepStrictEqual(world.told.length, 2, 'the third refusal is not sent again, so it is not told as one that was');
  assert.deepStrictEqual(world.stopped, [{ importJobId: world.imports().map(jobIdOf)[2], waitMs: null, spentMs: 370000 }]);
  // Without a clock that moves, the same refusals are sent again to the end: nothing but time ends a retry early.
  const quick = clockedImport({ sends: Array.from({ length: 5 }, () => () => lockAnswer()) });
  await quick.post();
  assert.strictEqual(quick.imports().length, 5);
});

test('a client with no listener never waits for a refused import: the budget, like the retry, belongs to the caller that asked for it', async () => {
  const world = clockedImport({ sends: [askingFor(180).seconds, ok()], observing: false });
  assert.strictEqual((await world.post()).status, 429);
  assert.deepStrictEqual([world.imports().length, world.waits.length, world.jobReads().length], [1, 0, 0]);
});

test('an import is sent FIVE times at most: when the fifth is refused too, that answer comes back as it came — each of the first four was followed by a wait and a proof, the fifth by neither', async () => {
  const world = refusedImport({ sends: Array.from({ length: 6 }, () => lockAnswer()) }); // a sixth would be answered, too: it must never be asked
  const res = await world.post();
  assert.deepStrictEqual([res.status, res.body.error.code], [429, '0x8004F064']);
  assert.strictEqual(world.imports().length, 5);
  assert.strictEqual(world.jobReads().length, 4);
  assert.deepStrictEqual(world.waits, [10000, 20000, 40000, 60000]);
  const ids = world.imports().map(jobIdOf);
  assert.strictEqual(new Set(ids).size, 5, 'every send carried a job id of its own');
  assert.ok(ids.every((id) => GUID.test(id)));
  assert.deepStrictEqual(world.jobReads().map((r) => decodeURIComponent(new URL(r.url).pathname.split('/').pop())), ids.slice(0, 4).map((id) => `importjobs(${id})`), 'each read named the job of the send it followed');
  assert.deepStrictEqual(world.told.map((event) => [event.importJobId, event.nextImportJobId, event.waitedMs]), ids.slice(0, 4).map((id, i) => [id, ids[i + 1], [10000, 20000, 40000, 60000][i]]));
});

test('a send that follows a refusal is judged on its own: a gateway\'s answer, or none at all, is the failure and is not sent again — and it names ITS job, not the refused one\'s', async () => {
  const gateway = refusedImport({ sends: [lockAnswer(), lockAnswer(), { statusCode: 504, headers: {}, body: JSON.stringify({ error: { code: '0x80048d19', message: 'gateway timeout' } }) }, ok()] });
  const res = await gateway.post();
  assert.deepStrictEqual([res.status, res.body.error.message], [504, 'gateway timeout']);
  assert.deepStrictEqual([gateway.imports().length, gateway.jobReads().length, gateway.waits.length], [3, 2, 2], 'two refusals, two proofs, two waits — and no fourth send');
  const lost = refusedImport({ sends: [lockAnswer(), { error: 'Request timed out' }, ok()] });
  await assert.rejects(lost.post(), (err) => {
    const [first, second] = lost.imports().map(jobIdOf);
    assert.strictEqual(lost.imports().length, 2);
    assert.notStrictEqual(second, first);
    assert.ok(err.message.includes(`import job ${second} (importjobs(${second})`), err.message);
    assert.ok(!err.message.includes(first), 'the refused job is not the one to look up');
    return true;
  });
});

test('the exception is ImportSolution\'s alone, and the listener\'s to turn on: a refused ExportSolution or StageSolution is sent once, and so is an import for a caller that gave no listener', async () => {
  for (const [action, body, observing] of [['ExportSolution', { SolutionName: 'x' }, true], ['StageSolution', { CustomizationFile: 'x' }, true], ['ImportSolution', importBody(), false]]) {
    const world = refusedImport({ sends: [lockAnswer(), ok()], observing });
    const res = await world.client.post(`${ENV}/api/data/v9.0/${action}`, body);
    assert.strictEqual(res.status, 429, action);
    assert.deepStrictEqual([world.requests.length, world.waits.length], [1, 0], `${action}: one request, no job read, no wait`);
  }
});

// What the SDK does with the plan's client: it posts ImportSolution and raises a ConnectionError — the bundle's own class — for an answer that is not a success, as ensureSuccess does.
function importingOver() {
  return async (env, plan) => {
    const client = plan.httpClient(env);
    const sdk = {
      importSolution: async (options) => {
        const res = await client.post(`${env}/api/data/v9.0/ImportSolution`, importBody(options.importJobId));
        if (res.status >= 400) throw new ConnectionError(`${env}/api/data/v9.0/ImportSolution`, res.status, String((res.body && res.body.error && res.body.error.message) || ''));
        return { appliedSettings: { environmentVariables: ['new_Url'], connectionReferences: ['new_Dataverse'] } };
      },
    };
    return stubSdkFirst(sdk)(env, plan);
  };
}
const APPLIED = { environmentVariables: ['new_Url'], connectionReferences: ['new_Dataverse'] };
// The sentence an import that was sent more than once adds to its failure: how many sends, why each before the last was refused, and what the run waited.
const RESENT = (sends, waits) => `ImportSolution was sent ${sends} times. Each send before the last was refused before it started (HTTP 429, code 0x8004F064: another operation held the environment's customization lock) and the job it named did not exist (importjobs(<id>) answered 404). The run waited ${waits} before sending again.`;

test('import: a refused send that is sent again is reported — the job that RAN, how many sends it took, and each refusal with the wait that followed it', async () => {
  const { request } = importFiles();
  const platform = platformFor({ sends: [lockAnswer(), lockAnswer({ headers: { 'retry-after': '30' } }), ok()] });
  const result = await ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB });
  const ids = platform.imports().map(jobIdOf);
  assert.strictEqual(ids.length, 3);
  assert.deepStrictEqual(result, {
    source: 'sdk',
    importJobId: ids[2],
    applied: APPLIED,
    importAttempts: 3,
    importRefusals: [
      { importJobId: JOB, status: 429, code: '0x8004F064', waitedMs: 10000 },
      { importJobId: ids[1], status: 429, code: '0x8004F064', waitedMs: 30000 },
    ],
  });
  assert.notStrictEqual(ids[2], JOB);
  assert.ok(!JSON.stringify(result).includes(SECRET) && !JSON.stringify(result).includes(CONNECTION_ID));
});

test('import: when the sends run out the failure is the platform\'s own refusal with the count added — the last job named, each refused one listed — and pac is never asked', async () => {
  const { request } = importFiles();
  const platform = platformFor({ sends: Array.from({ length: 5 }, () => lockAnswer()) });
  const { pac, calls } = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
  await assert.rejects(ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, pac, readJobLog: async () => null }), (err) => {
    const ids = platform.imports().map(jobIdOf);
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${LOCK_REFUSAL.message} ${RESENT(5, '10 s, 20 s, 40 s, 60 s')} It sends at most 5 times: run the import again when the other operation has finished.`);
    assert.deepStrictEqual([err.code, err.statusCode, err.importJobId, err.importAttempts], ['CONNECTION_ERROR', 429, ids[4], 5]);
    assert.deepStrictEqual(err.importRefusals, ids.slice(0, 4).map((id, i) => ({ importJobId: id, status: 429, code: '0x8004F064', waitedMs: [10000, 20000, 40000, 60000][i] })));
    assert.ok(!('fallback' in err), 'a refusal that outlasted the sends is not a reason to ask pac');
    assert.deepStrictEqual(ss.describeFailure(err), {
      ok: false, error: err.message, code: 'CONNECTION_ERROR', status: 429, importJobId: ids[4], importAttempts: 5, importRefusals: err.importRefusals,
    });
    return true;
  });
  assert.deepStrictEqual(calls, [], 'pac was not run');
});

// What an import that was refused and not sent again for want of time reports: the platform's own refusal, the sends there were (as for sends that ran out), and the plugin's own sentence — numbers only —
// that says why the next one was not made.
const NOT_SENT_AGAIN = (wait, spent) => `ImportSolution was not sent again: the next wait, ${wait}, does not fit in what is left of the 5 minutes this run spends in all on sending it again (${spent} are spent). Run the import again when the other operation has finished.`;
const OUT_OF_TIME = (spent) => `ImportSolution was not sent again: the 5 minutes this run spends in all on sending it again were used up (${spent}) before the next send. Run the import again when the other operation has finished.`;

test('import: a refusal whose Retry-After does not fit the five minutes is the platform\'s own refusal, and a sentence that says why it was not sent again — nothing was sent again, so no count and no list', async () => {
  const { request } = importFiles();
  const world = clockedImport({ sends: [askingFor(600).seconds, ok()] });
  const { pac, calls } = fakePac(() => ({ status: 0, stdout: '', stderr: '' }));
  await assert.rejects(ss.runImport(request, { http: world.http, sdkFirst: importingOver(), importJobId: JOB, pac, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${LOCK_REFUSAL.message} ${NOT_SENT_AGAIN('600 s', '0 s')}`);
    assert.deepStrictEqual(ss.describeFailure(err), { ok: false, error: err.message, code: 'CONNECTION_ERROR', status: 429, importJobId: JOB });
    return true;
  });
  assert.deepStrictEqual([world.imports().length, world.jobReads().length, world.waits.length, calls.length], [1, 0, 0, 0], 'one send, nothing read, nothing waited for, pac not asked');
});

test('import: after sends that were made, the refusal that cannot be waited out is the last answer — the attempts so far are counted and listed, the job named is the last send\'s, and the sentence says why', async () => {
  const { request } = importFiles();
  const world = clockedImport({ sends: [askingFor(120).seconds, askingFor(120).seconds, askingFor(120).seconds, ok()] });
  await assert.rejects(ss.runImport(request, { http: world.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    const ids = world.imports().map(jobIdOf);
    assert.strictEqual(ids.length, 3);
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${LOCK_REFUSAL.message} ${RESENT(3, '120 s, 120 s')} ${NOT_SENT_AGAIN('120 s', '240 s')}`);
    assert.deepStrictEqual([err.code, err.statusCode, err.importJobId, err.importAttempts], ['CONNECTION_ERROR', 429, ids[2], 3]);
    assert.deepStrictEqual(err.importRefusals, [
      { importJobId: JOB, status: 429, code: '0x8004F064', waitedMs: 120000 },
      { importJobId: ids[1], status: 429, code: '0x8004F064', waitedMs: 120000 },
    ]);
    assert.deepStrictEqual(ss.describeFailure(err), { ok: false, error: err.message, code: 'CONNECTION_ERROR', status: 429, importJobId: ids[2], importAttempts: 3, importRefusals: err.importRefusals });
    return true;
  });
});

test('import: a retry whose budget the proof read used up says so — the refusal, the sends there were, and a sentence that names the time that was spent', async () => {
  const { request } = importFiles();
  const world = clockedImport({ sends: Array.from({ length: 5 }, () => () => lockAnswer()), readMs: 100000 });
  await assert.rejects(ss.runImport(request, { http: world.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    const ids = world.imports().map(jobIdOf);
    assert.strictEqual(ids.length, 3);
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${LOCK_REFUSAL.message} ${RESENT(3, '10 s, 20 s')} ${OUT_OF_TIME('370 s')}`);
    assert.deepStrictEqual([err.importJobId, err.importAttempts], [ids[2], 3]);
    return true;
  });
});

test('import: a retry whose wait overran the five minutes says so — the refusal, the sends there were, and the time that was spent — for a refusal with 0x80071151 as for a lock code', async () => {
  for (const [what, refusal, message, resent] of [
    ['0x80071151', concurrencyAnswer, CONCURRENCY_REFUSAL.message, RESENT_AS('0x80071151')],
    ['a lock code', lockAnswer, LOCK_REFUSAL.message, RESENT],
  ]) {
    // Every timer resumes 140 s late: the first wait ends at 150 s, the second, which was asked for 20 s, at 310 s.
    const world = clockedImport({ sends: Array.from({ length: 5 }, () => () => refusal()), lateMs: 140000 });
    await assert.rejects(ss.runImport(importFiles().request, { http: world.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
      const ids = world.imports().map(jobIdOf);
      assert.strictEqual(ids.length, 2, what);
      assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${message} ${resent(2, '10 s')} ${OUT_OF_TIME('310 s')}`, what);
      assert.deepStrictEqual([err.importJobId, err.importAttempts], [ids[1], 2], what);
      return true;
    }, what);
  }
});

test('import: when the sends run out the failure is not described as one that ran out of time, and a retry that ended for time is not described as one whose sends ran out', async () => {
  const { request } = importFiles();
  const sends = clockedImport({ sends: Array.from({ length: 5 }, () => () => lockAnswer()) });
  await assert.rejects(ss.runImport(request, { http: sends.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.ok(err.message.endsWith('It sends at most 5 times: run the import again when the other operation has finished.'));
    assert.ok(!err.message.includes('was not sent again'));
    return true;
  });
  const time = clockedImport({ sends: [askingFor(120).seconds, askingFor(120).seconds, askingFor(120).seconds, ok()] });
  await assert.rejects(ss.runImport(importFiles().request, { http: time.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.ok(!err.message.includes('It sends at most 5 times'));
    return true;
  });
});

test('import: a send that follows a refusal and is answered by a gateway says it was not sent again and names its own job — with the refusals before it counted', async () => {
  const { request } = importFiles();
  const platform = platformFor({ sends: [lockAnswer(), { statusCode: 504, headers: {}, body: JSON.stringify({ error: { code: '0x80048d19', message: 'the gateway gave up' } }) }] });
  await assert.rejects(ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    const [, second] = platform.imports().map(jobIdOf);
    assert.strictEqual(err.message, `HTTP 504 from ${ENV}/api/data/v9.0/ImportSolution: the gateway gave up The last send was not sent again: the environment may have accepted it.${ss.lookUpJob(second)} ${RESENT(2, '10 s')}`);
    assert.deepStrictEqual([err.statusCode, err.importJobId, err.importAttempts], [504, second, 2]);
    assert.deepStrictEqual(err.importRefusals, [{ importJobId: JOB, status: 429, code: '0x8004F064', waitedMs: 10000 }]);
    return true;
  });
});

test('import: a refusal that is NOT sent again — its job exists, or the read failed — is today\'s failure, to the word: no count, no list, the job that was sent', async () => {
  for (const job of [{ statusCode: 200, headers: {}, body: JSON.stringify({ importjobid: JOB }) }, { statusCode: 503, headers: {}, body: '{}' }, { error: 'Request timed out' }]) {
    const { request } = importFiles();
    const platform = platformFor({ sends: [lockAnswer(), ok()], job });
    await assert.rejects(ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
      assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${LOCK_REFUSAL.message}`);
      assert.deepStrictEqual([err.statusCode, err.importJobId, 'importAttempts' in err, 'importRefusals' in err], [429, JOB, false, false]);
      assert.deepStrictEqual(ss.describeFailure(err), { ok: false, error: err.message, code: 'CONNECTION_ERROR', status: 429, importJobId: JOB });
      return true;
    });
    assert.strictEqual(platform.imports().length, 1);
  }
});

test('import: an import that is sent once and succeeds prints what it always printed — no count, no list — and a send that is refused with another 429 is today\'s failure', async () => {
  const { request } = importFiles();
  const platform = platformFor({ sends: [ok()] });
  assert.deepStrictEqual(await ss.runImport(request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB }), { source: 'sdk', importJobId: JOB, applied: APPLIED });
  const throttled = platformFor({ sends: [lockAnswer({ code: '0x80072322', message: 'Number of requests exceeded the limit of 6000 over time window of 300 seconds.' }), ok()] });
  await assert.rejects(ss.runImport(importFiles().request, { http: throttled.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: Number of requests exceeded the limit of 6000 over time window of 300 seconds.`);
    assert.deepStrictEqual([err.importJobId, 'importAttempts' in err], [JOB, false]);
    return true;
  });
  assert.deepStrictEqual([throttled.imports().length, throttled.jobReads().length, throttled.waits.length], [1, 0, 0]);
});

test('import: nothing the settings file holds is in a sentence the resend adds — the platform\'s words that quote it are withheld, and the run\'s own words, which hold no value, are shown whole whatever the value is', async () => {
  // The platform's refusal quotes the secret, the connection id and a plain value; every send is refused, so the failure carries all of the resend's sentences.
  const quoting = lockAnswer({ message: `${LOCK_REFUSAL.message} (value ${SECRET}, connection ${CONNECTION_ID}, plain ${PLAIN})` });
  const quoted = importFiles(filled());
  const platform = platformFor({ sends: Array.from({ length: 5 }, () => quoting) });
  await assert.rejects(ss.runImport(quoted.request, { http: platform.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 429 from ${ENV}/api/data/v9.0/ImportSolution: ${WITHHELD('message')} ${RESENT(5, '10 s, 20 s, 40 s, 60 s')} It sends at most 5 times: run the import again when the other operation has finished.`);
    const printed = JSON.stringify(ss.describeFailure(err));
    for (const value of [SECRET, CONNECTION_ID, PLAIN]) assert.ok(!printed.includes(value), value);
    return true;
  });
  // A short value that is a word of the platform's text — "another" — withholds THAT text, and not the plugin's own sentences, which say "another", "sent", "times" and "job" themselves.
  for (const value of ['another', 'sent', 'times', 'job', 'it']) {
    const { request } = importFiles({ EnvironmentVariables: [{ SchemaName: 'new_Text', Value: value }], ConnectionReferences: [] });
    const refused = platformFor({ sends: [lockAnswer(), lockAnswer(), { statusCode: 504, headers: {}, body: JSON.stringify({ error: { code: '0x80048d19', message: `the gateway gave up (${value})` } }) }] });
    await assert.rejects(ss.runImport(request, { http: refused.http, sdkFirst: importingOver(), importJobId: JOB, readJobLog: async () => null }), (err) => {
      const [, , third] = refused.imports().map(jobIdOf);
      assert.strictEqual(err.message, `HTTP 504 from ${ENV}/api/data/v9.0/ImportSolution: ${WITHHELD('message')} The last send was not sent again: the environment may have accepted it.${ss.lookUpJob(third)} ${RESENT(3, '10 s, 20 s')}`, value);
      return true;
    });
  }
});

test('a settings feature the SDK does not apply goes to pac without a single send, proof or wait — the resend and the pac fallback never meet', async () => {
  const { request } = importFiles(template({ Workflows: [{ WorkflowId: 'x' }] }));
  const platform = platformFor({ sends: [lockAnswer(), ok()] });
  const sdk = sdkStub().sdk;
  sdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', listSentence('Workflows')); };
  const { pac, calls } = fakePac(() => ({ status: 0, stdout: 'Solution Imported successfully.\n', stderr: '' }));
  const result = await ss.runImport(request, { http: platform.http, sdkFirst: stubSdkFirst(sdk), pac, importJobId: JOB });
  assert.strictEqual(result.source, 'pac');
  assert.ok(!('importAttempts' in result) && !('importRefusals' in result) && !('importJobId' in result), 'pac mints its own job and was never refused');
  assert.strictEqual(calls.length, 1);
  assert.deepStrictEqual([platform.requests.length, platform.waits.length], [0, 0]);
});

// --- the reason an import's own log gives ----------------------------------------------------------------------------------------------------

// A failed import's log, shaped as the platform writes it (abridged; every value is synthetic): the root says the job failed, and each component that failed has a `result`.
const JOB_LOG = [
  '<importexportxml start="639000000000000000" stop="639000000100000000" progress="100" processed="true" succeeded="failure" status="Import failed">',
  '  <solutionManifests>',
  '    <solutionManifest>',
  '      <UniqueName>ContosoDemo</UniqueName><Version>1.0.0.0</Version><Managed>0</Managed>',
  '      <result result="success" errorcode="0x0" errortext="" />',
  '    </solutionManifest>',
  '  </solutionManifests>',
  '  <entities>',
  '    <entity name="new_ticket"><result result="failure" errorcode="0x80048d19" errortext="Could not find connection &apos;00000000000000000000000000000abc&apos; for API &apos;shared_sharepointonline&apos;." /></entity>',
  '    <entity name="new_other"><result result="failure" errorcode="0x80040203" errortext="A second failure" /></entity>',
  '  </entities>',
  '</importexportxml>',
].join('\n');

test('the first failure a job log reports is the COMPLETE text of its first failing result — XML references decoded, nothing collapsed or cut — else the root status', () => {
  assert.strictEqual(ss.jobLogFailure(JOB_LOG), "Could not find connection '00000000000000000000000000000abc' for API 'shared_sharepointonline'.");
  // Either quote, any order of attributes, a result that succeeded is skipped, a failure with no text is skipped; line breaks and white space in the text are as the log has them.
  assert.strictEqual(ss.jobLogFailure(`<importexportxml succeeded='failure'><a><result errortext='first&#10;line &amp; &lt;b&gt; &#x41;' RESULT='Failure'/></a></importexportxml>`), 'first\nline & <b> A');
  assert.strictEqual(ss.jobLogFailure('<importexportxml succeeded="failure" status="x"><result result="success" errortext="not this"/><result result="failure" errortext=""/><result result="failure" errortext="this one"/></importexportxml>'), 'this one');
  assert.strictEqual(ss.jobLogFailure('<result result="failure" errortext="  runs   of &#9;white&#10;&#10;space  "/>'), '  runs   of \twhite\n\nspace  ');
  // No failing result with text: the root's status — and only when the root does not say the job succeeded.
  assert.strictEqual(ss.jobLogFailure('<importexportxml succeeded="failure" status="Import failed"><result result="success"/></importexportxml>'), 'its status is Import failed');
  assert.strictEqual(ss.jobLogFailure('<importexportxml status="Import failed"></importexportxml>'), 'its status is Import failed');
  assert.strictEqual(ss.jobLogFailure('<importexportxml succeeded="success" status="Completed"></importexportxml>'), null);
  // Nothing readable: null, so the failure stands as it was.
  for (const log of [null, undefined, '', 'not xml', '<importexportxml></importexportxml>', '<result result="failure"/>']) assert.strictEqual(ss.jobLogFailure(log), null, String(log));
  // No cut and no collapsing here — the caller sanitizes this whole text first (importFailure) — for a result's text and for the root's status alike.
  assert.strictEqual(ss.jobLogFailure(`<result result="failure" errortext="${'x'.repeat(2000)}"/>`), 'x'.repeat(2000));
  assert.strictEqual(ss.jobLogFailure(`<importexportxml succeeded="failure" status="${'s'.repeat(2000)}"/>`), `its status is ${'s'.repeat(2000)}`);
  assert.strictEqual(ss.jobLogFailure('<result result="failure" errortext="&#99999999999;&#xZZ; &bogus;"/>'), '&#99999999999;&#xZZ; &bogus;', 'what is not an entity is left as it is');
});

test('oneLine makes the runs of white space one space and cuts a text to 600 characters — what is done to a text that has been through the sanitizer, never before it', () => {
  assert.strictEqual(ss.oneLine('a  b\n\tc \r\n d'), 'a b c d');
  assert.strictEqual(ss.oneLine('  padded  '), 'padded');
  assert.strictEqual(ss.oneLine('x'.repeat(600)), 'x'.repeat(600), 'exactly 600: not cut');
  assert.strictEqual(ss.oneLine('x'.repeat(601)), `${'x'.repeat(600)}…`);
  assert.strictEqual(ss.oneLine(`${'x'.repeat(599)}\n\n\nyz`), `${'x'.repeat(599)} …`, 'the collapsing comes first, so what is counted is what is shown');
  assert.strictEqual(ss.oneLine('x'.repeat(10), 5), 'xxxxx…');
});

test('a failed import reads its job\'s log once and puts the first failure\'s text in the message — through the same cleaning — and keeps the original message when the log cannot be read', async () => {
  const reads = [];
  const { request } = importFiles(filledPlain());
  const { sdk } = sdkStub();
  sdk.importSolution = failAs(importFailureOf(400, 'An unexpected error occurred.'));
  const log = JOB_LOG.replace('00000000000000000000000000000abc', CONNECTION_ID);
  await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog: async (env, id) => { reads.push([env, id]); return log; } }), (err) => {
    assert.strictEqual(err.message, `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: An unexpected error occurred. The import job's log (importjobs(${JOB})) says: Could not find connection '<redacted>' for API 'shared_sharepointonline'.`);
    assert.strictEqual(err.importJobId, JOB);
    return true;
  });
  assert.deepStrictEqual(reads, [[ENV, JOB]], 'one read, of this job, in this environment');

  // A log that quotes a SECRET is withheld like any other text; the original message stays.
  const secret = importFiles();
  const secretSdk = sdkStub().sdk;
  secretSdk.importSolution = failAs(importFailureOf(400, 'An unexpected error occurred.'));
  await assert.rejects(ss.runImport(secret.request, { sdkFirst: stubSdkFirst(secretSdk), importJobId: JOB, readJobLog: async () => `<result result="failure" errortext="bad value ${SECRET}"/>` }), (err) => {
    assert.ok(!err.message.includes(SECRET));
    assert.match(err.message, /An unexpected error occurred\. The import job's log \(importjobs\([0-9a-f-]+\)\) says: <the job log's text withheld: it repeated a value from the settings file>$/);
    return true;
  });

  // A read that fails, or says nothing, leaves the failure as it was.
  for (const read of [async () => { throw new Error('the environment is slow'); }, async () => null, async () => 'not a log']) {
    const { request: again } = importFiles();
    const { sdk: failing } = sdkStub();
    failing.importSolution = failAs(importFailureOf(400, 'An unexpected error occurred.'));
    await assert.rejects(ss.runImport(again, { sdkFirst: stubSdkFirst(failing), importJobId: JOB, readJobLog: read }), (err) => {
      assert.strictEqual(err.message, `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: An unexpected error occurred.`);
      assert.strictEqual(err.importJobId, JOB);
      return true;
    });
  }
});

// A job log whose one failing result says `text`, escaped as the platform writes an attribute.
const logSaying = (text) => `<importexportxml succeeded="failure" status="Import failed"><entity><result result="failure" errortext="${text.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;')}"/></entity></importexportxml>`;
// What an import of `value` (a variable the package says is of type `type`) fails with when the job's log says `log` and the answer was "An unexpected error occurred."
async function failedImportWithLog(value, log, type = SECRET_TYPE) {
  const { request } = importFiles({ EnvironmentVariables: [{ SchemaName: 'new_Url', Value: value }], ConnectionReferences: [] });
  const { sdk } = sdkStub();
  sdk.importSolution = failAs(importFailureOf(400, 'An unexpected error occurred.'));
  try {
    await ss.runImport(request, { ...stagedAs({ new_Url: type }, sdk), importJobId: JOB, readJobLog: async () => log });
  } catch (err) {
    return err.message;
  }
  return assert.fail('the import should have failed');
}
const LOG_LEAD = `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: An unexpected error occurred. The import job's log (importjobs(${JOB})) says: `;

test('the job log\'s words are sanitized COMPLETE, and only then made one line and cut — a secret that crosses the 600th character, or that holds runs of white space, is never shown in part', async () => {
  const secret = 'CedarPineSecret0123456789';
  // The secret begins before the 600th character of the line and ends after it — and at every offset around that cut — in a failing result and in the root's status.
  for (const before of [0, 1, 100, 570, 575, 580, 590, 592, 595, 599, 600, 601, 700]) {
    assert.strictEqual(await failedImportWithLog(secret, logSaying(`${'x'.repeat(before)}${secret} was refused`)), `${LOG_LEAD}${WITHHELD("the job log's text")}`, `${before} characters before it`);
    assert.strictEqual(await failedImportWithLog(secret, `<importexportxml succeeded="failure" status="${'x'.repeat(before)}${secret}"/>`), `${LOG_LEAD}${WITHHELD("the job log's text")}`, `${before} characters before it, in the status`);
  }
  // A secret made of runs of white space: the log's text is read as it is, so the value is found — one line made of it first would no longer hold it.
  const spaced = 'Cedar  Pine Secret42';
  for (const text of [`Value '${spaced}' was refused`, `Value '${spaced}'\n\n was refused`, `Value\t'${spaced}' was refused`]) {
    assert.strictEqual(await failedImportWithLog(spaced, logSaying(text)), `${LOG_LEAD}${WITHHELD("the job log's text")}`, JSON.stringify(text));
  }
  // A value that is not a secret is replaced in the complete text, and the line is made after: its white space is kept for the search, and what is cut is only the end.
  assert.strictEqual(await failedImportWithLog(spaced, logSaying(`Value '${spaced}'\n\n was refused`), TEXT_TYPE), `${LOG_LEAD}Value '<redacted>' was refused`);
  const plainAtTheCut = await failedImportWithLog(PLAIN, logSaying(`${'x'.repeat(592)}${PLAIN} was refused`), TEXT_TYPE);
  assert.strictEqual(plainAtTheCut, `${LOG_LEAD}${'x'.repeat(592)}<redacte…`);
  // The text is cut to 600 characters when nothing in it is a value, and not before.
  assert.strictEqual(await failedImportWithLog(secret, logSaying('y'.repeat(600))), `${LOG_LEAD}${'y'.repeat(600)}`);
  assert.strictEqual(await failedImportWithLog(secret, logSaying('y'.repeat(601))), `${LOG_LEAD}${'y'.repeat(600)}…`);
  assert.strictEqual(await failedImportWithLog(secret, logSaying(`a  b\n\nc ${'y'.repeat(598)}`)), `${LOG_LEAD}a b c ${'y'.repeat(594)}…`);
});

test('nothing in a text is trusted for looking like the SDK\'s status line — a job log\'s, a warning\'s, pac\'s, or what the SDK\'s own message goes on to say — only the line the SDK\'s error itself has', async () => {
  const secret = 'CedarPineSecret42';
  const lookalike = `HTTP 400 from https://diagnostic.example/${secret}: rejected`;
  // The job log.
  assert.strictEqual(await failedImportWithLog(secret, logSaying(lookalike)), `${LOG_LEAD}${WITHHELD("the job log's text")}`);
  assert.strictEqual(await failedImportWithLog(PLAIN, logSaying(`HTTP 400 from https://diagnostic.example/${PLAIN}: rejected`), TEXT_TYPE), `${LOG_LEAD}HTTP 400 from https://diagnostic.example/<redacted>: rejected`);
  // The SDK's own message: the line it really has is kept in front of the notice; what the platform said after it, which can look like another one, is text.
  const { request } = importFiles({ EnvironmentVariables: [{ SchemaName: 'new_Url', Value: secret }], ConnectionReferences: [] });
  const { sdk } = sdkStub();
  sdk.importSolution = failAs(withWarnings(importFailureOf(400, lookalike), [lookalike, `HTTP 400 from ${ENV}/api/data/v9.0/stagesolutionuploads(x): cleanup said ${secret}`]));
  await assert.rejects(ss.runImport(request, { ...stagedAs({ new_Url: SECRET_TYPE }, sdk), importJobId: JOB, readJobLog: async () => null }), (err) => {
    assert.strictEqual(err.message, `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: ${WITHHELD('message')}`);
    assert.deepStrictEqual(err.warnings, [WITHHELD('warning'), WITHHELD('warning')], 'no warning keeps a line of its own, whatever it begins with');
    assert.ok(!JSON.stringify({ message: err.message, warnings: err.warnings }).includes(secret));
    return true;
  });
  // An error whose message does not begin with what its own fields would make has no status line to keep — the whole of it is withheld.
  const rebuilt = importFiles({ EnvironmentVariables: [{ SchemaName: 'new_Url', Value: secret }], ConnectionReferences: [] });
  const rebuiltSdk = sdkStub().sdk;
  rebuiltSdk.importSolution = failAs(Object.assign(new Error(`HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: ${secret}`), { url: `${ENV}/api/data/v9.0/ImportSolution`, statusCode: 409, code: 'CONNECTION_ERROR' }));
  await assert.rejects(ss.runImport(rebuilt.request, { ...stagedAs({ new_Url: SECRET_TYPE }, rebuiltSdk), importJobId: JOB, readJobLog: async () => null }), (err) => err.message === WITHHELD('message'));
  // pac's text.
  const viaPac = importFiles({ EnvironmentVariables: [{ SchemaName: 'new_Url', Value: secret }], ConnectionReferences: [], CanvasApps: [{ Name: 'x' }] });
  const pacSdk = sdkStub().sdk;
  pacSdk.importSolution = async () => { throw refusal('INVALID_ARGUMENT', listSentence('CanvasApps')); };
  const { pac } = fakePac(() => ({ status: 1, stdout: '', stderr: `Error: ${lookalike}` }));
  await assert.rejects(ss.runImport(viaPac.request, { sdkFirst: stubSdkFirst(pacSdk), pac }), (err) => {
    assert.strictEqual(err.message, `\`pac solution import\` failed (exit 1): ${WITHHELD('text')}`);
    return true;
  });
});

test('the job log is read only for a failure of the ImportSolution request itself: not for a refusal before it, a staging, an export, or a pac import', async () => {
  const reads = [];
  const readJobLog = async () => { reads.push(1); return JOB_LOG; };
  const cases = [
    importFailureOf(400, 'staging said no', `${ENV}/api/data/v9.0/StageSolution`),
    importFailureOf(400, 'export said no', `${ENV}/api/data/v9.0/ExportSolution`),
    refusal('INVALID_ARGUMENT', 'importSolution: deploymentSettings does not match the package'),
    refusal('SOLUTION_STAGING_FAILED', 'did not pass'),
    new Error('plain'),
  ];
  for (const failure of cases) {
    const { request } = importFiles();
    const { sdk } = sdkStub();
    sdk.importSolution = failAs(failure);
    await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, readJobLog }));
  }
  const viaPac = importFiles({ ...filled(), CanvasApps: [{ Name: 'x' }] });
  const { sdk: pacSdk } = sdkStub();
  pacSdk.importSolution = failAs(refusal('INVALID_ARGUMENT', listSentence('CanvasApps')));
  const failed = fakePac(() => ({ status: 1, stdout: '', stderr: 'Error: x' }));
  await assert.rejects(ss.runImport(viaPac.request, { sdkFirst: stubSdkFirst(pacSdk), pac: failed.pac, importJobId: JOB, readJobLog }));
  assert.deepStrictEqual(reads, [], 'no log was read');
});

test('without a stand-in the log is one GET of importjobs(<id>)?$select=data through the az-token client, once — a failing read is not repeated and its 401 is not refreshed', async () => {
  const sent = [];
  const answer = (statusCode, body) => ({ statusCode, headers: {}, body: typeof body === 'string' ? body : JSON.stringify(body) });
  const http = (answers) => ({ getToken: () => 'dataverse-token', sleep: async () => {}, request: async (options) => { sent.push(options); return answers.shift(); } });
  const reason = await ss.readImportJobFailure(ENV, JOB, { http: http([answer(200, { importjobid: JOB, data: JOB_LOG })]) });
  assert.strictEqual(reason, "Could not find connection '00000000000000000000000000000abc' for API 'shared_sharepointonline'.");
  assert.strictEqual(sent.length, 1);
  assert.strictEqual(sent[0].method, 'GET');
  assert.strictEqual(new URL(sent[0].url).origin, ENV);
  assert.strictEqual(decodeURIComponent(new URL(sent[0].url).pathname + new URL(sent[0].url).search).replace(/^\/api\/data\/v9\.\d+\//, ''), `importjobs(${JOB})?$select=data`);
  for (const status of [500, 503, 401, 404, 429]) {
    sent.length = 0;
    assert.strictEqual(await ss.readImportJobFailure(ENV, JOB, { http: http([answer(status, '{}'), answer(200, { data: JOB_LOG })]) }), null, String(status));
    assert.strictEqual(sent.length, 1, `HTTP ${status}: one request`);
  }
  assert.strictEqual(await ss.readImportJobFailure(ENV, JOB, { http: http([{ error: 'Request timed out' }]) }), null);
  assert.strictEqual(await ss.readImportJobFailure(ENV, JOB, { http: http([answer(200, { data: 42 })]) }), null);
  assert.strictEqual(await ss.readImportJobFailure(ENV, JOB, { http: http([answer(200, 'plain text')]) }), null);
});

test('a job log that is never answered does not hold the failure report: the wait is bounded, and the failure stands as it was', { timeout: 15000 }, async () => {
  const hanging = { getToken: () => 'dataverse-token', sleep: async () => {}, request: () => new Promise(() => {}) };
  const started = Date.now();
  assert.strictEqual(await ss.readImportJobFailure(ENV, JOB, { http: hanging, jobLogTimeoutMs: 40 }), null);
  assert.ok(Date.now() - started < 10000, 'it gave up at its own limit');
  // Through a whole import: the original message, the job named, nothing else.
  const { request } = importFiles();
  const { sdk } = sdkStub();
  sdk.importSolution = failAs(importFailureOf(400, 'An unexpected error occurred.'));
  await assert.rejects(ss.runImport(request, { sdkFirst: stubSdkFirst(sdk), importJobId: JOB, http: hanging, jobLogTimeoutMs: 40 }), (err) => {
    assert.strictEqual(err.message, `HTTP 400 from ${ENV}/api/data/v9.0/ImportSolution: An unexpected error occurred.`);
    assert.strictEqual(err.importJobId, JOB);
    return true;
  });
});

// --- what a failure prints ----------------------------------------------------------------------------------------------------------

test('a failure is described by its message and — each only when there is one — its code, the HTTP status, the reason pac was asked, its import job and its cleanup warnings', () => {
  assert.deepStrictEqual(ss.describeFailure(new Error('plain')), { ok: false, error: 'plain' });
  const error = Object.assign(new Error('boom'), { code: 'PAC_FAILED', fallback: { code: 'X', message: 'y' }, importJobId: JOB });
  assert.deepStrictEqual(ss.describeFailure(error, () => ['a staged copy was left']), {
    ok: false, error: 'boom', code: 'PAC_FAILED', fallback: { code: 'X', message: 'y' }, importJobId: JOB, warnings: ['a staged copy was left'],
  });
  assert.deepStrictEqual(ss.describeFailure(error, () => []), { ok: false, error: 'boom', code: 'PAC_FAILED', fallback: { code: 'X', message: 'y' }, importJobId: JOB });
  // The status of the answer that failed it is a field of its own — it stays when the text is withheld — and is left out when there was no answer (0) or no status.
  assert.strictEqual(ss.describeFailure(Object.assign(new Error('m'), { code: 'CONNECTION_ERROR', statusCode: 504 })).status, 504);
  assert.ok(!('status' in ss.describeFailure(Object.assign(new Error('m'), { statusCode: 0 }))));
  assert.ok(!('status' in ss.describeFailure(new Error('m'))));
  // An error that carries its warnings already read and cleaned (an import's) is described by those; the raw ones are not read again.
  const cleaned = Object.assign(new Error('m'), { warnings: ['cleaned'] });
  assert.deepStrictEqual(ss.describeFailure(cleaned, () => { throw new Error('the raw warnings must not be read'); }).warnings, ['cleaned']);
});

async function runCli(argv, { readCleanupWarnings = () => undefined } = {}) {
  const emitted = [];
  const requires = {
    './lib/dataverse-auth.js': {
      ...realAuth,
      emitResult: (ok, payload, options) => {
        emitted.push({ ok, payload, options });
        const exit = new Error(`process.exit(${ok ? 0 : 1})`);
        exit.exitCode = ok ? 0 : 1;
        throw exit;
      },
    },
    './lib/maker-sdk.js': { readCleanupWarnings },
    './lib/sdk-first.js': { sdkFirst: async () => { throw new Error('unexpected SDK read'); } },
  };
  const cli = loadCli(SCRIPT, { requires, argv });
  let exitCode = 0;
  try {
    await cli.main();
  } catch (err) {
    if (typeof err.exitCode !== 'number') throw err;
    exitCode = err.exitCode;
  }
  return { emitted, exitCode, stdout: cli.stdoutText(), stderr: cli.stderrText() };
}

test('a usage error exits 1 with the problem and the usage on stderr, and prints nothing on stdout', async () => {
  const run = await runCli(['template', '--env', ENV, '--out', 'o.json']);
  assert.strictEqual(run.exitCode, 1);
  assert.strictEqual(run.stdout, '');
  assert.match(run.stderr, /✗ name the solution to export with --solution, or a package with --package/);
  assert.match(run.stderr, /Usage:\n {2}node scripts\/solution-settings\.js template --env/);
  assert.deepStrictEqual(run.emitted, []);
});

test('a failure prints one ok:false line as ASCII with the SDK\'s cleanup warnings, and exits 1', async () => {
  const dir = tempDir();
  const { pkg } = files(dir);
  // The real script, unstubbed past its edges: a package that is not a zip fails the run before any SDK is made.
  fs.writeFileSync(pkg, 'text');
  const run = await runCli(['template', '--package', pkg, '--out', path.join(dir, 'o.json')], { readCleanupWarnings: () => ['a staged copy was left'] });
  assert.strictEqual(run.exitCode, 1);
  assert.strictEqual(run.emitted[0].ok, false);
  assert.deepStrictEqual(run.emitted[0].options, { ascii: true });
  assert.strictEqual(run.emitted[0].payload.code, 'PACKAGE_UNREADABLE');
  assert.deepStrictEqual(run.emitted[0].payload.warnings, ['a staged copy was left']);
});