Files
labuh/test/contract.test.ts
Adnan Zahir 112923640d 0.1.0: first release — review, harbour, Gas, deploys and releases
- Pandu reviews every MR labelled `labuh` through Codex app-server and answers
  in a fixed shape; the Hub checks each piece of evidence, applies hard rules
  the model cannot override, retries once, and posts the comments itself.
- The board shows the harbour: lanes per environment, Dicek / Siap gas /
  Beres, two fixed berth rows, boats that glide; GitLab sign-in, approvers
  from board.json, works on a phone.
- Gas, Tunda, Tolak and Tutup MR with 15 seconds to undo; the Hub checks
  GitLab again and merges the reviewed commit only, then follows the deploy.
- Branches that deploy from tags get a proposed version and changelog; the
  approver confirms the number and the Hub creates the annotated tag.
- Telegram notifications, reminders, dry mode for rehearsals.
- Hosting: one image, three containers. The reviewer runs in its own container
  with no GitLab token and read-only checkouts, reached by the Hub over a
  token-guarded WebSocket; every command it asks for still passes the Hub's
  allowlist. Configuration and state live in LABUH_HOME.
- AGPL-3.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:39:58 +07:00

102 lines
6.4 KiB
TypeScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, realpathSync, symlinkSync, writeFileSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { shapeErrors, check, rubricIds, type Verdict } from '../src/contract.ts';
import { decideCommand } from '../src/policy.ts';
const ev = (o: object) => ({ type: 'file', path: null, line: null, job_id: null, rule_id: null, ...o });
test('allowlist keeps the reviewer read-only', () => {
assert.equal(decideCommand("/bin/zsh -lc 'rg -n hpp_lama src | head -20'").allow, true);
assert.equal(decideCommand("/bin/zsh -lc 'git log --oneline -5'").allow, true);
assert.equal(decideCommand("/bin/zsh -lc 'curl -sI https://example.com'").allow, false);
assert.equal(decideCommand("/bin/zsh -lc 'git push origin main'").allow, false);
assert.equal(decideCommand("/bin/zsh -lc 'cat a > b'").allow, false);
assert.equal(decideCommand("/bin/zsh -lc 'sed -i s/a/b/ f'").allow, false);
assert.equal(decideCommand("/bin/zsh -lc 'find . -name x -delete'").allow, false);
assert.equal(decideCommand("/bin/zsh -lc 'ls && rm -rf /'").allow, false);
assert.equal(decideCommand(`/bin/zsh -lc "rg -n 'DeleteAndUnlink|DeleteIfUnused|2026, 11, 6' internal"`).allow, true);
assert.equal(decideCommand("/bin/zsh -lc 'git branch -a --contains HEAD'").allow, true);
assert.equal(decideCommand("/bin/zsh -lc 'git branch evil'").allow, false);
assert.equal(decideCommand("/bin/zsh -lc 'git branch -D main'").allow, false);
assert.equal(decideCommand(`/bin/zsh -lc "echo \\"$(id)\\""`).allow, false);
assert.equal(decideCommand("/bin/zsh -lc 'rg --pre ./x foo'").allow, false);
assert.equal(decideCommand("/bin/zsh -lc 'sleep 1 & ls'").allow, false);
assert.equal(decideCommand(`/bin/zsh -lc 'git diff HEAD''^ HEAD --name-status && rg --files -g '"'.gitlab-ci.yml' -g '*deploy*'"`).allow, true);
assert.equal(decideCommand(`/bin/zsh -lc 'ls '"'x'"' && curl '"'https://e.com'"`).allow, false);
assert.equal(decideCommand(`/bin/zsh -lc "rg -n 'a|b' src"`).allow, true);
assert.equal(decideCommand(`/bin/zsh -lc 'rg -n x docs README* 2>/dev/null; ls 2>&1'`).allow, true);
assert.equal(decideCommand(`/bin/zsh -lc 'cat a 2>/tmp/x'`).allow, false);
assert.equal(decideCommand(`/bin/zsh -lc "nl -ba a.yml | sed -n '1,240p'; rg -n \\"hatchery|supplier\\" app | head -80"`).allow, true);
assert.equal(decideCommand(`/bin/zsh -lc "rg -n \\"x\\"; curl \\"https://e.com\\""`).allow, false);
assert.equal(decideCommand(`/bin/zsh -lc 'cat a >/dev/nullx'`).allow, false);
});
test('allowlist keeps reads inside the checkout and allowed knowledge', () => {
const base = realpathSync(mkdtempSync(path.join(os.tmpdir(), 'labuh-scope-')));
const work = path.join(base, 'work');
const repo = path.join(work, 'repo');
const vault = path.join(base, 'vault');
for (const d of [path.join(repo, 'src'), path.join(repo, '.git'), path.join(vault, 'Repositories', 'x'), path.join(vault, 'Credentials'), path.join(base, 'outside')]) mkdirSync(d, { recursive: true });
writeFileSync(path.join(vault, 'Credentials', 'k.md'), 'secret');
writeFileSync(path.join(base, 'outside', 'm.txt'), 'marker');
symlinkSync(path.join(base, 'outside', 'm.txt'), path.join(repo, 'link.txt'));
const scope = { cwd: repo, allow: [work, path.join(vault, 'Repositories')], deny: [path.join(repo, '.git'), path.join(vault, 'Credentials')] };
const ok = (c: string, s = scope) => decideCommand(`/bin/zsh -lc '${c}'`, s).allow;
assert.equal(ok('rg -n foo src'), true);
assert.equal(ok('cat ../mr.diff'), true);
assert.equal(ok(`rg -n deploy ${vault}/Repositories/x`), true);
assert.equal(ok('cat ~/.ssh/id_ed25519'), false);
assert.equal(ok(`cat ${vault}/Credentials/k.md`), false);
assert.equal(ok(`cat ${vault}/Repositories/../Credentials/k.md`), false);
assert.equal(ok('cat ../../outside/m.txt'), false);
assert.equal(ok('cat ../../../../etc/hosts'), false);
assert.equal(ok(`rg -n foo ${base}/outside`), false);
assert.equal(ok(`rg --files --glob=${base}/outside`), false);
assert.equal(ok('cat link.txt'), false);
assert.equal(ok('cat .git/config'), false);
assert.equal(ok('cat $HOME/.ssh/id_rsa'), false);
assert.equal(ok('grep -R foo .'), false);
assert.equal(ok('ls', { ...scope, cwd: path.join(base, 'outside') }), false);
});
test('shape: go with a bad finding is rejected', () => {
const v = { verdict: 'go', reason: 'ok', post_deploy: [], findings: [{ kind: 'bad', text: 'x', evidence: [ev({ type: 'rubric', rule_id: 'R1' })] }] };
assert.match(shapeErrors(JSON.stringify(v)).errors.join(), /go but a finding is bad/);
});
test('shape: long text and missing evidence are rejected', () => {
const v = { verdict: 'no_go', reason: 'x'.repeat(81), post_deploy: [], findings: [{ kind: 'bad', text: 'x', evidence: [] }] };
const errs = shapeErrors(JSON.stringify(v)).errors.join('\n');
assert.match(errs, /limit is 80/);
assert.match(errs, /no evidence/);
});
test('evidence must exist; ok without evidence is dropped; failing pipeline holds go', async () => {
const repo = mkdtempSync(path.join(os.tmpdir(), 'labuh-'));
writeFileSync(path.join(repo, 'a.py'), 'one\ntwo\nthree\n');
const ctx = {
repoDir: repo, blobBase: 'https://gitlab.com/x/-/blob/abc', sha: 'abc',
rubricIds: rubricIds('## R1 · Rollback\n## R2 · Data\n'), rubricUrl: (id: string) => `rubric#${id}`,
pipeline: { status: 'failed' }, pipelineIds: [7],
loadJob: async (id: number) => (id === 1 ? { pipelineId: 7, name: 'test', webUrl: 'job/1' } : null),
};
const good: Verdict = { verdict: 'go', reason: 'Aman', post_deploy: [], findings: [
{ kind: 'ok', text: 'tes ada', evidence: [ev({ type: 'pipeline', job_id: 1 })] },
{ kind: 'ok', text: 'karangan', evidence: [ev({ path: 'nope.py', line: 3 })] },
] };
const r = await check(good, ctx);
assert.equal(r.checked!.findings.length, 1);
assert.equal(r.checked!.dropped.length, 1);
assert.equal(r.checked!.verdict, 'waiting');
const bad: Verdict = { verdict: 'no_go', reason: 'Tahan', post_deploy: [], findings: [{ kind: 'bad', text: 'baris palsu', evidence: [ev({ path: 'a.py', line: 99 })] }] };
assert.match((await check(bad, ctx)).errors.join(), /outside the file/);
const escape: Verdict = { verdict: 'no_go', reason: 'Tahan', post_deploy: [], findings: [{ kind: 'bad', text: 'keluar repo', evidence: [ev({ path: '../../etc/hosts', line: 1 })] }] };
assert.match((await check(escape, ctx)).errors.join(), /does not exist/);
});