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.

15.2 KB
Raw Blame
'use strict';

const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const test = require('node:test');
const { spawnSync } = require('node:child_process');
const { shellBlocks } = require('./helpers/markdown-shell-blocks');
const { createSkillEntry } = require('./helpers/create-skill');

const root = path.resolve(__dirname, '../..');
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8');
const updates = read('skills/check-updates/SKILL.md');
// The router with the branches it includes (template acquisition, creation continuation).
const create = createSkillEntry();
// Each build order explains template preparation in its own flow: data model first in its Step 5,
// screens first in the reference its Step 3.2 points to.
const preparation = ['data-model-first.md', 'template-preparation.md']
  .map((file) => [file, read(`skills/create-mobile-app/references/${file}`)]);
const lifecycle = read('shared/references/mobile-release-lifecycle.md');

test('native updates are evaluated independently of outdated-host discovery', () => {
  assert.match(updates, /even when the host is absent from `outdated\.json`/);
  assert.match(updates, /The plan below reads the recorded source state and installed host identity/);
  assert.match(updates, /Equal supported targets are valid/);
  assert.match(updates, /Do not require or fabricate template advancement/);
  assert.match(updates, /even if `hostInstallRequired` is false/);
  assert.doesNotMatch(updates, /Do not run `upgrade-template`/);
});

test('rehearsal, full-chain approval, drift checks and recovery are separate gates', () => {
  const rehearsal = updates.indexOf('Test the upgrade in a copy of my app');
  const transaction = updates.indexOf('clean full-chain plan');
  const approval = updates.indexOf('Apply the tested upgrade to my app');
  const apply = updates.indexOf('### Apply only the approved plan');
  assert.ok(rehearsal >= 0 && rehearsal < transaction && transaction < approval && approval < apply);
  assert.doesNotMatch(updates, /repeat dry-run and apply until/);
  assert.match(updates, /nonzero preview is never automatically a clean plan/);
  assert.match(updates, /`--no-install` to simulate state advancement/);
  assert.match(updates, /under `<working_dir>`/);
  assert.match(updates, /Changed inputs invalidate approval/);
  assert.match(updates, /restore only touched files/);
  assert.match(updates, /rollback or journal recovery cannot complete safely/);
  assert.match(updates, /Never report `DONE` for a partial migration/);
  assert.match(updates, /Skipping excludes all coupled packages/);
});

test('upgrade failures require actionable app-owned repairs without weakening validation', () => {
  assert.match(updates, /For every failed preparation, install, migration or validation check/);
  assert.match(updates, /Explain the failure, its impact and a concrete possible fix/);
  assert.match(updates, /minimal app-owned source\/configuration fixes in the test copy/);
  assert.match(updates, /Test-copy approval covers these\s+repairs, not original-app edits/);
  assert.match(updates, /never\s+weaken types\/checks, suppress diagnostics or disable offline/);
  assert.match(updates, /Rerun the failed check, then all mandatory checks and relevant existing app\s+tests/);
  assert.match(updates, /including the approved app-code fixes in their rehearsed order/);
  assert.match(updates, /After successful recovery, diagnose and repair the failure in the test copy/);
  assert.match(updates, /Existing plugin-restart, publication, approval and recovery gates still apply/);
  assert.match(updates, /Never patch npm packages via `node_modules`, `patch-package`/);
});

test('local workflows carry unverified compatibility without gating installation or startup', () => {
  assert.match(create, /resolve-mobile-release\.js[^\n]*--local-development/);
  assert.match(create, /Compatibility warnings do not block installation, preparation or trying the app/);
  assert.match(create, /do not add a pre-start\s+repair gate/);
  assert.match(lifecycle, /packageCompatibility: "unverified"/);
  assert.match(lifecycle, /not a resolved release or evidence\s+of native support/);
  assert.match(lifecycle, /If Dev Player actually fails,\s+invoke `\/debug-app`/);
  assert.match(lifecycle, /Deployment still requires\s+strict project\/base validation/);
  const shared = read('shared/shared-instructions.md');
  const validation = shellBlocks(shared).find(block => block.includes('scripts/validate-mobile-files.js'));
  assert.match(validation, /--local-development/);
  assert.match(read('skills/edit-app/SKILL.md'), /resolve-mobile-release\.js[^\n]*--local-development/);
  assert.match(read('skills/debug-app/SKILL.md'), /compare the actual Player runtime and installed\s+package versions/);
  for (const file of [
    'agents/native-app-planner.md', 'agents/screen-planner.md', 'agents/screen-builder.md',
    'skills/create-mobile-app/references/requirements-discovery.md',
  ]) assert.match(read(file), /local-development-only/, file);
  for (const helper of ['add-camera', 'add-geolocation', 'add-pdf-report', 'add-pdf-viewer', 'add-pen-input']) {
    assert.match(read(`skills/add-native/${helper}/SKILL.md`), /resolve-mobile-release\.js[^\n]*--local-development/, helper);
  }
});

