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.

119 KB
Raw Blame
'use strict';
// Stable create identities (`createIdentity: "stable-v1"`) for declared views and charts, through the real engine
// (lib/sdk-build.js runSdkBuild), the real vendored SDK and the fake Dataverse org: the create is sent with the id the spec
// derives, the binding is kept in the managed binding store, and a rerun from the same or a fresh workspace binds that row
// instead of creating a second one. A spec without the field builds exactly as before.

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { createMakerSdk, createNodeWorkspaceStorage } = require('../vendor/cds-maker-sdk.cjs');
const { runSdkBuild } = require('../lib/sdk-build.js');
const { validateAppSpec, CREATE_IDENTITY_STABLE } = require('../lib/app-spec.js');
const { lintAppSpec } = require('../lib/spec-lint.js');
const { ACTIVATION_VALUE } = require('../lib/creates-identity.js');
const { createStableCreates, stableViewIdentityFor } = require('../lib/creates-stable-build.js');
const { createPassthroughCreates } = require('../lib/creates-passthrough.js');
const { openManagedBindings, openManagedContext, readManagedManifest, PREFIX } = require('../lib/managed-bindings.js');
const { createPublicationBarrier } = require('../lib/publication-barrier.js');
const { createAttemptSendObserver } = require('../lib/managed-feature-contract.js');
const { buildModelApp, authorizationKeys } = require('../build-model-app.js');
const { shellTokens } = require('./helpers/shell-tokens.js');
const { createFakeBuildOrg } = require('./helpers/fake-app-build-org.js');
const { gatedClient } = require('./helpers/gated-fake-client.js');
const { seedManagedDebt } = require('./helpers/managed-debt.js');
const { verifySpec } = require('../lib/verify-spec.js');
const { appComponentsFor, declaredAppPins } = require('../lib/app-components.js');
const { indexArtifacts } = require('../lib/apply-snapshot-index.js');

const dirs = [];
test.after(() => { for (const d of dirs) fs.rmSync(d, { recursive: true, force: true }); });

const ENTITIES = { contoso_item: { attributes: { contoso_name: 'String', contoso_code: 'String', contoso_kind: 'Picklist' } } };
const SOLUTION = 'ContosoStable';
const APP = 'contoso_stableids';

function makeSpec(extra = {}) {
  return {
    solution: { uniqueName: SOLUTION, displayName: 'Contoso Stable', publisherPrefix: 'contoso' },
    app: { name: 'Stable Ids', uniqueName: APP },
    entities: [{
      schemaName: 'contoso_item', displayName: 'Item', existing: true, primaryAttribute: { schemaName: 'contoso_name', displayName: 'Name' },
      columns: [{ schemaName: 'contoso_code', displayName: 'Code', type: 'Text' }],
    }],
    views: [{ entity: 'contoso_item', name: 'Declared Items', reconcile: 'declared', columns: ['contoso_name', 'contoso_code'] }],
    charts: [{ entity: 'contoso_item', name: 'By Kind', chartType: 'Pie', groupBy: 'contoso_kind', reconcile: 'declared' }],
    appShell: { areas: [{ label: 'Main', groups: [{ label: 'Records', subAreas: [{ entity: 'contoso_item', title: 'Items' }] }] }] },
    ...extra,
  };
}
const stableSpec = (extra) => makeSpec({ createIdentity: 'stable-v1', ...extra });
const VIEW_KEY = 'contoso_item/Declared Items';
const CHART_KEY = 'contoso_item/By Kind';

// The ids the spec alone derives (no store, no SDK): what the first create must be sent with, in every workspace.
function derived(spec) {
  const s = createStableCreates({ spec, sdk: null, store: null, basePort: createPassthroughCreates() });
  return { view: s.identityFor('view', VIEW_KEY).id, chart: s.identityFor('chart', CHART_KEY).id };
}

function newDir() {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'create-stable-ids-'));
  dirs.push(dir);
  return dir;
}

const storeFiles = (dir) => (fs.existsSync(dir) ? fs.readdirSync(dir).filter((f) => f.startsWith(PREFIX)) : []);
const storeOf = (org, dir, barrier = null) => openManagedBindings({ workspaceDir: dir, environment: org.origin, appUniqueName: APP, solutionUniqueName: SOLUTION, barrier });

// One build over a fresh SDK on `dir`, as the CLI's durable workspace is reused by a re-run.
async function build(org, dir, spec, opts = {}) {
  const barrier = opts.publicationBarrier || createPublicationBarrier();
  const sdk = opts.sdk || createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier, opts.sendObserver || null) });
  if (!opts.sdk) await sdk.initWorkspace();
  const mark = org.calls.length;
  let out;
  let err;
  try {
    out = await runSdkBuild(spec, { sdk, apply: true, publish: false, workspaceDir: dir, appDir: dir, env: org.origin, warn: () => {}, phases: ['views', 'charts'], publicationBarrier: barrier, ...opts, sdk });
  } catch (e) {
    err = e;
  }
  const calls = org.calls.slice(mark);
  return { out, err, sdk, calls, writes: calls.filter((c) => c.method !== 'GET'), tablePublishes: org.tablePublishes(mark) };
}

// The CLI applies only a full build; its engine is narrowed to the two phases these tests are about (every other phase is covered elsewhere).
const viewsAndCharts = (s, o) => runSdkBuild(s, { ...o, phases: ['views', 'charts'] });

const creates = (r, collection) => r.writes.filter((c) => c.method === 'POST' && c.path.startsWith(`/${collection}`));

// Everything a build's stop says: its message, its causes and the findings it carries, as one text.
function chainText(err) {
  const out = [];
  const seen = new Set();
  const walk = (e) => {
    if (!e || typeof e !== 'object' || seen.has(e)) return;
    seen.add(e);
    out.push(String(e.message || ''));
    for (const f of Array.isArray(e.findings) ? e.findings : []) out.push(`${f.code}: ${f.message}`);
    walk(e.cause);
  };
  walk(err);
  return out.join('\n');
}

// --- grammar --------------------------------------------------------------------------------------------------------------------

test('the grammar takes createIdentity absent or exactly the one value the identity module activates on', () => {
  assert.equal(CREATE_IDENTITY_STABLE, ACTIVATION_VALUE, 'the grammar and the identity module name one value');
  const errorsOf = (spec) => validateAppSpec(spec).errors.filter((e) => /createIdentity/.test(e));
  assert.deepEqual(errorsOf(makeSpec()), []);
  assert.deepEqual(errorsOf(stableSpec({ minimumPluginVersion: '2.21.0' })), []);
  for (const bad of ['stable-v2', 'Stable-V1', true, null, '', 1]) {
    const errors = errorsOf(makeSpec({ createIdentity: bad }));
    assert.equal(errors.length, 1, JSON.stringify(bad));
    assert.match(errors[0], /^create-identity-invalid: createIdentity must be "stable-v1" or absent/);
  }
  const floor = (spec) => lintAppSpec(spec).warnings.filter((w) => /when using createIdentity/.test(w));
  assert.equal(floor(stableSpec()).length, 1, 'no floor: older plugins ignore the field');
  assert.equal(floor(stableSpec({ minimumPluginVersion: '2.20.0' })).length, 1);
  assert.deepEqual(floor(stableSpec({ minimumPluginVersion: '2.21.0' })), []);
  assert.deepEqual(floor(makeSpec()), []);
});

// --- the engine -------------------------------------------------------------------------------------------------------------------

test('a stable view and chart are created once at the derived ids, bound in the store, and a rerun binds them with no write', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec();
  const ids = derived(spec);
  const first = await build(org, dir, spec);
  assert.equal(first.err, undefined, first.err && first.err.message);
  assert.equal(first.out.created.views['contoso_item|Declared Items'], ids.view);
  assert.equal(first.out.created.charts['contoso_item|By Kind'], ids.chart);
  assert.equal(creates(first, 'savedqueries').length, 1);
  assert.equal(creates(first, 'savedqueryvisualizations').length, 1);
  assert.equal(storeFiles(dir).length, 1, 'the bindings are kept in one store file');
  const store = storeOf(org, dir);
  for (const [kind, key, id] of [['view', VIEW_KEY, ids.view], ['chart', CHART_KEY, ids.chart]]) {
    const g = store.readGroup(kind, key);
    assert.equal(g.pending, null, `${kind}: no attempt is left pending`);
    assert.equal(g.committed.root.id, id);
    assert.equal(g.committed.records[0].origin, 'created', `${kind}: the acknowledged create is recorded as created`);
  }
  const bytes = fs.readFileSync(path.join(dir, storeFiles(dir)[0]), 'utf8');

  // A new process over the same workspace: the same ids, nothing sent, and the store unchanged.
  const again = await build(org, dir, spec);
  assert.equal(again.err, undefined, again.err && again.err.message);
  assert.deepEqual(again.writes, []);
  assert.equal(storeOf(org, dir).readGroup('view', VIEW_KEY).committed.root.id, ids.view);
  assert.equal(fs.readFileSync(path.join(dir, storeFiles(dir)[0]), 'utf8'), bytes);
});

test('a fresh workspace on the same environment finds the rows at the derived ids: unbound and untouched without the adoption approval, adopted with it', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const spec = stableSpec();
  const ids = derived(spec);
  const first = await build(org, newDir(), spec);
  assert.equal(first.err, undefined, first.err && first.err.message);

  // A row at the derived id is an observation, not a binding: nothing proves this workspace made it.
  const dir = newDir();
  const said = [];
  const fresh = await build(org, dir, spec, { warn: (m) => said.push(String(m)) });
  assert.equal(fresh.err, undefined, fresh.err && fresh.err.message);
  assert.deepEqual(fresh.writes, [], 'an equal row with no approval is left as it is');
  let store = storeOf(org, dir);
  assert.equal(store.readGroup('view', VIEW_KEY).committed, null, 'no binding without the adoption approval');
  assert.equal(store.readGroup('chart', CHART_KEY).committed, null);
  // The clean result is not silent: the maker is told each row is not managed, and the one approval that binds it.
  for (const [kind, key, id] of [['view', VIEW_KEY, ids.view], ['chart', CHART_KEY, ids.chart]]) {
    const lines = said.filter((m) => m.includes(`${kind}-not-adopted`));
    assert.equal(lines.length, 1, `${kind}: one warning\n${said.join('\n')}`);
    assert.ok(lines[0].includes(id), `${kind}: the warning names the row`);
    assert.ok(lines[0].endsWith(`--authorize "adopt:${kind}:${key}"`), `${kind}: the warning names the approval as one quoted argument: ${lines[0]}`);
    // Pasted into a shell, the printed hint is the one argument the CLI's own parser accepts, and it names exactly this row.
    const tokens = shellTokens(lines[0].slice(lines[0].lastIndexOf('--authorize')));
    assert.deepEqual(tokens, ['--authorize', `adopt:${kind}:${key}`]);
    const parsed = authorizationKeys(tokens, spec);
    assert.equal(parsed.error, null, parsed.error);
    assert.deepEqual([...parsed.adopt], [`${kind}:${key}`]);
  }
  // Nor does it give authority to change the row: an edited declaration without the approval writes nothing.
  const edited = stableSpec();
  edited.views[0].description = 'Edited without the adoption approval';
  edited.charts[0].description = 'Edited without the adoption approval';
  const edit = await build(org, dir, edited);
  assert.ok(edit.err, 'the unbound rows are refused');
  assert.deepEqual(edit.writes, [], 'nothing is overwritten on identity alone');
  store = storeOf(org, dir);
  assert.equal(store.readGroup('view', VIEW_KEY).committed, null);

  const adoptSaid = [];
  const adopted = await build(org, dir, spec, { authorizedAdoption: new Set([`view:${VIEW_KEY}`, `chart:${CHART_KEY}`]), warn: (m) => adoptSaid.push(String(m)) });
  assert.equal(adopted.err, undefined, adopted.err && adopted.err.message);
  assert.deepEqual(adoptSaid.filter((m) => /-not-adopted/.test(m)), [], 'a bound row is not reported as unmanaged');
  assert.equal(creates(adopted, 'savedqueries').length, 0, 'the view at the derived id is bound, not created again');
  assert.equal(creates(adopted, 'savedqueryvisualizations').length, 0);
  assert.equal(org.fake.rows('savedqueries').filter((r) => r.data.name === 'Declared Items').length, 1);
  store = storeOf(org, dir);
  assert.equal(store.readGroup('view', VIEW_KEY).committed.root.id, ids.view);
  assert.equal(store.readGroup('view', VIEW_KEY).committed.records[0].origin, 'adopted', 'nothing proves this workspace made the row');
  assert.equal(store.readGroup('chart', CHART_KEY).committed.records[0].origin, 'adopted');
});

// The warning a build really prints for a view or a chart whose name holds `$`, a backtick or a double quote. A spelling every shell passes on unchanged is pasted into each
// real shell and names exactly that row to the CLI's parser; a name with none (a `"`, an apostrophe with `$`) gets the exact key as a JSON string and no spelling.
for (const [name, spelled] of [['cost $5', true], ['a`b', true], ['say "hi"', false], ['n" end', false], ['it\'s $5', false]]) {
  test(`the not-adopted warning for a view and a chart named ${JSON.stringify(name)} ${spelled ? 'is an approval each real shell passes on as the one key it names' : 'gives the exact key and no shell spelling'}`, async (t) => {
    const { runPrintedFlags, skipWithoutShells } = require('./helpers/printed-flag-shells.js');
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const spec = stableSpec({
      views: [{ entity: 'contoso_item', name, reconcile: 'declared', columns: ['contoso_name', 'contoso_code'] }],
      charts: [{ entity: 'contoso_item', name, chartType: 'Pie', groupBy: 'contoso_kind', reconcile: 'declared' }],
    });
    const first = await build(org, newDir(), spec);
    assert.equal(first.err, undefined, first.err && first.err.message);
    const said = [];
    const fresh = await build(org, newDir(), spec, { warn: (m) => said.push(String(m)) });
    assert.equal(fresh.err, undefined, fresh.err && fresh.err.message);
    const printed = ['view', 'chart'].map((kind) => {
      const lines = said.filter((m) => m.includes(`${kind}-not-adopted`));
      assert.equal(lines.length, 1, `${kind}: one warning\n${said.join('\n')}`);
      return lines[0];
    });
    if (!spelled) {
      for (const [i, kind] of ['view', 'chart'].entries()) {
        assert.ok(printed[i].includes(JSON.stringify(`adopt:${kind}:contoso_item/${name}`)), `the exact key is printed as a JSON string: ${printed[i]}`);
        assert.ok(/ONE --authorize argument/.test(printed[i]) && !/rename|no spelling|no way of writing/.test(printed[i]), printed[i]);
      }
      return;
    }
    assert.ok(printed.every((p) => !/<approval>/.test(p)), `a spelling is printed, not a placeholder: ${printed[0]}`);
    const ran = runPrintedFlags(printed);
    if (skipWithoutShells(t, ran)) return;
    for (const { shell, argv } of ran) {
      for (const [i, kind] of ['view', 'chart'].entries()) {
        const key = `adopt:${kind}:contoso_item/${name}`;
        assert.deepEqual(argv[i], ['--authorize', key], `${shell}: ${printed[i]}`);
        const parsed = authorizationKeys(argv[i], spec);
        assert.equal(parsed.error, null, `${shell}: ${parsed.error}`);
        assert.deepEqual([...parsed.adopt], [`${kind}:contoso_item/${name}`]);
      }
    }
  });
}

