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.1 KB
Raw Blame
'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { normalizeCatalogFamilies } = require('../lib/template-catalog');

const {
  parseArgs,
  renderTemplateBrowser,
  validateRenderedTemplateBrowser,
} = require('../render-template-browser');

function tempDir() {
  return fs.mkdtempSync(path.join(os.tmpdir(), 'template-browser-test-'));
}

for (const emptyFamily of [true, false]) {
  test(`nested SPA browser honors variant preview overrides with ${emptyFamily ? 'empty' : 'nonempty'} family previews`, (t) => {
    const dir = tempDir();
    t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
    const pair = (name) => [{
      id: name, displayName: `${name} page`,
      desktop: `https://example.test/${name}-wide.png`,
      mobile: `https://example.test/${name}-narrow.png`,
    }];
    const template = {
      id: 'supplier-portal', displayName: 'Supplier Portal', kind: 'spa',
      description: 'Supplier invoice portal', keywords: [],
      responsivePreviews: emptyFamily ? [] : pair('family'),
      previewImages: ['https://example.test/ignored-family.png'],
      variants: {
        react: {},
        vue: { responsivePreviews: pair('vue'), previewImages: ['https://example.test/ignored-vue.png'] },
        angular: { previewImages: ['https://example.test/angular-legacy.png'] },
        astro: emptyFamily ? { responsivePreviews: [] } : { previewImages: [] },
      },
    };
    for (const [shape, family] of [
      ['raw', template],
      ['normalized', normalizeCatalogFamilies({ templates: [template] })[0]],
    ]) {
      const templatesJsonPath = path.join(dir, `${shape}.json`);
      const outputPath = path.join(dir, `${shape}.html`);
      fs.writeFileSync(templatesJsonPath, JSON.stringify({ TEMPLATES_JSON: [family] }));
      assert.equal(renderTemplateBrowser({ templatesJsonPath, outputPath }).status, 'ok');
      const html = fs.readFileSync(outputPath, 'utf8');
      assert.match(html, /vue-wide\.png/);
      assert.match(html, /vue-narrow\.png/);
      assert.match(html, /angular-legacy\.png/);
      const groups = html.split('<div class="variant-previews">').slice(1);
      assert.equal(groups.length, 4);
      const [react, vue, angular, astro] = groups;
      assert.match(react, /class="field-label">React</);
      assert.match(vue, /class="field-label">Vue</);
      assert.match(vue, /vue page - desktop/);
      assert.match(vue, /vue-wide\.png/);
      assert.match(vue, /vue-narrow\.png/);
      assert.doesNotMatch(vue, /family-wide\.png|ignored-vue/);
      assert.match(angular, /class="field-label">Angular</);
      assert.match(angular, /angular-legacy\.png/);
      assert.doesNotMatch(angular, /family-wide\.png/);
      assert.match(astro, /class="field-label">Astro</);
      assert.match(astro, /No preview image yet/);
      assert.doesNotMatch(astro, /family-wide\.png/);
      if (emptyFamily) assert.match(react, /No preview image yet/);
      else {
        assert.match(react, /family-wide\.png/);
        assert.match(react, /family-narrow\.png/);
      }
      assert.doesNotMatch(html, /ignored-family|ignored-vue/);
      assert.doesNotMatch(html, /data-tab="template-2"/);
    }
  });
}

test('SPA browser uses responsive page labels and pairs ahead of legacy images', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'templates.json');
  const outputPath = path.join(dir, 'browser.html');
  const template = {
    displayName: '311 Portal', kind: 'spa', framework: 'react',
    previewImages: ['https://legacy.example/ignored.png'],
    responsivePreviews: [{
      id: 'requests', displayName: 'Service <requests>',
      desktop: 'https://example.com/wide.png', mobile: 'https://example.com/narrow.png',
    }],
  };
  fs.writeFileSync(dataPath, JSON.stringify({ TEMPLATES_JSON: [template] }));
  assert.equal(renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath }).status, 'ok');
  const html = fs.readFileSync(outputPath, 'utf8');
  assert.match(html, /Service &lt;requests&gt; - desktop/);
  assert.match(html, /Service &lt;requests&gt; - mobile/);
  assert.match(html, /wide\.png/);
  assert.match(html, /narrow\.png/);
  assert.doesNotMatch(html, /legacy\.example/);
  fs.writeFileSync(dataPath, JSON.stringify({ TEMPLATES_JSON: [{ ...template, responsivePreviews: [] }] }));
  const emptyPath = path.join(dir, 'empty.html');
  renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath: emptyPath });
  assert.match(fs.readFileSync(emptyPath, 'utf8'), /No preview image yet/);
});

