mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-05-16 17:32:13 +08:00
Add discussion audit gate
This commit is contained in:
committed by
Affaan Mustafa
parent
0b6763463f
commit
6887f2952d
258
tests/scripts/discussion-audit.test.js
Normal file
258
tests/scripts/discussion-audit.test.js
Normal file
@@ -0,0 +1,258 @@
|
||||
/**
|
||||
* Tests for scripts/discussion-audit.js
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const { execFileSync, spawnSync } = require('child_process');
|
||||
|
||||
const SCRIPT = path.join(__dirname, '..', '..', 'scripts', 'discussion-audit.js');
|
||||
const { DISCUSSION_QUERY } = require(path.join(__dirname, '..', '..', 'scripts', 'lib', 'github-discussions'));
|
||||
|
||||
function createTempDir(prefix) {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
}
|
||||
|
||||
function cleanup(dirPath) {
|
||||
fs.rmSync(dirPath, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
function discussionGhKey(owner, name, first = 100) {
|
||||
return `api graphql -f owner=${owner} -f name=${name} -F first=${first} -f query=${DISCUSSION_QUERY}`;
|
||||
}
|
||||
|
||||
function writeGhShim(rootDir, responses) {
|
||||
const shimPath = path.join(rootDir, 'gh-shim.js');
|
||||
fs.writeFileSync(shimPath, `
|
||||
const responses = ${JSON.stringify(responses)};
|
||||
const args = process.argv.slice(2);
|
||||
const key = args.join(' ');
|
||||
if (process.env.GITHUB_TOKEN) {
|
||||
console.error('GITHUB_TOKEN should be unset by default');
|
||||
process.exit(42);
|
||||
}
|
||||
if (!Object.prototype.hasOwnProperty.call(responses, key)) {
|
||||
console.error('Unexpected gh args: ' + key);
|
||||
process.exit(3);
|
||||
}
|
||||
process.stdout.write(JSON.stringify(responses[key]));
|
||||
`);
|
||||
return shimPath;
|
||||
}
|
||||
|
||||
function run(args = [], options = {}) {
|
||||
const env = {
|
||||
...process.env,
|
||||
...(options.env || {})
|
||||
};
|
||||
|
||||
return execFileSync('node', [SCRIPT, ...args], {
|
||||
cwd: options.cwd || path.join(__dirname, '..', '..'),
|
||||
env,
|
||||
encoding: 'utf8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
timeout: 10000
|
||||
});
|
||||
}
|
||||
|
||||
function runProcess(args = [], options = {}) {
|
||||
const env = {
|
||||
...process.env,
|
||||
...(options.env || {})
|
||||
};
|
||||
|
||||
return spawnSync('node', [SCRIPT, ...args], {
|
||||
cwd: options.cwd || path.join(__dirname, '..', '..'),
|
||||
env,
|
||||
encoding: 'utf8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
timeout: 10000
|
||||
});
|
||||
}
|
||||
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(` PASS ${name}`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log(` FAIL ${name}`);
|
||||
console.log(` Error: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function runTests() {
|
||||
console.log('\n=== Testing discussion-audit.js ===\n');
|
||||
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
|
||||
if (test('passes when discussions have maintainer touch and accepted answers', () => {
|
||||
const rootDir = createTempDir('discussion-audit-pass-');
|
||||
|
||||
try {
|
||||
const shimPath = writeGhShim(rootDir, {
|
||||
[discussionGhKey('affaan-m', 'everything-claude-code')]: {
|
||||
data: {
|
||||
repository: {
|
||||
hasDiscussionsEnabled: true,
|
||||
discussions: {
|
||||
totalCount: 2,
|
||||
nodes: [
|
||||
{
|
||||
number: 1923,
|
||||
title: 'Does Continuous Learning v2 work with VS Code Claude Code?',
|
||||
url: 'https://github.com/example/discussions/1923',
|
||||
updatedAt: '2026-05-15T19:08:52Z',
|
||||
authorAssociation: 'NONE',
|
||||
category: { name: 'Q&A', isAnswerable: true },
|
||||
answer: { url: 'https://github.com/example/discussions/1923#discussioncomment-1', authorAssociation: 'OWNER' },
|
||||
comments: { nodes: [] }
|
||||
},
|
||||
{
|
||||
number: 73,
|
||||
title: 'Compacting during workflow',
|
||||
url: 'https://github.com/example/discussions/73',
|
||||
updatedAt: '2026-05-15T00:00:00Z',
|
||||
authorAssociation: 'NONE',
|
||||
category: { name: 'General', isAnswerable: false },
|
||||
answer: null,
|
||||
comments: { nodes: [{ authorAssociation: 'MEMBER' }] }
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const parsed = JSON.parse(run([
|
||||
'--json',
|
||||
'--repo',
|
||||
'affaan-m/everything-claude-code'
|
||||
], {
|
||||
cwd: rootDir,
|
||||
env: {
|
||||
ECC_GH_SHIM: shimPath,
|
||||
GITHUB_TOKEN: 'must-be-removed'
|
||||
}
|
||||
}));
|
||||
|
||||
assert.strictEqual(parsed.ready, true);
|
||||
assert.strictEqual(parsed.totals.needingMaintainerTouch, 0);
|
||||
assert.strictEqual(parsed.totals.missingAcceptedAnswer, 0);
|
||||
assert.ok(parsed.checks.some(check => check.id === 'discussion-accepted-answers' && check.status === 'pass'));
|
||||
} finally {
|
||||
cleanup(rootDir);
|
||||
}
|
||||
})) passed++; else failed++;
|
||||
|
||||
if (test('fails when Q&A lacks accepted answer and maintainer touch', () => {
|
||||
const rootDir = createTempDir('discussion-audit-fail-');
|
||||
|
||||
try {
|
||||
const shimPath = writeGhShim(rootDir, {
|
||||
[discussionGhKey('affaan-m', 'everything-claude-code')]: {
|
||||
data: {
|
||||
repository: {
|
||||
hasDiscussionsEnabled: true,
|
||||
discussions: {
|
||||
totalCount: 1,
|
||||
nodes: [
|
||||
{
|
||||
number: 1239,
|
||||
title: 'Losing context',
|
||||
url: 'https://github.com/example/discussions/1239',
|
||||
updatedAt: '2026-05-15T00:00:00Z',
|
||||
authorAssociation: 'NONE',
|
||||
category: { name: 'Q&A', isAnswerable: true },
|
||||
answer: null,
|
||||
comments: { nodes: [] }
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const result = runProcess([
|
||||
'--json',
|
||||
'--repo',
|
||||
'affaan-m/everything-claude-code',
|
||||
'--exit-code'
|
||||
], {
|
||||
cwd: rootDir,
|
||||
env: { ECC_GH_SHIM: shimPath }
|
||||
});
|
||||
const parsed = JSON.parse(result.stdout);
|
||||
|
||||
assert.strictEqual(result.status, 2);
|
||||
assert.strictEqual(parsed.ready, false);
|
||||
assert.strictEqual(parsed.totals.needingMaintainerTouch, 1);
|
||||
assert.strictEqual(parsed.totals.missingAcceptedAnswer, 1);
|
||||
assert.ok(parsed.top_actions.some(action => action.id === 'discussion-maintainer-touch'));
|
||||
assert.ok(parsed.top_actions.some(action => action.id === 'discussion-accepted-answers'));
|
||||
} finally {
|
||||
cleanup(rootDir);
|
||||
}
|
||||
})) passed++; else failed++;
|
||||
|
||||
if (test('writes markdown output as a durable operator artifact', () => {
|
||||
const rootDir = createTempDir('discussion-audit-markdown-');
|
||||
const outputPath = path.join(rootDir, 'artifacts', 'discussion-audit.md');
|
||||
|
||||
try {
|
||||
const shimPath = writeGhShim(rootDir, {
|
||||
[discussionGhKey('affaan-m', 'everything-claude-code')]: {
|
||||
data: {
|
||||
repository: {
|
||||
hasDiscussionsEnabled: true,
|
||||
discussions: { totalCount: 0, nodes: [] }
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const stdout = run([
|
||||
'--markdown',
|
||||
'--write',
|
||||
outputPath,
|
||||
'--repo',
|
||||
'affaan-m/everything-claude-code'
|
||||
], {
|
||||
cwd: rootDir,
|
||||
env: { ECC_GH_SHIM: shimPath }
|
||||
});
|
||||
const written = fs.readFileSync(outputPath, 'utf8');
|
||||
|
||||
assert.strictEqual(stdout, written);
|
||||
assert.ok(written.includes('# ECC Discussion Audit'));
|
||||
assert.ok(written.includes('Answerable discussions missing accepted answer'));
|
||||
assert.ok(written.includes('- none'));
|
||||
} finally {
|
||||
cleanup(rootDir);
|
||||
}
|
||||
})) passed++; else failed++;
|
||||
|
||||
if (test('cli help and invalid args exit cleanly', () => {
|
||||
const help = runProcess(['--help']);
|
||||
assert.strictEqual(help.status, 0);
|
||||
assert.ok(help.stdout.includes('Usage: node scripts/discussion-audit.js'));
|
||||
|
||||
const invalid = runProcess(['--format', 'xml']);
|
||||
assert.strictEqual(invalid.status, 1);
|
||||
assert.ok(invalid.stderr.includes('Invalid format'));
|
||||
})) passed++; else failed++;
|
||||
|
||||
console.log(`\nPassed: ${passed}`);
|
||||
console.log(`Failed: ${failed}`);
|
||||
|
||||
if (failed > 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
runTests();
|
||||
@@ -46,6 +46,7 @@ function buildExpectedPublishPaths(repoRoot) {
|
||||
"scripts/ci/scan-supply-chain-iocs.js",
|
||||
"scripts/consult.js",
|
||||
"scripts/claw.js",
|
||||
"scripts/discussion-audit.js",
|
||||
"scripts/doctor.js",
|
||||
"scripts/status.js",
|
||||
"scripts/sessions-cli.js",
|
||||
@@ -123,6 +124,7 @@ function main() {
|
||||
"scripts/catalog.js",
|
||||
"scripts/ci/scan-supply-chain-iocs.js",
|
||||
"scripts/consult.js",
|
||||
"scripts/discussion-audit.js",
|
||||
"scripts/work-items.js",
|
||||
"scripts/platform-audit.js",
|
||||
".gemini/GEMINI.md",
|
||||
|
||||
@@ -9,6 +9,7 @@ const path = require('path');
|
||||
const { execFileSync, spawnSync } = require('child_process');
|
||||
|
||||
const SCRIPT = path.join(__dirname, '..', '..', 'scripts', 'platform-audit.js');
|
||||
const { DISCUSSION_QUERY } = require(path.join(__dirname, '..', '..', 'scripts', 'lib', 'github-discussions'));
|
||||
|
||||
function createTempDir(prefix) {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
@@ -30,6 +31,7 @@ function seedRepo(rootDir, overrides = {}) {
|
||||
name: 'everything-claude-code',
|
||||
scripts: {
|
||||
'platform:audit': 'node scripts/platform-audit.js',
|
||||
'discussion:audit': 'node scripts/discussion-audit.js',
|
||||
'observability:ready': 'node scripts/observability-readiness.js',
|
||||
'security:ioc-scan': 'node scripts/ci/scan-supply-chain-iocs.js',
|
||||
'harness:audit': 'node scripts/harness-audit.js'
|
||||
@@ -78,6 +80,10 @@ function seedRepo(rootDir, overrides = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
function discussionGhKey(owner, name, first = 100) {
|
||||
return `api graphql -f owner=${owner} -f name=${name} -F first=${first} -f query=${DISCUSSION_QUERY}`;
|
||||
}
|
||||
|
||||
function writeGhShim(rootDir, responses) {
|
||||
const shimPath = path.join(rootDir, 'gh-shim.js');
|
||||
fs.writeFileSync(shimPath, `
|
||||
@@ -237,7 +243,7 @@ function runTests() {
|
||||
const shimPath = writeGhShim(projectRoot, {
|
||||
'pr list --repo affaan-m/everything-claude-code --state open --json number,title,isDraft,mergeStateStatus,updatedAt,url,author': [],
|
||||
'issue list --repo affaan-m/everything-claude-code --state open --json number,title,updatedAt,url,author,labels': [],
|
||||
'api graphql -f owner=affaan-m -f name=everything-claude-code -F first=100 -f query=query($owner: String!, $name: String!, $first: Int!) { repository(owner: $owner, name: $name) { hasDiscussionsEnabled discussions(first: $first, orderBy: {field: UPDATED_AT, direction: DESC}) { totalCount nodes { number title url updatedAt authorAssociation comments(first: 20) { nodes { authorAssociation } } } } } }': {
|
||||
[discussionGhKey('affaan-m', 'everything-claude-code')]: {
|
||||
data: {
|
||||
repository: {
|
||||
hasDiscussionsEnabled: true,
|
||||
@@ -250,6 +256,8 @@ function runTests() {
|
||||
url: 'https://github.com/example/discussions/73',
|
||||
updatedAt: '2026-05-15T00:00:00Z',
|
||||
authorAssociation: 'NONE',
|
||||
category: { name: 'General', isAnswerable: false },
|
||||
answer: null,
|
||||
comments: { nodes: [{ authorAssociation: 'OWNER' }] }
|
||||
}
|
||||
]
|
||||
@@ -276,7 +284,9 @@ function runTests() {
|
||||
assert.strictEqual(parsed.github.totals.openPrs, 0);
|
||||
assert.strictEqual(parsed.github.totals.openIssues, 0);
|
||||
assert.strictEqual(parsed.github.totals.discussionsNeedingMaintainerTouch, 0);
|
||||
assert.strictEqual(parsed.github.totals.discussionsMissingAcceptedAnswer, 0);
|
||||
assert.ok(parsed.checks.some(check => check.id === 'github-discussion-touch' && check.status === 'pass'));
|
||||
assert.ok(parsed.checks.some(check => check.id === 'github-discussion-answers' && check.status === 'pass'));
|
||||
} finally {
|
||||
cleanup(projectRoot);
|
||||
}
|
||||
@@ -299,7 +309,7 @@ function runTests() {
|
||||
const shimPath = writeGhShim(projectRoot, {
|
||||
'pr list --repo affaan-m/everything-claude-code --state open --json number,title,isDraft,mergeStateStatus,updatedAt,url,author': prs,
|
||||
'issue list --repo affaan-m/everything-claude-code --state open --json number,title,updatedAt,url,author,labels': [],
|
||||
'api graphql -f owner=affaan-m -f name=everything-claude-code -F first=100 -f query=query($owner: String!, $name: String!, $first: Int!) { repository(owner: $owner, name: $name) { hasDiscussionsEnabled discussions(first: $first, orderBy: {field: UPDATED_AT, direction: DESC}) { totalCount nodes { number title url updatedAt authorAssociation comments(first: 20) { nodes { authorAssociation } } } } } }': {
|
||||
[discussionGhKey('affaan-m', 'everything-claude-code')]: {
|
||||
data: {
|
||||
repository: {
|
||||
hasDiscussionsEnabled: true,
|
||||
@@ -312,6 +322,8 @@ function runTests() {
|
||||
url: 'https://github.com/example/discussions/1239',
|
||||
updatedAt: '2026-05-15T00:00:00Z',
|
||||
authorAssociation: 'NONE',
|
||||
category: { name: 'Q&A', isAnswerable: true },
|
||||
answer: null,
|
||||
comments: { nodes: [] }
|
||||
}
|
||||
]
|
||||
@@ -336,7 +348,9 @@ function runTests() {
|
||||
assert.strictEqual(parsed.ready, false);
|
||||
assert.ok(parsed.top_actions.some(action => action.id === 'github-open-pr-budget'));
|
||||
assert.ok(parsed.top_actions.some(action => action.id === 'github-discussion-touch'));
|
||||
assert.ok(parsed.top_actions.some(action => action.id === 'github-discussion-answers'));
|
||||
assert.strictEqual(parsed.github.totals.discussionsNeedingMaintainerTouch, 1);
|
||||
assert.strictEqual(parsed.github.totals.discussionsMissingAcceptedAnswer, 1);
|
||||
} finally {
|
||||
cleanup(projectRoot);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user