test('a spec without createIdentity is unchanged: a fresh id per workspace, no store file, and the same requests', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const spec = makeSpec();
  const ids = derived(stableSpec());
  const dir = newDir();
  const r = await build(org, dir, spec);
  assert.equal(r.err, undefined, r.err && r.err.message);
  assert.notEqual(r.out.created.views['contoso_item|Declared Items'], ids.view);
  assert.notEqual(r.out.created.charts['contoso_item|By Kind'], ids.chart);
  assert.deepEqual(storeFiles(dir), []);
  // Control: the same build with the field sends the same requests in the same order, at other ids.
  const org2 = createFakeBuildOrg({ entities: ENTITIES });
  const s = await build(org2, newDir(), stableSpec());
  assert.equal(s.err, undefined, s.err && s.err.message);
  const shape = (x) => x.writes.map((c) => `${c.method} ${c.path.replace(/\([0-9a-f-]{36}\)/gi, '(<id>)')}`);
  assert.deepEqual(shape(s), shape(r));
});

test('without createIdentity, a declared view and chart found equal by name are left as before: nothing written, no adoption warning', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const first = await build(org, newDir(), makeSpec());
  assert.equal(first.err, undefined, first.err && first.err.message);
  const said = [];
  const fresh = await build(org, newDir(), makeSpec(), { warn: (m) => said.push(String(m)) });
  assert.equal(fresh.err, undefined, fresh.err && fresh.err.message);
  assert.deepEqual(fresh.writes, []);
  // No binding store records a declared view's binding there, so no approval would bind it: none is advised.
  assert.deepEqual(said.filter((m) => /-not-adopted/.test(m)), []);
});

test('a local copy with an edit the server has not seen stops the stable item before any write, keeps the bytes, and stops again', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec({ charts: [] });
  const ids = derived(stableSpec());
  const first = await build(org, dir, spec);
  assert.equal(first.err, undefined, first.err && first.err.message);

  const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: org.httpClient });
  await sdk.initWorkspace();
  await sdk.fetchArtifact('view', ids.view);
  await sdk.updateElement('view', ids.view, '/description', 'An edit the server has not seen');
  const snapshot = () => fs.readdirSync(dir, { recursive: true }).filter((f) => fs.statSync(path.join(dir, f)).isFile() && !String(f).includes('.lease'))
    .sort().map((f) => [f, fs.readFileSync(path.join(dir, f), 'utf8')]);
  const before = snapshot();
  for (let run = 0; run < 2; run += 1) {
    const r = await build(org, dir, spec);
    assert.ok(r.err, `run ${run}: the build stops`);
    assert.match(r.err.message, /create-completion-required/);
    // The stop says what was found and what to do, and points to no approval: none settles it.
    assert.match(r.err.message, /create-completion-required — the local copy of the view [0-9a-f-]{36} in the workspace /);
    assert.match(r.err.message, /Do what the message above says, then run the build again; it is not retried automatically\./);
    assert.doesNotMatch(r.err.message, /approval named above/);
    assert.deepEqual(r.writes, [], `run ${run}: nothing is written`);
    assert.deepEqual(snapshot(), before, `run ${run}: the workspace bytes are unchanged`);
  }
});

// A create whose answer was lost after the row was stored: the transport throws after the fake saved it.
function loseNextCreateAnswer(org, collection) {
  const original = org.httpClient.post;
  let lost = false;
  org.httpClient.post = async (url, body, ...rest) => {
    const response = await original(url, body, ...rest);
    if (!lost && new RegExp(`/${collection}(?:\\?|$)`, 'i').test(url)) {
      lost = true;
      throw Object.assign(new Error('response lost after save'), { code: 'ECONNRESET' });
    }
    return response;
  };
  return () => { org.httpClient.post = original; };
}

// The maker's step after a lost create: the build kept the local draft (create-local-draft-kept) and stops on it until the maker
// removes it with the SDK's own local discard. It must be the draft the build reported keeping.
async function removeKeptDraft(run, kind, id) {
  assert.match(String(run.err && run.err.message), /create-local-draft-kept/);
  assert.equal((await readLocalCreateState(run.sdk, kind, id)).state, 'pending');
  await run.sdk.deleteArtifact(kind, id);
}

for (const [kind, key, collection] of [['view', VIEW_KEY, 'savedqueries'], ['chart', CHART_KEY, 'savedqueryvisualizations']]) {
  test(`a ${kind} whose create answer was lost and whose draft remains tells the maker to follow the steps, never to give the approval they mention`, async () => {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const spec = stableSpec(kind === 'view' ? { charts: [] } : { views: [] });
    const restore = loseNextCreateAnswer(org, collection);
    const first = await build(org, dir, spec);
    restore();
    for (const [what, run] of [['the run that lost the answer', first], ['the next run, which finds the draft', await build(org, dir, spec)]]) {
      const message = String(run.err && run.err.message);
      assert.match(message, /create-completion-required|create-local-draft-kept/, what);
      assert.match(message, new RegExp(`add --authorize "adopt:${kind}:${key}" if the build asks for it`), `${what}: the conditional step quotes its key`);
      assert.match(message, /Follow the steps above, then run the build again; it is not retried automatically\.$/, what);
      assert.doesNotMatch(message, /approval named above|Do what the message above says/, what);
    }
  });
}

test('an attempt an earlier run left pending holds its table until this build proves it, then the hold ends', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec({ charts: [] });
  const ids = derived(stableSpec());
  // An earlier run marked the create as sending and lost the answer after the row was stored.
  const restore = loseNextCreateAnswer(org, 'savedqueries');
  const first = await build(org, dir, spec);
  restore();
  assert.ok(first.err, 'the run whose answer was lost stops');
  const pending = storeOf(org, dir).readGroup('view', VIEW_KEY).pending;
  assert.ok(pending, 'and leaves its attempt pending');
  assert.equal(pending.root.id, ids.view);
  // The build kept its local draft (it never removes one); the maker removes it, as the halt says.
  await removeKeptDraft(first, 'view', pending.root.id);

  // Control: the same store, opened on a barrier with no build to prove it, holds the table.
  const held = createPublicationBarrier();
  require('../lib/managed-bindings.js').openManagedContext({ workspaceDir: dir, environment: org.origin, appUniqueName: APP, solutionUniqueName: SOLUTION, barrier: held });
  assert.equal(held.allow('contoso_item', 'no proof'), false);

  // No answer acknowledged the create, so nothing proves this run made the row: the proof alone binds nothing.
  const refused = await build(org, dir, spec, { publicationBarrier: createPublicationBarrier() });
  assert.match(String(refused.err && refused.err.message), /managed-adoption-required/);
  assert.deepEqual(refused.writes, [], 'nothing is sent again');
  assert.ok(storeOf(org, dir).readGroup('view', VIEW_KEY).pending, 'the attempt is kept');

  const barrier = createPublicationBarrier();
  const r = await build(org, dir, spec, { publicationBarrier: barrier, authorizedAdoption: new Set([`view:${VIEW_KEY}`]) });
  assert.equal(r.err, undefined, r.err && r.err.message);
  assert.equal(creates(r, 'savedqueries').length, 0, 'the row the lost answer made is bound, not created again');
  const g = storeOf(org, dir).readGroup('view', VIEW_KEY);
  assert.equal(g.pending, null);
  assert.equal(g.committed.root.id, ids.view);
  assert.equal(barrier.blocks('contoso_item'), false, 'the proved attempt no longer holds its table');
});
test('a store the build cannot trust halts before any write: managed-binding-store-unreadable, never retried', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec();
  const first = await build(org, dir, spec);
  assert.equal(first.err, undefined, first.err && first.err.message);
  const file = path.join(dir, storeFiles(dir)[0]);
  fs.writeFileSync(file, '{ "not": "a store"');
  const r = await build(org, dir, spec);
  assert.equal(r.err && r.err.code, 'managed-binding-store-unreadable', r.err && r.err.stack);
  assert.equal(r.err.transient, false);
  // The file is the record of changes not yet proved: the remedy keeps it, never moves, deletes or resets it, and never puts back an older
  // copy — a backup, the build's own included, can miss an attempt or a publish sent after it was made.
  assert.match(r.err.message, /keep this file/i);
  assert.doesNotMatch(r.err.message, /aside|delete|remove the file|reset/i);
  assert.match(r.err.message, /never put back an older copy/i);
  assert.doesNotMatch(r.err.message, /put back the build's own copy/i);
  assert.deepEqual(r.writes, []);
  assert.equal(fs.readFileSync(file, 'utf8'), '{ "not": "a store"');

  // The CLI opens the same store once, through the same opener, and halts the same way.
  const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: org.httpClient });
  await sdk.initWorkspace();
  const mark = org.calls.length;
  let cliErr;
  let cliOut;
  try {
    // The CLI validates the spec first; its chart's Choice column is not declared in this spec, so the CLI is given the view alone.
    cliOut = await buildModelApp(stableSpec({ charts: [] }), { apply: true, env: org.origin, workspaceDir: dir, appDir: dir, retryDelayMs: 0 }, { sdk, provisionSdk: sdk, runBuild: viewsAndCharts, warn: () => {}, log: () => {} });
  } catch (e) {
    cliErr = e;
  }
  assert.equal(cliErr && cliErr.code, 'managed-binding-store-unreadable', JSON.stringify(cliOut));
  assert.deepEqual(org.calls.slice(mark).filter((c) => c.method !== 'GET'), []);
});

test('the CLI hands every attempt the one store it opened, and a stable view is created once at its derived id', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec({ charts: [] });
  const ids = derived(stableSpec());
  const seen = [];
  const runBuild = (s, o) => { seen.push(o.bindingGroups); return viewsAndCharts(s, o); };
  for (let run = 0; run < 2; run += 1) {
    // The CLI's client is gated by the barrier it hands the build (build-model-app.js makeSdk); so is this one.
    const publicationBarrier = createPublicationBarrier();
    const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, publicationBarrier) });
    await sdk.initWorkspace();
    const mark = org.calls.length;
    const out = await buildModelApp(spec, { apply: true, env: org.origin, workspaceDir: dir, appDir: dir, retryDelayMs: 0 }, { sdk, provisionSdk: sdk, runBuild, publicationBarrier, warn: () => {}, log: () => {} });
    assert.equal(out.ok, true, JSON.stringify(out.errors));
    const posts = org.calls.slice(mark).filter((c) => c.method === 'POST' && c.path.startsWith('/savedqueries'));
    assert.equal(posts.length, run === 0 ? 1 : 0, `run ${run}`);
  }
  assert.equal(seen.length, 2);
  assert.ok(seen.every((g) => g && g.store), 'each invocation hands its attempt the store it opened');
  assert.equal(storeOf(org, dir).readGroup('view', VIEW_KEY).committed.root.id, ids.view);
});

// --- recovery, coverage and the managed settlement ------------------------------------------------------------------------------

const workspaceArgs = (org, dir) => ({ workspaceDir: dir, environment: org.origin, appUniqueName: APP, solutionUniqueName: SOLUTION });
const isViewCreate = (url) => /\/savedqueries(?:\?|$)/i.test(url);

for (const acknowledged of [false, true]) test(`a pending attempt${acknowledged ? ' whose create was acknowledged' : ''} is proved by the expectation it recorded, never re-baselined from the current declaration`, async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const spec = stableSpec({ charts: [] });
  spec.views[0].description = 'A later declaration, not the pending expectation';
  assert.equal((await build(org, newDir(), spec)).err, undefined);
  // An attempt whose recorded expectation the row does not hold: the build does not adopt today's row as its proof — also
  // when the create's answer came back and only the commit is missing, where no adoption approval is asked for.
  const dir = newDir();
  seedManagedDebt(workspaceArgs(org, dir), { key: VIEW_KEY, id: derived(stableSpec()).view, table: 'contoso_item', acknowledged });
  const barrier = createPublicationBarrier();
  const r = await build(org, dir, spec, { publicationBarrier: barrier, authorizedAdoption: new Set([`view:${VIEW_KEY}`]) });
  assert.ok(r.err, 'the attempt does not prove');
  const g = storeOf(org, dir).readGroup('view', VIEW_KEY);
  assert.ok(g.pending, 'it stays pending');
  assert.equal(g.committed, null, 'and nothing is committed over it');
  assert.equal(barrier.allow('contoso_item', 'proof check'), false, 'its table stays withheld');
  assert.deepEqual(r.tablePublishes, []);
});

test('a lost create keeps its recorded content expectation when the row and the declaration both change afterwards', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const firstDir = newDir();
  const spec = stableSpec({ charts: [] });
  spec.views[0].description = 'Original intended description';
  const restore = loseNextCreateAnswer(org, 'savedqueries');
  const first = await build(org, firstDir, spec);
  restore();
  const before = storeOf(org, firstDir).readGroup('view', VIEW_KEY);
  assert.ok(before.pending, first.err && first.err.message);
  const id = before.pending.root.id;
  await org.httpClient.patch(`${org.origin}/api/data/v9.2/savedqueries(${id})`, { description: 'Different content after the interruption' });
  const dir = newDir();
  for (const file of storeFiles(firstDir)) fs.copyFileSync(path.join(firstDir, file), path.join(dir, file));
  const changed = structuredClone(spec);
  changed.views[0].description = 'Different content after the interruption';
  const barrier = createPublicationBarrier();
  const r = await build(org, dir, changed, { publicationBarrier: barrier, authorizedAdoption: new Set([`view:${VIEW_KEY}`]) });
  const after = storeOf(org, dir).readGroup('view', VIEW_KEY);
  assert.ok(after.pending, 'the attempt is not retired against later content and a changed declaration');
  assert.deepEqual(after.pending.expected, before.pending.expected, 'its expectation is the one written before the send');
  assert.equal(after.committed, null);
  assert.deepEqual(r.writes, [], 'nothing is sent over it');
  assert.equal(barrier.allow('contoso_item', 'after recovery'), false);
});

