180 lines
5.2 KiB
TypeScript
180 lines
5.2 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
import { join, resolve } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
import { readFixture } from './fixture.js';
|
|
|
|
const f = readFixture();
|
|
const root = resolve(import.meta.dirname, '..');
|
|
const directory = await mkdtemp(join(tmpdir(), 'gitza-mcp-artifact-'));
|
|
function run(command: string, args: string[], cwd = root) {
|
|
const result = spawnSync(command, args, { cwd, encoding: 'utf8', timeout: 120000 });
|
|
assert.equal(result.status, 0, command + ' failed: ' + result.stderr);
|
|
return result.stdout.trim();
|
|
}
|
|
async function check(command: string, args: string[], upstream = f.upstream) {
|
|
const transport = new StdioClientTransport({
|
|
command,
|
|
args,
|
|
env: {
|
|
PATH: process.env.PATH!,
|
|
GITZA_TOKEN: f.readToken,
|
|
GITZA_URL: upstream,
|
|
GITZA_REPOSITORIES: 'alice/project',
|
|
GITZA_DEVELOPMENT: 'true',
|
|
},
|
|
stderr: 'pipe',
|
|
});
|
|
const client = new Client({ name: 'artifact-check', version: '1' });
|
|
let stderr = '';
|
|
transport.stderr?.on('data', (chunk) => {
|
|
stderr += String(chunk);
|
|
});
|
|
try {
|
|
await client.connect(transport);
|
|
assert.equal((await client.listTools()).tools.length, 15);
|
|
const result = await client.callTool({
|
|
name: 'get_file',
|
|
arguments: { repository: 'alice/project', path: 'src/hello.txt' },
|
|
});
|
|
assert.notEqual(result.isError, true);
|
|
assert.match(JSON.stringify(result), /one/);
|
|
} finally {
|
|
await client.close();
|
|
}
|
|
assert.ok(!stderr.includes(f.readToken));
|
|
}
|
|
try {
|
|
const [packed] = JSON.parse(
|
|
run('npm', ['pack', '--ignore-scripts', '--json', '--pack-destination', directory]),
|
|
);
|
|
const files = packed.files.map((file: { path: string }) => file.path) as string[];
|
|
assert.ok(
|
|
files.includes('dist/cli.js') &&
|
|
files.includes('migrations/001_initial.sql') &&
|
|
files.includes('LICENSE'),
|
|
);
|
|
assert.ok(!files.some((path) => /(^|\/)(tests|src|\.env|node_modules)(\/|$)/.test(path)));
|
|
run('npm', [
|
|
'install',
|
|
'--prefix',
|
|
directory,
|
|
'--ignore-scripts',
|
|
'--no-audit',
|
|
'--no-fund',
|
|
join(directory, packed.filename),
|
|
]);
|
|
await check(process.execPath, [join(directory, 'node_modules/@gitza/mcp/dist/cli.js'), 'stdio']);
|
|
process.stdout.write(
|
|
'Packed npm consumer: initialize, tools/list and private file read passed.\n',
|
|
);
|
|
if (process.env.MCP_TEST_IMAGE) {
|
|
assert.ok(
|
|
f.project?.startsWith('gitza-mcp-test-'),
|
|
'Container tests require the isolated fixture project.',
|
|
);
|
|
assert.equal(
|
|
run('docker', [
|
|
'image',
|
|
'inspect',
|
|
'--format',
|
|
'{{.Config.User}}',
|
|
process.env.MCP_TEST_IMAGE,
|
|
]),
|
|
'node',
|
|
);
|
|
await check(
|
|
'docker',
|
|
[
|
|
'run',
|
|
'--rm',
|
|
'-i',
|
|
'--read-only',
|
|
'--cap-drop=ALL',
|
|
'--security-opt=no-new-privileges',
|
|
'--network',
|
|
'container:' + f.project + '-forgejo-1',
|
|
'-e',
|
|
'GITZA_TOKEN',
|
|
'-e',
|
|
'GITZA_URL',
|
|
'-e',
|
|
'GITZA_REPOSITORIES',
|
|
'-e',
|
|
'GITZA_DEVELOPMENT',
|
|
process.env.MCP_TEST_IMAGE,
|
|
'stdio',
|
|
],
|
|
'http://127.0.0.1:3000',
|
|
);
|
|
process.stdout.write(
|
|
'Non-root, read-only container: initialize, tools/list and private file read passed.\n',
|
|
);
|
|
const database = new URL(f.env.DATABASE_URL!);
|
|
database.hostname = 'postgres';
|
|
database.port = '5432';
|
|
const environment = { ...f.env, DATABASE_URL: database.href, HOST: '0.0.0.0', PORT: '3400' };
|
|
const envFile = join(directory, 'container.env');
|
|
await writeFile(
|
|
envFile,
|
|
Object.entries(environment)
|
|
.map(([key, value]) => key + '=' + value)
|
|
.join('\n'),
|
|
{ mode: 0o600 },
|
|
);
|
|
const network = f.project + '_default';
|
|
run('docker', [
|
|
'run',
|
|
'--rm',
|
|
'--network',
|
|
network,
|
|
'--env-file',
|
|
envFile,
|
|
process.env.MCP_TEST_IMAGE,
|
|
'migrate',
|
|
]);
|
|
const name = f.project + '-artifact';
|
|
run('docker', [
|
|
'run',
|
|
'--rm',
|
|
'-d',
|
|
'--name',
|
|
name,
|
|
'--network',
|
|
network,
|
|
'--env-file',
|
|
envFile,
|
|
'--read-only',
|
|
'--cap-drop=ALL',
|
|
'--security-opt=no-new-privileges',
|
|
process.env.MCP_TEST_IMAGE,
|
|
]);
|
|
try {
|
|
run('docker', [
|
|
'exec',
|
|
name,
|
|
'node',
|
|
'--input-type=module',
|
|
'-e',
|
|
`
|
|
import { spawnSync } from 'node:child_process';
|
|
for (let i = 0; i < 30; i++) {
|
|
if (spawnSync(process.execPath, ['dist/healthcheck.js']).status === 0) process.exit(0);
|
|
await new Promise(r => setTimeout(r, 250));
|
|
}
|
|
process.exit(1);
|
|
`,
|
|
]);
|
|
process.stdout.write(
|
|
'Hosted container: migration command and PostgreSQL readiness passed.\n',
|
|
);
|
|
} finally {
|
|
run('docker', ['stop', '--time', '25', name]);
|
|
}
|
|
}
|
|
} finally {
|
|
await rm(directory, { recursive: true, force: true });
|
|
}
|