test('strict template discovery is limited to empty targets, not existing fresh starters', () => {
  const acquisition = create.slice(create.indexOf('**New/empty-directory acquisition only:**'),
    create.indexOf('**Existing fresh starters, installed or awaiting installation:**'));
  assert.match(acquisition, /discover published template versions through npm/);
  assert.match(acquisition, /resolve-mobile-release\.js --template-version/);
  assert.match(acquisition, /`BLOCKED` before scaffolding/);
  const existing = create.slice(create.indexOf('**Existing fresh starters, installed or awaiting installation:**'),
    create.indexOf('**Fresh template required for creation.**'));
  assert.match(existing, /skip npm template discovery and strict `--template-version` resolution/);
  assert.match(existing, /shipped-lock installation and local diagnostic/);
  assert.ok(create.indexOf('before any registry request') < create.indexOf('**New/empty-directory acquisition only:**'));
});

test('individual package failures allow approved repairs or safe skipping, not immediate abandonment', () => {
  const packageUpdates = updates.slice(updates.indexOf('For each approved JavaScript-only package update in Steps 3 and 4:'));
  assert.match(packageUpdates, /any approved app-code repair files \(including modes and absent-file state\)/);
  assert.match(updates, /obtain approval and back up additional\s+app-code repair files before editing them/);
  assert.match(packageUpdates, /If any command fails, follow the repair loop above/);
  assert.match(packageUpdates, /If the user declines the repair, restore safely, record the package as skipped and continue/);
  assert.match(packageUpdates, /stop mutations if restoration is unsafe/);
  assert.doesNotMatch(packageUpdates, /return `BLOCKED` with the failed command, and do not offer later packages/);
});

test('JavaScript updates retain approval and audit safeguards without native bumps', () => {
  assert.match(updates, /Skip package.*recommended default/);
  assert.match(updates, /validate-mobile-files\.js --upgrade-only/);
  assert.equal((updates.match(/npm outdated --json --depth=0/g) || []).length, 1);
  assert.doesNotMatch(updates, /Run `npm install --ignore-scripts`/);
  assert.match(updates, /installed transitive closure adds native code/);
  assert.match(updates, /isDirect: true/);
  assert.match(updates, /Ignore string-only `via` rollups/);
  assert.match(updates, /Never run `npx expo install --fix`/);
  for (const checkpoint of [
    'check_mobile_app_plugin_version', 'update_native_host_dependency',
    'update_microsoft_dependencies', 'update_remaining_npm_dependencies',
  ]) assert.ok(updates.includes(`\`${checkpoint}\``), checkpoint);
});

