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/strict');
const {
parsePacTenantId,
tenantIdFromToken,
validateCliTenantAlignment,
} = require('../lib/cli-tenant-alignment');
const { parseArgs, run } = require('../validate-cli-tenant-alignment');
const TENANT_A = '72f988bf-86f1-41af-91ab-2d7cd011db47';
const TENANT_B = '11111111-2222-3333-4444-555555555555';
function fakeJwt(tenantId) {
return [
Buffer.from(JSON.stringify({ alg: 'none' })).toString('base64url'),
Buffer.from(JSON.stringify({ tid: tenantId })).toString('base64url'),
'',
].join('.');
}
function fakeExecFile({ pacTenant = TENANT_A, azTenant = TENANT_A } = {}) {
return (command, args) => {
if (command === 'pac' && args.join(' ') === 'auth who') {
return `Connected as [email protected]\nTenant ID: ${pacTenant}\n`;
}
if (command === 'pac' && args.join(' ') === 'env who') {
return 'Org URL: https://org.crm.dynamics.com/\n';
}
if (command === 'az' && args.join(' ') === 'account show --query tenantId -o tsv') {
return `${azTenant}\n`;
}
throw new Error(`unexpected command: ${command} ${args.join(' ')}`);
};
}
function fakeWindowsExecFile({ pacTenant = TENANT_A, azTenant = TENANT_A } = {}) {
return (command, args) => {
if (command === 'cmd.exe' && args.join(' ') === '/d /s /c pac.exe auth who') {
return `Connected as [email protected]\nTenant ID: ${pacTenant}\n`;
}
if (command === 'cmd.exe' && args.join(' ') === '/d /s /c pac.exe env who') {
return 'Org URL: https://org.crm.dynamics.com/\n';
}
if (command === 'cmd.exe' && args.join(' ') === '/d /s /c az.cmd account show --query tenantId -o tsv') {
return `${azTenant}\n`;
}
if (command === 'pac' || command === 'az') {
throw new Error('Windows cannot exec cmd shims directly');
}
throw new Error(`unexpected command: ${command} ${args.join(' ')}`);
};
}
function fakeFallbackExecFile({ platform = 'linux' } = {}) {
return (command, args) => {
const joined = args.join(' ');
if (platform === 'win32') {
if (command === 'cmd.exe' && joined === '/d /s /c pac.exe auth who') {
return `Tenant ID: ${TENANT_A}\n`;
}
if (command === 'cmd.exe' && joined === '/d /s /c pac.exe env who') {
return 'Environment ID: 11111111-1111-1111-1111-111111111111\n';
}
if (command === 'cmd.exe' && joined === '/d /s /c pac.exe auth list') {
return '[1] * Active user Public https://org.crm.dynamics.com\n';
}
if (command === 'cmd.exe' && joined === '/d /s /c az.cmd account show --query tenantId -o tsv') {
return `${TENANT_A}\n`;
}
} else {
if (command === 'pac' && joined === 'auth who') return `Tenant ID: ${TENANT_A}\n`;
if (command === 'pac' && joined === 'env who') return 'Environment ID: 11111111-1111-1111-1111-111111111111\n';
if (command === 'pac' && joined === 'auth list') return '[1] * Active user Public https://org.crm.dynamics.com\n';
if (command === 'az' && joined === 'account show --query tenantId -o tsv') return `${TENANT_A}\n`;
}
throw new Error(`unexpected command: ${command} ${joined}`);
};
}
test('parsePacTenantId extracts Tenant ID from PAC auth output', () => {
assert.equal(parsePacTenantId(`User: u\nTenant ID: ${TENANT_A}\n`), TENANT_A);
assert.equal(parsePacTenantId(`Tenant: ${TENANT_A}`), TENANT_A);
assert.equal(parsePacTenantId('Tenant ID: not-a-guid'), null);
});
test('tenantIdFromToken extracts tid from an Azure CLI access token', () => {
assert.equal(tenantIdFromToken(fakeJwt(TENANT_A)), TENANT_A);
assert.equal(tenantIdFromToken('not.jwt'), null);
});
test('validateCliTenantAlignment succeeds when PAC, Azure account, and token tenants match', () => {
assert.deepEqual(validateCliTenantAlignment({
envUrl: 'https://org.crm.dynamics.com',
token: fakeJwt(TENANT_A),
}, {
execFile: fakeExecFile(),
platform: 'linux',
}), {
ok: true,
pacTenantId: TENANT_A,
azTenantId: TENANT_A,
tokenTenantId: TENANT_A,
expectedEnvironmentUrl: 'https://org.crm.dynamics.com',
pacEnvironmentUrl: 'https://org.crm.dynamics.com',
mismatches: [],
error: null,
});
});
test('validateCliTenantAlignment invokes Windows cmd shims through cmd.exe', () => {
assert.equal(validateCliTenantAlignment({
envUrl: 'https://org.crm.dynamics.com',
token: fakeJwt(TENANT_A),
}, {
execFile: fakeWindowsExecFile(),
platform: 'win32',
}).ok, true);
});
test('validateCliTenantAlignment falls back to the active PAC auth profile on POSIX and Windows', () => {
for (const platform of ['linux', 'win32']) {
assert.equal(validateCliTenantAlignment({
envUrl: 'https://org.crm.dynamics.com',
token: fakeJwt(TENANT_A),
}, {
execFile: fakeFallbackExecFile({ platform }),
platform,
}).ok, true);
}
});
test('validateCliTenantAlignment blocks when PAC and Azure tenants differ', () => {
const result = validateCliTenantAlignment({
envUrl: 'https://org.crm.dynamics.com',
token: fakeJwt(TENANT_A),
}, {
execFile: fakeExecFile({ pacTenant: TENANT_B, azTenant: TENANT_A }),
platform: 'linux',
});
assert.equal(result.ok, false);
assert.deepEqual(result.mismatches, ['pac-vs-az', 'pac-vs-token']);
assert.match(result.error, /different tenants/i);
});
test('validateCliTenantAlignment blocks when PAC targets a different environment', () => {
const result = validateCliTenantAlignment({
envUrl: 'https://target.crm.dynamics.com',
token: fakeJwt(TENANT_A),
}, {
execFile: fakeExecFile(),
platform: 'linux',
});
assert.equal(result.ok, false);
assert.deepEqual(result.mismatches, ['pac-vs-environment']);
assert.equal(result.expectedEnvironmentUrl, 'https://target.crm.dynamics.com');
assert.equal(result.pacEnvironmentUrl, 'https://org.crm.dynamics.com');
});
test('run requires an environment URL and acquires the token internally', () => {
assert.deepEqual(parseArgs(['--envUrl', 'https://org.crm.dynamics.com']), {
envUrl: 'https://org.crm.dynamics.com',
});
assert.equal(run([]).ok, false);
assert.match(run([]).error, /--envUrl <url>/);
assert.equal(run(['--envUrl', 'https://org.crm.dynamics.com'], {
execFile: fakeExecFile(),
platform: 'linux',
getAuthToken: () => fakeJwt(TENANT_A),
}).ok, true);
});