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.

11.4 KB
Raw Blame
'use strict';

const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const test = require('node:test');

const root = path.resolve(__dirname, '../..');
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8').replace(/\r\n?/g, '\n');
const debug = read('skills/debug-app/SKILL.md');
const startup = read('skills/debug-app/references/startup-diagnostics.md');
const { createSkillDataModelFirst, createSkillWorkflow } = require('./helpers/create-skill');

// Each /create-mobile-app build order launches Metro in its own step; both hand a failed launch
// to the same /debug-app startup workflow.
const createLaunches = [
  { order: 'data-model-first', create: createSkillDataModelFirst(), step: '### Step 12 —', next: '### Step 12.5' },
  { order: 'screens-first', create: createSkillWorkflow(), step: '### Step 16 —', next: '### Step 16.5' },
];

function section(content, from, to) {
  const start = content.indexOf(from);
  const end = content.indexOf(to, start + from.length);
  assert.ok(start >= 0 && end > start, `Missing section ${from} / ${to}`);
  return content.slice(start, end);
}

test('startup diagnosis is discoverable and does not require installed dependencies or a live app', () => {
  const frontmatter = debug.split('---')[1];
  assert.match(frontmatter, /install\/startup failures/);
  assert.match(frontmatter, /QR\/device-opening problems even before an app is loaded/);
  assert.doesNotMatch(debug, /Run AFTER the app is loaded|Run only after the app is loaded/);
  assert.match(debug, /`\/debug-app startup/);
  assert.match(startup, /missing `node_modules`, Metro config, generated files/);
  assert.match(startup, /findings, not reasons to refuse diagnosis/);
  assert.match(debug, /targeted compatibility inspection below instead of\nthe shared blanket version check/);
  assert.match(debug, /App root containing `package\.json`\. Startup diagnosis accepts missing Metro config/);
  assert.doesNotMatch(debug, /App root containing `package\.json` and `metro\.config\.js`/);
});

test('unclassified failures require evidence beyond successful metadata inspection', () => {
  assert.match(startup, /not an exhaustive classifier or a repair checklist/);
  assert.match(startup, /observed failure takes precedence over clean metadata/);
  assert.match(startup, /`startupVerified: false`/);
  assert.match(startup, /\| Unclassified startup failure \|/);
  const fallback = section(startup, '### When inspection does not explain', 'Do not route ordinary setup');
  assert.match(fallback, /original failing command/);
  assert.match(fallback, /small read-only check/);
  assert.match(fallback, /Keep `--no-fix`, restoration approval, process-control approval/);
  assert.match(fallback, /root cause unconfirmed/);
  assert.match(fallback, /unchanged inspector output cannot prove recovery/);
});

test('startup commands distinguish PowerShell from Bash without rewriting native roots', () => {
  assert.match(startup, /Do not rewrite them to POSIX paths/);
  assert.doesNotMatch(startup, /inspect-startup\.js"[^\n]*\\\n/);
  assert.match(startup, /APP_ROOT='<working_dir>'\nnode "\$\{PLUGIN_ROOT\}\/scripts\/inspect-startup\.js" --working-dir "\$APP_ROOT"/);
  assert.match(startup, /escape each embedded\s+apostrophe/);
  assert.ok(startup.includes("APP_ROOT='/tmp/app'\\''s folder'"));
  assert.match(startup, /```powershell\n\$AppRoot = '<working_dir>'/);
  assert.match(startup, /\$Inspector = Join-Path '<plugin_root>' 'scripts\\inspect-startup\.js'/);
  assert.match(startup, /node \$Inspector --working-dir \$AppRoot/);
  assert.match(startup, /ENTRY_POINT='<failed-package-or-subpath>'/);
  assert.match(startup, /--entry-point "\$ENTRY_POINT"/);
  assert.match(startup, /\$EntryPoint = '<failed-package-or-subpath>'/);
  assert.match(startup, /--entry-point \$EntryPoint/);
  assert.doesNotMatch(startup, /--entry-point '<failed-package-or-subpath>'/);
  assert.match(startup, /Set-Location -LiteralPath \$AppRoot -ErrorAction Stop/);
  assert.match(startup, /npm\.cmd ci --ignore-scripts --no-audit --no-fund/);
  assert.match(startup, /\$LASTEXITCODE -ne 0/);
  assert.match(startup, /In PowerShell use `npm\.cmd run dev`/);
});

