Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion web/scripts/__tests__/check-governance-health.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type {
ActivityData,
Comment,
Expand All @@ -20,6 +20,7 @@ import {
extractRole,
hadQuorumFailure,
inferEligibleVoterCount,
parseArgs,
percentile,
resolveActivityFile,
} from '../check-governance-health';
Expand Down Expand Up @@ -87,6 +88,52 @@ function minimalData(overrides: Partial<ActivityData> = {}): ActivityData {
};
}

afterEach(() => {
vi.restoreAllMocks();
});

function stubProcessExit(): ReturnType<typeof vi.spyOn> {
return vi.spyOn(process, 'exit').mockImplementation(((code?: number) => {
throw new Error(`process.exit:${code ?? 0}`);
}) as typeof process.exit);
}

// ──────────────────────────────────────────────
// parseArgs
// ──────────────────────────────────────────────

describe('parseArgs', () => {
it('defaults to text output', () => {
expect(parseArgs([])).toEqual({ json: false });
});

it('enables json output with --json', () => {
expect(parseArgs(['--json'])).toEqual({ json: true });
});

it('prints help and exits 0 for --help', () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const exitSpy = stubProcessExit();

expect(() => parseArgs(['--help'])).toThrow('process.exit:0');
expect(logSpy).toHaveBeenCalledWith(
'Usage: npm run check-governance-health -- [--json]'
);
expect(exitSpy).toHaveBeenCalledWith(0);
});

it('rejects unknown flags', () => {
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const exitSpy = stubProcessExit();

expect(() => parseArgs(['--jsom'])).toThrow('process.exit:1');
expect(errorSpy).toHaveBeenCalledWith(
'Unknown argument "--jsom". Expected --json or --help.'
);
expect(exitSpy).toHaveBeenCalledWith(1);
});
});

// ──────────────────────────────────────────────
// extractRole
// ──────────────────────────────────────────────
Expand Down
35 changes: 33 additions & 2 deletions web/scripts/check-governance-health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const DEFAULT_ACTIVITY_FILE = join(
'activity.json'
);

interface CliOptions {
json: boolean;
}

// ──────────────────────────────────────────────
// Types
// ──────────────────────────────────────────────
Expand Down Expand Up @@ -708,14 +712,41 @@ export function resolveActivityFile(
return env.ACTIVITY_FILE ?? DEFAULT_ACTIVITY_FILE;
}

function printHelp(): void {
console.log('Usage: npm run check-governance-health -- [--json]');
}

export function parseArgs(argv: string[]): CliOptions {
const options: CliOptions = {
json: false,
};

for (const arg of argv) {
if (arg === '--json') {
options.json = true;
continue;
}

if (arg === '--help') {
printHelp();
process.exit(0);
}

console.error(`Unknown argument "${arg}". Expected --json or --help.`);
process.exit(1);
}

return options;
}

function isDirectExecution(): boolean {
if (!process.argv[1]) return false;
return resolve(process.argv[1]) === resolve(fileURLToPath(import.meta.url));
}

async function main(): Promise<void> {
const activityFile = resolveActivityFile();
const isJson = process.argv.includes('--json');
const { json } = parseArgs(process.argv.slice(2));

if (!existsSync(activityFile)) {
console.error(`Activity file not found: ${activityFile}`);
Expand All @@ -728,7 +759,7 @@ async function main(): Promise<void> {
const data = JSON.parse(readFileSync(activityFile, 'utf-8')) as ActivityData;
const report = buildHealthReport(data);

if (isJson) {
if (json) {
console.log(JSON.stringify(report, null, 2));
} else {
printReport(report);
Expand Down
Loading