test('the attempt is journaled as sending, with its complete expected content, before the create leaves', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const original = org.httpClient.post.bind(org.httpClient);
  const seen = [];
  org.httpClient.post = async (url, body, ...args) => {
    if (isViewCreate(url)) seen.push(storeOf(org, dir).readGroup('view', VIEW_KEY).pending);
    return original(url, body, ...args);
  };
  const r = await build(org, dir, stableSpec({ charts: [] }));
  assert.equal(r.err, undefined, r.err && r.err.message);
  assert.equal(seen.length, 1);
  assert.equal(seen[0].phase, 'sending');
  for (const field of ['sentProjectionSha256', 'preservationSha256']) assert.match(String(seen[0].expected[field]), /^[0-9a-f]{64}$/, field);
  assert.equal(seen[0].expected.rootId, derived(stableSpec()).view);
});

test('a table publish whose outcome was not confirmed fences the table in every later build, a new sibling view included', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec({ charts: [] });
  org.refuseNextTablePublish(503);
  const first = await build(org, dir, spec);
  assert.equal(first.tablePublishes.length, 1, 'one publish was sent and answered 503');
  assert.equal(storeOf(org, dir).readGroup('view', VIEW_KEY).committed.publication.state, 'unconfirmed');
  const next = structuredClone(spec);
  next.views.push({ ...next.views[0], name: 'Sibling View' });
  const second = await build(org, dir, next);
  assert.match(String(second.err && second.err.message), /managed-publication-unconfirmed/);
  assert.deepEqual(second.tablePublishes, [], 'the scope is not published again past an unconfirmed send');
});

// A plain publish of a table a confirmed managed view is on owes that view nothing, but it ships the view's saved layer
// again: its dispatch is journaled with the view carried, so a restart after a lost answer neither edits the view nor
// publishes the table again.
for (const observed of [false, true]) {
  test(`a carried table publish whose answer was lost fences the next build: no managed PATCH and no publish${observed ? ' (the attempt send observer inside the barrier)' : ''}`, async () => {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const spec = stableSpec({ charts: [] });
    const run = (s) => build(org, dir, s, observed ? { sendObserver: createAttemptSendObserver() } : {});
    const seed = await run(spec);
    assert.equal(seed.err, undefined, seed.err && seed.err.message);
    assert.equal(storeOf(org, dir).readGroup('view', VIEW_KEY).committed.publication.state, 'confirmed');

    const ordinary = makeSpec({ charts: [] });
    ordinary.views[0].name = 'Plain sibling after managed publication';
    org.refuseNextTablePublish(503);
    const carry = await run(ordinary);
    assert.equal(carry.tablePublishes.length, 1, 'the carried publish was sent once and answered 503');
    assert.match(String(carry.err && carry.err.message), /managed-publication-unconfirmed/);
    const carried = storeOf(org, dir).readGroup('view', VIEW_KEY).committed.publication;
    assert.equal(carried.state, 'unconfirmed', 'the carried view is journaled under the lost dispatch');
    assert.ok(carried.carriedFrom, 'it keeps the settled state it was carried from');

    const edit = structuredClone(spec);
    edit.views[0].description = 'New content while the earlier carried publish is uncertain';
    const next = await run(edit);
    const patches = next.writes.filter((c) => c.method === 'PATCH' && /^\/savedqueries\(/.test(c.path));
    assert.equal(patches.length, 0, 'an earlier uncertain carried publish fences a new managed edit before its PATCH');
    assert.deepEqual(next.tablePublishes, [], 'a restart never replays a carried publish whose dispatch was not disproved');
    assert.match(String(next.err && next.err.message), /managed-publication-unconfirmed/);
  });
}

// A failed publish is RETURNED, not thrown, by both the SDK (`{ shipped: false, publish: { kind: 'failed' } }`) and the
// Dataverse client (`{ status: 500 }`). The managed publish callback must turn that into a throw, or the read-back that follows
// would "confirm" a carried view that was live before the send. A 500 may have run, so it is not claimed as never started.
test('a carried publish answered as failed by a returned value, not a throw, is never confirmed', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec({ charts: [] });
  const seed = await build(org, dir, spec);
  assert.equal(seed.err, undefined, seed.err && seed.err.message);

  const barrier = createPublicationBarrier();
  const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier) });
  await sdk.initWorkspace();
  const sdkPublishes = [];
  const failed = (type, id) => ({ type, id, shipped: false, publish: { kind: 'failed', error: { code: 'publish-failed', message: 'refused' } } });
  sdk.publishArtifact = async (type, id) => { sdkPublishes.push([type, id]); return failed(type, id); };
  sdk.publishArtifacts = async (list) => { sdkPublishes.push(...list.map((x) => [x.type, x.id])); return list.map((x) => failed(x.type, x.id)); };
  const ordinary = makeSpec({ charts: [] });
  ordinary.views[0].name = 'Plain sibling whose table publish is answered as failed';
  org.refuseNextTablePublish(500);
  const r = await build(org, dir, ordinary, { sdk, publicationBarrier: barrier });
  assert.equal(r.tablePublishes.length, 1, 'the routed publish went to PublishXml and was answered 500');
  assert.ok(r.err, 'the build stops');
  assert.deepEqual(sdkPublishes, [], 'a routed table is never published through the SDK, whose failure is a returned value');
  const pub = storeOf(org, dir).readGroup('view', VIEW_KEY).committed.publication;
  assert.equal(pub.state, 'unconfirmed', 'the returned failure is not taken for a success, nor for a request that never started');
  assert.ok(pub.carriedFrom);
});

// An answer the transport lost after the platform applied the request.
function loseNextAnswer(org, method, pattern) {
  const original = org.httpClient[method];
  let lost = false;
  org.httpClient[method] = async (url, ...rest) => {
    const response = await original(url, ...rest);
    if (!lost && pattern.test(url)) {
      lost = true;
      throw Object.assign(new Error('response lost after save'), { code: 'ECONNRESET' });
    }
    return response;
  };
  return () => { org.httpClient[method] = original; };
}

// A publish that paid a view's debt and whose answer was lost after the platform published it: before the next build changes
// that view, it confirms the earlier dispatch read-only with its own client (the published layer now holds the desired hash,
// which it did not before the send), never by sending it again.
test('a lost publish that paid a managed view its debt is confirmed read-only before the next change of that view, never sent again', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec({ charts: [] });
  const restore = loseNextAnswer(org, 'post', /\/PublishXml$/i);
  const first = await build(org, dir, spec);
  restore();
  assert.equal(first.tablePublishes.length, 1, 'the publish reached the platform');
  assert.ok(first.err, 'the run whose answer was lost stops');
  assert.equal(storeOf(org, dir).readGroup('view', VIEW_KEY).committed.publication.state, 'unconfirmed');

  const edit = structuredClone(spec);
  edit.views[0].description = 'A change after the lost publish';
  const mark = org.calls.length;
  const original = org.httpClient.patch;
  const atPatch = [];
  org.httpClient.patch = async (url, ...rest) => {
    if (/\/savedqueries\(/i.test(url)) atPatch.push({ state: storeOf(org, dir).readGroup('view', VIEW_KEY).committed.publication.state, publishes: org.tablePublishes(mark).length });
    return original(url, ...rest);
  };
  try { await build(org, dir, edit); } finally { org.httpClient.patch = original; }
  assert.equal(atPatch.length, 1, 'the change is made once the earlier dispatch is confirmed');
  assert.deepEqual(atPatch[0], { state: 'confirmed', publishes: 0 }, 'confirmed by a read before the PATCH; the earlier publish is not sent again');
});

// An edit of a row the build read first is never a create: its attempt records the row it read as its base observation, before
// the send, so no answer to it (nor a record that claims one) makes the row this workspace's own.
test('an edit records its base observation before the send; a lost edit of an adopted row is bound only with the adoption approval', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const spec = stableSpec({ charts: [] });
  assert.equal((await build(org, newDir(), spec)).err, undefined);
  const dir = newDir();
  const approval = new Set([`view:${VIEW_KEY}`]);
  const edited = structuredClone(spec);
  edited.views[0].description = 'An edit of a row this workspace did not make';
  const original = org.httpClient.patch;
  const seen = [];
  org.httpClient.patch = async (url, ...rest) => {
    if (/\/savedqueries\(/i.test(url)) seen.push(storeOf(org, dir).readGroup('view', VIEW_KEY).pending);
    return original(url, ...rest);
  };
  const restore = loseNextAnswer(org, 'patch', /\/savedqueries\(/i);
  const lost = await build(org, dir, edited, { authorizedAdoption: approval });
  restore();
  org.httpClient.patch = original;
  assert.ok(lost.err, 'the run whose answer was lost stops');
  assert.equal(seen.length, 1);
  assert.equal(seen[0].phase, 'sending');
  assert.ok(seen[0].expected.baseObservation, 'the edit records the row it read as its base');
  assert.match(String(seen[0].expected.baseObservation.savedSha256), /^[0-9a-f]{64}$/);
  const pending = storeOf(org, dir).readGroup('view', VIEW_KEY).pending;
  assert.ok(pending, 'the lost edit stays pending');
  assert.equal(pending.acknowledgedRoot, false);

  const refused = await build(org, dir, edited);
  assert.match(String(refused.err && refused.err.message), /managed-adoption-required/, 'an edit is never a create this workspace can bind without the approval');
  const bound = await build(org, dir, edited, { authorizedAdoption: approval });
  assert.equal(bound.err, undefined, bound.err && bound.err.message);
  assert.deepEqual(bound.writes.filter((c) => c.method === 'PATCH' && /^\/savedqueries\(/.test(c.path)), [], 'the lost edit is proved, not sent again');
  const g = storeOf(org, dir).readGroup('view', VIEW_KEY);
  assert.equal(g.pending, null);
  assert.equal(g.committed.records[0].origin, 'adopted');
});

// A record an older build wrote may claim an acknowledged create on an attempt whose base observation says it edited a row it
// read: the prover takes the store's rule (rootCreateAcknowledged), never the raw flag, so the row is adopted, not 'created'.
test('a pending edit whose record claims an acknowledged create is proved as an adoption, by the store rule rather than the raw flag', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const spec = stableSpec({ charts: [] });
  assert.equal((await build(org, newDir(), spec)).err, undefined);
  const dir = newDir();
  const approval = new Set([`view:${VIEW_KEY}`]);
  const edited = structuredClone(spec);
  edited.views[0].description = 'An edit an older build recorded as a create';
  const restore = loseNextAnswer(org, 'patch', /\/savedqueries\(/i);
  const lost = await build(org, dir, edited, { authorizedAdoption: approval });
  restore();
  assert.ok(lost.err);
  const file = path.join(dir, storeFiles(dir)[0]);
  const data = JSON.parse(fs.readFileSync(file, 'utf8'));
  const attempt = data.attempts.find((a) => a.family === 'view' && a.key === VIEW_KEY);
  assert.ok(attempt && attempt.expected.baseObservation, 'an edit attempt with a base observation');
  attempt.acknowledgedRoot = true;
  fs.writeFileSync(file, JSON.stringify(data, null, 2));
  assert.equal(storeOf(org, dir).readGroup('view', VIEW_KEY).pending.acknowledgedRoot, true, 'the store accepts the older record');

  const r = await build(org, dir, edited, { authorizedAdoption: approval });
  assert.equal(r.err, undefined, r.err && r.err.message);
  const g = storeOf(org, dir).readGroup('view', VIEW_KEY);
  assert.equal(g.pending, null);
  assert.equal(g.committed.records[0].origin, 'adopted', 'an edit is never recorded as this workspace\'s create');
});

// The intents' `declaration` is the catalog's key-part input (creates-catalog.js identityFor), not a store declaration: a view
// or chart group has no place for one (its consumer state is the baseline alone), so a plan never hands it to prepareAttempt and
// the store commits no originalDeclaration for either. Nothing in the build reads a group's declaration back.
test('a stable view and chart commit with their baseline alone: no declaration is handed to the store, none is committed', async () => {
  const { checkDeclaration, CODES } = require('../lib/managed-bindings.js');
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const r = await build(org, dir, stableSpec());
  assert.equal(r.err, undefined, r.err && r.err.message);
  const raw = JSON.parse(fs.readFileSync(path.join(dir, storeFiles(dir)[0]), 'utf8'));
  assert.equal(raw.groups.length, 2);
  for (const g of raw.groups) {
    assert.equal(g.originalDeclaration, undefined, `${g.family}: no original declaration`);
    assert.deepEqual(Object.keys(g.consumerState), ['baseline'], `${g.family}: its consumer state is the baseline alone`);
  }
  // The store's rule the build relies on: a view or chart declaration would be refused, so none is ever offered.
  for (const i of require('../lib/creates-stable-build.js').stableIntents(stableSpec())) {
    assert.throws(() => checkDeclaration(i.catalogKind, i.key, i.declaration), (e) => e.code === CODES.declarationRefused);
  }
});

test('a build without createIdentity cannot publish a table whose managed view has a draft its store did not prove', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const seed = await build(org, dir, stableSpec({ charts: [] }));
  assert.equal(seed.err, undefined, seed.err && seed.err.message);
  const id = derived(stableSpec()).view;
  await org.httpClient.patch(`${org.origin}/api/data/v9.0/savedqueries(${id})`, { description: 'A maker draft that must not be released with a sibling' });
  const next = makeSpec({ charts: [] });
  next.views[0].name = 'Unprotected sibling';
  const r = await build(org, dir, next);
  assert.match(String(r.err && r.err.message), /create-completion-required/);
  assert.deepEqual(r.tablePublishes, [], 'discovery routes the table through the coverage protocol whatever this spec declares');
  assert.ok(org.fake.unpublished().some((u) => u.id === id), 'the draft stays unpublished');
});

test('a failed solution membership keeps the attempt; the next run finishes the membership at the same id, then commits', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec({ charts: [] });
  const id = derived(stableSpec()).view;
  const stop = org.intercept((c) => c.method === 'POST' && /AddSolutionComponent/.test(c.path), () => ({ status: 503, headers: {}, body: { error: { message: 'membership not added' } } }));
  const first = await build(org, dir, spec);
  stop();
  assert.ok(first.err, 'the membership failed after the create');
  let g = storeOf(org, dir).readGroup('view', VIEW_KEY);
  assert.ok(g.pending, 'the attempt is kept');
  assert.equal(g.committed, null, 'no group is committed before the membership is proved');
  assert.deepEqual(first.tablePublishes, []);
  const second = await build(org, dir, spec);
  assert.equal(second.err, undefined, second.err && second.err.message);
  assert.equal(creates(second, 'savedqueries').length, 0, 'the row is not created again');
  const adds = second.writes.filter((c) => /AddSolutionComponent/.test(c.path));
  assert.equal(adds.length, 1);
  assert.equal(adds[0].body.ComponentId, id);
  g = storeOf(org, dir).readGroup('view', VIEW_KEY);
  assert.equal(g.pending, null);
  assert.equal(g.committed.root.id, id);
});