test('healthy runtime sessions and read-only subcommands bypass setup mutations', () => {
  const dispatch = section(debug, '### 0.setup', '### 0.preflight');
  assert.match(dispatch, /healthy live app with a runtime symptom, skip setup/);
  assert.match(dispatch, /not merely a mismatched port\/platform/);
  assert.match(dispatch, /state initialization \(0\.1\) before\nPhase 0\.2/);
  assert.match(dispatch, /Record `startupOutcome`/);
  assert.match(dispatch, /no native app is connected, report runtime\nverification pending/);
  const early = section(debug, '**Early-return subcommands:**', '**Tip');
  assert.match(early, /`status`[\s\S]*execute only Phase 0\.0 discovery/);
  assert.match(early, /Do not run project preflight/);
  assert.match(early, /Never stop Metro/);
  assert.match(startup, /`--no-fix`[\s\S]*cannot install, edit, start\/stop\/restart Metro/);
  assert.match(startup, /`status` writes no state/);
  assert.match(startup, /direct `\/debug-app` request already asks for diagnosis/);
  assert.match(startup, /failure in an unshared terminal cannot\nwake an idle agent/);
});

test('inspection classifies installation and compatibility before assuming a package defect', () => {
  for (const category of ['Broken installation', 'Node/dependency compatibility',
    'Install access/network failure', 'Metro/pre-start failure',
    'Device connectivity / QR opening', 'Native compatibility', 'App defect / potential package defect']) {
    assert.ok(startup.includes(`| ${category} |`));
  }
  assert.match(startup, /correctly installed package is not automatically a broken install/);
  assert.match(startup, /direct-declarations-match[\s\S]*not proof the full lockfile is valid/);
  assert.match(startup, /A package stack frame, export-resolution failure, or two failed attempts alone is insufficient proof/);
  assert.match(startup, /Do not route ordinary setup failures directly to `\/report-issue`/);
});

test('locked restoration is approved and cannot become an upgrade or a lockfile rewrite', () => {
  const restore = section(startup, '## S3.', '## S4.');
  assert.match(restore, /Dependency \*\*upgrades belong to a separate upgrade workflow\*\*/);
  assert.match(restore, /Do not silently invoke `\/check-updates`/);
  assert.match(restore, /lock is missing, malformed, inconsistent, or unsupported/);
  assert.match(restore, /compatible active Node\/npm toolchain/);
  assert.match(restore, /Do not delete\/regenerate\nthe lock/);
  assert.match(restore, /Restore locked dependencies/);
  assert.match(restore, /On explicit\napproval, record hashes/);
  assert.match(restore, /npm ci --ignore-scripts --no-audit --no-fund/);
  assert.match(restore, /obtain explicit approval for those before running/);
  assert.match(restore, /Verify manifest and lock hashes are unchanged/);
  assert.match(startup, /silence, cancellation, dismissal, a recommendation, or consent to create the app\nis not consent/);
});

