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.

29.9 KB
Raw Blame
'use strict';
// Coverage for the App Spec -> model-app-plan.md renderer (scripts/lib/app-spec-doc.js) and its
// CLI wrapper (scripts/write-app-spec-doc.js). The renderer replaces a freehand "abbreviated counts"
// summary the /app-builder skill used to write from prose — testers reported that prose plan skipped
// jobs-to-be-done, produced no readable design doc, and never proposed generative pages, all because
// prose instructions get silently ignored. Rendering deterministically from app-spec.json means the
// document is (a) complete, (b) always in sync, and (c) regenerable. These tests pin that contract.

const { test } = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const fs = require('node:fs');
const os = require('node:os');
const { execFileSync } = require('node:child_process');
const { renderAppSpecDoc } = require('../lib/app-spec-doc.js');

const CLI = path.join(__dirname, '..', 'write-app-spec-doc.js');

// A realistic multi-entity, multi-persona, multi-page spec. Kept in one factory so a single test can
// mutate a copy without perturbing the others. Matches the /app-builder authoring flow's shape.
function realisticSpec() {
  return {
    schemaVersion: 2,
    solution: { uniqueName: 'ContosoSupport', publisherPrefix: 'new' },
    app: {
      name: 'Support Desk',
      uniqueName: 'contoso_support',
      description: 'Manage customers, their support tickets, and ticket comments.',
    },
    entities: [
      {
        schemaName: 'new_customer', displayName: 'Customer',
        primaryAttribute: { schemaName: 'new_name', displayName: 'Name' },
        columns: [{ schemaName: 'new_segment', displayName: 'Segment', type: 'Choice', options: [{ label: 'Enterprise' }, { label: 'Mid-Market' }] }],
      },
      {
        schemaName: 'new_ticket', displayName: 'Ticket',
        primaryAttribute: { schemaName: 'new_name', displayName: 'Title' },
        columns: [
          { schemaName: 'new_priority', displayName: 'Priority', type: 'Choice', options: [{ label: 'Low' }, { label: 'High' }] },
          { schemaName: 'new_description', displayName: 'Description', type: 'Memo', required: true },
        ],
        hasNotes: true,
      },
    ],
    relationships: [
      { type: 'OneToMany', referenced: 'new_customer', referencing: 'new_ticket', lookup: { schemaName: 'new_CustomerId' } },
    ],
    personas: [{
      persona: 'Agent',
      appAccess: true,
      jobs: [
        {
          name: 'Triage new tickets',
          description: 'See what came in overnight',
          privileges: [{ entity: 'new_ticket', access: ['read', 'write'], scope: 'user' }, { entity: 'new_customer', access: ['read'], scope: 'organization' }],
          surfaces: ['Active Tickets', 'overview'],
        },
        {
          name: 'Resolve escalations',
          privileges: [{ entity: 'new_ticket', access: ['read', 'write', 'delete'], scope: 'organization' }],
          surfaces: ['Ticket Main'],
        },
      ],
    }],
    forms: [
      { entity: 'new_ticket', type: 'main', name: 'Ticket Main', layout: 'auto', subgrids: [{ childEntity: 'new_comment' }] },
    ],
    views: [
      { entity: 'new_ticket', name: 'Active Tickets', columns: ['new_name', 'new_priority'] },
    ],
    charts: [
      { entity: 'new_ticket', name: 'By Priority', chartType: 'bar', measure: 'count', groupBy: 'new_priority' },
    ],
    pages: [
      {
        key: 'overview', name: 'Overview', purpose: 'KPIs at a glance',
        dataSources: ['new_ticket'], source: { kind: 'intent' },
        navigatesTo: [{ targetKey: 'ticket-detail' }],
      },
      {
        key: 'ticket-detail', name: 'Ticket Detail', purpose: 'Drill into one ticket',
        dataSources: ['new_ticket'], source: { kind: 'tsx', codeFile: 'ticket-detail.tsx' },
      },
    ],
    appShell: {
      areas: [{
        label: 'Work', groups: [{ label: 'Tickets', subAreas: [
          { title: 'Overview', page: 'overview' },
          { title: 'All Tickets', entity: 'new_ticket' },
        ] }],
      }],
    },
    sampleData: { new_customer: [{ new_name: 'Acme' }, { new_name: 'Contoso' }] },
    design: { accentColor: '#0f6cbd', density: 'compact' },
    ai: { rowSummaries: 'auto' },
  };
}