test('a receipt that cannot be written after an acknowledged create is an outcome to recover, never a refusal made before the send', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const barrier = createPublicationBarrier();
  const store = storeOf(org, dir, barrier);
  const broken = new Proxy(store, {
    get(target, key) {
      if (key === 'noteSaved') return () => { throw Object.assign(new Error('the receipt cannot be persisted'), { code: 'creation-record-unwritten' }); };
      return target[key];
    },
  });
  const r = await build(org, dir, stableSpec({ charts: [] }), { publicationBarrier: barrier, bindingGroups: { store: broken, debt: [], absent: false } });
  assert.equal(creates(r, 'savedqueries').length, 1);
  assert.equal(org.fake.rows('savedqueries').filter((x) => x.data.name === 'Declared Items').length, 1);
  assert.ok(r.err, 'the build stops');
  assert.doesNotMatch(r.err.message, /was not created|nothing was written|view-create-refused|view-write-refused/, 'an acknowledged create is not reported as refused');
  assert.ok(store.readGroup('view', VIEW_KEY).pending, 'the attempt is kept for the next run');
});

test("the build's inline --verify reads the bound rows by the same managed manifest as standalone verify", async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const seeded = await build(org, dir, stableSpec({ charts: [] }));
  assert.equal(seeded.err, undefined, seeded.err && seeded.err.message);
  const id = derived(stableSpec()).view;
  org.intercept((c) => c.method === 'GET' && c.path.startsWith(`/savedqueries(${id})`), () => ({ status: 404, headers: {}, body: { error: { code: '0x80040217', message: 'bound row no longer exists' } } }));
  const next = stableSpec({ views: [], charts: [] });
  const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: org.httpClient });
  await sdk.initWorkspace();
  // The bound row is gone: a read by its id finds nothing, whatever else the table holds.
  const read = {
    findTable: async () => ({ logicalName: 'contoso_item' }),
    findColumns: async () => [{ logicalName: 'contoso_code' }],
    queryRecords: async () => [],
    sitemapXml: async () => '<SiteMap><Area><Group><SubArea Entity="contoso_item"/></Group></Area></SiteMap>',
  };
  let given;
  let inline;
  const out = await buildModelApp(next, { apply: true, verify: true, env: org.origin, workspaceDir: dir, appDir: dir, retryDelayMs: 0 }, {
    sdk, provisionSdk: sdk, runBuild: viewsAndCharts, warn: () => {}, log: () => {},
    verify: async (s, opts) => { given = opts; inline = await verifySpec(s, read, opts); return inline; },
  });
  assert.ok(out, 'the build returned');
  assert.ok(given && given.managedManifest, 'the inline verify is handed the managed manifest');
  const standalone = await verifySpec(next, read, { managedManifest: readManagedManifest(workspaceArgs(org, dir)) });
  assert.equal(inline.ok, false, 'a missing bound row is not a pass');
  assert.deepEqual(inline.missing, standalone.missing, 'both entries report the same missing bound row');
  assert.ok(inline.missing.some((m) => m.kind === 'managed-view'));
});

test('a removed declaration keeps its pending hold; a build recovers it by its recorded expectation, with the adoption approval, never by omission', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const declared = stableSpec({ charts: [] });
  const restore = loseNextCreateAnswer(org, 'savedqueries');
  const first = await build(org, dir, declared);
  restore();
  assert.ok(storeOf(org, dir).readGroup('view', VIEW_KEY).pending, first.err && first.err.message);
  await removeKeptDraft(first, 'view', storeOf(org, dir).readGroup('view', VIEW_KEY).pending.root.id);
  const removed = stableSpec({ views: [], charts: [] });

  // A spec without the field: nothing recovers it, and its table stays held.
  const plainBarrier = createPublicationBarrier();
  const plain = await build(org, dir, makeSpec({ views: [], charts: [] }), { publicationBarrier: plainBarrier });
  assert.deepEqual(plain.writes, []);
  assert.equal(plainBarrier.allow('contoso_item', 'omitted'), false);
  assert.equal(readManagedManifest(workspaceArgs(org, dir)).pending.length, 1);

  // Proved by its recorded expectation, but no answer acknowledged the create: it is not bound without the approval.
  const warnings = [];
  const heldBarrier = createPublicationBarrier();
  const held = await build(org, dir, removed, { publicationBarrier: heldBarrier, warn: (m) => warnings.push(String(m)) });
  assert.deepEqual(held.writes, [], 'nothing is sent for an undeclared attempt');
  assert.deepEqual(held.tablePublishes, []);
  assert.equal(heldBarrier.allow('contoso_item', 'not approved'), false, 'its table stays withheld');
  assert.ok(storeOf(org, dir).readGroup('view', VIEW_KEY).pending, 'the attempt is kept');
  assert.ok(warnings.some((w) => /managed-adoption-required/.test(w) && /no longer declared/.test(w)), warnings.join('\n'));

  // With the approval it is proved, joins the solution, is committed, and its table published once.
  const barrier = createPublicationBarrier();
  const r = await build(org, dir, removed, { publicationBarrier: barrier, authorizedAdoption: new Set([`view:${VIEW_KEY}`]) });
  assert.equal(r.err, undefined, r.err && r.err.message);
  assert.equal(creates(r, 'savedqueries').length, 0, 'never sent again');
  assert.deepEqual(r.tablePublishes, [['contoso_item']]);
  const g = storeOf(org, dir).readGroup('view', VIEW_KEY);
  assert.equal(g.pending, null);
  assert.equal(g.committed.root.id, derived(stableSpec()).view);
  assert.equal(barrier.blocks('contoso_item'), false, 'the hold ends');
});

test('a removed declaration whose row no longer holds its recorded expectation stays pending and withheld, even with the approval', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const restore = loseNextCreateAnswer(org, 'savedqueries');
  const first = await build(org, dir, stableSpec({ charts: [] }));
  restore();
  const id = storeOf(org, dir).readGroup('view', VIEW_KEY).pending.root.id;
  await removeKeptDraft(first, 'view', id);
  await org.httpClient.patch(`${org.origin}/api/data/v9.2/savedqueries(${id})`, { description: 'Changed after the interruption' });
  const warnings = [];
  const barrier = createPublicationBarrier();
  const r = await build(org, dir, stableSpec({ views: [], charts: [] }), { publicationBarrier: barrier, authorizedAdoption: new Set([`view:${VIEW_KEY}`]), warn: (m) => warnings.push(String(m)) });
  assert.deepEqual(r.writes, []);
  assert.deepEqual(r.tablePublishes, []);
  assert.ok(storeOf(org, dir).readGroup('view', VIEW_KEY).pending);
  assert.equal(barrier.allow('contoso_item', 'mismatch'), false);
  assert.ok(warnings.some((w) => /does not hold what its recorded change expected/.test(w)), warnings.join('\n'));
});