test('JavaScript package updates check installed native drift before accepting changes and roll back failures', () => {
  const javascript = updates.slice(updates.indexOf('## Step 3:'));
  const snapshot = javascript.indexOf('snapshot-native');
  const install = javascript.indexOf('Install with `--ignore-scripts`');
  const check = javascript.indexOf(' check-native ');
  assert.ok(snapshot >= 0 && snapshot < install && install < check);
  const commands = shellBlocks(javascript);
  assert.equal(commands.length, 2);
  assert.match(commands[0], /snapshot-native[\s\S]*--project-root '<working_dir>'/);
  assert.match(commands[1], /check-native[\s\S]*--baseline '<working_dir>\/\.tmp\/dependency-maintenance\/native-before\.json'/);
  assert.match(javascript, /A failed snapshot blocks installation/);
  assert.match(javascript, /Do not recapture the\s+baseline after installation/);
  assert.match(javascript, /including `check-native`, restore that package's snapshot/);
  assert.match(javascript, /does\s+not replace `check-native`/);
  assert.doesNotMatch(updates.slice(0, updates.indexOf('## Step 3:')), /snapshot-native|check-native/);
  assert.match(lifecycle, /Host-owned native migration\/repair/);
});

test('registry authentication recovery preserves the reviewed feed and interrupted-install boundary', () => {
  assert.match(updates, /For `E401`, pause mutations/);
  assert.match(updates, /user-approved credential-provider command against its `\.npmrc`/);
  assert.match(updates, /do not replace the project `\.npmrc`/);
  assert.match(updates, /retry the same reviewed publication/);
  assert.match(updates, /project-scoped journal and recovery instructions before resuming/);
});

test('requested Metro smoke tests distinguish bundle compilation from runtime behavior', () => {
  assert.match(updates, /a responding `\/status` endpoint alone does not verify bundling/);
  assert.match(updates, /load the intended route and exercise the customer\s+workflow/);
  assert.match(updates, /Report bundle and runtime results separately/);
  assert.match(updates, /do not fabricate\s+environment\/auth configuration/);
});

test('creation resolves immutable published package metadata before planning or data mutations', () => {
  assert.match(create, /Missing or incompatible published package metadata means `BLOCKED` before scaffolding or data-platform mutations/);
  assert.match(create, /mobile-template-lifecycle\.js acquire/);
  assert.match(create, /Separately approve dependency installation/);
  assert.match(create, /Never run acquisition over a non-empty directory/);
  assert.match(create, /Pass its sanitized compatibility context to every planner\/builder/);
  for (const [file, source] of preparation) {
    assert.match(source, /`app\.json` byte-for-byte/, file);
    assert.match(source, /project-rooted aliases/, file);
    assert.doesNotMatch(source, /Assume the upstream template ships it/, file);
  }
  assert.doesNotMatch(create, /npx degit|template#main/);
  assert.match(lifecycle, /no plugin-owned release catalogue/);
  assert.match(lifecycle, /host's `compatibility\/current\.json` owns/);
  assert.match(updates, /npm outdated --json --depth=0/);
  assert.match(updates, /--host-version "<exact-host-version>"/);
  assert.doesNotMatch(updates, /--release "<release-id>"|empty production catalogue/);
  assert.match(lifecycle, /Per-customer optional-permission selection during wrapping\s+is not part/);
  assert.match(create, /mobile-template-lifecycle\.js destination --destination/);
  assert.match(create, /does\s+not apply to an existing app or a creation continuation/);
  assert.match(create, /missing root remains `BLOCKED`/);
  assert.match(create, /Only after acquisition\s+approval may the package CLI create the destination/);
  const readme = read('README.md');
  assert.ok(readme.indexOf('copilot plugin install') < readme.indexOf('resolve-mobile-release.js'));
  assert.ok(readme.indexOf('export PLUGIN_ROOT=') < readme.indexOf('resolve-mobile-release.js'));
});

test('release workflow command extraction accepts LF and Windows CRLF checkouts', () => {
  for (const [file, language] of [['README.md', 'sh'], ['shared/references/mobile-release-lifecycle.md', 'bash']]) {
    const normalized = read(file).replace(/\r\n/g, '\n');
    const commands = shellBlocks(normalized, language);
    assert.ok(commands.length > 0);
    assert.deepEqual(shellBlocks(normalized.replace(/\n/g, '\r\n'), language), commands);
  }
});

test('README creation never installs after failed resolution, acquisition or directory entry', t => {
  const commands = shellBlocks(read('README.md'), 'sh').find(block => block.includes('--destination ./my-mobile-app'));
  assert.ok(commands);
  const bashPaths = process.platform === 'win32'
    ? (spawnSync('where.exe', ['bash'], { encoding: 'utf8' }).stdout || '').split(/\r?\n/)
    : [];
  const bash = bashPaths.find(entry => /[\\/]Git[\\/]/i.test(entry)) || 'bash';
  const stubs = `
node() {
  case "$1" in
    */resolve-mobile-release.js) [ "$FAILURE_STAGE" != resolution ] ;;
    */mobile-template-lifecycle.js)
      [ "$FAILURE_STAGE" != acquisition ] || return 1
      [ "$FAILURE_STAGE" != directory ] || return 0
      "$REAL_NODE" -e 'require("node:fs").mkdirSync("my-mobile-app")'
      ;;
    *) return 99 ;;
  esac
}
npm() {
  [ "$1" = ci ] || return 98
  "$REAL_NODE" -e 'const fs=require("node:fs"); fs.writeFileSync("install-ran.txt", fs.realpathSync.native(process.cwd()))'
}
`;
  for (const failure of ['resolution', 'acquisition', 'directory', 'none']) {
    const directory = fs.mkdtempSync(path.join(os.tmpdir(), "mobile creation owner's "));
    t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
    const destination = path.join(directory, 'my-mobile-app');
    if (failure === 'acquisition') {
      fs.mkdirSync(destination);
      fs.writeFileSync(path.join(destination, 'customer.txt'), 'preserve customer content');
    }
    const result = spawnSync(bash, ['-s'], {
      cwd: directory, input: `${stubs}\n${commands}`, encoding: 'utf8', timeout: 10000,
      env: {
        ...process.env, FAILURE_STAGE: failure, PLUGIN_ROOT: '/fixture plugin',
        REAL_NODE: process.execPath.replaceAll('\\', '/'),
      },
    });
    assert.equal(result.error, undefined);
    assert.equal(result.status === 0, failure === 'none', result.stderr);
    assert.equal(fs.existsSync(path.join(directory, 'install-ran.txt')), false);
    assert.equal(fs.existsSync(path.join(destination, 'install-ran.txt')), failure === 'none');
    if (failure === 'none') {
      assert.equal(fs.readFileSync(path.join(destination, 'install-ran.txt'), 'utf8'), fs.realpathSync.native(destination));
    }
    if (failure === 'acquisition') {
      assert.equal(fs.readFileSync(path.join(destination, 'customer.txt'), 'utf8'), 'preserve customer content');
    }
  }
});

test('deployment instructions require all three actual selected-base values', () => {
  const commands = shellBlocks(lifecycle).find(block => block.includes('--base-version'));
  assert.ok(commands);
  for (const option of ['version', 'fingerprint', 'runtime-version']) {
    assert.ok(commands.includes(`--base-${option} "<actual-base-${option}>"`));
  }
  assert.match(lifecycle, /Obtain all three values[\s\S]*from the selected deployment target/);
  assert.match(lifecycle, /not by copying expected policy values just to pass/);
});