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.

12.2 KB
Raw Blame
'use strict';
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 ownership = require('../lib/page-ownership-records.js');
const snapshots = require('../lib/apply-snapshot-store.js');
const { loadCli } = require('./helpers/cli-harness.js');
const { fakeGenpage } = require('./helpers/fake-genpage.js');
const { withPageType } = require('./helpers/page-component-type.js');
const { isFormScan, noFormRows } = require('./helpers/form-scan.js');
const { withAppBuildLease, buildLockName } = require('../lib/app-operation-lease.js');
const { appUniqueName } = require('../lib/app-unique-name.js');

const ENV = 'https://contoso.crm.dynamics.com';
const OTHER_ENV = 'https://fabrikam.crm.dynamics.com';
const APP = 'contoso_pages';
const ID = '11111111-1111-4111-8111-111111111111';
function workspace(t) {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'contoso-receipt-clear-'));
  t.after(() => fs.rmSync(root, { recursive: true, force: true }));
  const dir = path.join(root, '.maker-workspace');
  fs.mkdirSync(dir);
  fs.writeFileSync(path.join(dir, 'manifest.json'), JSON.stringify({ instanceUrl: ENV, artifacts: [] }));
  return dir;
}

async function runClear(dir, { sdk, spec, realEngine = false, store } = {}) {
  const emissions = [];
  const cli = loadCli(path.resolve(__dirname, '..', 'teardown-model-app.js'), {
    argv: ['--env', ENV, '--spec', '@contoso-app-spec.json', '--workspace', dir, '--apply', '--allow-destructive', '--clear-workspace'],
    env: { POWER_PLATFORM_SKILLS_TELEMETRY_MODEL_APPS_OPTOUT: '1' },
    requires: {
      ...(!realEngine ? {
        './lib/app-spec.js': { migrateAppSpec: (s) => s, validateAppSpec: () => ({ ok: true }) },
        './lib/sdk-teardown.js': { runTeardown: async () => ({ ok: true, dryRun: false, deleted: {} }) },
      } : {}),
      ...(store ? { './lib/apply-snapshot-store.js': store } : {}),
      './lib/dataverse-auth.js': {
        ...require('../lib/dataverse-auth.js'),
        readJsonArg: () => spec || ({ app: { name: 'Contoso Pages', uniqueName: APP }, solution: { uniqueName: 'ContosoPages', publisherPrefix: 'contoso' } }),
        emitResult: (ok, result) => { emissions.push({ ok, result }); },
      },
      './lib/sdk-http-client.js': { createAzHttpClient: () => ({}) },
      './lib/maker-sdk.js': {
        createNodeWorkspaceStorage: () => ({}),
        createMakerSdk: () => sdk || ({ initWorkspace: async () => {} }),
      },
    },
  });
  await cli.main();
  assert.equal(emissions.length, 1);
  assert.equal(emissions[0].ok, true, `refusing cache cleanup does not rerun or fail a completed remote teardown: ${JSON.stringify(emissions[0].result)}\n${cli.stderrText()}`);
  return cli.stderrText();
}

for (const [name, app, env] of [
  ['another app', 'contoso_other', ENV],
  ['another environment', APP, OTHER_ENV],
]) {
  test(`clear-workspace keeps an unconsumed receipt for ${name}`, async (t) => {
    const dir = workspace(t);
    ownership.recordPageCreation(dir, app, 'overview', ID, 'Overview', env);
    const file = fs.readdirSync(dir).find((f) => f.startsWith('page-ownership.'));
    const bytes = fs.readFileSync(path.join(dir, file));
    const log = await runClear(dir);
    assert.ok(fs.existsSync(dir));
    assert.deepEqual(fs.readFileSync(path.join(dir, file)), bytes);
    assert.match(log, /skipped --clear-workspace/);
    assert.match(log, new RegExp(app));
    assert.match(log, new RegExp(ID));
    assert.match(log, /original.*environment|resume|reconcile/i);
    // The CLI names the record by its canonical path; a temp folder reached through an alias (macOS
    // /var -> /private/var, a Windows 8.3 name or junction) spells the same file differently.
    assert.ok(log.includes(path.join(dir, file)) || log.includes(path.join(fs.realpathSync.native(dir), file)), 'each remaining record file must be named');
    assert.match(log, /teardown consumes.*deletes.*confirms.*absence/i);
    assert.match(log, /confirmed.*gone.*deleted by hand/i);
  });
}