test('the documented recovery keeps the store: no advice to move, delete or reset it', () => {
  const doc = fs.readFileSync(path.join(__dirname, '..', '..', 'references', 'app-spec-schema-advanced.md'), 'utf8');
  const start = doc.indexOf('## createIdentity');
  assert.ok(start >= 0);
  const next = doc.indexOf('\n## ', start + 3);
  const section = doc.slice(start, next < 0 ? undefined : next);
  assert.doesNotMatch(section, /\baside\b|delete (it|the (store|file))|reset (it|the store)|start (over|fresh)/i);
  // Restoring an older copy is not recovery: it can miss an attempt or a publish sent after it was made.
  assert.match(section, /never put back an older copy/i);
  assert.doesNotMatch(section, /put back the build's own copy/i);
  assert.match(section, /--authorize adopt:view:/);
  assert.match(section, /declaration/);
});

// --- the build's own unacknowledged local create ---------------------------------------------------------------------------------
const { readLocalCreateState } = require('../lib/creates-local.js');
const { execFileSync } = require('node:child_process');
const normalize = (g) => String(g || '').replace(/[{}]/g, '').toLowerCase();
const { withAppBuildLease } = require('../lib/app-operation-lease.js');
const { appUniqueName } = require('../lib/app-unique-name.js');

// Every file under `dir`, by relative path, to compare a workspace byte for byte.
function filesOf(dir) {
  const out = {};
  for (const name of fs.readdirSync(dir, { recursive: true })) {
    const file = path.join(dir, name);
    if (fs.statSync(file).isFile()) out[name] = fs.readFileSync(file, 'base64');
  }
  return out;
}

for (const kind of ['view', 'chart']) {
  const collection = kind === 'view' ? 'savedqueries' : 'savedqueryvisualizations';
  const key = kind === 'view' ? VIEW_KEY : CHART_KEY;
  const only = () => stableSpec(kind === 'view' ? { charts: [] } : { views: [] });

  // The tradeoff of never removing a local draft: a clean lost create no longer continues by itself. Its draft is kept, the next
  // build stops on it and writes nothing, and once the maker removes the draft (the SDK's own local discard) the build after
  // proves the row from the server and binds it — never creating it again.
  test(`a ${kind} create whose answer was lost keeps its local draft; the next build stops until the maker removes it, then proves the row`, async () => {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const barrier = createPublicationBarrier();
    const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier) });
    await sdk.initWorkspace();
    let discards = 0;
    const discard = sdk.deleteArtifact.bind(sdk);
    sdk.deleteArtifact = async (...args) => { discards++; return discard(...args); };
    const restore = loseNextCreateAnswer(org, collection);
    const first = await build(org, dir, only(), { sdk, publicationBarrier: barrier });
    restore();
    const pending = storeOf(org, dir).readGroup(kind, key).pending;
    assert.ok(pending, first.err && first.err.message);
    assert.match(String(first.err && first.err.message), /create-local-draft-kept/);
    assert.equal(discards, 0, 'the build never removes a local draft');
    assert.equal((await readLocalCreateState(sdk, kind, pending.root.id)).state, 'pending', 'the draft is kept');
    const stopped = await build(org, dir, only(), { authorizedAdoption: new Set([`${kind}:${key}`]) });
    assert.match(String(stopped.err && stopped.err.message), /create-completion-required/);
    assert.deepEqual(stopped.writes, [], 'nothing is written while the draft is there');
    // The maker settles the draft through the SDK's public discard.
    await discard(kind, pending.root.id);
    const r = await build(org, dir, only(), { authorizedAdoption: new Set([`${kind}:${key}`]) });
    assert.equal(r.err, undefined, r.err && r.err.message);
    assert.equal(creates(r, collection).length, 0, 'bound, not created again');
    assert.equal(storeOf(org, dir).readGroup(kind, key).pending, null);
  });

  for (const field of ['name', 'description']) {
    test(`a kept ${kind} local draft of a lost create, edited in its ${field} — outside what the build reconciles — stops every rerun with the edit and the files kept`, async () => {
      const org = createFakeBuildOrg({ entities: ENTITIES });
      const dir = newDir();
      const barrier = createPublicationBarrier();
      const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier) });
      await sdk.initWorkspace();
      const restore = loseNextCreateAnswer(org, collection);
      const first = await build(org, dir, only(), { sdk, publicationBarrier: barrier });
      restore();
      const pending = storeOf(org, dir).readGroup(kind, key).pending;
      assert.ok(pending, first.err && first.err.message);
      const id = pending.root.id;
      const label = 'A maker\'s local edit the declaration does not carry';
      await sdk.updateElement(kind, id, `/${field}`, label);
      assert.equal((await readLocalCreateState(sdk, kind, id)).state, 'pending');
      const before = filesOf(dir);
      for (let i = 0; i < 2; i++) {
        const next = await build(org, dir, only(), { authorizedAdoption: new Set([`${kind}:${key}`]) });
        assert.match(String(next.err && next.err.message), /create-completion-required/, `rerun ${i + 1}`);
        assert.deepEqual(next.writes, [], `rerun ${i + 1} writes nothing`);
        assert.equal((await next.sdk.getArtifact(kind, id))[field], label, `rerun ${i + 1} keeps the edit`);
        assert.deepEqual(filesOf(dir), before, `rerun ${i + 1} keeps every file as it was`);
      }
      assert.ok(storeOf(org, dir).readGroup(kind, key).pending, 'the attempt is kept');
    });
  }

  // A maker edits the local draft (here through a second SDK over the same workspace) while the build's create request is
  // outstanding, then the answer is lost. Being the same run, and the copy being a pending create, does not make the draft the
  // build's to remove: it now holds the maker's edit, so it is kept byte for byte and the build says so.
  test(`a ${kind} draft edited while its create answer is outstanding is kept with the edit, and the build says why`, async () => {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const id = derived(stableSpec())[kind];
    const edit = 'Maker edit during the outstanding create';
    const barrier = createPublicationBarrier();
    const observer = createAttemptSendObserver();
    const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier, observer) });
    await sdk.initWorkspace();
    const post = org.httpClient.post.bind(org.httpClient);
    let edited = false;
    let buildLeaseHeld = false;
    org.httpClient.post = async (url, ...args) => {
      const response = await post(url, ...args);
      if (!edited && new RegExp(`/${collection}(?:\\?|$)`).test(url)) {
        edited = true;
        // The edit is local authoring, not a second build: the build's lease is held throughout and refuses one.
        await assert.rejects(withAppBuildLease({ workspaceDir: dir, appUniqueName: appUniqueName(only()) }, () => assert.fail('another build must not enter')), { code: 'build-locked' });
        buildLeaseHeld = true;
        const editor = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: org.httpClient });
        await editor.initWorkspace();
        await editor.updateElement(kind, id, '/name', edit);
        const during = await readLocalCreateState(editor, kind, id);
        assert.equal(during.state, 'pending');
        assert.equal(during.draft.name, edit);
        throw Object.assign(new Error('response lost after the local edit was saved'), { code: 'ECONNRESET' });
      }
      return response;
    };
    const first = await build(org, dir, only(), { sdk, publicationBarrier: barrier, sendObserver: observer });
    org.httpClient.post = post;
    assert.equal(edited, true);
    assert.equal(buildLeaseHeld, true);
    const local = await readLocalCreateState(sdk, kind, id);
    assert.equal(local.state, 'pending', 'same-run membership does not prove the draft still holds only what the build made');
    assert.equal(local.draft.name, edit);
    assert.match(String(first.err && first.err.message), /create-local-draft-kept/);
    assert.deepEqual(first.writes.filter((c) => c.method === 'DELETE'), []);
    assert.ok(storeOf(org, dir).readGroup(kind, key).pending, 'the attempt is kept');
    const before = filesOf(dir);
    const next = await build(org, dir, only(), { authorizedAdoption: new Set([`${kind}:${key}`]) });
    assert.match(String(next.err && next.err.message), /create-completion-required/);
    assert.deepEqual(next.writes, []);
    assert.deepEqual(filesOf(dir), before, 'the next build keeps every file as it was');
  });

  // An edit that lands between the SDK writing the draft and the push: the workspace no longer holds the draft the build made, so
  // the create is not sent at all, and the draft is kept with the edit.
  test(`a ${kind} draft edited before its push is not sent and is kept with the edit`, async () => {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const id = derived(stableSpec())[kind];
    const edit = 'Maker edit before the push';
    const barrier = createPublicationBarrier();
    const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier) });
    await sdk.initWorkspace();
    const createArtifact = sdk.createArtifact.bind(sdk);
    sdk.createArtifact = async (...args) => {
      const art = await createArtifact(...args);
      const editor = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: org.httpClient });
      await editor.initWorkspace();
      await editor.updateElement(kind, id, '/name', edit);
      return art;
    };
    const first = await build(org, dir, only(), { sdk, publicationBarrier: barrier });
    assert.ok(first.err, 'the build stops');
    assert.equal(creates(first, collection).length, 0, 'a draft the build cannot show is its own is never sent');
    assert.match(String(first.err.message), /create-local-draft-changed[\s\S]*create-local-draft-kept/);
    const local = await readLocalCreateState(sdk, kind, id);
    assert.equal(local.state, 'pending');
    assert.equal(local.draft.name, edit);
  });

  // A second local process (another SDK on the same workspace, which the build's lease does not exclude) edits the draft at the
  // two points a check of the workspace cannot cover. Run as a real separate process, while the build holds its lease.
  const editElsewhere = async (dir, org, id, field, value) => {
    await assert.rejects(withAppBuildLease({ workspaceDir: dir, appUniqueName: appUniqueName(only()) }, () => assert.fail('another build must not enter')), { code: 'build-locked' });
    const env = { ...process.env };
    delete env.NODE_TEST_CONTEXT;
    const out = JSON.parse(execFileSync(process.execPath, [path.join(__dirname, 'helpers', 'edit-local-draft.js'), dir, org.origin, kind, id, field, value], { env, encoding: 'utf8', timeout: 60000 }));
    assert.equal(out.edited, true);
    assert.notEqual(out.pid, process.pid);
  };

  // Window 1: after the create's answer was lost, when a cleanup would compare the draft and then remove it. The build removes
  // no local draft at all, so there is no comparison an edit can slip past: the edit is kept, and so is the attempt.
  test(`a ${kind} draft another process edits after its create answer was lost is never removed`, async () => {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const id = derived(stableSpec())[kind];
    const edit = 'Maker edit after the lost answer';
    const barrier = createPublicationBarrier();
    const observer = createAttemptSendObserver();
    const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier, observer) });
    await sdk.initWorkspace();
    let discards = 0;
    sdk.deleteArtifact = async () => { discards++; throw new Error('the build must not remove a local draft'); };
    const post = org.httpClient.post.bind(org.httpClient);
    let edited = false;
    org.httpClient.post = async (url, ...args) => {
      const response = await post(url, ...args);
      if (!edited && new RegExp(`/${collection}(?:\\?|$)`).test(url)) {
        edited = true;
        await editElsewhere(dir, org, id, 'name', edit);
        throw Object.assign(new Error('response lost after the create was applied'), { code: 'ECONNRESET' });
      }
      return response;
    };
    const first = await build(org, dir, only(), { sdk, publicationBarrier: barrier, sendObserver: observer });
    org.httpClient.post = post;
    assert.equal(edited, true);
    assert.equal(discards, 0, 'no local discard is attempted');
    const local = await readLocalCreateState(sdk, kind, id);
    assert.equal(local.state, 'pending');
    assert.equal(local.draft.name, edit, 'the other process\'s edit is kept');
    assert.match(String(first.err && first.err.message), /create-local-draft-kept/);
    assert.ok(storeOf(org, dir).readGroup(kind, key).pending, 'the attempt is kept, not released');
    const next = await build(org, dir, only(), { authorizedAdoption: new Set([`${kind}:${key}`]) });
    assert.match(String(next.err && next.err.message), /create-completion-required/);
    assert.deepEqual(next.writes, [], 'nothing is sent again');
  });

  // Window 2: after the draft was made and checked, before the push reads it. The create request the push dispatches is
  // compared at the transport with the body the draft the build made serialises to, so the edited draft is refused unsent.
  for (const field of ['name', 'description']) {
    test(`a ${kind} draft another process edits in its ${field} just before the push is refused at the dispatch, unsent`, async () => {
      const org = createFakeBuildOrg({ entities: ENTITIES });
      const dir = newDir();
      const id = derived(stableSpec())[kind];
      const edit = `Maker ${field} just before the push`;
      const barrier = createPublicationBarrier();
      const observer = createAttemptSendObserver();
      const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier, observer) });
      await sdk.initWorkspace();
      const push = sdk.pushArtifact.bind(sdk);
      let edited = false;
      sdk.pushArtifact = async (type, artifactId, ...args) => {
        if (type === kind && normalize(artifactId) === normalize(id) && !edited) {
          edited = true;
          await editElsewhere(dir, org, id, field, edit);
        }
        return push(type, artifactId, ...args);
      };
      const first = await build(org, dir, only(), { sdk, publicationBarrier: barrier, sendObserver: observer });
      assert.equal(edited, true);
      assert.equal(creates(first, collection).length, 0, 'the edited draft never reaches the service');
      assert.equal(first.err && first.err.code, 'create-local-draft-changed', first.err && first.err.message);
      const local = await readLocalCreateState(sdk, kind, id);
      assert.equal(local.state, 'pending');
      assert.equal(local.draft[field], edit, 'the edit is kept');
      const pending = storeOf(org, dir).readGroup(kind, key).pending;
      assert.ok(pending, 'the attempt is kept');
      assert.equal(pending.sendWindow && pending.sendWindow.possible, 0, 'the refused create is never counted as possibly dispatched');
      const next = await build(org, dir, only());
      assert.match(String(next.err && next.err.message), /create-completion-required/);
      assert.deepEqual(next.writes, [], 'nothing is sent while the draft is there');
    });
  }

  // The native SDK pushes a draft as a PATCH once it has been saved: a second SDK on the same workspace (which the build's lease
  // does not exclude) saves the draft and edits it between the build's check and its push. The pinned row's whole write boundary
  // is closed, so the PATCH is refused unsent and the create is never acknowledged as an update of the maker's edit.
  test(`a ${kind} draft another SDK saves and edits before the push is never sent as a PATCH`, async () => {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const id = derived(stableSpec())[kind];
    const barrier = createPublicationBarrier();
    const observer = createAttemptSendObserver();
    const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier, observer) });
    await sdk.initWorkspace();
    const transmitted = [];
    const patch = org.httpClient.patch.bind(org.httpClient);
    org.httpClient.patch = async (...args) => { transmitted.push(args); return patch(...args); };
    const push = sdk.pushArtifact.bind(sdk);
    let saved = false;
    sdk.pushArtifact = async (type, artifactId, ...args) => {
      if (type === kind && normalize(artifactId) === normalize(id) && !saved) {
        await assert.rejects(withAppBuildLease({ workspaceDir: dir, appUniqueName: appUniqueName(only()) }, () => assert.fail('another build must not enter')), { code: 'build-locked' });
        const editor = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: org.httpClient });
        await editor.initWorkspace();
        assert.equal((await editor.pushArtifact(kind, id)).saved, true);
        await editor.updateElement(kind, id, '/name', 'Maker edit after an independent save');
        saved = true;
      }
      return push(type, artifactId, ...args);
    };
    const r = await build(org, dir, only(), { sdk, publicationBarrier: barrier, sendObserver: observer });
    org.httpClient.patch = patch;
    assert.equal(saved, true);
    assert.deepEqual(transmitted, [], 'the edited draft never reaches the service as an update');
    assert.match(String(r.err && r.err.message), /create-local-draft-changed/);
    const pending = storeOf(org, dir).readGroup(kind, key).pending;
    assert.ok(pending, 'the attempt is kept');
    assert.equal(pending.sendWindow && pending.sendWindow.possible, 0, 'the refused PATCH is never counted as dispatched');
  });

  // The recovery order the build gives for a kept draft. Its change is proved only against the declaration it was prepared from,
  // so the maker's change goes in AFTER the original has settled: putting it into the spec before the discard is refused (and
  // keeps the attempt); keeping the original, discarding the local copy, building, then applying the change works.
  test(`a kept ${kind} draft settles in the order the build gives: original declaration, local discard, build, then the change`, async () => {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const id = derived(stableSpec())[kind];
    const spec = only();
    const edit = 'A local description the maker wants to keep';
    const barrier = createPublicationBarrier();
    const observer = createAttemptSendObserver();
    const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier, observer) });
    await sdk.initWorkspace();
    const push = sdk.pushArtifact.bind(sdk);
    let edited = false;
    sdk.pushArtifact = async (type, artifactId, ...args) => {
      if (type === kind && normalize(artifactId) === normalize(id) && !edited) {
        edited = true;
        const editor = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: org.httpClient });
        await editor.initWorkspace();
        await editor.updateElement(kind, id, '/description', edit);
      }
      return push(type, artifactId, ...args);
    };
    const first = await build(org, dir, spec, { sdk, publicationBarrier: barrier, sendObserver: observer });
    assert.match(String(first.err && first.err.message), /create-local-draft-changed/);
    const said = chainText(first.err);
    assert.match(said, /create-local-draft-kept/);
    assert.match(said, /1\. keep the change you want[\s\S]*2\. keep the spec's declaration[\s\S]*3\. discard only the local copy[\s\S]*deleteArtifact[\s\S]*removes nothing on the service and leaves the managed binding store as it is[\s\S]*4\. run the build again with that original declaration[\s\S]*5\. only then put your change into the spec/);
    const token = storeOf(org, dir).readGroup(kind, key).pending.token;
    const changed = structuredClone(spec);
    changed[kind === 'view' ? 'views' : 'charts'][0].description = edit;
    // While the draft is there, the stop gives the same order.
    const stopped = await build(org, dir, spec, { sendObserver: createAttemptSendObserver() });
    assert.deepEqual(stopped.writes, []);
    assert.match(chainText(stopped.err), /create-completion-required[\s\S]*1\. keep the change you want/);
    await sdk.deleteArtifact(kind, id);
    // The change put into the spec before the original settled: refused, nothing written, the same attempt kept.
    const early = await build(org, dir, changed, { sendObserver: createAttemptSendObserver() });
    assert.match(chainText(early.err), /declared differently since its change was prepared/);
    assert.deepEqual(early.writes, []);
    assert.equal(storeOf(org, dir).readGroup(kind, key).pending.token, token);
    // The order the build gives: the original declaration first, then the change.
    const original = await build(org, dir, spec, { sendObserver: createAttemptSendObserver() });
    assert.equal(original.err, undefined, original.err && chainText(original.err));
    assert.equal(storeOf(org, dir).readGroup(kind, key).pending, null);
    const then = await build(org, dir, changed, { sendObserver: createAttemptSendObserver() });
    assert.equal(then.err, undefined, then.err && chainText(then.err));
    assert.ok(then.writes.some((c) => c.method === 'PATCH' && c.path.startsWith(`/${collection}(`) && c.body && c.body.description === edit), 'the change is applied after the original settled');
    assert.equal(storeOf(org, dir).readGroup(kind, key).pending, null);
  });
}

