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.

13.0 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');
// An apply that names no app folder and no workspace gets an app folder of its own: it holds its build lease in its workspace (helpers/own-app-folder.js).
const { inOwnFolder } = require('./helpers/own-app-folder.js');
const { runSdkBuild: runSdkBuildInPlace, appDef } = require('../lib/sdk-build.js');
const runSdkBuild = inOwnFolder(runSdkBuildInPlace);
const { membershipSpec, membershipSdk, FORM_A, FORM_B, DASHBOARD, META, APP } = require('./helpers/app-membership-sdk.js');
const { fakeGenpage } = require('./helpers/fake-genpage.js');

test('appDef carries resolved Main ids and excludes the conflicting created Main pin by id', () => {
  const result = { forms: { contoso_item: FORM_B, contact: DASHBOARD }, views: {}, charts: {} };
  const definition = appDef(membershipSpec(), result, { mainFormsByTable: { contoso_item: [FORM_A] } });
  assert.deepEqual(definition.components.mainFormsByTable, { contoso_item: [FORM_A] });
  assert.deepEqual(definition.components.forms, [DASHBOARD]);
  result.forms.contoso_item = FORM_A;
  assert.deepEqual(appDef(membershipSpec(), result, { mainFormsByTable: { contoso_item: [FORM_A] } }).components.forms, [FORM_A, DASHBOARD]);
});

for (const publish of [false, true]) {
  test(`fresh create pins after identity and republishes its new layer (publish=${publish})`, async () => {
    const fake = membershipSdk();
    const result = await runSdkBuild(membershipSpec(), { sdk: fake.sdk, apply: true, phases: ['app-shell', 'publish'], publish });
    assert.equal(result.created.app, APP);
    assert.deepEqual(fake.pushes[0].components.mainFormsByTable, { contoso_item: [FORM_A] });
    const firstPush = fake.events.findIndex(([event]) => event === 'push');
    const pin = fake.events.findIndex(([event, action]) => event === 'post' && action === '/AddAppComponents');
    // The PublishXml that names the app: with --publish the build first publishes the tables it changed, those whose Main forms its own write of the app adds,
    // and those the app is proven to hold forms or charts of.
    const republish = fake.events.findIndex(([event, action, body]) => event === 'post' && action === '/PublishXml' && /<appmodule>/.test(body.ParameterXml));
    assert.ok(pin > firstPush, JSON.stringify(fake.events));
    assert.ok(republish > pin, 'a fresh create already published before pinning; its hidden tables must not be left pending');
    // This build changes no table and the app does not exist yet, so with --publish the one table it publishes before the app is created is the navigation's:
    // the SDK adds its active Main forms when it creates the app. The table the spec only pins in `app.tables` is a table component, which the SDK expands no
    // form of, so it is not named. Without --publish nothing is published ahead of the app.
    const tableEvents = fake.events.filter(([event, action, body]) => event === 'post' && action === '/PublishXml' && !/<appmodule>/.test(body.ParameterXml));
    assert.deepEqual(tableEvents.map(([, , body]) => body.ParameterXml), publish ? ['<importexportxml><entities><entity>contoso_item</entity></entities></importexportxml>'] : []);
    if (publish) {
      assert.ok(fake.events.indexOf(tableEvents[0]) < fake.events.findIndex(([event]) => event === 'create'), 'and it is published before the app is created');
    }
    assert.equal(fake.pending, false);
    assert.equal(fake.events.filter(([event, url]) => event === 'get' && url.includes("objecttypecode eq 'contoso_item'")).length, 1,
      'resolve the directive once per run, even across a fetch and finalizer push');
    assert.ok(fake.tableIds.includes(META.task));
  });
}

test('omitted membership directives cost no component reads or extra publish', async () => {
  const fake = membershipSdk();
  await runSdkBuild(membershipSpec({ hidden: false, mainForms: false }), { sdk: fake.sdk, apply: true, phases: ['app-shell'] });
  assert.ok(!fake.events.some(([event]) => event === 'get' || event === 'post'));
  assert.equal(fake.pushes[0].components.mainFormsByTable, undefined);
});

