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.
JavaScript claude-code-pluginclaude-code-skillscode-appsgithub-copilot-pluginpower-appspower-pagespower-platform
// plugins/model-apps/scripts/tests/validate-flags.test.js
// Contract for the shared CLI flag validator and the "did you mean" matcher behind it.
'use strict';
const test = require('node:test');
const assert = require('node:assert');
const { execFileSync } = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { validateFlags, parseArgs } = require('../lib/dataverse-auth.js');
const { nearestName } = require('../lib/nearest-name.js');
const PLUGIN = path.join(__dirname, '..', '..');
const script = (n) => path.join(PLUGIN, 'scripts', n);
const SPEC = path.join(PLUGIN, 'samples', 'app-spec.project-tracker.json');
// A build opens its workspace — by default `.maker-workspace` beside the spec — and the SDK binds it to the first
// environment that opens it. The spec here is a committed sample, so a run that used the default would leave a
// workspace bound to the placeholder environment in `samples/`, where a later real build using that folder as its app
// folder would be halted. These runs name a temporary one instead.
const SAMPLES_WORKSPACE = path.join(PLUGIN, 'samples', '.maker-workspace');
const workspaces = [];
function tempWorkspace() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'validate-flags-ws-'));
workspaces.push(dir);
return dir;
}
test.after(() => { for (const dir of workspaces) fs.rmSync(dir, { recursive: true, force: true }); });
function run(args) {
try {
const stdout = execFileSync(process.execPath, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
return { code: 0, stdout, stderr: '' };
} catch (err) {
return { code: err.status, stdout: err.stdout || '', stderr: err.stderr || '' };
}
}
test('validateFlags accepts a declared flag set', () => {
assert.strictEqual(validateFlags(['--env', 'https://x', '--apply'], { known: ['env', 'apply'], needValue: ['env'] }), null);
});
test('validateFlags rejects an unknown flag and names it', () => {
const msg = validateFlags(['--nope', 'x'], { known: ['env'], needValue: ['env'] });
assert.match(msg, /unknown flag\(s\): --nope/);
});
test('validateFlags suggests the near miss that caused the swallowed token', () => {
// The bug this exists for: `--stagee ui` is silently dropped AND eats `ui`.
const msg = validateFlags(['--stagee', 'ui'], { known: ['stage', 'env'], needValue: ['stage'] });
assert.match(msg, /did you mean --stage\?/);
});
test('validateFlags reports an unknown flag BEFORE a value-less one it may have caused', () => {
// `--stagee ui --workspace` leaves --workspace looking bare; naming that would hide the typo.
const msg = validateFlags(['--stagee', 'ui', '--workspace'], { known: ['stage', 'workspace'], needValue: ['stage', 'workspace'] });
assert.match(msg, /unknown flag/);
assert.doesNotMatch(msg, /--workspace requires/);
});
test('validateFlags rejects a bare value-bearing flag', () => {
assert.match(validateFlags(['--stage'], { known: ['stage'], needValue: ['stage'] }), /--stage requires a value/);
});
test('validateFlags rejects --flag= and --flag "" as value-less', () => {
assert.match(validateFlags(['--stage='], { known: ['stage'], needValue: ['stage'] }), /--stage requires a value/);
assert.match(validateFlags(['--stage', ' '], { known: ['stage'], needValue: ['stage'] }), /--stage requires a value/);
});
test('validateFlags leaves a bare BOOLEAN flag alone', () => {
assert.strictEqual(validateFlags(['--apply'], { known: ['apply'], needValue: [] }), null);
});
test('validateFlags treats an absent flag as acceptable — requiredness is the CLI\'s call', () => {
assert.strictEqual(validateFlags([], { known: ['env'], needValue: ['env'] }), null);
});
test('validateFlags catches --__proto__, which never becomes an own property of flags', () => {
// parseArgs accumulates into a plain {}, so Object.keys() cannot see this one.
assert.strictEqual(Object.keys(parseArgs(['--__proto__', 'x']).flags).length, 0);
assert.match(validateFlags(['--__proto__', 'x'], { known: ['env'] }), /unknown flag/);
});
test('validateFlags fails loudly when needValue names a flag missing from known', () => {
// Guards the rename-one-list-not-the-other drift that would silently stop enforcing the value.
assert.throws(() => validateFlags([], { known: ['env'], needValue: ['stage'] }), /needValue but not in known/);
});
test('nearestName prefers an exact case-insensitive match over a near one', () => {
assert.strictEqual(nearestName('STAGE', ['stage', 'stages']), 'stage');
});
test('nearestName declines a suggestion beyond a single edit', () => {
assert.strictEqual(nearestName('stgee', ['stage']), null);
});
// --- End-to-end: the dangerous case this whole change exists for -------------------------------
test('build-model-app rejects a typo\'d phase selector instead of planning every phase', () => {
const existed = fs.existsSync(SAMPLES_WORKSPACE);
const base = ['--env', 'https://contoso.crm.dynamics.com', '--spec', '@' + SPEC, '--no-live-plan', '--workspace', tempWorkspace()];
const good = run([script('build-model-app.js'), ...base, '--stage', 'ui']);
assert.strictEqual(good.code, 0, good.stderr);
const planned = JSON.parse(good.stdout).planItems.length;
const typo = run([script('build-model-app.js'), ...base, '--stagee', 'ui']);
assert.notStrictEqual(typo.code, 0, 'a typo\'d phase selector must not exit 0');
assert.match(typo.stderr, /unknown flag\(s\): --stagee \(did you mean --stage\?\)/);
// The regression being pinned: before this guard the typo planned strictly MORE than --stage ui.
assert.ok(planned > 0 && planned < 9, `--stage ui should plan a subset, planned ${planned}`);
assert.strictEqual(fs.existsSync(SAMPLES_WORKSPACE), existed, 'the run left no workspace in the committed samples folder');
});
test('teardown-model-app rejects an unknown flag rather than ignoring it', () => {
const r = run([script('teardown-model-app.js'), '--env', 'https://contoso.crm.dynamics.com', '--spec', '@' + SPEC, '--allow-destructiv']);
assert.notStrictEqual(r.code, 0);
assert.match(r.stderr, /unknown flag\(s\): --allow-destructiv \(did you mean --allow-destructive\?\)/);
});
test('verify-model-app rejects an unknown flag rather than ignoring it', () => {
const r = run([script('verify-model-app.js'), '--env', 'https://contoso.crm.dynamics.com', '--spec', '@' + SPEC, '--workspce', 'x']);
assert.notStrictEqual(r.code, 0);
assert.match(r.stderr, /unknown flag\(s\): --workspce \(did you mean --workspace\?\)/);
});
// verify-model-app takes the flag the build takes (a build run with it writes its form captions in that language), reads it as the build does,
// and refuses a bad one — all before it reads the spec's environment, so none of these runs touches the network.
test('verify-model-app takes the build\'s --language-code, and refuses one that is no LCID, a pair that disagrees and a typo of it', () => {
const base = [script('verify-model-app.js'), '--env', 'https://contoso.crm.dynamics.com', '--spec', '@' + SPEC];
for (const [what, extra, message] of [
['a language tag', ['--language-code', 'de-DE'], /--language-code \/ --languageCode must be digits only, a positive integer LCID up to 65535 \(got 'de-DE'\)/],
['a pair that disagrees', ['--language-code', '1031', '--languageCode', '1036'], /--language-code '1031' and --languageCode '1036' disagree — pass only one/],
['a flag with no value', ['--languageCode'], /--languageCode/],
['a typo of it', ['--language-cod', '1036'], /unknown flag\(s\): --language-cod/],
]) {
const r = run([...base, ...extra]);
assert.strictEqual(r.code, 1, `${what}: ${r.stderr}`);
assert.match(r.stderr, message, what);
assert.match(r.stderr, /Usage: node verify-model-app\.js .*\[--language-code\|--languageCode <lcid>\]/, `${what}: the usage names the flag`);
}
});
test('a correct invocation still succeeds after the guard', () => {
const existed = fs.existsSync(SAMPLES_WORKSPACE);
const r = run([script('build-model-app.js'), '--env', 'https://contoso.crm.dynamics.com', '--spec', '@' + SPEC, '--no-live-plan', '--stage', 'ui', '--workspace', tempWorkspace()]);
assert.strictEqual(r.code, 0, r.stderr);
assert.strictEqual(JSON.parse(r.stdout).dryRun, true);
assert.strictEqual(fs.existsSync(SAMPLES_WORKSPACE), existed, 'the run left no workspace in the committed samples folder');
});
// --preview-push builds on the live plan of a dry run: with --apply or --no-live-plan it is refused before anything is read.
test('build-model-app refuses --preview-push with --apply or --no-live-plan, with its usage', () => {
for (const extra of [['--apply'], ['--no-live-plan']]) {
const r = run([script('build-model-app.js'), '--env', 'https://contoso.crm.dynamics.com', '--spec', '@' + SPEC, '--preview-push', ...extra, '--workspace', tempWorkspace()]);
assert.strictEqual(r.code, 1, `${extra}: ${r.stderr}`);
assert.match(r.stderr, /--preview-push runs on a dry run with the live plan; drop --apply \/ --no-live-plan/);
assert.match(r.stderr, /Usage: node scripts\/build-model-app\.js .*\[--preview-push\]/);
assert.strictEqual(r.stdout, '', 'no result: nothing ran');
}
});