grt
Version:
grt command-line interface โ test, diff, preview APIs
72 lines (60 loc) โข 2.41 kB
JavaScript
import axios from 'axios';
import { getToken } from './config.js';
import chalk from 'chalk';
export async function runTest({ owner, repo, ref, url }) {
const greatlabsToken = process.env.greatlabs_TOKEN || getToken();
console.log(`๐ Fetching schema for ${owner}/${repo}@${ref}...`);
try {
const schemaRes = await axios.get(`http://localhost:3000/schemas/${owner}/${repo}/${ref}`, {
headers: { Authorization: `Bearer ${greatlabsToken}` }
});
const schema = schemaRes.data;
const paths = schema.paths || {};
let passed = 0;
let failed = 0;
for (const [path, methods] of Object.entries(paths)) {
for (const [method, details] of Object.entries(methods)) {
const endpoint = `${url}${path}`;
try {
const res = await axios({
method,
url: endpoint,
validateStatus: () => true,
});
const status = res.status;
if (status >= 200 && status < 300) {
console.log(chalk.green(`โ
${method.toUpperCase()} ${endpoint} (${status})`));
passed++;
} else {
console.log(chalk.red(`โ ${method.toUpperCase()} ${endpoint} โ ${status}`));
failed++;
}
} catch (err) {
console.log(chalk.red(`โ ${method.toUpperCase()} ${endpoint} โ ${err.message}`));
failed++;
}
}
}
console.log(chalk.bold(`\n๐งช Test Summary: ${passed} passed, ${failed} failed`));
if (failed > 0) process.exit(1);
} catch (err) {
console.error('โ Failed to fetch schema:', err.message);
process.exit(1);
}
}
// usage.js โ shows mock/test/diff usage from backend
export async function runUsage({ owner, repo }) {
const token = getToken();
try {
const res = await axios.get(`http://localhost:3000/projects/${owner}/${repo}/usage`, {
headers: { Authorization: `Bearer ${token}` }
});
const { mockCalls, testRuns, diffsGenerated } = res.data;
console.log(chalk.blue(`\n๐ Usage Report for ${owner}/${repo}`));
console.log(`Mock Calls: ${mockCalls}`);
console.log(`Contract Tests: ${testRuns}`);
console.log(`Schema Diffs: ${diffsGenerated}\n`);
} catch (err) {
console.error(chalk.red('โ Failed to fetch usage stats:'), err.message);
}
}