test('a pinned create passes only the body it was pinned to; anything else that could create the row is refused unsent', async () => {
  const barrier = createPublicationBarrier();
  assert.equal(barrier.gatesTransport(), false);
  const sent = [];
  const gate = barrier.gateRequest(async (req) => { sent.push(req); return { statusCode: 204, headers: {}, body: '' }; });
  assert.equal(barrier.gatesTransport(), true);
  const id = 'a1d1e2a3-8ac8-5f10-8300-6ddd477124af';
  const url = 'https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries?$select=savedqueryid';
  const body = { name: 'Declared Items', fetchxml: '<fetch/>', savedqueryid: id };
  const send = (req) => gate({ headers: {}, ...req });
  const code = (answer) => JSON.parse(answer.body).error.code;
  const pinned = () => barrier.pinCreate({ collection: 'savedqueries', idField: 'savedqueryid', id: id.toUpperCase(), matches: (b) => JSON.stringify(b) === JSON.stringify(body) });
  // Before any pin, and after its release, every request goes as it came.
  await send({ method: 'POST', url, body: JSON.stringify({ ...body, name: 'Other' }) });
  assert.equal(sent.length, 1);
  let pin = pinned();
  await send({ method: 'POST', url, body: JSON.stringify(body) });
  assert.equal(sent.length, 2);
  assert.equal(pin.checked, true);
  await send({ method: 'POST', url, body: JSON.stringify({ name: 'Another row', savedqueryid: '{0B0B0B0B-0000-0000-0000-000000000001}' }) });
  await send({ method: 'GET', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries(${id})` });
  assert.equal(sent.length, 4, 'another row\'s create and a read are not the pinned create');
  assert.equal(pin.refused, false);
  const refusals = [
    { method: 'POST', url, body: JSON.stringify({ ...body, name: 'Edited' }) },
    { method: 'POST', url: 'https://contoso.crm.dynamics.com/api/data/v9.2/savedquer%69es', body: JSON.stringify({ ...body, savedqueryid: `{${id.toUpperCase()}}`, description: 'x' }) },
    { method: 'POST', url, body: '{not json' },
    { method: 'POST', url, body: JSON.stringify({ name: 'No id' }) },
    { method: 'POST', url: 'https://contoso.crm.dynamics.com/api/data/v9.2/$batch', body: `--b\r\nPOST savedqueries HTTP/1.1\r\n\r\n{"savedqueryid":"${id}"}\r\n--b--` },
  ];
  for (const req of refusals) {
    const answer = await send(req);
    assert.equal(answer.statusCode, 409, req.body);
    assert.equal(code(answer), 'create-local-draft-changed');
  }
  assert.equal(sent.length, 4, 'nothing refused reached the transport');
  assert.equal(pin.refused, true);
  pin.release();
  await send({ method: 'POST', url, body: JSON.stringify({ ...body, name: 'Edited' }) });
  assert.equal(sent.length, 5, 'a released pin checks nothing');
  pin = pinned();
  // A batch is read part by part, the way the service reads it (its Content-Type's boundary, change sets opened).
  const batch = (parts) => ({
    method: 'POST',
    url: 'https://contoso.crm.dynamics.com/api/data/v9.2/$batch',
    headers: { 'Content-Type': 'multipart/mixed; boundary=batch_p' },
    body: ['--batch_p', 'Content-Type: multipart/mixed; boundary=changeset_p', '', ...parts.flatMap((p, i) => ['--changeset_p', 'Content-Type: application/http', 'Content-Transfer-Encoding: binary', `Content-ID: ${i + 1}`, '', p]), '--changeset_p--', '--batch_p--', ''].join('\r\n'),
  });
  const otherRow = '0b0b0b0b-0000-0000-0000-000000000002';
  await send(batch([`POST /api/data/v9.2/savedqueries HTTP/1.1\r\nContent-Type: application/json\r\n\r\n{"savedqueryid":"${otherRow}","name":"Other"}`, `PATCH /api/data/v9.2/savedqueries(${otherRow}) HTTP/1.1\r\nContent-Type: application/json\r\n\r\n{"name":"Other again"}`]));
  await send({ method: 'PATCH', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries(${otherRow})`, body: JSON.stringify({ name: 'Another row' }) });
  await send({ method: 'POST', url: 'https://contoso.crm.dynamics.com/api/data/v9.2/PublishXml', body: JSON.stringify({ ParameterXml: '<importexportxml><entities><entity>contoso_item</entity></entities></importexportxml>' }) });
  await send({ method: 'HEAD', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries(${id})` });
  assert.equal(sent.length, 9, 'a change set of another row, a write of another row, a publish and a read pass');
  assert.equal(pin.refused, false);
  const escaped = JSON.stringify({ ...body, name: 'Not declared' }).replace(id, `\\u0061${id.slice(1)}`);
  assert.equal(escaped.includes(id), false);
  const rowWrites = [
    // The native SDK's push of a draft that another process has saved: an update of the pinned row.
    { method: 'PATCH', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries(${id})?$select=savedqueryid`, body: JSON.stringify({ name: 'Edited' }) },
    { method: 'PUT', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries(${id.toUpperCase()})/name`, body: JSON.stringify({ value: 'Edited' }) },
    { method: 'DELETE', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries({${id}})` },
    { method: 'PATCH', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries(savedqueryid=${id})`, body: JSON.stringify({ name: 'Edited' }) },
    { method: 'PATCH', url: 'https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries(name=\'Declared Items\')', body: JSON.stringify({ name: 'Edited' }) },
    { method: 'PATCH', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedquer%69es(${id})`, body: JSON.stringify({ name: 'Edited' }) },
    { method: 'POST', url: 'https://contoso.crm.dynamics.com/api/data/v9.2/AddSolutionComponent', body: JSON.stringify({ ComponentId: id.replace(/-/g, '').toUpperCase() }) },
    // A change set whose create names the row only through a JSON escape, a PATCH of the row inside a change set, the exact
    // create inside a change set, and a body that cannot be read as a batch.
    batch([`POST /api/data/v9.2/savedqueries HTTP/1.1\r\nContent-Type: application/json\r\n\r\n${escaped}`]),
    batch([`PATCH savedqueries(${id}) HTTP/1.1\r\nContent-Type: application/json\r\n\r\n{"name":"Edited"}`]),
    batch([`POST savedqueries HTTP/1.1\r\nContent-Type: application/json\r\n\r\n${JSON.stringify(body)}`]),
    { method: 'POST', url: 'https://contoso.crm.dynamics.com/api/data/v9.2/$batch', headers: { 'content-type': 'multipart/mixed; boundary=batch_p' }, body: 'not a batch' },
  ];
  for (const req of rowWrites) {
    const answer = await send(req);
    assert.equal(answer.statusCode, 409, `${req.method} ${req.url}`);
    assert.equal(code(answer), 'create-local-draft-changed');
  }
  assert.equal(sent.length, 9, 'no write of the pinned row reached the transport');
  pin.release();
  // Each pin is its own: releasing one leaves another pin of another row holding.
  const a = barrier.pinCreate({ collection: 'savedqueries', idField: 'savedqueryid', id, matches: () => false });
  const b = barrier.pinCreate({ collection: 'savedqueries', idField: 'savedqueryid', id: otherRow, matches: () => false });
  a.release();
  assert.equal((await send({ method: 'PATCH', url: `https://contoso.crm.dynamics.com/api/data/v9.2/savedqueries(${otherRow})`, body: '{}' })).statusCode, 409);
  assert.equal(b.refused, true);
  assert.equal(a.refused, false);
  b.release();
  assert.throws(() => barrier.pinCreate({ collection: 'savedqueries', idField: 'savedqueryid', id: '', matches: () => true }), TypeError);
});

// The pin reads a batch by the boundary its Content-Type's boundary PARAMETER declares, at the batch and at each change set: a
// `boundary=` inside another parameter's quoted value cannot point the reading at a decoy multipart in the preamble while the
// parts the service runs write the pinned row. Ordinary quoted boundaries and other rows' batches still pass.
test('a pinned row cannot be hidden from the pin by a boundary quoted inside another Content-Type parameter', async () => {
  const id = 'a1d1e2a3-8ac8-5f10-8300-6ddd477124af';
  const other = '0b0b0b0b-0000-0000-0000-000000000002';
  const part = (method, target, body = '{}') => ['Content-Type: application/http', 'Content-Transfer-Encoding: binary', 'Content-ID: 1', '', `${method} ${target} HTTP/1.1`, 'Content-Type: application/json', '', body].join('\r\n');
  const multipart = (boundary, parts) => parts.map((p) => `--${boundary}\r\n${p}\r\n`).join('') + `--${boundary}--\r\n`;
  const misleading = 'multipart/mixed; note="; boundary=decoy"; boundary=actual';
  const hidden = multipart('decoy', [part('PATCH', `savedqueries(${other})`)]) + multipart('actual', [part('PATCH', `savedqueries(${id})`, '{"name":"Changed"}')]);
  const screen = async (contentType, body) => {
    const barrier = createPublicationBarrier();
    const sent = [];
    const gate = barrier.gateRequest(async (req) => { sent.push(req); return { statusCode: 204, headers: {}, body: '' }; });
    const pin = barrier.pinCreate({ collection: 'savedqueries', idField: 'savedqueryid', id, matches: () => false });
    const answer = await gate({ method: 'POST', url: 'https://contoso.crm.dynamics.com/api/data/v9.2/$batch', headers: { 'content-type': contentType }, body });
    pin.release();
    return { answer, sent: sent.length, refused: pin.refused };
  };
  const refusedUnsent = (r, label) => {
    assert.equal(r.answer.statusCode, 409, label);
    assert.equal(JSON.parse(r.answer.body).error.code, 'create-local-draft-changed', label);
    assert.equal(r.sent, 0, label);
    assert.equal(r.refused, true, label);
  };
  refusedUnsent(await screen(misleading, hidden), 'the batch Content-Type');
  refusedUnsent(await screen('multipart/mixed; boundary=outer', multipart('outer', [`Content-Type: ${misleading}\r\n\r\n${hidden}`])), 'a change set Content-Type');
  refusedUnsent(await screen('multipart/mixed; boundary=a; boundary=b', multipart('a', [part('PATCH', `savedqueries(${other})`)])), 'two boundaries');
  // Controls: the same quoted parameter with only another row in the parts the service runs, and an ordinary quoted change set.
  const otherOnly = multipart('decoy', [part('PATCH', `savedqueries(${id})`)]) + multipart('actual', [part('PATCH', `savedqueries(${other})`)]);
  assert.equal((await screen(misleading, otherOnly)).sent, 1, 'a decoy in the preamble is not run, so naming the row there writes nothing');
  const changeSet = `Content-Type: multipart/mixed; boundary="changeset_q"\r\n\r\n${multipart('changeset_q', [part('PATCH', `savedqueries(${other})`)])}`;
  assert.equal((await screen('multipart/mixed; boundary="batch_q"', multipart('batch_q', [changeSet]))).sent, 1);
});

// Production runs creates in parallel (the engine's concurrency; charts through mapLimit). Each create holds its own pin until
// its push ends, so one create finishing never releases another's pin while that one is still to push: a second local process
// that edits the second chart then is refused at the dispatch.
test('one parallel create finishing never releases another create\'s pin', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = stableSpec({ views: [] });
  spec.charts.push({ ...spec.charts[0], name: 'Other Kind' });
  const firstId = derived(stableSpec()).chart;
  const barrier = createPublicationBarrier();
  const observer = createAttemptSendObserver();
  const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(dir), instanceUrl: org.origin, httpClient: gatedClient(org, barrier, observer) });
  await sdk.initWorkspace();
  let ready;
  let released;
  const secondReady = new Promise((resolve) => { ready = resolve; });
  const firstReleased = new Promise((resolve) => { released = resolve; });
  const pinCreate = barrier.pinCreate.bind(barrier);
  const releases = [];
  barrier.pinCreate = (args) => {
    const pin = pinCreate(args);
    const release = pin.release;
    pin.release = () => { releases.push(pin.id); release(); if (pin.id === firstId) released(); };
    return pin;
  };
  const push = sdk.pushArtifact.bind(sdk);
  let secondId;
  let releasedEarly;
  sdk.pushArtifact = async (kind, id, ...args) => {
    if (kind === 'chart' && normalize(id) === normalize(firstId)) await secondReady;
    else if (kind === 'chart') {
      secondId = normalize(id);
      ready();
      await firstReleased;
      releasedEarly = releases.includes(secondId);
      const env = { ...process.env };
      delete env.NODE_TEST_CONTEXT;
      const out = JSON.parse(execFileSync(process.execPath, [path.join(__dirname, 'helpers', 'edit-local-draft.js'), dir, org.origin, 'chart', id, 'name', 'Concurrent maker edit'], { env, encoding: 'utf8', timeout: 60000 }));
      assert.equal(out.edited, true);
    }
    return push(kind, id, ...args);
  };
  const r = await build(org, dir, spec, { sdk, publicationBarrier: barrier, sendObserver: observer, concurrency: 2 });
  assert.ok(secondId, 'both charts were pushed');
  assert.equal(releasedEarly, false, 'the first create\'s end left the second create\'s pin in place');
  assert.deepEqual(r.writes.filter((c) => c.method === 'POST' && c.body && normalize(c.body.savedqueryvisualizationid) === secondId), [], 'the edited second chart is never sent');
  assert.match(String(r.err && r.err.message), /create-local-draft-changed/);
  assert.ok(storeOf(org, dir).readGroup('chart', 'contoso_item/Other Kind').pending, 'its attempt is kept');
  assert.deepEqual(new Set(releases), new Set([normalize(firstId), secondId]), 'each pin was released by its own create');
});

// The commit of a view's change repeats its key, which the spec controls without a length limit, so the bytes the store reserves
// for it are worked out from the key (planFor's completionBytes): a long key gets a reservation its commit fits in, and a key whose
// commit could outgrow what one change may reserve is refused before anything is prepared or sent.
test('a long view key reserves the room its commit takes; a key too long to record is refused before the send', async () => {
  for (const [label, k] of [['plain', 'k'.repeat(384 * 1024)], ['escaped', '"'.repeat(200 * 1024)]]) {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const spec = stableSpec({ charts: [] });
    spec.views[0].key = k;
    let before = null;
    const post = org.httpClient.post.bind(org.httpClient);
    org.httpClient.post = async (url, ...args) => {
      const answer = await post(url, ...args);
      if (/\/AddSolutionComponent(?:\?|$)/i.test(url)) {
        const store = storeOf(org, dir);
        const pending = store.readGroup('view', k).pending;
        if (pending) before = { size: fs.statSync(store.file).size, ...pending.completionReserve };
      }
      return answer;
    };
    const r = await build(org, dir, spec, { sendObserver: createAttemptSendObserver() });
    assert.equal(r.err, undefined, r.err && r.err.message.slice(0, 400));
    assert.ok(before, label);
    const store = storeOf(org, dir);
    assert.ok(store.readGroup('view', k).committed, label);
    const growth = fs.statSync(store.file).size - before.size;
    assert.ok(growth <= before.bytes - before.used, `${label}: the commit grew the store by ${growth}, within the ${before.bytes - before.used} its attempt held`);
  }
  // Raw, this key is half of what its escaped copies take; each of its quotes is written as two bytes in the store.
  for (const k of ['k'.repeat(1536 * 1024), '"'.repeat(768 * 1024)]) {
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const dir = newDir();
    const spec = stableSpec({ charts: [] });
    spec.views[0].key = k;
    const r = await build(org, dir, spec, { sendObserver: createAttemptSendObserver() });
    assert.match(String(r.err && chainText(r.err)), /managed-binding-store-full[\s\S]*too long to record[\s\S]*nothing was sent/);
    assert.deepEqual(r.writes, [], 'nothing is sent');
    const g = storeOf(org, dir).readGroup('view', k);
    assert.equal(g.pending, null, 'no attempt is prepared');
    assert.equal(g.committed, null);
  }
});

// --- standalone discovery of a build whose spec leaves the app's unique name to be derived --------------------------------------
test('standalone discovery finds the store of a build whose spec omits app.uniqueName, by the name the build derived', async () => {
  const { openStandaloneManagedFence } = require('../lib/managed-standalone-fence.js');
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const appDir = newDir();
  const workspaceDir = path.join(appDir, '.maker-workspace');
  fs.mkdirSync(workspaceDir);
  const spec = stableSpec({ charts: [] });
  delete spec.app.uniqueName;
  assert.equal(validateAppSpec(spec).ok, true);
  fs.writeFileSync(path.join(appDir, 'app-spec.json'), JSON.stringify(spec));
  const built = await build(org, workspaceDir, spec, { appDir });
  assert.equal(built.err, undefined, built.err && built.err.message);
  assert.equal(storeOf(org, workspaceDir).readGroup('view', VIEW_KEY).committed.publication.state, 'confirmed');
  for (const where of [{ appDir }, { workspaceDir, spec }]) {
    const found = openStandaloneManagedFence({ environment: org.origin, ...where });
    assert.equal(found.absent, false, JSON.stringify(Object.keys(where)));
  }
});