test('parseArgs accepts templates JSON, output path, and open switch', () => {
  assert.deepEqual(parseArgs([
    '--templatesJsonPath', '/tmp/templates.json',
    '--outputPath', '/tmp/browser.html',
    '--open',
  ]), {
    templatesJsonPath: '/tmp/templates.json',
    outputPath: '/tmp/browser.html',
    open: true,
  });
});

test('renderTemplateBrowser renders static template details and preview images', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'templates.json');
  const outputPath = path.join(dir, 'browser.html');
  fs.writeFileSync(dataPath, JSON.stringify({
    TEMPLATES_JSON: [
      {
        displayName: 'Company "Portal"',
        description: 'Internal site',
        framework: 'react',
        audience: ['makers', 'developers'],
        keywords: ['portal', 'directory'],
        previewImages: [
          'https://raw.githubusercontent.com/o/r/sha/templates/spa/company/previews/home.png',
          'https://raw.githubusercontent.com/o/r/sha/templates/spa/company/previews/detail.png',
          'https://example.test/image.png" onerror="globalThis.pwned=1',
        ],
      },
      {
        displayName: '311 Portal',
        description: 'Citizen service request portal',
        framework: 'react',
        audience: ['makers'],
        keywords: ['311'],
        previewImages: [
          'https://raw.githubusercontent.com/o/r/sha/templates/spa/311/previews/home.png',
        ],
      },
    ],
  }));

  const result = renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath, open: false });
  const html = fs.readFileSync(outputPath, 'utf8');

  assert.deepEqual(result, { status: 'ok', output: outputPath, opened: false });
  assert.match(html, /Browse Power Pages templates/);
  assert.match(html, /Company "Portal"/);
  assert.match(html, /Company "Portal" <span class="nav-category">SPA<\/span>/);
  assert.match(html, /311 Portal/);
  assert.match(html, /home\.png/);
  assert.match(html, /detail\.png/);
  assert.match(html, /data-tab="template-1"/);
  assert.match(html, /data-tab="template-2"/);
  assert.match(html, /class="template-section active" id="template-1"/);
  assert.match(html, /class="template-section" id="template-2"/);
  assert.match(html, /class="preview-boundary"/);
  assert.match(html, /<figcaption>Preview 2<\/figcaption>/);
  assert.match(html, /class="preview-image"/);
  assert.match(html, /Preview 2 of Company &quot;Portal&quot;/);
  assert.match(html, /src="https:\/\/example\.test\/image\.png&quot; onerror=&quot;globalThis\.pwned=1"/);
  assert.doesNotMatch(html, /src="https:\/\/example\.test\/image\.png" onerror=/);
  assert.match(html, /border-bottom:2px solid transparent/);
  assert.match(html, /\.nav-btn\.active\{border-bottom-color:var\(--accent\);\}/);
  assert.match(html, /Citizen service request portal/);
  assert.doesNotMatch(html, /data-carousel/);
  assert.doesNotMatch(html, /carousel/);
  assert.doesNotMatch(html, /Audience/);
  assert.doesNotMatch(html, /makers/);
  assert.doesNotMatch(html, /developers/);
  assert.doesNotMatch(html, /picker/i);
  assert.doesNotMatch(html, /templates\.map/);
});

test('renderTemplateBrowser does not print the shared renderTemplate status line', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'templates.json');
  const outputPath = path.join(dir, 'browser.html');
  fs.writeFileSync(dataPath, JSON.stringify({
    TEMPLATES_JSON: [
      { displayName: 'Company Portal', description: 'Internal site', framework: 'react', previewImages: [] },
    ],
  }));
  const originalLog = console.log;
  const logs = [];
  console.log = (value) => logs.push(value);
  t.after(() => { console.log = originalLog; });

  renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath, open: false });

  assert.deepEqual(logs, []);
});

test('renderTemplateBrowser renders one family with read-only framework variants', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'families.json');
  const outputPath = path.join(dir, 'browser.html');
  fs.writeFileSync(dataPath, JSON.stringify({
    TEMPLATES_JSON: [
      {
        id: 'supplier-portal',
        displayName: 'Supplier Portal',
        description: 'Supplier invoice portal',
        kind: 'spa',
        keywords: ['supplier', 'invoice'],
        previewImages: ['https://raw.githubusercontent.com/o/r/sha/templates/spa/supplier/previews/home.png'],
        variants: [
          { variantId: 'supplier-portal/react', variantKey: 'react', framework: 'react' },
          { variantId: 'supplier-portal/vue', variantKey: 'vue', framework: 'vue' },
        ],
      },
    ],
  }));

  const result = renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath, open: false });
  const html = fs.readFileSync(outputPath, 'utf8');

  assert.deepEqual(result, { status: 'ok', output: outputPath, opened: false });
  assert.match(html, /Supplier Portal/);
  assert.match(html, /supplier invoice portal/i);
  assert.match(html, /React/);
  assert.match(html, /Vue/);
  assert.match(html, /Available frameworks/);
  assert.match(html, /data-tab="template-1"/);
  assert.doesNotMatch(html, /data-tab="template-2"/);
});