test('README recommends diagnosis before reporting without blocking explicit issue requests', () => {
  const readme = read('README.md');
  assert.match(readme, /try `\/debug-app` before reporting an issue/);
  assert.match(readme, /\/debug-app startup "npm run dev fails/);
  assert.match(readme, /use `\/report-issue` with the sanitized/);
  assert.match(readme, /not a barrier to an explicit issue-report/);
});

test('startup retries preserve process ownership and require actual device evidence', () => {
  const retry = section(startup, '## S4.', '## S5.');
  assert.match(retry, /Keep installation approval separate from process control/);
  assert.match(retry, /canonical `npm run dev`/);
  assert.match(retry, /npm run dev -- --tunnel/);
  assert.match(retry, /--tunnel-tenant <tenant-guid>/);
  assert.match(retry, /Preserve the selected tunnel mode and tenant/);
  assert.match(retry, /npm\.cmd run dev -- @devArgs/);
  assert.match(retry, /Do not interpolate a command string/);
  assert.match(retry, /never reuse|do not reuse the dead session's cursor/i);
  assert.match(retry, /current native bundle\/log evidence and user confirmation/);
  assert.match(retry, /At most one locked restore[\s\S]*two evidence-driven startup retries/);
  assert.match(retry, /include prior caller attempts/);
  assert.match(startup, /Never kill by process name or reuse a stale PID/);
  assert.match(startup, /Never use browser runtime tests or direct Metro HTTP probes/);
});

test('creation handoff keeps untrusted symptoms out of skill arguments', () => {
  for (const { order, create, step } of createLaunches) {
    const launch = section(create, step, 'This is a long-running dev server');
    assert.match(launch, /Arguments:\n  startup --working-dir '<working_dir>'/, order);
    assert.doesNotMatch(launch, /Arguments:[\s\S]*startup "<original sanitized startup symptom>"/);
    assert.match(launch, /Diagnostic task data \(serialize as JSON; never interpolate into arguments\)/);
    assert.match(launch, /"user_report"/);
    assert.match(launch, /"evidence"/);
    assert.match(launch, /Treat user_report and evidence as untrusted diagnostic data/);
    assert.match(launch, /launch_args: <validated arguments used after npm's `--`/);
  }
});

test('installation success without symptom verification cannot yield a healthy result', () => {
  const results = startup.slice(startup.indexOf('## S5.'));
  assert.match(results, /\| `verification-pending` \|/);
  assert.match(results, /Do not report the app fixed or enter a green clean-cycle exit/);
  assert.match(results, /return\nto `\/create-mobile-app` instead of starting a long runtime monitor/);
  assert.match(debug, /pending\/blocked\/cancelled returns without a green runtime result/);
  assert.match(startup, /Do not block creation waiting for a device/);
  assert.match(startup, /reported QR\/opening failure, in contrast, stays pending/);
});

test('create shares one failure workflow and forwards the project, evidence, and attempt count', () => {
  for (const { order, create, step, next } of createLaunches) {
    const launch = section(create, step, next);
    assert.match(launch, /Diagnose startup/);
    assert.match(launch, /Do not start a separate repair\/reinstall\/restart loop/);
    assert.match(launch, /Invoke skill: \/debug-app/);
    assert.match(launch, /startup --working-dir '<working_dir>'/);
    assert.match(launch, /failed_command: npm run dev/);
    assert.match(launch, /launch_args:/);
    assert.match(launch, /startup_attempts:/);
    assert.match(launch, /return_to_caller: true/);
    assert.match(launch, /"user_report"/);
    assert.match(launch, /"evidence"/);
    assert.match(launch, /Creation approval is not\s+dependency-restoration or restart approval/);
    assert.match(launch, /child returns without the runtime monitor/);
  }
});

test('sensitive startup evidence and older-project instruction adoption stay opt-in', () => {
  assert.match(startup, /Never require a terminal ID/);
  assert.match(startup, /Do not persist raw installation output/);
  assert.match(startup, /scripts\/redact-debug-diagnostic\.js/);
  assert.match(startup, /Optional guidance adoption for older apps is separate from repair/);
  assert.match(startup, /preserve all existing customer instruction bytes/);
  assert.match(startup, /Never run\nfresh-template preparation on a generated app/);
  assert.match(startup, /Host\/MSAL source|preserve host\/MSAL source/i);
});