// A markdown table row is `| c1 | c2 | c3 |`. The renderer promises `|` inside a cell VALUE is
// escaped as `\|` and newlines are flattened — otherwise a stray pipe in a description or purpose
// silently breaks the column count and the reviewer sees a garbled table. Count pipes that are NOT
// preceded by a backslash: for a row with N columns that number is exactly N+1.
function unescapedPipeCount(row) {
  let n = 0;
  for (let i = 0; i < row.length; i++) if (row[i] === '|' && row[i - 1] !== '\\') n += 1;
  return n;
}

test('the routing description is shown beside the tile text, and only when the spec sets one (#583)', () => {
  const spec = realisticSpec();
  assert.ok(!renderAppSpecDoc(spec, {}).includes('Routing description'));
  spec.app.aiDescription = 'Support work. Prefer My Tickets for your own assigned items.';
  const md = renderAppSpecDoc(spec, {});
  const at = md.indexOf('**Routing description:** Support work. Prefer My Tickets for your own assigned items.');
  assert.ok(at > md.indexOf(spec.app.description), 'after the tile description');
  assert.ok(at < md.indexOf('## Environment'), 'in the overview, before the first section');
});

test('renders every expected section heading in document order', () => {
  const md = renderAppSpecDoc(realisticSpec(), { envUrl: 'https://x.crm.dynamics.com' });
  const expected = [
    '# Support Desk',
    '## Environment',
    '## Jobs to be done',
    '## Data model',
    '## Surfaces',
    '### Generative pages',
    '### Forms',
    '### Views',
    '### Charts',
    '## Navigation',
    '## Security',
    '## Sample data',
    '## Design contract',
    '## AI features',
  ];
  let cursor = -1;
  for (const heading of expected) {
    const at = md.indexOf(heading);
    assert.notEqual(at, -1, `missing section "${heading}"`);
    assert.ok(at > cursor, `section "${heading}" is out of document order`);
    cursor = at;
  }
});