test('clear-workspace still clears a genuine disposable workspace without ownership records', async (t) => {
  const dir = workspace(t);
  const log = await runClear(dir);
  assert.equal(fs.existsSync(dir), false);
  assert.match(log, /cleared workspace/);
});

// #807: the clear runs while the teardown still holds the app's build lease (the real lease and the real store here), so a build of the app
// that starts between the deletes and the clear is refused instead of having its workspace, lock and all, moved and deleted under it.
test('clear-workspace runs under the app build lease: a build of the app started at the clear is refused', async (t) => {
  const dir = workspace(t);
  const spec = { app: { name: 'Contoso Pages', uniqueName: APP }, solution: { uniqueName: 'ContosoPages', publisherPrefix: 'contoso' } };
  const lockPath = path.join(dir, buildLockName(appUniqueName(spec)));
  let atClear = null;
  const store = {
    ...snapshots,
    clearWorkspace: (target, deps) => {
      // The lock is looked for first: the concurrent build's lease, tried synchronously next, would make one of its own if it were free.
      const lockHeld = fs.existsSync(lockPath);
      const build = withAppBuildLease({ workspaceDir: dir, appUniqueName: appUniqueName(spec), warn: () => {} }, async () => 'a build ran');
      build.catch(() => {});
      atClear = { lockHeld, build };
      return snapshots.clearWorkspace(target, deps);
    },
  };
  const log = await runClear(dir, { spec, store });
  assert.match(log, /cleared workspace/);
  assert.equal(fs.existsSync(dir), false);
  assert.ok(atClear, 'the clear ran');
  assert.equal(atClear.lockHeld, true, 'the teardown still holds the build lock when the clear starts');
  await assert.rejects(atClear.build, (e) => e.code === 'build-locked');
});

// A teardown from a folder that never had a workspace makes one for its lease and removes it again once the lease is let go; the clear
// then finds nothing to clear, as before the clear moved under the lease.
test('clear-workspace from a folder that never had a workspace leaves none behind', async (t) => {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'contoso-receipt-clear-'));
  t.after(() => fs.rmSync(root, { recursive: true, force: true }));
  const dir = path.join(root, 'app', '.maker-workspace');
  const log = await runClear(dir);
  assert.deepEqual(fs.readdirSync(root), [], 'the folders the teardown made are gone');
  assert.match(log, /skipped --clear-workspace: nothing to clear/);
});