// A workspace path that names a file, or lies under one, cannot be a folder, so it holds no store. Windows answers ENOENT for a
// path under a file and POSIX answers ENOTDIR; a path that IS a file answers ENOTDIR on both. Each means "no workspace there", as a
// missing folder does, so discovery finds no debt and the caller's own folder check (lib/safe-fs.js) is what refuses the write.
test('standalone discovery reads a workspace path that is a file, or lies under one, as holding no store on every platform', () => {
  const { openStandaloneManagedFence } = require('../lib/managed-standalone-fence.js');
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const file = path.join(newDir(), 'app-folder.txt');
  fs.writeFileSync(file, 'a file, not a folder\n');
  for (const workspaceDir of [file, path.join(file, '.maker-workspace')]) {
    const found = openStandaloneManagedFence({ environment: org.origin, workspaceDir });
    assert.deepEqual([found.absent, found.debt], [true, []], workspaceDir);
  }
});

// --- #845: two declared views of one name on one table, each under its own key ------------------------------------------------------

// A reader over the fake org's saved queries that applies the filter verify sends, as the service does: by the row's id, or by its
// table and name (every row of that name in the order the org holds them: all of them for a paginated read, else up to `top`).
const savedQueryReader = (org, reads = []) => ({
  findTable: async () => ({ logicalName: 'contoso_item' }),
  findColumns: async () => [{ logicalName: 'contoso_code' }],
  sitemapXml: async () => '<SiteMap><Area><Group><SubArea Entity="contoso_item"/></Group></Area></SiteMap>',
  queryRecords: async (set, q) => {
    if (set !== 'savedquery') return [];
    reads.push(q.filter);
    const all = org.fake.rows('savedqueries').map((r) => r.data);
    const byId = /^savedqueryid eq ([0-9a-f-]{36})$/.exec(q.filter);
    if (byId) return all.filter((r) => r.savedqueryid === byId[1]);
    const byName = /^returnedtypecode eq '([^']*)' and name eq '([^']*)'$/.exec(q.filter);
    const named = byName ? all.filter((r) => r.returnedtypecode === byName[1] && r.name === byName[2]) : [];
    return q.paginate ? named : named.slice(0, q.top || 50);
  },
});
const twin = (key, columns) => ({ entity: 'contoso_item', name: 'Twin', key, reconcile: 'declared', columns });
const twinSpec = (views) => stableSpec({ minimumPluginVersion: '2.21.0', views, charts: [] });
const viewChecks = (r) => r.checks.filter((c) => c.kind === 'view' || c.kind === 'view-columns').map((c) => [c.kind, c.present, c.detail]);

test('#845: two same-named declared views under their own keys are both recorded as created, and each is verified against its own row', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = twinSpec([twin('v1', ['contoso_name']), twin('v2', ['contoso_name', 'contoso_code'])]);
  assert.deepEqual(validateAppSpec(spec).errors, []);
  const built = await build(org, dir, spec);
  assert.equal(built.err, undefined, built.err && built.err.message);
  const rows = org.fake.rows('savedqueries').map((r) => r.data).filter((r) => r.name === 'Twin');
  assert.equal(rows.length, 2, 'the build made a row for each key');
  const s = createStableCreates({ spec, sdk: null, store: null, basePort: createPassthroughCreates() });
  const [id1, id2] = ['v1', 'v2'].map((k) => s.identityFor('view', k).id);
  assert.deepEqual(new Set(rows.map((r) => r.savedqueryid)), new Set([id1, id2]));
  // The record of created views holds both: the `entity|name` entry as before (the last of the pair), the other in its own list.
  assert.deepEqual(built.out.created.views, { 'contoso_item|Twin': id2 });
  assert.deepEqual(built.out.created.sameNamedViews, [{ entity: 'contoso_item', name: 'Twin', key: 'v1', id: id1 }]);
  assert.deepEqual(appComponentsFor(built.out.created).views, [id2, id1]);
  // The table is published once, as for one view.
  assert.equal(built.tablePublishes.filter((names) => names.includes('contoso_item')).length, 1);

  // Verify with the bindings of this workspace: each view is read by its bound id and held to its own columns.
  const managedManifest = readManagedManifest(workspaceArgs(org, dir));
  const reads = [];
  const r = await verifySpec(spec, savedQueryReader(org, reads), { managedManifest });
  assert.equal(r.ok, true, JSON.stringify(r.missing));
  assert.deepEqual(viewChecks(r), [['view', true, ''], ['view-columns', true, ''], ['view', true, ''], ['view-columns', true, '']]);
  assert.deepEqual(reads.sort(), [`savedqueryid eq ${id1}`, `savedqueryid eq ${id1}`, `savedqueryid eq ${id2}`, `savedqueryid eq ${id2}`].sort(), 'each view read by its bound id (and its managed check)');
  // A declaration that is not what its own row holds fails for that view only, named by its key.
  const drifted = await verifySpec(twinSpec([twin('v1', ['contoso_name']), twin('v2', ['contoso_name', 'contoso_kind'])]), savedQueryReader(org), { managedManifest });
  assert.deepEqual(drifted.missing.map((c) => [c.kind, c.name, c.detail]), [['view-columns', 'contoso_item.Twin', 'missing column(s): contoso_kind (view key v2)']]);

  // A downloaded spec pins each view by its viewId: read by that id without the bindings too.
  const pinned = twinSpec([{ ...twin('v1', ['contoso_name']), viewId: id1 }, { ...twin('v2', ['contoso_name', 'contoso_code']), viewId: id2 }]);
  const byPin = await verifySpec(pinned, savedQueryReader(org));
  assert.equal(byPin.ok, true, JSON.stringify(byPin.missing));
  assert.equal(byPin.checks.filter((c) => c.kind === 'view-columns' && c.present).length, 2);

  // Neither binding nor pin: under createIdentity each view's own row is the one at the id the build creates it at, read by that id
  // alone, so each is held to its own columns, as a bound view is, and no row of the name is read for either.
  const unboundReads = [];
  const unbound = await verifySpec(spec, savedQueryReader(org, unboundReads));
  assert.equal(unbound.ok, true, JSON.stringify(unbound.missing));
  assert.deepEqual(viewChecks(unbound), [['view', true, ''], ['view-columns', true, ''], ['view', true, ''], ['view-columns', true, '']]);
  assert.deepEqual((unbound.unverified || []).filter((u) => u.startsWith('view')), []);
  assert.deepEqual(unboundReads, [`savedqueryid eq ${id1}`, `savedqueryid eq ${id2}`]);
});

test('#845: under createIdentity a twin is read at its stable id alone: a row there is its own, an empty answer leaves it to the rows nobody owns, any other answer fails it', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = twinSpec([twin('v1', ['contoso_name']), twin('v2', ['contoso_name', 'contoso_code'])]);
  const built = await build(org, dir, spec);
  assert.equal(built.err, undefined, built.err && built.err.message);
  const s = createStableCreates({ spec, sdk: null, store: null, basePort: createPassthroughCreates() });
  const [id1, id2] = ['v1', 'v2'].map((k) => s.identityFor('view', k).id);
  // Verify derives the very ids the build creates at, from the spec alone.
  assert.deepEqual(spec.views.map((v) => stableViewIdentityFor(spec, v)), [{ key: 'v1', id: id1 }, { key: 'v2', id: id2 }]);
  const { createIdentity, ...legacy } = spec;
  void createIdentity;
  assert.equal(stableViewIdentityFor(legacy, spec.views[0]), null, 'no stable id without createIdentity');
  const reader = savedQueryReader(org);
  const answering = (answer) => ({ ...reader, queryRecords: async (set, q) => (q.filter === `savedqueryid eq ${id2}` ? answer() : reader.queryRecords(set, q)) });
  const at = (detail) => [['view', true, ''], ['view-columns', true, ''], ['view', false, `the view at its stable id ${id2} could not be read: ${detail}; a view of the same name is not taken in its place (view key v2)`]];
  const listOf = (rows, more) => Object.defineProperty(rows, 'hasMore', { value: more, enumerable: false });
  for (const [answer, detail] of [
    [() => { throw new Error('service unavailable'); }, 'service unavailable'],
    [() => null, 'the read did not return a list of rows'],
    [() => listOf([], true), 'the read answered no row but reported more'],
    [() => ['row'], 'the read did not return a view row'],
    [() => [{ savedqueryid: id1, returnedtypecode: 'contoso_item' }], `the read answered another row (${id1})`],
    [() => [{ savedqueryid: id2 }], 'the row was read without its table'],
    [() => [{ savedqueryid: id2.toUpperCase(), returnedtypecode: 'contoso_other' }], "the row belongs to table 'contoso_other', not 'contoso_item'"],
  ]) {
    const r = await verifySpec(spec, answering(answer));
    assert.equal(r.ok, false, detail);
    assert.deepEqual(viewChecks(r), at(detail), detail);
  }
  // The row v2 holds was made before its stable id (here: moved off it): a complete empty answer there leaves v2 to a row of its
  // name that nobody owns — never v1's, which is v1's own at its stable id. It is present only, its content unverified.
  const row2 = org.fake.rows('savedqueries').find((r) => r.data.savedqueryid === id2);
  const MOVED = 'bbbbbbbb-0000-4000-8000-0000000000b2';
  row2.data.savedqueryid = MOVED;
  const reads = [];
  const moved = await verifySpec(spec, savedQueryReader(org, reads));
  assert.equal(moved.ok, true, JSON.stringify(moved.missing));
  assert.deepEqual(moved.checks.filter((c) => c.kind === 'view' || c.kind === 'view-columns').map((c) => [c.kind, c.present, c.note || '']), [
    ['view', true, ''], ['view-columns', true, ''],
    ['view', true, 'present only: a distinct row is available, but view key "v2" is not attributed to a row'],
  ]);
  assert.deepEqual(moved.unverified.filter((u) => u.startsWith('view')), ['view-content:contoso_item.Twin (view key "v2": no managed binding, viewId or observed stable id identifies its row; content is not proven for this key)']);
  assert.deepEqual(reads, [`savedqueryid eq ${id1}`, `savedqueryid eq ${id2}`, "returnedtypecode eq 'contoso_item' and name eq 'Twin'"]);
  // With v1's row moved off its stable id too, and v2's renamed, both are left to the rows of the name, and one row is not two.
  org.fake.rows('savedqueries').find((r) => r.data.savedqueryid === id1).data.savedqueryid = 'bbbbbbbb-0000-4000-8000-0000000000b1';
  row2.data.name = 'Moved';
  const short = await verifySpec(spec, savedQueryReader(org));
  assert.deepEqual(viewChecks(short), [['view', false, 'only 1 distinct unclaimed row(s) named "Twin" on contoso_item were read for 2 declared view(s) (keys: "v1", "v2"); each declaration needs a row of its own']]);
});

test('#845: a pin naming another twin\'s stable id is an identity conflict, and a stable id that cannot be computed leaves its view unverified', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const spec = twinSpec([twin('v1', ['contoso_name']), twin('v2', ['contoso_name', 'contoso_code'])]);
  const built = await build(org, newDir(), spec);
  assert.equal(built.err, undefined, built.err && built.err.message);
  const id2 = stableViewIdentityFor(spec, spec.views[1]).id;
  const reads = [];
  const crossed = await verifySpec(twinSpec([{ ...twin('v1', ['contoso_name']), viewId: `{${id2.toUpperCase()}}` }, twin('v2', ['contoso_name', 'contoso_code'])]), savedQueryReader(org, reads));
  assert.equal(crossed.ok, false);
  assert.deepEqual(viewChecks(crossed), [['view', false, `identity conflict: savedquery ${id2} is claimed by view keys "v1" (viewId), "v2" (stable id); each declaration needs a row of its own`]]);
  assert.deepEqual(reads, [], 'nothing is read for either');
  // An invalid createIdentity cannot derive an id: the view is unverified with the reason, and its name is not read in its place.
  const id1 = stableViewIdentityFor(spec, spec.views[0]).id;
  const invalidReads = [];
  const invalid = await verifySpec({ ...twinSpec([{ ...twin('v1', ['contoso_name']), viewId: id1 }, twin('v2', ['contoso_name', 'contoso_code'])]), createIdentity: 'stable-v0' }, savedQueryReader(org, invalidReads));
  assert.deepEqual(viewChecks(invalid), [['view', true, ''], ['view-columns', true, '']]);
  const open = invalid.unverified.filter((u) => u.startsWith('view'));
  assert.equal(open.length, 1, JSON.stringify(open));
  assert.match(open[0], /^view:contoso_item\.Twin \(view key "v2": its stable id could not be computed: .*createIdentity must be "stable-v1" or absent.* — unverified, not proven correct\)$/);
  assert.deepEqual(invalidReads, [`savedqueryid eq ${id1}`]);
});

test('#845: the stable id of a pinned twin is not its row, so a sibling pinned to it is no conflict', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const spec = twinSpec(['v1', 'v2', 'v3'].map((k) => twin(k, ['contoso_name'])));
  const built = await build(org, newDir(), spec);
  assert.equal(built.err, undefined, built.err && built.err.message);
  const [s1, s2, s3] = spec.views.map((v) => stableViewIdentityFor(spec, v).id);
  // v1 is pinned to the row built for v2, and v2 to the one built at v1's stable id, which v1 no longer uses; v3 keeps its own.
  const crossed = twinSpec([{ ...twin('v1', ['contoso_name']), viewId: s2 }, { ...twin('v2', ['contoso_name']), viewId: s1 }, twin('v3', ['contoso_name'])]);
  const reads = [];
  const r = await verifySpec(crossed, savedQueryReader(org, reads));
  assert.equal(r.ok, true, JSON.stringify(r.missing));
  assert.deepEqual(viewChecks(r), Array(3).fill([['view', true, ''], ['view-columns', true, '']]).flat());
  assert.deepEqual(reads, [`savedqueryid eq ${s2}`, `savedqueryid eq ${s1}`, `savedqueryid eq ${s3}`]);
});