for (const livePlan of [false, true]) {
  test(`the ${livePlan ? 'live' : 'offline'} dry run lists each exact Main-form removal, with its scope and the app publish it owes`, async () => {
    const spec = membershipSpec({ hidden: false });
    spec.app.mainFormsOnUpdate = 'exact';
    spec.minimumPluginVersion = '2.21.0';
    const fake = membershipSdk({ existing: true });
    const result = await runSdkBuild(spec, { sdk: fake.sdk, apply: false, phases: ['app-shell'], livePlan, emit: () => {} });
    const removals = result.planItems.filter((item) => item.op === 'app-main-forms-removal');
    assert.equal(removals.length, 1, JSON.stringify(result.planItems));
    assert.equal(removals[0].phase, 'app-shell');
    assert.equal(removals[0].destructive, true);
    assert.match(removals[0].description, /removes other Main forms of contoso_item from the app's membership; no form is deleted/);
    assert.match(removals[0].label, /--allow-destructive/);
    assert.deepEqual(removals[0].publication, { kind: 'app', app: 'contoso_membership' });
    assert.ok(!fake.events.some(([event]) => ['post', 'create', 'push', 'publish'].includes(event)), 'a dry run writes nothing');
  });
}

test('the live dry run of a new app lists the exact Main forms without counting a removal', async () => {
  const spec = membershipSpec({ hidden: false });
  spec.app.mainFormsOnUpdate = 'exact';
  spec.minimumPluginVersion = '2.21.0';
  const fake = membershipSdk({ existing: false });
  const result = await runSdkBuild(spec, { sdk: fake.sdk, apply: false, phases: ['app-shell'], livePlan: true, emit: () => {} });
  const removal = result.planItems.find((item) => item.op === 'app-main-forms-removal');
  assert.ok(removal, JSON.stringify(result.planItems));
  assert.equal(removal.destructive, false);
  assert.match(removal.label, /the app is new, so no Main form is taken out/);
});

test('a dry run without exact Main forms lists no removal and keeps its plan items unchanged', async () => {
  for (const livePlan of [false, true]) {
    const fake = membershipSdk({ existing: true });
    const result = await runSdkBuild(membershipSpec({ hidden: false }), { sdk: fake.sdk, apply: false, phases: ['app-shell'], livePlan, emit: () => {} });
    assert.ok(result.planItems.length > 0);
    for (const item of result.planItems) {
      for (const key of ['op', 'destructive', 'description', 'publication']) assert.ok(!(key in item), `${key} on ${item.label}`);
    }
  }
});

test('a navigation table redundantly listed in app.tables is proven without another pin or publish', async () => {
  const fake = membershipSdk();
  const spec = membershipSpec({ mainForms: false });
  spec.app.tables = ['CONTOSO_ITEM'];
  await runSdkBuild(spec, { sdk: fake.sdk, apply: true, phases: ['app-shell'] });
  assert.ok(!fake.events.some(([event]) => event === 'post'));
});

test('unresolved hidden tables and Main names halt before creating or pushing the app', async () => {
  for (const fake of [membershipSdk({ malformedTable: true }), membershipSdk({ forms: [] })]) {
    await assert.rejects(runSdkBuild(membershipSpec(), { sdk: fake.sdk, apply: true, phases: ['app-shell'] }), /task|Summary/);
    assert.ok(!fake.events.some(([event]) => event === 'create' || event === 'push' || event === 'post'));
  }
});

test('a pin proof failure halts rather than reporting a completed app', async () => {
  const fake = membershipSdk({ dropPin: true });
  await assert.rejects(runSdkBuild(membershipSpec(), { sdk: fake.sdk, apply: true, phases: ['app-shell'] }), /task.*missing/i);
});

// componenttype 80 is appmodule, 62 sitemap:
// https://learn.microsoft.com/power-apps/developer/data-platform/reference/entities/solutioncomponent
const APP_MODULE = 80;
const SITEMAP_COMPONENT = 62;

test('a fresh app joins its solution before the hidden-table pin can fail', async () => {
  const fake = membershipSdk({ dropPin: true });
  const joins = [];
  fake.sdk.addSolutionComponent = async (o) => { joins.push(o.componentType); fake.events.push(['join', o.componentType, o.componentId]); };
  await assert.rejects(runSdkBuild(membershipSpec(), { sdk: fake.sdk, apply: true, phases: ['app-shell'] }), /task.*missing/i);
  const join = fake.events.findIndex(([event, type, id]) => event === 'join' && type === APP_MODULE && id === APP);
  const pin = fake.events.findIndex(([event, action]) => event === 'post' && action === '/AddAppComponents');
  assert.ok(join >= 0 && join < pin, `the app module joined before the pin: ${JSON.stringify(fake.events)}`);
  assert.ok(joins.includes(SITEMAP_COMPONENT), 'and its sitemap with it');
});

test('a re-run that reuses the app re-asserts its solution membership', async () => {
  const fake = membershipSdk({ existing: true });
  const joins = [];
  fake.sdk.addSolutionComponent = async (o) => { joins.push([o.componentType, o.componentId, o.solutionUniqueName]); };
  await runSdkBuild(membershipSpec(), { sdk: fake.sdk, apply: true, phases: ['app-shell'] });
  const sol = membershipSpec().solution.uniqueName;
  assert.ok(joins.some(([type, id, s]) => type === APP_MODULE && id === APP && s === sol), JSON.stringify(joins));
});

test('existing page-less app-shell push re-supplies the map after fetch and preserves other components', async () => {
  const fake = membershipSdk({ existing: true });
  const get = fake.sdk.getArtifact;
  let initial = true;
  fake.sdk.getArtifact = async (...args) => {
    const art = await get(...args);
    if (initial) {
      initial = false;
      await fake.sdk.addElement('app', APP, '', { components: { forms: [FORM_B, DASHBOARD], views: ['view-kept'], charts: ['chart-kept'], dashboards: [DASHBOARD] } });
      return await fake.sdk.getArtifact(...args);
    }
    return art;
  };
  await runSdkBuild(membershipSpec({ aiDescription: 'New routing' }), { sdk: fake.sdk, apply: true, phases: ['app-shell'] });
  assert.deepEqual(fake.pushes[0].components.mainFormsByTable, { contoso_item: [FORM_A] });
  assert.deepEqual(fake.pushes[0].components.forms, [DASHBOARD], 'filter Main ids, not same-named dashboard/QuickView components');
  assert.deepEqual(fake.pushes[0].components.views, ['view-kept']);
  assert.deepEqual(fake.pushes[0].components.charts, ['chart-kept']);
  const pin = fake.events.findIndex(([event, action]) => event === 'post' && action === '/AddAppComponents');
  const push = fake.events.findIndex(([event]) => event === 'push');
  const pub = fake.events.findIndex(([event]) => event === 'publish');
  assert.ok(pin >= 0 && pin < push && push < pub, JSON.stringify(fake.events));
});

test('existing page-backed routing-description push re-supplies the allow-list without rewriting navigation', async () => {
  const fake = membershipSdk({ existing: true, page: true });
  await runSdkBuild(membershipSpec({ page: true, aiDescription: 'New routing' }), { sdk: fake.sdk, apply: true, phases: ['app-shell'] });
  assert.equal(fake.pushes.length, 1);
  assert.deepEqual(fake.pushes[0].components.mainFormsByTable, { contoso_item: [FORM_A] });
  assert.ok(fake.pushes[0].siteMap.areas[0].groups[0].subAreas.some((sub) => sub.type === 'GenPage'));
  assert.equal(fake.pending, false);
});

for (const publish of [false, true]) {
  test(`existing app pins independently of app-shell push and finalizer keeps the directive (publish=${publish})`, async (t) => {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'app-membership-build-'));
    t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
    const fake = membershipSdk({ existing: true });
    await runSdkBuild(membershipSpec(), {
      sdk: fake.sdk, apply: true, phases: ['app-shell', 'pages', 'publish'], publish, appDir: dir,
      genpage: fakeGenpage(),
    });
    assert.equal(fake.pushes.length, 1, 'existing-app app-shell defers its push to the pages finalizer');
    assert.deepEqual(fake.pushes[0].components.mainFormsByTable, { contoso_item: [FORM_A] });
    const pin = fake.events.findIndex(([event, action]) => event === 'post' && action === '/AddAppComponents');
    const push = fake.events.findIndex(([event]) => event === 'push');
    assert.ok(pin >= 0 && pin < push, JSON.stringify(fake.events));
    assert.equal(fake.pending, false);
  });
}

test('SDK additive-membership warnings name the extra Main forms and the Maker remedy', async () => {
  const warnings = [];
  const fake = membershipSdk({
    existing: true,
    warnings: [`Main-form components outside the allow-list remain for table 'contoso_item': [${FORM_B}]. This option controls additions only; it does not remove existing components. Runtime form availability has not been verified.`],
  });
  await runSdkBuild(membershipSpec({ hidden: false }), { sdk: fake.sdk, apply: true, phases: ['app-shell'], warn: (line) => warnings.push(line) });
  assert.ok(warnings.some((line) => /already offers Main form.*Alternate.*contoso_item/.test(line)), warnings.join('; '));
  assert.ok(warnings.some((line) => /cannot remove.*Maker.*Forms/.test(line)), warnings.join('; '));
});