mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-27 10:24:00 +01:00
feat(health): Step 1 detects tools with gstack-checks detect --for health, no check lost (GST-116 PR 2)
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
a305302963
commit
99b2dd7287
8 files changed
+256
-70
No files matched your search
+31
@@ -0,0 +1,31 @@
|
||||
# /health Step 1 auto-detection, frozen verbatim from health/SKILL.md.tmpl at a305302
|
||||
# (v1.91.365.0) before /health moved onto gstack-checks detect --for health (GST-116 PR 2).
|
||||
# test/health-detect-no-loss.test.ts runs it beside the new detector: every line it prints
|
||||
# must still be found. Do not edit; it is the baseline, not live code.
|
||||
# Type checker
|
||||
[ -f tsconfig.json ] && echo "TYPECHECK: tsc --noEmit"
|
||||
|
||||
# Linter
|
||||
[ -f biome.json ] || [ -f biome.jsonc ] && echo "LINT: biome check ."
|
||||
setopt +o nomatch 2>/dev/null || true
|
||||
ls eslint.config.* .eslintrc.* .eslintrc 2>/dev/null | head -1 | xargs -I{} echo "LINT: eslint ."
|
||||
[ -f .pylintrc ] || [ -f pyproject.toml ] && grep -q "pylint\|ruff" pyproject.toml 2>/dev/null && echo "LINT: ruff check ."
|
||||
|
||||
# Test runner
|
||||
[ -f package.json ] && grep -q '"test"' package.json 2>/dev/null && echo "TEST: $(node -e "console.log(JSON.parse(require('fs').readFileSync('package.json','utf8')).scripts.test)" 2>/dev/null)"
|
||||
[ -f pyproject.toml ] && grep -q "pytest" pyproject.toml 2>/dev/null && echo "TEST: pytest"
|
||||
[ -f Cargo.toml ] && echo "TEST: cargo test"
|
||||
[ -f go.mod ] && echo "TEST: go test ./..."
|
||||
|
||||
# Dead code
|
||||
command -v knip >/dev/null 2>&1 && echo "DEADCODE: knip"
|
||||
[ -f package.json ] && grep -q '"knip"' package.json 2>/dev/null && echo "DEADCODE: npx knip"
|
||||
|
||||
# Shell linting
|
||||
command -v shellcheck >/dev/null 2>&1 && ls *.sh scripts/*.sh bin/*.sh 2>/dev/null | head -1 | xargs -I{} echo "SHELL: shellcheck"
|
||||
|
||||
# GBrain presence — only report as a dimension if gbrain is actually
|
||||
# set up; otherwise skip so machines without gbrain aren't penalized.
|
||||
if command -v gbrain >/dev/null 2>&1 && [ -f "$HOME/.gbrain/config.json" ]; then
|
||||
echo "GBRAIN: gbrain doctor --json (wrapped in timeout 5s)"
|
||||
fi
|
||||
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* /health Step 1 moved onto gstack-checks detect --for health (GST-116 PR 2). The acceptance bar: every check the old
|
||||
* Step 1 found is still found. The old shell block is frozen verbatim in test/fixtures/health-detect-legacy.sh; each
|
||||
* fixture repo runs both under the same PATH and HOME, and every legacy (category, command) pair must appear in the
|
||||
* detector's DIMENSION lines.
|
||||
*/
|
||||
import { afterAll, describe, expect, test } from 'bun:test';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import * as fs from 'node:fs';
|
||||
import * as os from 'node:os';
|
||||
import * as path from 'node:path';
|
||||
import { HEALTH_DIMENSIONS, healthDimensions } from '../lib/checks';
|
||||
import { runScriptSync } from '../lib/script-invocation';
|
||||
import { BIN } from './helpers/pregate-fixture';
|
||||
|
||||
const LEGACY = path.join(import.meta.dir, 'fixtures', 'health-detect-legacy.sh');
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'health-detect-'));
|
||||
afterAll(() => fs.rmSync(tmp, { recursive: true, force: true }));
|
||||
|
||||
const stubs = path.join(tmp, 'stubs');
|
||||
fs.mkdirSync(stubs);
|
||||
for (const tool of ['knip', 'shellcheck', 'gbrain']) {
|
||||
fs.writeFileSync(path.join(stubs, tool), '#!/bin/sh\nexit 0\n');
|
||||
fs.chmodSync(path.join(stubs, tool), 0o755);
|
||||
}
|
||||
const gbrainHome = path.join(tmp, 'home-gbrain');
|
||||
fs.mkdirSync(path.join(gbrainHome, '.gbrain'), { recursive: true });
|
||||
fs.writeFileSync(path.join(gbrainHome, '.gbrain', 'config.json'), '{}\n');
|
||||
const bareHome = path.join(tmp, 'home-bare');
|
||||
fs.mkdirSync(bareHome);
|
||||
|
||||
const PKG = JSON.stringify({ name: 'fx', scripts: { test: 'vitest run', lint: 'eslint .' }, devDependencies: { knip: '^5.0.0' } });
|
||||
const FIXTURES: Record<string, Record<string, string>> = {
|
||||
empty: {},
|
||||
tsconfig: { 'tsconfig.json': '{}' },
|
||||
'biome-json': { 'biome.json': '{}' },
|
||||
'biome-jsonc': { 'biome.jsonc': '{}' },
|
||||
'eslint-flat': { 'eslint.config.mjs': 'export default [];' },
|
||||
'eslintrc-json': { '.eslintrc.json': '{}' },
|
||||
'eslintrc-bare': { '.eslintrc': '{}' },
|
||||
'pyproject-ruff-pytest': { 'pyproject.toml': '[tool.ruff]\n[tool.pytest.ini_options]\n' },
|
||||
'pyproject-pylint': { 'pyproject.toml': '[tool.pylint]\n' },
|
||||
'pylintrc-only': { '.pylintrc': '[MASTER]\n' },
|
||||
'package-test-knip': { 'package.json': PKG },
|
||||
cargo: { 'Cargo.toml': '[package]\nname = "fx"\n' },
|
||||
go: { 'go.mod': 'module fx\n' },
|
||||
'shell-scripts-dir': { 'scripts/build.sh': 'echo hi\n' },
|
||||
'shell-root-and-bin': { 'run.sh': 'echo hi\n', 'bin/tool.sh': 'echo hi\n' },
|
||||
everything: { 'tsconfig.json': '{}', 'biome.json': '{}', 'eslint.config.js': '', 'pyproject.toml': 'ruff pytest\n', 'package.json': PKG, 'Cargo.toml': '', 'go.mod': 'module fx\n', 'scripts/a.sh': '' },
|
||||
};
|
||||
|
||||
function repo(name: string, files: Record<string, string>): string {
|
||||
const dir = path.join(tmp, 'repos', name);
|
||||
for (const [file, body] of Object.entries(files)) {
|
||||
fs.mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, file), body);
|
||||
}
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
return dir;
|
||||
}
|
||||
|
||||
const NORMALIZE: Record<string, string> = { 'gbrain doctor --json (wrapped in timeout 5s)': 'timeout 5s gbrain doctor --json' };
|
||||
const pairs = (lines: Array<[string, string]>) => lines.map(([d, c]) => `${d}: ${c}`).sort();
|
||||
|
||||
function legacy(dir: string, env: NodeJS.ProcessEnv): string[] {
|
||||
const r = spawnSync('bash', [LEGACY], { cwd: dir, env, encoding: 'utf8', timeout: 30_000 });
|
||||
expect(r.status).toBe(0);
|
||||
return pairs(r.stdout.split('\n').flatMap(line => {
|
||||
const m = /^(TYPECHECK|LINT|TEST|DEADCODE|SHELL|GBRAIN): (.+)$/.exec(line);
|
||||
return m ? [[m[1].toLowerCase(), NORMALIZE[m[2]] ?? m[2]] as [string, string]] : [];
|
||||
}));
|
||||
}
|
||||
|
||||
function detector(dir: string, env: NodeJS.ProcessEnv): { found: string[]; none: string[] } {
|
||||
const r = runScriptSync(BIN('gstack-checks'), ['detect', '--for', 'health'], { cwd: dir, env, encoding: 'utf8', timeout: 60_000 });
|
||||
expect(r.status).toBe(0);
|
||||
const lines = (r.stdout ?? '').split('\n').filter(Boolean).map(line => /^DIMENSION (\S+) (.+?) \((.*)\)$/.exec(line)!);
|
||||
expect(lines.every(Boolean)).toBe(true);
|
||||
return { found: pairs(lines.filter(m => m[2] !== 'none').map(m => [m[1], m[2]])), none: lines.filter(m => m[2] === 'none').map(m => m[1]) };
|
||||
}
|
||||
|
||||
const ENVS = {
|
||||
'tools on PATH, gbrain configured': { PATH: `${stubs}${path.delimiter}${process.env.PATH}`, HOME: gbrainHome },
|
||||
'machine PATH, no gbrain config': { PATH: process.env.PATH, HOME: bareHome },
|
||||
};
|
||||
|
||||
describe('/health detection: no check lost (GST-116 PR 2)', () => {
|
||||
// Value: protects=/health still finds every tool its old Step 1 shell block found; fails_when=a ported rule drops a
|
||||
// category or command (knip, shellcheck, gbrain, a lint or test rule) for any fixture; why_new=/health moved onto the
|
||||
// detector in this change; seam=the frozen legacy block run beside the bin; no_claim=that the found tools succeed
|
||||
for (const [envName, envVars] of Object.entries(ENVS)) {
|
||||
test(`every legacy line is found: ${envName}`, () => {
|
||||
const env = { ...process.env, ...envVars };
|
||||
let legacyTotal = 0;
|
||||
for (const [name, files] of Object.entries(FIXTURES)) {
|
||||
const dir = repo(name, files);
|
||||
const old = legacy(dir, env);
|
||||
const now = detector(dir, env);
|
||||
legacyTotal += old.length;
|
||||
expect({ name, lost: old.filter(p => !now.found.includes(p)) }).toEqual({ name, lost: [] });
|
||||
expect(new Set([...now.found.map(p => p.split(':')[0]), ...now.none])).toEqual(new Set(HEALTH_DIMENSIONS));
|
||||
}
|
||||
expect(legacyTotal).toBeGreaterThan(20);
|
||||
});
|
||||
}
|
||||
|
||||
// Value: protects=the matrix exercises knip, shellcheck and gbrain, not just the manifest rules; fails_when=a stub stops
|
||||
// being found so the no-loss comparison above runs with those categories empty; why_new=new fixture; seam=none
|
||||
test('the stubbed run covers every category on both sides', () => {
|
||||
const env = { ...process.env, ...ENVS['tools on PATH, gbrain configured'] };
|
||||
const dir = repo('everything', FIXTURES.everything);
|
||||
expect(new Set(legacy(dir, env).map(p => p.split(':')[0]))).toEqual(new Set(HEALTH_DIMENSIONS));
|
||||
expect(detector(dir, env).none).toEqual([]);
|
||||
});
|
||||
|
||||
// Value: protects=a Health Stack section wins row for row, test rows included, and nothing is auto-detected beside it;
|
||||
// fails_when=health mode drops the test row /verify leaves to pregate, or mixes manifest detection in; why_new=keepTests
|
||||
// is new; seam=none
|
||||
test('a Health Stack section is returned as written', () => {
|
||||
const dir = repo('health-stack', { 'tsconfig.json': '{}', 'CLAUDE.md': '# x\n\n## Health Stack\n\n- typecheck: tsc --noEmit -p app\n- test: bun test\n- deadcode: knip --production\n- shell: shellcheck *.sh scripts/*.sh\n\n## Other\n' });
|
||||
const d = healthDimensions(dir, { PATH: stubs, HOME: gbrainHome });
|
||||
expect(d.checks.map(c => [c.dimension, c.command])).toEqual([['typecheck', 'tsc --noEmit -p app'], ['test', 'bun test'], ['deadcode', 'knip --production'], ['shell', 'shellcheck *.sh scripts/*.sh']]);
|
||||
expect(d.missing).toEqual([{ dimension: 'lint', reason: 'not in the CLAUDE.md Health Stack section' }, { dimension: 'gbrain', reason: 'not in the CLAUDE.md Health Stack section' }]);
|
||||
});
|
||||
|
||||
// Value: protects=an unavailable category says why, so Step 2 records SKIPPED with a reason; fails_when=a none line
|
||||
// loses its reason or shellcheck's two causes collapse; why_new=new output; seam=none
|
||||
test('none lines name the reason', () => {
|
||||
const empty = healthDimensions(repo('empty', {}), { PATH: '', HOME: bareHome });
|
||||
expect(empty.checks).toEqual([]);
|
||||
expect(empty.missing.find(m => m.dimension === 'shell')?.reason).toBe('shellcheck is not on PATH');
|
||||
const noScripts = healthDimensions(repo('tsconfig', FIXTURES.tsconfig), { PATH: stubs, HOME: bareHome });
|
||||
expect(noScripts.missing.find(m => m.dimension === 'shell')?.reason).toBe('no *.sh file in the root, scripts/ or bin/');
|
||||
expect(noScripts.missing.find(m => m.dimension === 'gbrain')?.reason).toBe('gbrain is not on PATH or ~/.gbrain/config.json is absent');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user