test('absent optional blocks omit their section entirely', () => {
  // The empty-block omission is what keeps the design doc honest: a section header with no content
  // reads as "we considered this and left it blank", which is not what an absent block means.
  const s = realisticSpec();
  delete s.sampleData;
  delete s.design;
  delete s.ai;
  delete s.charts;
  delete s.dashboards; // never had them, but be explicit
  const md = renderAppSpecDoc(s);
  assert.doesNotMatch(md, /^## Sample data$/m);
  assert.doesNotMatch(md, /^## Design contract$/m);
  assert.doesNotMatch(md, /^## AI features$/m);
  assert.doesNotMatch(md, /^### Charts$/m);
  assert.doesNotMatch(md, /^### Classic dashboards$/m);
  // The mandatory sections that do NOT depend on optional blocks are still present.
  assert.match(md, /^## Data model$/m);
  assert.match(md, /^## Surfaces$/m);
});

test('a pipe or newline inside a cell value is escaped/flattened so the row keeps its column count', () => {
  // Regression: cell values (an app description, a job name, a page purpose) arrive from author
  // prose or from a downloaded spec. A stray `|` or `\n` in any of them would break a table row
  // silently — the reviewer would see a garbled table but no error. This is the important one:
  // assert structurally by counting unescaped pipes per row, not just that a substring appears.
  const s = realisticSpec();
  s.app.description = 'Track suppliers | contracts\nand renewals';
  s.personas[0].jobs[0].name = 'Triage | urgent\nissues';
  s.pages[0].purpose = 'KPIs | trends\nat a glance';
  const md = renderAppSpecDoc(s);

  // The Jobs table row for the persona/job with pipes must have exactly 4 unescaped pipes
  // (3 columns → 4 boundaries: Persona | Job | Surfaces). And the escaped literal must be present.
  const jobRow = md.split('\n').find((l) => l.includes('Triage \\|'));
  assert.ok(jobRow, `job row with escaped pipe not found. Rendered doc:\n${md}`);
  assert.equal(unescapedPipeCount(jobRow), 4, `job row column boundary count: "${jobRow}"`);

  // The Generative pages table row for the page with pipes must have exactly 7 boundaries
  // (6 columns: Page | Key | Purpose | Reads | Navigates to | State).
  const pageRow = md.split('\n').find((l) => l.includes('| Overview |'));
  assert.ok(pageRow, `overview page row not found. Rendered doc:\n${md}`);
  assert.equal(unescapedPipeCount(pageRow), 7, `page row column boundary count: "${pageRow}"`);
  assert.ok(pageRow.includes('KPIs \\|'), `page purpose pipe should be escaped: "${pageRow}"`);

  // The app description appears in the overview paragraph (not a table) — verify the newline is
  // flattened to a space so it does not silently open a new block that changes document order.
  assert.match(md, /Track suppliers \| contracts and renewals/);
});

test('a tsx-built page renders with its codeFile; an intent page renders as intent', () => {
  const md = renderAppSpecDoc(realisticSpec());
  const overviewRow = md.split('\n').find((l) => l.startsWith('| Overview '));
  const detailRow = md.split('\n').find((l) => l.startsWith('| Ticket Detail '));
  assert.match(overviewRow, /intent \(code not yet generated\)/);
  assert.match(detailRow, /built \(`ticket-detail\.tsx`\)/);
});

test('the security section unions privileges per table with max scope winning', () => {
  // Regression protection: two jobs on the same persona both touch new_ticket, one at `user` and
  // one at `organization`. The SDK unions them (max scope wins) and the reviewer should see ONE
  // row for new_ticket at `organization`, not two rows they have to merge mentally.
  const md = renderAppSpecDoc(realisticSpec());
  const secStart = md.indexOf('## Security');
  // Match the NEXT `## ` heading only — a plain indexOf('## ') would hit `### ` sub-headings
  // (Role: Agent) as a substring and slice the section down to nothing.
  const nextTop = md.slice(secStart + 3).search(/\n## /);
  const secEnd = nextTop === -1 ? md.length : secStart + 3 + nextTop;
  const section = md.slice(secStart, secEnd);

  const ticketRows = section.split('\n').filter((l) => /^\| new_ticket \|/.test(l));
  assert.equal(ticketRows.length, 1, `expected ONE unioned new_ticket row, got: ${JSON.stringify(ticketRows)}`);
  assert.match(ticketRows[0], /organization/, `max scope should win: "${ticketRows[0]}"`);
  // Access set is the union of both jobs, so all three levels must appear.
  for (const level of ['read', 'write', 'delete']) {
    assert.ok(ticketRows[0].includes(level), `unioned access must include ${level}: "${ticketRows[0]}"`);
  }
});

test('the no-personas warning callout is rendered when personas[] is absent', () => {
  const s = realisticSpec();
  delete s.personas;
  const md = renderAppSpecDoc(s);
  // Callouts are `> ⚠ …` blockquote lines inside `## Jobs to be done`.
  assert.match(md, /## Jobs to be done[\s\S]*?> ⚠ No `personas\[\]` were captured/);
});

test('the unmapped-jobs warning callout names the count when a job has no surfaces', () => {
  const s = realisticSpec();
  s.personas[0].jobs[1].surfaces = []; // drop mapping on one of two jobs
  const md = renderAppSpecDoc(s);
  assert.match(md, /> ⚠ 1 job\(s\) are not mapped to a surface/);
});

test('the no-pages warning callout appears when pages[] is empty', () => {
  const s = realisticSpec();
  s.pages = [];
  const md = renderAppSpecDoc(s);
  assert.match(md, /### Generative pages[\s\S]*?> ⚠ No generative pages\./);
});

// ---- CLI wrapper (write-app-spec-doc.js) --------------------------------------------------------

function makeWorkspace(spec) {
  // os.tmpdir() on Windows is %LOCALAPPDATA%\Temp, matching every other test in this suite.
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'appdoc-'));
  fs.writeFileSync(path.join(dir, 'app-spec.json'), JSON.stringify(spec, null, 2), 'utf8');
  return dir;
}

function runCli(args, cwd) {
  try {
    const stdout = execFileSync(process.execPath, [CLI, ...args], { encoding: 'utf8', cwd });
    return { code: 0, stdout, stderr: '' };
  } catch (e) {
    return { code: e.status, stdout: String(e.stdout || ''), stderr: String(e.stderr || '') };
  }
}

test('CLI writes the doc next to the spec by default and returns ok:true with byte count', () => {
  const dir = makeWorkspace(realisticSpec());
  try {
    const r = runCli(['--spec', '@' + path.join(dir, 'app-spec.json')]);
    assert.equal(r.code, 0, r.stderr);
    const out = JSON.parse(r.stdout);
    assert.equal(out.ok, true);
    assert.equal(out.docPath, path.join(dir, 'model-app-plan.md'));
    assert.ok(fs.existsSync(out.docPath), 'doc file was written');
    const contents = fs.readFileSync(out.docPath, 'utf8');
    assert.equal(out.bytes, Buffer.byteLength(contents, 'utf8'));
    assert.match(contents, /^# Support Desk — design/);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test('CLI --out honours a caller-supplied path', () => {
  const dir = makeWorkspace(realisticSpec());
  try {
    const custom = path.join(dir, 'nested', 'other-name.md');
    const r = runCli(['--spec', '@' + path.join(dir, 'app-spec.json'), '--out', custom]);
    assert.equal(r.code, 0, r.stderr);
    const out = JSON.parse(r.stdout);
    assert.equal(out.docPath, custom);
    assert.ok(fs.existsSync(custom), 'custom-path doc file was written');
    // The default location must NOT be written when --out is provided.
    assert.equal(fs.existsSync(path.join(dir, 'model-app-plan.md')), false);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test('CLI warnings equal designGaps(spec) — the JSON echo of the callouts in the document', () => {
  // The orchestrator surfaces warnings in chat rather than making the user open the file, so the
  // JSON `warnings` array MUST match designGaps exactly (order and text). Pinning the equality
  // prevents the two from drifting when a new gap is added to only one side.
  const { designGaps } = require('../write-app-spec-doc.js');
  const { migrateAppSpec } = require('../lib/app-spec.js');

  // A spec that trips all three gaps: no personas, no pages. (`personas` empty implies no jobs.)
  const s = realisticSpec();
  delete s.personas;
  s.pages = [];
  const dir = makeWorkspace(s);
  try {
    const r = runCli(['--spec', '@' + path.join(dir, 'app-spec.json')]);
    assert.equal(r.code, 0, r.stderr);
    const out = JSON.parse(r.stdout);
    const gaps = designGaps(migrateAppSpec(s));
    assert.deepEqual(out.warnings, gaps, 'CLI warnings must equal designGaps');
    assert.ok(out.warnings.some((w) => /no jobs-to-be-done captured/.test(w)), JSON.stringify(out.warnings));
    assert.ok(out.warnings.some((w) => /no generative pages/.test(w)), JSON.stringify(out.warnings));
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test('CLI warnings include unmapped-job entries with persona and job name', () => {
  const s = realisticSpec();
  s.personas[0].jobs[0].surfaces = []; // one of two jobs is unmapped
  const dir = makeWorkspace(s);
  try {
    const r = runCli(['--spec', '@' + path.join(dir, 'app-spec.json')]);
    assert.equal(r.code, 0, r.stderr);
    const out = JSON.parse(r.stdout);
    const unmapped = out.warnings.find((w) => /not mapped to a surface/.test(w));
    assert.ok(unmapped, `expected unmapped-job warning, got: ${JSON.stringify(out.warnings)}`);
    assert.match(unmapped, /Agent → Triage new tickets/);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test('CLI warnings are empty when the spec has personas with mapped jobs and pages', () => {
  const dir = makeWorkspace(realisticSpec());
  try {
    const r = runCli(['--spec', '@' + path.join(dir, 'app-spec.json')]);
    assert.equal(r.code, 0, r.stderr);
    const out = JSON.parse(r.stdout);
    assert.deepEqual(out.warnings, [], `expected no gaps, got: ${JSON.stringify(out.warnings)}`);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test('CLI fails cleanly (exit 1, message to stderr) when --spec is missing', () => {
  // Failure paths matter for the skill: a raw Node stack in stderr would leak into chat and
  // confuse the user. Assert the friendly usage line instead of a stack frame.
  const r = runCli([]);
  assert.equal(r.code, 1);
  assert.match(r.stderr, /Usage:/);
  assert.doesNotMatch(r.stderr, /at Object\.<anonymous>/);
});

test('CLI fails cleanly when the spec file does not exist', () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'appdoc-bad-'));
  try {
    const r = runCli(['--spec', '@' + path.join(dir, 'does-not-exist.json')]);
    assert.equal(r.code, 1);
    assert.match(r.stderr, /cannot read app spec/);
    assert.doesNotMatch(r.stderr, /at Object\.<anonymous>/);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test('CLI fails cleanly when the spec is not valid JSON', () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'appdoc-badjson-'));
  try {
    fs.writeFileSync(path.join(dir, 'app-spec.json'), '{ not: json', 'utf8');
    const r = runCli(['--spec', '@' + path.join(dir, 'app-spec.json')]);
    assert.equal(r.code, 1);
    assert.match(r.stderr, /cannot read app spec/);
    assert.doesNotMatch(r.stderr, /at Object\.<anonymous>/);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

// --- Corrections from adversarial review -------------------------------------------

test('privilege scope is unioned per (table, ACCESS), never per table', () => {
  // Collapsing to one scope per table rendered `read@organization` + `write@user` as
  // "read, write @ organization" — OVERSTATING write access. A document that misreports the access
  // model is worse than no document. Mirrors evals/.../facts.js `unionPrivileges`.
  const md = renderAppSpecDoc({
    app: { name: 'A' },
    personas: [{ persona: 'P', jobs: [
      { name: 'j1', privileges: [{ entity: 'new_ticket', access: ['read'], scope: 'organization' }] },
      { name: 'j2', privileges: [{ entity: 'new_ticket', access: ['write'], scope: 'user' }] },
    ] }],
  });
  const rows = md.split('\n').filter((l) => l.startsWith('| new_ticket'));
  assert.equal(rows.length, 2, `expected a split, got:\n${rows.join('\n')}`);
  assert.ok(rows.some((r) => /\| read \|.*organization/.test(r)), 'read stays at organization');
  assert.ok(rows.some((r) => /\| write \|.*user/.test(r)), 'write is NOT promoted to organization');
});

test('accesses that genuinely share a scope stay on one row', () => {
  const md = renderAppSpecDoc({
    app: { name: 'A' },
    personas: [{ persona: 'P', jobs: [
      { name: 'j1', privileges: [{ entity: 'new_ticket', access: ['read', 'write'], scope: 'businessUnit' }] },
      { name: 'j2', privileges: [{ entity: 'new_ticket', access: ['create'], scope: 'businessUnit' }] },
    ] }],
  });
  const rows = md.split('\n').filter((l) => l.startsWith('| new_ticket'));
  assert.equal(rows.length, 1, 'one scope -> one row');
  for (const a of ['read', 'write', 'create']) assert.match(rows[0], new RegExp(a));
});

test('the publisher prefix is read from its real field name', () => {
  // Was reading `solution.prefix`, which no valid spec sets — the row silently never rendered.
  const md = renderAppSpecDoc({ app: { name: 'A' }, solution: { uniqueName: 'S', publisherPrefix: 'contoso' } });
  assert.match(md, /\| Publisher prefix \| contoso \|/);
});

test('a malformed or half-typed spec renders instead of throwing', () => {
  // The renderer runs against WORK-IN-PROGRESS specs during authoring, so a crash kills the flow.
  // Shape errors are validateAppSpec's job to report; this must degrade, not explode.
  const malformed = [
    {}, null, undefined,
    { personas: {} }, { personas: [null] }, { personas: [{ persona: 'P', jobs: [null] }] },
    { personas: [{ persona: 'P', jobs: 'x' }] }, { personas: [{ persona: 'P', jobs: [{ name: 'j', surfaces: 'x' }] }] },
    { personas: [{ persona: 'P', jobs: [{ name: 'j', privileges: [null] }] }] },
    { personas: [{ persona: 'P', jobs: [{ name: 'j', privileges: [{ entity: 'e', access: 'read' }] }] }] },
    { entities: {} }, { entities: [null] }, { entities: [{ schemaName: 'e', columns: 'x' }] },
    { entities: [{ schemaName: 'e', columns: [{ schemaName: 'c', options: [null] }] }] },
    { pages: [{ key: 'p', name: 'P', dataSources: 'new_ticket' }] },
    { pages: [{ key: 'p', name: 'P', navigatesTo: [null] }] },
    { forms: [{ name: 'F', entity: 'e', subgrids: [null] }] },
    { views: [{ name: 'V', entity: 'e', columns: 'x' }] },
    { dashboards: [{ name: 'D', tiles: 'x' }] },
    { appShell: { areas: 'x' } }, { appShell: { areas: [null] } },
    { appShell: { areas: [{ label: 'A', groups: 'x' }] } },
    { sampleData: { e: 'x' } }, { relationships: {} },
  ];
  for (const spec of malformed) {
    assert.doesNotThrow(() => renderAppSpecDoc(spec), `threw on ${JSON.stringify(spec)}`);
  }
});

// --- Corrections from adversarial review -------------------------------------------

test('forms render their canonical formType, not the non-existent `type` field', () => {
  // Reading `f.type` rendered every QuickCreate/QuickView form as "main", hiding a real difference
  // in what the form does. `formType` is canonical and defaults to Main (app-spec.js:480).
  const md = renderAppSpecDoc({ app: { name: 'A' }, forms: [
    { name: 'Quick Add', entity: 'new_ticket', formType: 'QuickCreate' },
    { name: 'Card', entity: 'new_ticket', formType: 'QuickView' },
    { name: 'Plain', entity: 'new_ticket' },
  ] });
  assert.match(md, /\| Quick Add \| new_ticket \| QuickCreate \|/);
  assert.match(md, /\| Card \| new_ticket \| QuickView \|/);
  assert.match(md, /\| Plain \| new_ticket \| Main \|/, 'absent formType defaults to Main');
});

test('an authored tabs[] layout is reported as explicit, not "auto"', () => {
  // The build treats an explicit layout differently (it prunes fields the author dropped), so
  // calling it "auto" misstates the design.
  const md = renderAppSpecDoc({ app: { name: 'A' }, forms: [
    { name: 'Structured', entity: 'e', tabs: [{ label: 'T1' }, { label: 'T2' }] },
    { name: 'Auto', entity: 'e' },
  ] });
  assert.match(md, /\| Structured \|.*\| explicit \(2 tabs\) \|/);
  assert.match(md, /\| Auto \|.*\| auto \|/);
});

test('view filters and sort are rendered — two views differing only by them must not read alike', () => {
  const base = { app: { name: 'A' }, views: [{ name: 'Queue', entity: 'e', columns: ['a'] }] };
  const filtered = { app: { name: 'A' }, views: [{ name: 'Queue', entity: 'e', columns: ['a'],
    filters: [{ attr: 'ownerid', op: 'eq-userid' }], sort: [{ attr: 'createdon', dir: 'desc' }] }] };
  const a = renderAppSpecDoc(base);
  const b = renderAppSpecDoc(filtered);
  assert.notEqual(a, b, 'a filtered+sorted view must render differently from a bare one');
  assert.match(b, /ownerid eq-userid/);
  assert.match(b, /createdon desc/);
});

test('command-bar buttons are rendered, including flyout children', () => {
  const md = renderAppSpecDoc({ app: { name: 'A' }, commands: [
    { label: 'Escalate', entity: 'new_ticket', library: 'x.js', function: 'T.escalate' },
    { label: 'More', entity: 'new_ticket', type: 'FlyoutAnchor', children: [{ label: 'Archive', function: 'T.archive' }] },
  ] });
  assert.match(md, /### Command-bar buttons/);
  assert.match(md, /\| Escalate \| new_ticket \|.*T\.escalate/);
  assert.match(md, /↳ Archive/);
  // And a spec WITHOUT commands must not grow the section.
  assert.ok(!/### Command-bar buttons/.test(renderAppSpecDoc({ app: { name: 'A' } })));
});

test('the CLI reports design gaps instead of crashing on a malformed personas block', () => {
  // designGaps() was unguarded while the renderer was guarded, so the CLI wrote the document and
  // THEN exited 1 with a raw stack — the caller saw a crash next to a file that looked fine.
  const { designGaps } = require('../write-app-spec-doc.js');
  for (const spec of [
    {}, null, undefined,
    { personas: {} }, { personas: [null] },
    { personas: [{ persona: 'P', jobs: {} }] },
    { personas: [{ persona: 'P', jobs: [null] }] },
    { personas: [{ persona: 'P', jobs: [{ name: 'j', surfaces: 'x' }] }] },
    { pages: 'x' },
  ]) {
    assert.doesNotThrow(() => designGaps(spec), `threw on ${JSON.stringify(spec)}`);
    assert.ok(Array.isArray(designGaps(spec)));
  }
});

test('every committed fixture spec renders with its real values, not empty cells', () => {
  // Three separate field-name guesses shipped before this guard: `solution.prefix` (real:
  // publisherPrefix), `forms[].type` (real: formType), and `{column,descending}` for view sort
  // (real: {attr,dir}). Each rendered an empty or wrong cell while every hand-written test passed,
  // because the tests used the same wrong names as the code. Rendering the REAL committed fixtures
  // and asserting their REAL values appear is what catches that class of mistake.
  const fixturesDir = path.resolve(__dirname, '..', '..', '..', '..', 'evals', 'model-apps', 'app-builder', 'fixtures');
  const dirs = fs.readdirSync(fixturesDir, { withFileTypes: true }).filter((d) => d.isDirectory());
  assert.ok(dirs.length >= 3, `expected real fixtures, found ${dirs.length}`);

  for (const d of dirs) {
    const specPath = path.join(fixturesDir, d.name, 'app-spec.json');
    if (!fs.existsSync(specPath)) continue;
    const spec = JSON.parse(fs.readFileSync(specPath, 'utf8'));
    const md = renderAppSpecDoc(spec, { envUrl: 'https://x.crm.dynamics.com' });

    if (spec.solution && spec.solution.publisherPrefix) {
      assert.match(md, new RegExp(`\\| Publisher prefix \\| ${spec.solution.publisherPrefix} \\|`), `${d.name}: publisher prefix missing`);
    }
    for (const v of spec.views || []) {
      assert.ok(md.includes(v.name), `${d.name}: view "${v.name}" missing`);
      for (const srt of v.sort || []) {
        assert.ok(md.includes(`${srt.attr} ${srt.dir || 'asc'}`), `${d.name}: view "${v.name}" sort "${srt.attr}" not rendered`);
      }
    }
    for (const p of spec.pages || []) assert.ok(md.includes(p.key), `${d.name}: page "${p.key}" missing`);
    for (const per of spec.personas || []) {
      assert.ok(md.includes(per.persona), `${d.name}: persona "${per.persona}" missing`);
      for (const j of per.jobs || []) {
        assert.ok(md.includes(j.name), `${d.name}: job "${j.name}" missing`);
        for (const s of j.surfaces || []) assert.ok(md.includes(s), `${d.name}: surface "${s}" missing`);
      }
    }
    // No table row should be entirely empty cells — the signature of a wrong field name.
    for (const line of md.split('\n')) {
      if (/^\|/.test(line) && /^\|(\s*(—|)\s*\|)+$/.test(line)) assert.fail(`${d.name}: all-empty table row: ${line}`);
    }
  }
});

// --- Icon depictions (tester ask: approve what the glyph will LOOK like, before the SVG exists) ---

test('a table icon renders its depiction, and an undescribed icon is called out', () => {
  const md = renderAppSpecDoc({ app: { name: 'A' }, entities: [
    { schemaName: 'new_project', displayName: 'Project', vectorIcon: 'new_project_icon', iconDescription: 'a briefcase with a handle' },
    { schemaName: 'new_task', displayName: 'Task', vectorIcon: 'new_task_icon' },
  ] });
  assert.match(md, /\*\*Nav icon:\*\* a briefcase with a handle/);
  assert.match(md, /Task[\s\S]*?\*\*Nav icon:\*\* _no description/);
  // The web-resource NAME must not be what the reviewer is shown — it describes nothing.
  assert.ok(!md.includes('new_project_icon'), 'the web-resource name is not the approval artifact');
});

test('a table with no icon at all gets no Nav icon line', () => {
  const md = renderAppSpecDoc({ app: { name: 'A' }, entities: [{ schemaName: 'account', displayName: 'Account', existing: true }] });
  assert.ok(!/\*\*Nav icon:\*\*/.test(md));
});

test('sitemap area/group/subarea depictions render, and entity subareas defer to the table', () => {
  const md = renderAppSpecDoc({ app: { name: 'A' }, appShell: { areas: [{
    label: 'Delivery', iconDescription: 'a rocket taking off',
    groups: [{ label: 'Work', subAreas: [
      { entity: 'new_project', title: 'Projects' },
      { page: 'overview', title: 'Overview', iconDescription: 'a speedometer at three-quarter dial' },
    ] }],
  }] } });
  assert.match(md, /\*\*Delivery\*\* — icon: a rocket taking off/);
  assert.match(md, /Overview .*— icon: a speedometer at three-quarter dial/);
  // An entity subarea's nav glyph comes from the TABLE's icon, so describing it here would show the
  // reviewer a depiction that is not the one actually rendered.
  assert.match(md, /Projects .*— icon: the table's own/);
});

// #583 Gap 1 — the exclusions are rendered BESIDE the traceability table so scope is reviewed
// rather than assumed. A reviewer cannot approve an omission they were never shown, and two apps
// built over the same tables are told apart by what each one declines to do.
test('#583 deliberate persona exclusions are rendered into the design document', () => {
  const spec = realisticSpec();
  const first = spec.personas[0];
  first.excludes = ['Approving budgets — handled in the Finance app', 'Editing customer master data'];
  const md = renderAppSpecDoc(spec);

  assert.match(md, /Deliberately out of scope/, 'the section must exist');
  for (const x of first.excludes) {
    assert.ok(md.includes(x), `the exclusion ${JSON.stringify(x)} must be shown verbatim`);
  }
  assert.ok(md.indexOf('Deliberately out of scope') > md.indexOf('| Persona |'),
    'exclusions belong beside the traceability table, not before it');

  // A spec that records none must not grow an empty heading — an empty section reads as
  // "nothing is out of scope", which is a different claim from "this was not captured".
  const none = realisticSpec();
  for (const p of none.personas) delete p.excludes;
  assert.doesNotMatch(renderAppSpecDoc(none), /Deliberately out of scope/);
});