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
'use strict';
const { test } = require('node:test');
const assert = require('node:assert');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const vm = require('node:vm');
const { provisionEntities } = require(path.join(__dirname, '..', 'provision-entities.js'));
const { loadCli } = require('./helpers/cli-harness.js');
const { validateFlagsFromParsed, realAuth } = require('./helpers/fake-auth.js');
const input = { solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
entities: [{ schemaName: 'cr_candidate', displayName: 'Candidate', pluralName: 'Candidates', primaryAttribute: { schemaName: 'cr_name' },
columns: [{ schemaName: 'cr_status', type: 'Choice', options: ['Applied', 'Hired'] }] }], relationships: [] };
function mockDeps() {
const calls = [];
const sdk = {
queryRecords: async () => [{ solutionid: 's' }], createPublisher: async () => ({ id: 'p' }), createSolution: async () => ({ id: 's' }),
createTable: async (o) => { calls.push('createTable'); return { logicalName: o.schemaName.toLowerCase(), entitySetName: `${o.schemaName.toLowerCase()}s`, metadataId: `tbl-${o.schemaName}` }; },
updateTable: async () => { calls.push('updateTable'); return {}; },
createColumn: async (l, o) => { calls.push('createColumn'); return { logicalName: o.schemaName.toLowerCase(), metadataId: `col-${o.schemaName}` }; },
createGlobalOptionSet: async () => ({ metadataId: 'g' }), insertStatusValue: async () => 1, createAlternateKey: async () => ({}),
createCustomerColumn: async (l, o) => ({ logicalName: o.schemaName.toLowerCase(), metadataId: `col-${o.schemaName}` }),
createRecordsBulk: async (e, rows) => rows.map((_, i) => `${e}-${i}`),
createRelationship: async (o) => ({ schemaName: o.schemaName, metadataId: `rel-${o.schemaName}` }),
};
const provision = { findTables: async () => [], findColumns: async () => [], fetchEntityMetadata: async (l) => ({ logicalName: l, entitySetName: `${l}s`, relationships: [] }), queryRecords: async () => [{ solutionid: 's' }] };
return { sdk, provision, calls };
}
test('dry-run validates + plans, no SDK writes', async () => {
const d = mockDeps();
const r = await provisionEntities(input, { apply: false }, { sdk: d.sdk, provision: d.provision });
assert.strictEqual(r.dryRun, true);
assert.strictEqual(d.calls.length, 0);
});
test('apply provisions and returns resolved names', async () => {
const d = mockDeps();
const r = await provisionEntities(input, { apply: true }, { sdk: d.sdk, provision: d.provision });
assert.strictEqual(r.ok, true);
assert.ok(d.calls.includes('createTable'));
assert.strictEqual(r.entities[0].logicalName, 'cr_candidate');
assert.strictEqual(r.entities[0].entitySetName, 'cr_candidates');
assert.strictEqual(r.entities[0].metadataId, 'tbl-cr_candidate', 'entity metadataId surfaced');
assert.ok(r.columns.length > 0, 'columns returned');
assert.strictEqual(r.columns[0].logicalName, 'cr_status', 'column logicalName is real SDK value');
assert.strictEqual(r.columns[0].metadataId, 'col-cr_status', 'column metadataId surfaced');
});
test('rejects an invalid input before any write', async () => {
const d = mockDeps();
const r = await provisionEntities({ solution: { uniqueName: 'Default' }, entities: [] }, { apply: true }, { sdk: d.sdk, provision: d.provision });
assert.strictEqual(r.ok, false);
assert.strictEqual(d.calls.length, 0);
});
test('quick-create: the enable step is planned AND counted so [n/total] never drifts', async () => {
// The provision-entities CLI has its OWN plan/count separate from sdk-build's planFor; both must
// account for the quick-create step the shared provisionDataModel executes. This CLI input has no
// forms[], so quickCreateEnabledFor fires only on an explicit entities[].quickCreate === true.
const qcInput = {
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
entities: [{ schemaName: 'cr_candidate', displayName: 'Candidate', primaryAttribute: { schemaName: 'cr_name' }, quickCreate: true, columns: [] }],
relationships: [],
};
// dry-run: the plan lists the enable step
const dry = await provisionEntities(qcInput, { apply: false }, mockDeps());
assert.ok(dry.plan.some((p) => /enable quick create on cr_candidate/.test(p)), 'dry-run plan includes the quick-create step');
// apply: capture the [n/total] stream and prove n never exceeds total (count matches the executor)
const d = mockDeps();
const events = [];
await provisionEntities(qcInput, { apply: true }, { sdk: d.sdk, provision: d.provision, emit: (e) => events.push(e) });
assert.ok(d.calls.includes('updateTable'), 'the quick-create updateTable step ran');
const total = events[0] && events[0].total;
assert.ok(events.every((e) => e.n <= e.total), `no step emits n > total (total=${total})`);
assert.ok(events.some((e) => /enable quick create/.test(e.label || '')), 'the quick-create step is narrated');
});
test('no quick-create step is planned/counted without the flag (regression guard)', async () => {
const dry = await provisionEntities(input, { apply: false }, mockDeps());
assert.ok(!dry.plan.some((p) => /enable quick create/.test(p)), 'no quick-create step without the opt-in');
});
test('dry-run plan includes non-trivial provision steps and skips non-actions', async () => {
const rich = {
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
globalChoices: [{ name: 'cr_priority', options: ['Low', 'High'] }],
entities: [
{
schemaName: 'cr_parent',
displayName: 'Parent',
primaryAttribute: { schemaName: 'cr_name' },
columns: [
{ schemaName: 'cr_priority', type: 'Choice', globalChoice: 'cr_priority' },
{ schemaName: 'cr_childid', type: 'Lookup' },
],
statusReasons: [{ label: 'Archived', state: 'Inactive' }],
alternateKeys: [{ schemaName: 'cr_parent_key', columns: ['cr_name'] }],
},
{ schemaName: 'cr_child', displayName: 'Child', primaryAttribute: { schemaName: 'cr_name' }, columns: [] },
],
relationships: [
{ type: 'OneToMany', schemaName: 'cr_parent_children', referenced: 'cr_parent', referencing: 'cr_child', lookup: { schemaName: 'cr_parentid' } },
{ type: 'ManyToMany', schemaName: 'cr_parent_child_nn', entity1: 'cr_parent', entity2: 'cr_child' },
],
sampleData: { cr_parent: [{ cr_name: 'A' }, { cr_name: 'B' }], cr_child: [] },
};
const dry = await provisionEntities(rich, { apply: false, sampleData: true }, mockDeps());
assert.deepStrictEqual(dry.plan, [
'solution Default',
'global choice cr_priority',
'table cr_parent',
'column cr_parent.cr_priority (Choice)',
'status reason cr_parent: Archived',
'alt key cr_parent.cr_parent_key',
'table cr_child',
'relationship 1:N cr_parent->cr_child',
'relationship N:N cr_parent<->cr_child',
'2 record(s) -> cr_parent',
]);
});
test('dry-run plan lists the relationships in the order the apply creates them — a lookup labelled in several languages first', async () => {
// The plan used to follow the declared order while the apply creates a lookup with a language-map label before
// the others (lib/entity-provision.js relationshipsInCreateOrder), so the two disagreed whenever a spec — which
// this entry point accepts as well — gave one of them an LCID map.
const bilingual = {
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
entities: [
{ schemaName: 'cr_parent', displayName: 'Parent', primaryAttribute: { schemaName: 'cr_name' }, columns: [] },
{ schemaName: 'cr_child', displayName: 'Child', primaryAttribute: { schemaName: 'cr_name' }, columns: [] },
{ schemaName: 'cr_other', displayName: 'Other', primaryAttribute: { schemaName: 'cr_name' }, columns: [] },
],
relationships: [
{ type: 'ManyToMany', schemaName: 'cr_parent_child_nn', entity1: 'cr_parent', entity2: 'cr_child' },
{ type: 'OneToMany', schemaName: 'cr_parent_children', referenced: 'cr_parent', referencing: 'cr_child', lookup: { schemaName: 'cr_parentid' } },
{ type: 'OneToMany', schemaName: 'cr_other_children', referenced: 'cr_other', referencing: 'cr_child', lookup: { schemaName: 'cr_otherid', displayName: { 1033: 'Other', 3082: 'Otro' } } },
],
};
const dry = await provisionEntities(bilingual, { apply: false }, mockDeps());
assert.deepStrictEqual(dry.plan.filter((p) => /^relationship /.test(p)), [
'relationship 1:N cr_other->cr_child',
'relationship N:N cr_parent<->cr_child',
'relationship 1:N cr_parent->cr_child',
]);
const d = mockDeps();
const created = [];
d.sdk.createRelationship = async (o) => { created.push(o.schemaName); return { schemaName: o.schemaName, metadataId: `rel-${o.schemaName}` }; };
const applied = await provisionEntities(bilingual, { apply: true }, { sdk: d.sdk, provision: d.provision, provisionedLanguages: [1033, 3082] });
assert.strictEqual(applied.ok, true, JSON.stringify(applied));
assert.deepStrictEqual(created, ['cr_other_children', 'cr_parent_child_nn', 'cr_parent_children'], 'the apply creates them in the order the plan lists');
});
test('a spec with no language-map lookup label keeps the declared order in the plan', async () => {
const plain = {
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
entities: [
{ schemaName: 'cr_parent', displayName: 'Parent', primaryAttribute: { schemaName: 'cr_name' }, columns: [] },
{ schemaName: 'cr_child', displayName: 'Child', primaryAttribute: { schemaName: 'cr_name' }, columns: [] },
],
relationships: [
{ type: 'ManyToMany', schemaName: 'cr_parent_child_nn', entity1: 'cr_parent', entity2: 'cr_child' },
{ type: 'OneToMany', schemaName: 'cr_parent_children', referenced: 'cr_parent', referencing: 'cr_child', lookup: { schemaName: 'cr_parentid' } },
],
};
const dry = await provisionEntities(plain, { apply: false }, mockDeps());
assert.deepStrictEqual(dry.plan.filter((p) => /^relationship /.test(p)), ['relationship N:N cr_parent<->cr_child', 'relationship 1:N cr_parent->cr_child']);
});
test('apply surfaces fallback metadata for existing columns and relationships', async () => {
const existingInput = {
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
entities: [
{ schemaName: 'cr_parent', displayName: 'Parent', primaryAttribute: { schemaName: 'cr_name' }, columns: [{ schemaName: 'cr_code', type: 'Text' }] },
{ schemaName: 'cr_child', displayName: 'Child', primaryAttribute: { schemaName: 'cr_name' }, columns: [] },
],
relationships: [
{ type: 'OneToMany', schemaName: 'cr_parent_children', referenced: 'cr_parent', referencing: 'cr_child', lookup: { schemaName: 'cr_parentid' } },
{ type: 'ManyToMany', schemaName: 'cr_parent_child_nn', entity1: 'cr_parent', entity2: 'cr_child' },
],
};
const d = mockDeps();
d.provision.findTables = async (schemaName) => [{ logicalName: schemaName.toLowerCase(), entitySetName: `${schemaName.toLowerCase()}s` }];
d.provision.findColumns = async () => [{ logicalName: 'cr_code' }];
d.provision.fetchEntityMetadata = async (logical) => ({
logicalName: logical,
entitySetName: `${logical}s`,
// The SDK's projection rows: the other table and its attribute; an existing 1:N is skipped only when they say which one it is.
relationships: [
{ schemaName: 'cr_parent_children', type: 'OneToMany', relatedEntity: 'cr_child', relatedAttribute: 'cr_parentid' },
{ schemaName: 'cr_parent_child_nn', type: 'ManyToMany', relatedEntity: 'cr_child', relatedAttribute: '' },
],
});
const r = await provisionEntities(existingInput, { apply: true }, { sdk: d.sdk, provision: d.provision });
assert.deepStrictEqual(r.columns, [{ table: 'cr_parent', schemaName: 'cr_code', logicalName: 'cr_code' }]);
assert.deepStrictEqual(r.relationships, [
{ kind: '1n', schemaName: 'cr_parent_children' },
// The projection lists the N:N but says nothing of its intersect table, so it cannot show which N:N the name belongs to: the build tries the create,
// as it does for any relationship it could not prove. A real environment answers that create with a collision and the relationship is read then
// (relationship-identity.test.js); this double has no collision, so the create succeeds.
{ kind: 'nn', schemaName: 'cr_parent_child_nn', metadataId: 'rel-cr_parent_child_nn' },
]);
assert.ok(!d.calls.includes('createColumn'), 'existing columns are not recreated');
});
test('apply seeds requested sample data and returns created record ids', async () => {
const seedInput = {
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
entities: [{
schemaName: 'cr_parent',
displayName: 'Parent',
primaryAttribute: { schemaName: 'cr_name' },
columns: [],
alternateKeys: [{ schemaName: 'cr_parent_key', columns: ['cr_name'] }],
}],
relationships: [],
sampleData: { cr_parent: [{ cr_name: 'A' }, { cr_name: 'B' }] },
};
const d = mockDeps();
let seedGroup;
d.sdk.seedRecordGraph = async (groups) => {
[seedGroup] = groups;
return { createdIds: { cr_parent: ['id-a', 'id-b'] } };
};
const r = await provisionEntities(seedInput, { apply: true, sampleData: true }, { sdk: d.sdk, provision: d.provision });
assert.strictEqual(seedGroup.matchOn, 'cr_name', 'a single-column alternate key makes sample seeding idempotent');
assert.deepStrictEqual(r.records, { cr_parent: ['id-a', 'id-b'] });
});
// #447: this CLI is the genpage create flow's data-model path, so it must resolve the organization's
// base language exactly as build-model-app.js does. It builds its own `spec` object from `input`,
// which is easy to leave a field out of -- and because resolveLanguageCode degrades to 1033 rather
// than throwing, a dropped field is invisible until a non-English org fails on a DateTime column.
test('provision-entities resolves the org base language for labels (#447)', async () => {
const d = mockDeps();
d.provision.queryRecords = async (set) => (set === 'organization' ? [{ languagecode: 1031 }] : [{ solutionid: 's' }]);
const seen = [];
d.sdk.createColumn = async (l, o) => { seen.push(o); return { logicalName: o.schemaName.toLowerCase(), metadataId: 'c' }; };
const r = await provisionEntities(input, { apply: true }, { sdk: d.sdk, provision: d.provision });
assert.strictEqual(r.ok, true);
assert.ok(seen.length > 0, 'a column was created');
assert.strictEqual(seen[0].languageCode, 1031, 'the org language reached the column create');
});
test('provision-entities honors an explicit language override and an input languageCode', async () => {
const d = mockDeps();
d.provision.queryRecords = async (set) => (set === 'organization' ? [{ languagecode: 1031 }] : [{ solutionid: 's' }]);
const seen = [];
d.sdk.createColumn = async (l, o) => { seen.push(o); return { logicalName: o.schemaName.toLowerCase(), metadataId: 'c' }; };
await provisionEntities(input, { apply: true, languageCode: 3082 }, { sdk: d.sdk, provision: d.provision });
assert.strictEqual(seen[0].languageCode, 3082, 'opts.languageCode wins over the org base language');
const seen2 = [];
const d2 = mockDeps();
d2.provision.queryRecords = async (set) => (set === 'organization' ? [{ languagecode: 1031 }] : [{ solutionid: 's' }]);
d2.sdk.createColumn = async (l, o) => { seen2.push(o); return { logicalName: o.schemaName.toLowerCase(), metadataId: 'c' }; };
await provisionEntities({ ...input, languageCode: 1036 }, { apply: true }, { sdk: d2.sdk, provision: d2.provision });
assert.strictEqual(seen2[0].languageCode, 1036, 'input.languageCode is carried into the spec, not dropped');
});
test('provision-entities surfaces a language fallback through deps.warn', async () => {
// Without this the genpage path would fall back to 1033 in silence and the user would see only an
// opaque Dataverse 400 later -- the build path would explain itself and this one would not.
const d = mockDeps();
d.provision.queryRecords = async (set) => {
if (set === 'organization') throw new Error('org read blocked');
return [{ solutionid: 's' }];
};
const warnings = [];
const r = await provisionEntities(input, { apply: true }, { sdk: d.sdk, provision: d.provision, warn: (m) => warnings.push(m) });
assert.strictEqual(r.ok, true, 'a failed language read must not fail provisioning');
assert.ok(
warnings.some((w) => /base language/i.test(w) && /--language-code/.test(w)),
'the fallback must reach deps.warn; got: ' + JSON.stringify(warnings)
);
});
// makeSdk/main are otherwise never called: the tests above inject mockDeps into provisionEntities().
// emitResult() ends the process, so a cleanup that runs in a finally the printer never returns to
// leaks both throwaway workspaces. The stub throws where the real printer exits, and only after both
// directories are gone. validateFlagsFromParsed cannot see an unknown flag (parseArgs drops it), so
// the real checker runs first; the rebuilt-argv check still guards the success-path `known` list.
const PROVISION_SCRIPT = path.join(__dirname, '..', 'provision-entities.js');
const PROVISION_ENV = 'https://contoso.crm.dynamics.com';
function cliProvisionInput() {
return {
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
entities: [{
schemaName: 'cr_candidate',
displayName: 'Candidate',
pluralName: 'Candidates',
primaryAttribute: { schemaName: 'cr_name' },
columns: [{ schemaName: 'cr_status', type: 'Choice', options: ['Applied', 'Hired'] }],
}],
relationships: [],
sampleData: { cr_candidate: [{ cr_name: 'Ada' }, { cr_name: 'Grace' }] },
};
}
function provisionHarness({ initThrows = null, constructThrows = null, provisioned = [1033, 3082, 1036], onEmit = null } = {}) {
const d = mockDeps();
const seen = { envs: [], http: [], cols: [], order: [], removed: [], storages: [], solution: undefined, emitted: null };
d.sdk.seedRecordGraph = async () => ({ createdIds: { cr_candidate: ['rec-0', 'rec-1'] } });
d.sdk.createColumn = async (_logical, column) => {
d.calls.push('createColumn');
seen.cols.push(column);
return { logicalName: column.schemaName.toLowerCase(), metadataId: `col-${column.schemaName}` };
};
d.provision.queryRecords = async (set) => (set === 'organization' ? [{ languagecode: 1033 }] : [{ solutionid: 's' }]);
const requires = {
'node:fs': {
mkdtempSync: (prefix) => {
const dir = prefix + String(seen.storages.length);
seen.storages.push(dir);
seen.order.push('mkdtemp');
return dir;
},
rmSync: (dir, opts) => {
seen.order.push('rm');
seen.removed.push({ dir, opts });
},
},
// The throwaway workspaces are made by the plugin's one maker of them (lib/temp-workspace.js), which works on the real file system: the harness stands
// in for it with the very call the script made before it existed, so what this test pins — both are made, and both removed before the result is
// printed — is as it was.
'./lib/temp-workspace.js': { makeTempWorkspace: (prefix) => requires['node:fs'].mkdtempSync(path.join(os.tmpdir(), prefix)) },
'./lib/sdk-http-client.js': {
createAzHttpClient: (env) => {
seen.http.push(env);
return { env };
},
},
'./lib/maker-sdk.js': {
createNodeWorkspaceStorage: (root) => ({ root }),
createMakerSdk: (opts) => {
seen.envs.push(opts.instanceUrl);
if (opts.solutionUniqueName) seen.solution = opts.solutionUniqueName;
if (constructThrows) throw new Error(constructThrows);
const base = opts.solutionUniqueName ? d.sdk : d.provision;
return Object.assign({}, base, {
initWorkspace: async () => { if (initThrows) throw new Error(initThrows); },
});
},
},
'./lib/dataverse-auth.js': {
parseArgs: realAuth.parseArgs,
validateFlags: (passed, contract) => {
const direct = realAuth.validateFlags(passed, contract);
if (direct) return direct;
return validateFlagsFromParsed(() => realAuth.parseArgs(passed).flags)(passed, contract);
},
readAliasedFlag: realAuth.readAliasedFlag,
readJsonArg: realAuth.readJsonArg,
readProvisionedLanguages: async () => provisioned,
emitResult: (ok, payload) => {
seen.order.push('emit');
if (seen.removed.length < 2) throw new Error(`emit before workspace cleanup (removed ${seen.removed.length})`);
seen.emitted = { ok, payload };
if (onEmit) return onEmit(ok, payload);
const err = new Error(`process.exit(${ok ? 0 : 1})`);
err.exitCode = ok ? 0 : 1;
throw err;
},
},
};
return { d, seen, requires };
}
function assertCleanedBeforeEmit(seen) {
assert.deepStrictEqual(seen.removed.map((r) => r.dir), seen.storages, 'both workspaces makeSdk created are the ones removed');
assert.ok(seen.removed.every((r) => r.opts && r.opts.recursive === true && r.opts.force === true));
const emitAt = seen.order.lastIndexOf('emit');
assert.ok(emitAt > 1, 'emit ran');
assert.ok(seen.order.slice(0, emitAt).filter((step) => step === 'rm').length >= 2, 'cleanup ran before the emitter exited: ' + seen.order.join(','));
}
test('CLI provisions the requested input and cleans its workspace before emit', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'provision-cli-'));
const inputFile = path.join(dir, 'input.json');
fs.writeFileSync(inputFile, JSON.stringify(cliProvisionInput()));
const inputArg = '@' + inputFile;
async function viaLoadCli(argv, opts) {
const h = provisionHarness(opts);
const cli = loadCli(PROVISION_SCRIPT, { argv, requires: h.requires });
let exitErr = null;
try { await cli.main(); } catch (err) { exitErr = err; }
return { ...h, exitErr, stderr: cli.stderrText(), stdout: cli.stdoutText() };
}
// The entry `main().catch(emitResult)` only arms when the file is the process main. loadCli cannot
// set that, so thrown init/provision failures are driven through the same compile with require.main
// pointed at the module — otherwise a deleted catch would still look green.
function viaEntry(argv, opts) {
let finish;
const done = new Promise((resolve) => { finish = resolve; });
const h = provisionHarness({ ...opts, onEmit: () => finish() });
const source = fs.readFileSync(PROVISION_SCRIPT, 'utf8');
const mod = { exports: {} };
const stderr = [];
const customRequire = (id) => {
if (Object.prototype.hasOwnProperty.call(h.requires, id)) return h.requires[id];
const noExt = id.replace(/\.js$/, '');
if (Object.prototype.hasOwnProperty.call(h.requires, noExt)) return h.requires[noExt];
if (id.startsWith('.')) return require(path.resolve(path.dirname(PROVISION_SCRIPT), id));
return require(id);
};
customRequire.main = mod;
const sandboxProcess = {
argv: ['node', PROVISION_SCRIPT, ...argv],
env: {},
platform: process.platform,
execPath: process.execPath,
exit: (code) => { h.seen.exitCode = code; finish(); },
stdout: { write: () => true },
stderr: { write: (s) => { stderr.push(String(s)); return true; } },
};
const fn = vm.compileFunction(source, ['require', 'module', 'exports', 'process'], { filename: PROVISION_SCRIPT });
fn(customRequire, mod, mod.exports, sandboxProcess);
return done.then(() => ({ ...h, stderr: stderr.join('') }));
}
try {
const dry = await viaLoadCli(['--env', PROVISION_ENV, '--input', inputArg, '--sample-data']);
assert.strictEqual(dry.exitErr && dry.exitErr.exitCode, 0);
assert.strictEqual(dry.seen.emitted.ok, true);
assert.strictEqual(dry.seen.emitted.payload.dryRun, true);
assert.ok(dry.seen.emitted.payload.plan.includes('table cr_candidate'));
assert.ok(dry.seen.emitted.payload.plan.includes('2 record(s) -> cr_candidate'));
assert.deepStrictEqual(dry.d.calls, [], 'dry-run must not write');
assert.ok(dry.seen.envs.every((env) => env === PROVISION_ENV));
assert.deepStrictEqual(dry.seen.http, [PROVISION_ENV]);
assertCleanedBeforeEmit(dry.seen);
const applied = await viaLoadCli(['--env', PROVISION_ENV, '--input', inputArg, '--apply', '--sample-data', '--language-code', '3082']);
assert.strictEqual(applied.exitErr && applied.exitErr.exitCode, 0, applied.stderr);
const body = applied.seen.emitted.payload;
assert.strictEqual(body.ok, true);
assert.strictEqual(body.entities[0].logicalName, 'cr_candidate');
assert.strictEqual(body.entities[0].entitySetName, 'cr_candidates');
assert.strictEqual(body.entities[0].metadataId, 'tbl-cr_candidate');
assert.strictEqual(body.columns[0].logicalName, 'cr_status');
assert.strictEqual(body.columns[0].metadataId, 'col-cr_status');
assert.deepStrictEqual(body.records, { cr_candidate: ['rec-0', 'rec-1'] });
assert.strictEqual(applied.seen.cols[0].languageCode, 3082, '--language-code must reach the column create');
assert.strictEqual(applied.seen.solution, 'Default');
assert.ok(applied.d.calls.includes('createTable'));
assert.ok(applied.seen.envs.every((env) => env === PROVISION_ENV));
assertCleanedBeforeEmit(applied.seen);
const camel = await viaLoadCli(['--env', PROVISION_ENV, '--input', inputArg, '--apply', '--languageCode', '1036']);
assert.strictEqual(camel.exitErr && camel.exitErr.exitCode, 0, camel.stderr);
assert.strictEqual(camel.seen.cols[0].languageCode, 1036, '--languageCode is the same flag, not a dropped alias');
const badInput = path.join(dir, 'bad.json');
fs.writeFileSync(badInput, JSON.stringify({ solution: { uniqueName: 'Default' }, entities: [], relationships: [] }));
const refused = await viaLoadCli(['--env', PROVISION_ENV, '--input', '@' + badInput, '--apply']);
assert.strictEqual(refused.exitErr && refused.exitErr.exitCode, 1);
assert.strictEqual(refused.seen.emitted.ok, false);
assert.ok(refused.seen.emitted.payload.errors.some((e) => /publisherPrefix/i.test(e)));
assert.deepStrictEqual(refused.d.calls, [], 'a refused input must not write');
assertCleanedBeforeEmit(refused.seen);
const badLcid = await viaLoadCli(['--env', PROVISION_ENV, '--input', inputArg, '--language-code', '1O33']);
assert.strictEqual(badLcid.exitErr && badLcid.exitErr.exitCode, 1);
assert.match(badLcid.stderr, /--language-code \/ --languageCode must be digits only/);
assert.match(badLcid.stderr, /got '1O33'/);
assert.deepStrictEqual(badLcid.seen.storages, [], 'an invalid LCID exits before makeSdk');
assert.strictEqual(badLcid.seen.emitted, null);
const unknown = await viaLoadCli(['--env', PROVISION_ENV, '--bogus', 'x', '--input', inputArg]);
assert.strictEqual(unknown.exitErr && unknown.exitErr.exitCode, 1);
assert.match(unknown.stderr, /unknown flag\(s\): --bogus/);
assert.deepStrictEqual(unknown.seen.storages, []);
assert.deepStrictEqual(unknown.d.calls, []);
const initFailed = await viaEntry(['--env', PROVISION_ENV, '--input', inputArg, '--apply'], { initThrows: 'workspace init failed' });
assert.strictEqual(initFailed.seen.emitted.ok, false);
assert.match(initFailed.seen.emitted.payload.message, /workspace init failed/);
assert.deepStrictEqual(initFailed.d.calls, []);
assert.ok(initFailed.seen.envs.includes(PROVISION_ENV));
assertCleanedBeforeEmit(initFailed.seen);
const constructFailed = await viaEntry(['--env', PROVISION_ENV, '--input', inputArg, '--apply'], { constructThrows: 'bundle missing' });
assert.strictEqual(constructFailed.seen.emitted.ok, false);
assert.match(constructFailed.seen.emitted.payload.message, /bundle missing/);
assert.deepStrictEqual(constructFailed.d.calls, []);
assertCleanedBeforeEmit(constructFailed.seen);
const unprovisioned = await viaEntry(
['--env', PROVISION_ENV, '--input', inputArg, '--apply', '--language-code', '3082'],
{ provisioned: [1033] }
);
assert.strictEqual(unprovisioned.seen.emitted.ok, false);
assert.match(unprovisioned.seen.emitted.payload.message, /3082 is not provisioned/);
assert.ok(!unprovisioned.d.calls.includes('createTable'), 'an unprovisioned language must not write tables');
assertCleanedBeforeEmit(unprovisioned.seen);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
// --- primaryAttribute.maxLength through this entry point -----------------------------------------------------------
// This is the entry point of /genpage and /app-builder: it writes the publisher and the solution BEFORE its first table, and
// puts the name column's length in both table creates as it is written. The input gate is the only thing between a value the
// platform refuses (or a string, which would be sent as a string) and those writes.
function withPrimaryAttribute(primaryAttribute, columns) {
return { ...input, entities: [{ ...input.entities[0], primaryAttribute, ...(columns ? { columns } : {}) }] };
}
test('an invalid primaryAttribute.maxLength is refused before anything is called — no publisher, no solution, no table', async () => {
for (const maxLength of ['120', 5000, 851, 0, 1.5, -3, null]) {
const d = mockDeps();
const touched = [];
const watch = (target) => new Proxy(target, { get: (t, key) => (typeof t[key] === 'function' ? (...args) => { touched.push(String(key)); return t[key](...args); } : t[key]) });
const r = await provisionEntities(withPrimaryAttribute({ schemaName: 'cr_name', maxLength }), { apply: true }, { sdk: watch(d.sdk), provision: watch(d.provision) });
assert.strictEqual(r.ok, false, `${JSON.stringify(maxLength)} must be refused`);
assert.ok(r.errors.some((e) => /^entity 'cr_candidate': primaryAttribute\.maxLength must be a whole number from 1 to 850 \(got /.test(e)), JSON.stringify(r.errors));
assert.deepStrictEqual(touched, [], `${JSON.stringify(maxLength)}: nothing was called`);
}
});
test('the name column asks for the length given (1 to 850), else 100 — auto-numbered or not — in the table create', async () => {
const askedBy = async (primaryAttribute) => {
const d = mockDeps();
const creates = [];
d.sdk.createTable = async (o) => { creates.push(o); return { logicalName: o.schemaName.toLowerCase(), entitySetName: `${o.schemaName.toLowerCase()}s`, metadataId: 'tbl' }; };
// Reads the length back as asked, so a given length needs no update.
d.provision.dataverse = { get: async (url) => (/StringAttributeMetadata/.test(url) ? { status: 200, body: { MaxLength: primaryAttribute.maxLength } } : { status: 404, body: null }) };
const r = await provisionEntities(withPrimaryAttribute(primaryAttribute), { apply: true }, { sdk: d.sdk, provision: d.provision });
assert.strictEqual(r.ok, true, JSON.stringify(r));
return creates[0].primaryColumnMaxLength;
};
// 100 is what the previous release always sent, for a plain and an auto-numbered name column alike.
assert.strictEqual(await askedBy({ schemaName: 'cr_name' }), 100);
assert.strictEqual(await askedBy({ schemaName: 'cr_name', autoNumberFormat: 'CAND-{SEQNUM:4}' }), 100);
assert.strictEqual(await askedBy({ schemaName: 'cr_name', maxLength: 1 }), 1);
assert.strictEqual(await askedBy({ schemaName: 'cr_name', maxLength: 850 }), 850);
assert.strictEqual(await askedBy({ schemaName: 'cr_name', maxLength: 120, autoNumberFormat: 'CAND-{SEQNUM:4}' }), 120);
});
test('primary column length: the step is planned AND counted, so [n/total] never drifts, whether it updates, finds the length right, or fails', async () => {
const sized = withPrimaryAttribute({ schemaName: 'cr_name', maxLength: 120 });
const dry = await provisionEntities(sized, { apply: false }, mockDeps());
// Listed where the build runs it: last in the data model, after the relationships — the length is set once nothing labelled is left to create.
assert.deepStrictEqual(dry.plan, ['solution Default', 'table cr_candidate', 'column cr_candidate.cr_status (Choice)', 'primary column length cr_candidate.cr_name']);
const related = {
...sized,
entities: [...sized.entities, { schemaName: 'cr_note', displayName: 'Note', primaryAttribute: { schemaName: 'cr_name' }, columns: [] }],
relationships: [{ type: 'OneToMany', schemaName: 'cr_candidate_notes', referenced: 'cr_candidate', referencing: 'cr_note', lookup: { schemaName: 'cr_candidateid' } }],
};
assert.deepStrictEqual((await provisionEntities(related, { apply: false }, mockDeps())).plan, [
'solution Default', 'table cr_candidate', 'column cr_candidate.cr_status (Choice)', 'table cr_note', 'relationship 1:N cr_candidate->cr_note', 'primary column length cr_candidate.cr_name',
]);
const plain = await provisionEntities(input, { apply: false }, mockDeps());
assert.ok(!plain.plan.some((p) => /primary column length/.test(p)), 'no step when the input gives no length');
for (const [what, read, updateFails] of [['updated', 850, false], ['already right', 120, false], ['update fails', 850, true]]) {
const d = mockDeps();
const updates = [];
d.provision.dataverse = { get: async (url) => (/StringAttributeMetadata/.test(url) ? { status: 200, body: { MaxLength: read } } : { status: 404, body: null }) };
d.sdk.updateColumn = async (table, column, opts) => { updates.push([table, column, opts]); if (updateFails) throw new Error('refused'); return {}; };
const events = [];
const r = await provisionEntities(related, { apply: true }, { sdk: d.sdk, provision: d.provision, emit: (e) => events.push(e), warn: () => {} });
assert.strictEqual(r.ok, true, what);
const done = events.filter((e) => e.status !== 'start');
assert.ok(events.every((e) => e.n <= e.total), `${what}: no step emits n > total`);
assert.strictEqual(done[done.length - 1].n, done[0].total, `${what}: the last step is the total`);
assert.strictEqual(done.filter((e) => /^primary column length /.test(e.label)).length, 1, `${what}: the step is narrated once`);
assert.match(done[done.length - 1].label, /^primary column length cr_candidate\.cr_name/, `${what}: and it is the last step, after the relationship`);
assert.ok(done.findIndex((e) => /^relationship /.test(e.label)) < done.length - 1, `${what}: the relationship came before it`);
assert.deepStrictEqual(updates, read === 120 ? [] : [['cr_candidate', 'cr_name', { maxLength: 120 }]], what);
}
});
test('a key over the name column of a table whose length is set waits for it: the plan lists it after the length step, the apply makes it there, and n never passes total', async () => {
const keyed = {
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
entities: [{
schemaName: 'cr_candidate', displayName: 'Candidate', primaryAttribute: { schemaName: 'cr_name', maxLength: 120 }, columns: [{ schemaName: 'cr_code', type: 'Text' }],
alternateKeys: [{ schemaName: 'cr_code_key', columns: ['cr_code'] }, { schemaName: 'cr_name_key', columns: ['cr_name'] }],
}],
relationships: [],
};
const dry = await provisionEntities(keyed, { apply: false }, mockDeps());
assert.deepStrictEqual(dry.plan, [
'solution Default', 'table cr_candidate', 'column cr_candidate.cr_code (Text)', 'alt key cr_candidate.cr_code_key', 'primary column length cr_candidate.cr_name', 'alt key cr_candidate.cr_name_key',
]);
const d = mockDeps();
const order = [];
d.provision.dataverse = { get: async (url) => (/StringAttributeMetadata/.test(url) ? { status: 200, body: { MaxLength: 850 } } : { status: 404, body: null }) };
d.sdk.updateColumn = async () => { order.push('updateColumn'); return {}; };
d.sdk.createAlternateKey = async (table, options) => { order.push(`createAlternateKey ${options.schemaName}`); return {}; };
const events = [];
const r = await provisionEntities(keyed, { apply: true }, { sdk: d.sdk, provision: d.provision, emit: (e) => events.push(e), warn: () => {} });
assert.strictEqual(r.ok, true, JSON.stringify(r));
assert.deepStrictEqual(order, ['createAlternateKey cr_code_key', 'updateColumn', 'createAlternateKey cr_name_key'], 'the key over the name column is made once its length is set');
const done = events.filter((e) => e.status !== 'start');
assert.ok(events.every((e) => e.n <= e.total), 'no step emits n > total');
assert.strictEqual(done[done.length - 1].n, done[0].total, 'the last step is the total');
assert.strictEqual(done[done.length - 1].label, 'alt key cr_candidate.cr_name_key');
});
test('a key over the name column whose label goes out in one language waits like a string, whichever language that is; one labelled in several languages keeps its place', async () => {
const keyed = (displayName, languageCode) => ({
solution: { uniqueName: 'Default', publisherPrefix: 'cr' },
...(languageCode ? { languageCode } : {}),
entities: [{
schemaName: 'cr_candidate', displayName: 'Candidate', primaryAttribute: { schemaName: 'cr_name', maxLength: 120 }, columns: [],
alternateKeys: [{ schemaName: 'cr_name_key', columns: ['cr_name'], displayName }],
}],
relationships: [],
});
const waits = ['solution Default', 'table cr_candidate', 'primary column length cr_candidate.cr_name', 'alt key cr_candidate.cr_name_key'];
const keeps = ['solution Default', 'table cr_candidate', 'alt key cr_candidate.cr_name_key', 'primary column length cr_candidate.cr_name'];
const rows = [
['a string', keyed('Name Key'), {}, waits],
['a map of one language where no language is named', keyed({ 1033: 'Name Key' }), {}, waits],
['a map of the spec\'s language', keyed({ 3082: 'Clave' }, 3082), {}, waits],
['a map of the language the command line names', keyed({ 3082: 'Clave' }), { languageCode: 3082 }, waits],
['a map of another language than the spec\'s: it is one label all the same', keyed({ 1033: 'Name Key' }, 3082), {}, waits],
['a map of another language than the command line\'s', keyed({ 3082: 'Clave' }, 3082), { languageCode: 1033 }, waits],
['a map of the base language under another language on the command line', keyed({ 1033: 'Name Key' }), { languageCode: 3082 }, waits],
['a map of several languages', keyed({ 1033: 'Name Key', 3082: 'Clave' }, 1033), {}, keeps],
['a map of several languages, whatever the command line names', keyed({ 1033: 'Name Key', 3082: 'Clave' }, 1033), { languageCode: 3082 }, keeps],
];
for (const [what, spec, opts, expected] of rows) {
const dry = await provisionEntities(spec, { apply: false, ...opts }, mockDeps());
assert.deepStrictEqual(dry.plan, expected, what);
}
// The apply orders each of them the way the plan does: the length is set and then the key is made — or, for a label in several languages, the key and then the length.
for (const [what, spec, opts, expected] of rows) {
const d = mockDeps();
const order = [];
d.provision.dataverse = { get: async (url) => (/StringAttributeMetadata/.test(url) ? { status: 200, body: { MaxLength: 850 } } : { status: 404, body: null }) };
d.sdk.updateColumn = async () => { order.push('updateColumn'); return {}; };
d.sdk.createAlternateKey = async (table, options) => { order.push(`createAlternateKey ${options.schemaName}`); return {}; };
const r = await provisionEntities(spec, { apply: true, ...opts }, { sdk: d.sdk, provision: d.provision, emit: () => {}, warn: () => {} });
assert.strictEqual(r.ok, true, `${what}: ${JSON.stringify(r)}`);
assert.deepStrictEqual(order, expected === waits ? ['updateColumn', 'createAlternateKey cr_name_key'] : ['createAlternateKey cr_name_key', 'updateColumn'], what);
}
});