test('#845: a bound or pinned view whose row is gone is missing, and its namesake is not taken in its place', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const spec = twinSpec([twin('v1', ['contoso_name']), twin('v2', ['contoso_name', 'contoso_code'])]);
  const built = await build(org, dir, spec);
  assert.equal(built.err, undefined, built.err && built.err.message);
  const managedManifest = readManagedManifest(workspaceArgs(org, dir));
  const id2 = built.out.created.views['contoso_item|Twin'];
  const reader = savedQueryReader(org);
  const without = { ...reader, queryRecords: async (set, q) => (q.filter === `savedqueryid eq ${id2}` ? [] : reader.queryRecords(set, q)) };
  const r = await verifySpec(spec, without, { managedManifest });
  assert.equal(r.ok, false);
  assert.deepEqual(r.missing.filter((c) => c.kind === 'view').map((c) => [c.name, c.detail]), [
    ['contoso_item.Twin', `the view its managed binding (v2) is bound to (${id2}) was not found on its table; a view of the same name is not taken in its place (view key v2)`],
  ]);
  // A pin that names another row than the binding is a conflict to correct, not a pass.
  const id1 = built.out.created.sameNamedViews.find((v) => v.key === 'v1').id;
  const crossed = await verifySpec(twinSpec([twin('v1', ['contoso_name']), { ...twin('v2', ['contoso_name', 'contoso_code']), viewId: id1 }]), reader, { managedManifest });
  assert.deepEqual(crossed.missing.map((c) => [c.kind, c.detail]), [
    ['view', `its viewId ${id1} is not the view its managed binding (v2) is bound to (${id2}); correct or remove the viewId (view key v2)`],
  ]);
  // An attempt still pending for the key proves nothing: unverified, never a pass or a failure.
  const open = { ...managedManifest, groups: managedManifest.groups.filter((g) => g.key !== 'v2'), pending: [{ family: 'view', key: 'v2', code: 'managed-settlement-pending' }] };
  const held = await verifySpec(spec, reader, { managedManifest: open });
  assert.ok(held.unverified.includes('view:contoso_item.Twin (its managed binding (v2) is not settled (managed-settlement-pending) — unverified, not proven correct)'));
  assert.deepEqual(viewChecks(held), [['view', true, ''], ['view-columns', true, '']], 'only the settled view is checked');
  // A binding store that cannot be read proves no binding either: each declared view is unverified, as a declared chart is,
  // never read by its name in place of the row the store may bind.
  const unreadable = { revision: 0, groups: [], pending: [], invalid: [{ family: 'store', key: 'managed-bindings', code: 'managed-binding-store-unreadable' }] };
  const blind = await verifySpec(spec, reader, { managedManifest: unreadable });
  assert.deepEqual(viewChecks(blind), [], 'no view is checked by its name');
  for (const key of ['v1', 'v2']) {
    assert.ok(blind.unverified.includes(`view:contoso_item.Twin (its managed binding (${key}) is not settled (managed-binding-store-unreadable) — unverified, not proven correct)`), JSON.stringify(blind.unverified));
  }
});

test('#845 control: a spec without same-named views records and verifies exactly as before', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const dir = newDir();
  const other = { entity: 'contoso_item', name: 'Other', reconcile: 'declared', columns: ['contoso_code'] };
  const spec = twinSpec([twin('v1', ['contoso_name']), other]);
  const built = await build(org, dir, spec);
  assert.equal(built.err, undefined, built.err && built.err.message);
  assert.deepEqual(Object.keys(built.out.created.views), ['contoso_item|Twin', 'contoso_item|Other']);
  assert.equal(built.out.created.sameNamedViews, undefined);
  assert.equal(built.tablePublishes.filter((names) => names.includes('contoso_item')).length, 1);
  // Without bindings, a declared view is read by its table and name, one row, as it always was.
  const reads = [];
  const r = await verifySpec(spec, savedQueryReader(org, reads));
  assert.equal(r.ok, true, JSON.stringify(r.missing));
  assert.deepEqual(viewChecks(r), [['view', true, ''], ['view-columns', true, ''], ['view', true, ''], ['view-columns', true, '']]);
  assert.deepEqual(reads, ["returnedtypecode eq 'contoso_item' and name eq 'Twin'", "returnedtypecode eq 'contoso_item' and name eq 'Other'"]);
  // An unkeyed legacy view of the same name as a declared one is read by its name too, and its content checked, as before.
  const legacyReads = [];
  const legacy = await verifySpec({ ...spec, views: [{ entity: 'contoso_item', name: 'Other', columns: ['contoso_code'] }, other] }, savedQueryReader(org, legacyReads));
  assert.equal(legacy.ok, true, JSON.stringify(legacy.missing));
  assert.equal(legacy.checks.filter((c) => c.kind === 'view-columns' && c.present).length, 2);
  assert.deepEqual(legacyReads, ["returnedtypecode eq 'contoso_item' and name eq 'Other'", "returnedtypecode eq 'contoso_item' and name eq 'Other'"]);
});
test('#845: a view named like another view plus a key keeps its own entry, and the same-named pair is still recorded in full', async () => {
  for (const thirdFirst of [true, false]) {
    // `Twin|v1` is a legal view name of its own; a record that joined `entity|name|key` would give its entry to the first Twin.
    const third = { ...twin('third', ['contoso_code']), name: 'Twin|v1' };
    const pair = [twin('v1', ['contoso_name']), twin('v2', ['contoso_name', 'contoso_code'])];
    const spec = twinSpec(thirdFirst ? [third, ...pair] : [...pair, third]);
    assert.deepEqual(validateAppSpec(spec).errors, []);
    const org = createFakeBuildOrg({ entities: ENTITIES });
    const built = await build(org, newDir(), spec);
    assert.equal(built.err, undefined, built.err && built.err.message);
    const s = createStableCreates({ spec, sdk: null, store: null, basePort: createPassthroughCreates() });
    const [id1, id2, own] = ['v1', 'v2', 'third'].map((k) => s.identityFor('view', k).id);
    assert.deepEqual(new Set(org.fake.rows('savedqueries').map((r) => r.data.savedqueryid)), new Set([id1, id2, own]), `thirdFirst=${thirdFirst}`);
    // The name entries are what they always were: `Twin|v1` is its own view, `Twin` the last of the pair.
    assert.deepEqual(built.out.created.views, { 'contoso_item|Twin|v1': own, 'contoso_item|Twin': id2 }, `thirdFirst=${thirdFirst}`);
    assert.equal(indexArtifacts(spec, built.out.created).views['contoso_item|Twin|v1'].viewId, own);
    assert.equal(indexArtifacts(spec, built.out.created).views['contoso_item|Twin'].viewId, id2);
    // The first Twin is recorded too, apart from the name entries, so the app's components and declared pins hold all three.
    assert.deepEqual(built.out.created.sameNamedViews, [{ entity: 'contoso_item', name: 'Twin', key: 'v1', id: id1 }]);
    assert.deepEqual(new Set(appComponentsFor(built.out.created).views), new Set([id1, id2, own]));
    assert.deepEqual(new Set(declaredAppPins(built.out.created).views), new Set([id1, id2, own]));
    assert.equal(appComponentsFor(built.out.created).views.length, 3);
  }
});

// The views of more than one name in one pass, and rows given to the reader as they are, rather than built: the reader holds them as
// the fake org would, so the filters verify sends are applied the same way.
const named = (name, key, columns = ['contoso_name']) => ({ entity: 'contoso_item', name, ...(key === undefined ? {} : { key }), reconcile: 'declared', columns });
const heldRow = (savedqueryid, name) => ({
  savedqueryid, returnedtypecode: 'contoso_item', name,
  layoutxml: '<grid><row><cell name="contoso_name"/></row></grid>',
  fetchxml: '<fetch><entity name="contoso_item"><attribute name="contoso_name"/></entity></fetch>',
});
const holding = (rows) => ({ fake: { rows: () => rows.map((data) => ({ data })) } });
// The id the build creates a view at, from a spec of that view alone: a build of the whole spec refuses two keys of one id first.
const builtIdOf = (spec, v) => createStableCreates({ spec: { ...spec, views: [v] }, sdk: null, store: null, basePort: createPassthroughCreates() })
  .identityFor('view', v.key || `${v.entity}/${v.name}`).id;

test('#845: a stable id two declarations of different names derive is claimed once in the pass, and neither group is read', async () => {
  // The implicit key of the first Twin is "contoso_item/Twin"; an Other keyed "twin" derives the same id.
  const spec = twinSpec([named('Twin'), named('Twin', 'second-twin'), named('Other', 'twin'), named('Other', 'second-other')]);
  assert.deepEqual(validateAppSpec(spec).errors, []);
  const ids = spec.views.map((v) => builtIdOf(spec, v));
  assert.equal(ids[0], ids[2]);
  const rows = [heldRow(ids[0], 'Other'), heldRow(ids[1], 'Twin'), heldRow(ids[3], 'Other')];
  for (const reverse of [false, true]) {
    const s = structuredClone(spec);
    if (reverse) s.views.reverse();
    const keys = reverse ? '"twin" (stable id), "contoso_item/Twin" (stable id)' : '"contoso_item/Twin" (stable id), "twin" (stable id)';
    const conflict = `identity conflict: savedquery ${ids[0]} is claimed by view keys ${keys}; each declaration needs a row of its own`;
    const reads = [];
    const r = await verifySpec(s, savedQueryReader(holding(rows), reads));
    assert.equal(r.ok, false, `reverse=${reverse}`);
    assert.deepEqual(viewChecks(r), [['view', false, conflict], ['view', false, conflict]], `reverse=${reverse}`);
    assert.deepEqual(reads, [], 'four declarations cannot stand on three rows; nothing is read for either name');
  }
});

test('#845: a row at the stable id of a view of another name is that view\'s, never a spare row of the name it was renamed to', async () => {
  const spec = twinSpec([named('Twin', 'first-twin'), named('Twin', 'second-twin'), named('Other', 'first-other'), named('Other', 'second-other')]);
  const ids = spec.views.map((v) => builtIdOf(spec, v));
  assert.equal(new Set(ids).size, 4);
  // first-twin's own row was renamed "Other"; one more row of that name is the only one nobody owns.
  const SPARE = 'ffffffff-1111-4111-8111-111111111111';
  const rows = [heldRow(ids[0], 'Other'), heldRow(ids[1], 'Twin'), heldRow(SPARE, 'Other')];
  for (const reverse of [false, true]) {
    const s = structuredClone(spec);
    if (reverse) s.views.reverse();
    const keys = reverse ? '"second-other", "first-other"' : '"first-other", "second-other"';
    const short = ['view', false, `only 1 distinct unclaimed row(s) named "Other" on contoso_item were read for 2 declared view(s) (keys: ${keys}); each declaration needs a row of its own`];
    const twins = [['view', true, ''], ['view-columns', true, ''], ['view', true, ''], ['view-columns', true, '']];
    const reads = [];
    const r = await verifySpec(s, savedQueryReader(holding(rows), reads));
    assert.equal(r.ok, false, `reverse=${reverse}`);
    assert.deepEqual(viewChecks(r), reverse ? [short, ...twins] : [...twins, short], `reverse=${reverse}`);
    // Each view is read at its stable id; the two Other views, with nothing there, by their name once.
    assert.deepEqual(reads.slice().sort(), [...ids.map((id) => `savedqueryid eq ${id}`), "returnedtypecode eq 'contoso_item' and name eq 'Other'"].sort());
  }
});

test('#845: a binding another workspace persisted for a pin at the implicit twin\'s stable id is an identity conflict, read from the inventory alone', async () => {
  const org = createFakeBuildOrg({ entities: ENTITIES });
  const d1 = newDir();
  const d2 = newDir();
  const first = await build(org, d1, twinSpec([named('Twin')]));
  assert.equal(first.err, undefined, first.err && first.err.stack);
  const physical = org.fake.rows('savedqueries')[0].data.savedqueryid;
  // A second workspace adopts that row for key b, pinned to it, and persists the binding.
  const second = await build(org, d2, twinSpec([{ ...named('Twin', 'b'), viewId: physical }]), { authorizations: ['adopt:view:b'] });
  assert.equal(second.err, undefined, second.err && second.err.stack);
  const managedManifest = readManagedManifest(workspaceArgs(org, d2));
  assert.equal(managedManifest.groups.find((g) => g.key === 'b').root.id, physical);
  const snapshot = () => fs.readdirSync(d2).map((n) => [n, fs.statSync(path.join(d2, n)).isFile() ? fs.readFileSync(path.join(d2, n)).toString('base64') : null]);
  const before = snapshot();
  const reads = [];
  const r = await verifySpec(twinSpec([named('Twin'), named('Twin', 'b')]), savedQueryReader(org, reads), { managedManifest });
  assert.deepEqual(snapshot(), before, 'verify writes nothing to the workspace');
  assert.equal(r.ok, false);
  assert.deepEqual(viewChecks(r), [['view', false, `identity conflict: savedquery ${physical} is claimed by view keys "contoso_item/Twin" (stable id), "b" (managed binding); each declaration needs a row of its own`]]);
  assert.equal(reads.length, 1, `only the managed inventory is read: ${JSON.stringify(reads)}`);
});

test('#845: a sibling whose stable id cannot be computed leaves the unread twin unverified, never matched against the rows of the name', async () => {
  // A lone combining accent is a valid view name with no character a stable id can be derived from.
  const NAME = '\u0301';
  const spec = twinSpec([named(NAME), named(NAME, 'b')]);
  assert.deepEqual(validateAppSpec(spec).errors, []);
  const idB = builtIdOf(spec, spec.views[1]);
  const underivable = `view:contoso_item.${NAME} (view key "contoso_item/${NAME}": its stable id could not be computed: create identity: name '${NAME}' has no characters that identify it — unverified, not proven correct)`;
  const blocked = `view:contoso_item.${NAME} (view key "b": another declaration's stable identity is unresolved, so a distinct-row assignment is not proven)`;
  for (const rows of [[], [heldRow('ffffffff-1111-4111-8111-111111111111', NAME)]]) {
    for (const reverse of [false, true]) {
      const s = structuredClone(spec);
      if (reverse) s.views.reverse();
      const reads = [];
      const r = await verifySpec(s, savedQueryReader(holding(rows), reads));
      const label = `rows=${rows.length} reverse=${reverse}`;
      assert.deepEqual(viewChecks(r), [], label);
      assert.deepEqual(r.unverified.filter((u) => u.startsWith('view')), reverse ? [blocked, underivable] : [underivable, blocked], label);
      assert.deepEqual(reads, [`savedqueryid eq ${idB}`], `b is read at its stable id alone, and the name is never read: ${label}`);
    }
  }
});