for (const outcome of ['deleted', 'confirmed absent']) {
  test(`build creates, teardown retires ${outcome} page records, and clear-workspace succeeds`, async (t) => {
    const dir = workspace(t);
    const appDir = path.dirname(dir);
    fs.writeFileSync(path.join(appDir, 'overview.tsx'), 'export default () => null;');
    const spec = {
      schemaVersion: 2,
      solution: { uniqueName: 'ContosoPages', publisherPrefix: 'contoso' },
      app: { name: 'Contoso Pages', uniqueName: APP },
      entities: [{ schemaName: 'contoso_item', displayName: 'Item', primaryAttribute: { schemaName: 'contoso_name', displayName: 'Name' } }],
      pages: [{ key: 'overview', name: 'Overview', source: { kind: 'tsx', codeFile: 'overview.tsx' } }],
      appShell: { areas: [{ label: 'Main', groups: [{ label: 'Pages', subAreas: [{ page: 'overview' }] }] }] },
    };
    const appId = '22222222-2222-4222-8222-222222222222';
    const layerId = '33333333-3333-4333-8333-333333333333';
    const sitemapId = '44444444-4444-4444-8444-444444444444';
    const state = { app: true, page: false, manifest: null, solution: true, creates: 0, deletes: 0 };
    const sdk = {
      initWorkspace: async () => {},
      queryRecords: async (entity) => {
        if (entity === 'appmodule') return state.app ? [{ appmoduleid: appId, appmoduleidunique: layerId, uniquename: APP }] : [];
        if (entity === 'appmodulecomponent') return [{ objectid: sitemapId, componenttype: 62 }];
        if (entity === 'sitemap') return state.app ? [{ sitemapxml: '<SiteMap />' }] : [];
        if (entity === 'webresource') return state.manifest ? [{ webresourceid: 'manifest-id', content: state.manifest }] : [];
        return [];
      },
      // The pages in their saved layer, which is where teardown looks for them; the navigation's saved layer is not modelled here, so reading it fails as it does without a client.
      // The page table's component type is answered in front: the build's pages join the spec's solution (lib/page-solution.js).
      dataverse: {
        get: withPageType(async (url) => {
          if (isFormScan(url)) return noFormRows();
          if (!url.startsWith('/uxagentprojects/Microsoft.Dynamics.CRM.RetrieveUnpublishedMultiple()')) throw new Error(`not modelled: ${url}`);
          return { status: 200, body: { value: state.page ? [{ uxagentprojectid: ID, name: 'Overview', componentstate: 0 }] : [] } };
        }),
      },
      createWebResource: async (o) => { state.manifest = Buffer.from(o.content, 'utf8').toString('base64'); return { id: 'manifest-id' }; },
      updateWebResource: async (id, o) => { state.manifest = Buffer.from(o.content, 'utf8').toString('base64'); },
      addSolutionComponent: async () => {},
      resolveArtifact: async (kind, identity) => {
        if (kind === 'app') return state.app ? [{ id: appId, name: spec.app.name, appModuleIdUnique: layerId }] : [];
        if (kind === 'webResource') return state.manifest && identity.name === `${APP}_pagemanifest` ? [{ id: 'manifest-id', name: identity.name }] : [];
        if (kind === 'solution') return state.solution ? [{ id: 'solution-id', name: spec.solution.uniqueName }] : [];
        return [];
      },
      deleteAppCascade: async () => { state.app = false; return { success: true, deleted: [], failures: [] }; },
      deleteRemoteArtifact: async (kind, id) => {
        assert.equal(kind, 'genPage');
        assert.equal(id, ID);
        state.page = false;
        state.deletes++;
      },
      deleteWebResource: async () => { state.manifest = null; },
      deleteSolution: async () => { state.solution = false; },
      deleteTable: async (logical) => { assert.equal(logical, 'contoso_item'); },
    };
    const genpage = fakeGenpage({
      newId: () => ID,
      onCreate: () => {
        state.page = true;
        state.creates++;
      },
    });
    await require('../lib/sdk-build.js').runSdkBuild(spec, {
      sdk, apply: true, env: ENV, appDir, workspaceDir: dir, phases: ['pages'],
      changedOnly: { resolvedAppId: appId, skipSitemapFinalize: true },
      genpage,
    });
    assert.equal(state.creates, 1);
    assert.equal(genpage.of('update').length, 0, 'this is an acknowledged CREATE, not a reused id');
    assert.equal(ownership.readPageOwnership(dir, APP, ENV).created.length, 1);
    assert.equal(ownership.checkPageOwnershipClearable(dir).ok, false);
    if (outcome === 'confirmed absent') state.page = false;
    const log = await runClear(dir, { sdk, spec, realEngine: true });
    assert.equal(state.deletes, outcome === 'deleted' ? 1 : 0);
    assert.equal(state.page, false);
    assert.equal(state.manifest, null);
    assert.equal(state.solution, false);
    assert.equal(fs.existsSync(dir), false, 'all page records were retired before the cache was cleared');
    assert.match(log, /cleared workspace/);
    assert.doesNotMatch(log, /skipped --clear-workspace/);
  });
}

test('a receipt arriving during atomic workspace isolation is retained before recursive deletion', (t) => {
  const dir = workspace(t);
  let isolated;
  const result = snapshots.clearWorkspace(dir, {
    beforeRename: () => ownership.recordPageCreation(dir, 'contoso_other', 'overview', ID, 'Overview', ENV),
    beforeRemove: (aside) => {
      isolated = aside;
      if (fs.readdirSync(aside).some((f) => f.startsWith('page-ownership.'))) throw new Error('unconsumed ownership record arrived during clear');
    },
  });
  assert.equal(result.ok, false);
  assert.ok(isolated && fs.existsSync(isolated));
  assert.equal(ownership.readPageOwnership(isolated, 'contoso_other', ENV).created.length, 1);
  assert.match(result.reason, /kept|retained/);
  const lease = snapshots.acquireLease(isolated);
  assert.equal(lease.ok, true, 'a retained workspace must be resumable in the same process');
  snapshots.releaseLease(lease);
});