test('renderTemplateBrowser accepts raw nested variant maps from the manifest', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'families-raw.json');
  const outputPath = path.join(dir, 'browser.html');
  fs.writeFileSync(dataPath, JSON.stringify({
    TEMPLATES_JSON: [
      {
        id: 'supplier-portal',
        displayName: 'Supplier Portal',
        description: 'Supplier invoice portal',
        kind: 'spa',
        keywords: ['supplier'],
        previewImages: [],
        variants: {
          react: {},
          vue: {},
        },
      },
    ],
  }));

  renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath, open: false });
  const html = fs.readFileSync(outputPath, 'utf8');

  assert.match(html, /Available frameworks/);
  assert.match(html, /React/);
  assert.match(html, /Vue/);
  // Regression: the hero summary label must also read the object-map variants,
  // not just the "Available frameworks" chip row. Multiple variants collapse to
  // a count in the hero.
  assert.match(html, /template-framework">2 frameworks</);
});

test('renderTemplateBrowser shows non-SPA category and responsive previews', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'non-spa.json');
  const outputPath = path.join(dir, 'browser.html');
  fs.writeFileSync(dataPath, JSON.stringify({
    TEMPLATES_JSON: [{
      id: 'starter-layout-1',
      name: 'StarterLayout1',
      displayName: 'Starter Layout 1',
      description: 'Liquid starter layout',
      kind: 'non-spa',
      keywords: ['liquid'],
      previews: [{
        name: 'home',
        displayName: 'Home page',
        desktop: 'https://example.test/desktop.png',
        mobile: 'https://example.test/mobile.png',
      }],
    }],
  }));

  renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath, open: false });
  const html = fs.readFileSync(outputPath, 'utf8');

  assert.match(html, /Starter Layout 1 <span class="nav-category">Non-SPA<\/span>/);
  assert.match(html, /template-category">Non-SPA</);
  assert.match(html, /Liquid \+ Web Template/);
  assert.match(html, /Home page - desktop/);
  assert.match(html, /Home page - mobile/);
  assert.doesNotMatch(html, /Available frameworks/);
});

test('renderTemplateBrowser shows a single object-map variant in the hero label', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'families-single.json');
  const outputPath = path.join(dir, 'browser.html');
  fs.writeFileSync(dataPath, JSON.stringify({
    TEMPLATES_JSON: [
      {
        id: '311-portal',
        displayName: '311 Portal',
        description: 'Citizen service requests',
        kind: 'spa',
        keywords: ['311'],
        previewImages: [],
        variants: {
          react: {},
        },
      },
    ],
  }));

  renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath, open: false });
  const html = fs.readFileSync(outputPath, 'utf8');

  // Regression for the empty hero label: a single-variant object map must render
  // the framework name (not an empty string) in the hero.
  assert.match(html, /template-framework">React</);
});

test('renderTemplateBrowser reports opener failures without failing render', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'templates.json');
  const outputPath = path.join(dir, 'browser.html');
  fs.writeFileSync(dataPath, JSON.stringify({
    TEMPLATES_JSON: [{
      displayName: 'Company Portal',
      description: 'Internal site',
      framework: 'react',
      keywords: ['portal'],
      previewImages: [],
    }],
  }));

  const result = renderTemplateBrowser({
    templatesJsonPath: dataPath,
    outputPath,
    open: true,
  }, {
    execFileSync: () => { throw new Error('no opener'); },
  });

  assert.deepEqual(result, { status: 'ok', output: outputPath, opened: false, openError: 'no opener' });
  assert.equal(fs.existsSync(outputPath), true);
});

test('renderTemplateBrowser refuses to open an empty template browser', (t) => {
  const dir = tempDir();
  t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
  const dataPath = path.join(dir, 'templates.json');
  const outputPath = path.join(dir, 'browser.html');
  fs.writeFileSync(dataPath, JSON.stringify({ TEMPLATES_JSON: [] }));

  const result = renderTemplateBrowser({ templatesJsonPath: dataPath, outputPath, open: true }, {
    execFileSync: () => { throw new Error('should not open invalid browser'); },
  });

  assert.equal(result.status, 'invalid');
  assert.equal(result.opened, false);
  assert.match(result.validation.errors.join('\n'), /must contain at least one template family/);
  assert.match(result.validation.errors.join('\n'), /empty-state/);
});

test('validateRenderedTemplateBrowser catches missing framework labels', () => {
  const result = validateRenderedTemplateBrowser({
    templates: [{ displayName: 'Supplier Portal', variants: { react: {}, vue: {} } }],
    html: '<h2>Supplier Portal</h2><span>React</span>',
  });

  assert.equal(result.ok, false);
  assert.match(result.errors.join('\n'), /missing framework Vue/);
});