23f7624596
ADR-166 MCP Bridge Security Lock / Static-source security lock (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / Compose default binds loopback + Mongo has auth (push) Failing after 2s
CodeQL Advanced / Analyze (rust) (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / plugin-agent-federation bindHost default (push) Failing after 1s
ADR-166 MCP Bridge Security Lock / Runtime behavior — 401 + terminal gate + fail-closed (push) Failing after 4s
business-pods-smoke / smoke (push) Failing after 1s
all-plugins-smoke / smoke-all (push) Failing after 2s
CI/CD Pipeline / Security & Code Quality (push) Failing after 1s
CI/CD Pipeline / Test Suite (ubuntu-latest) (push) Failing after 1s
CI/CD Pipeline / Build & Package (macos-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (ubuntu-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (windows-latest) (push) Has been skipped
CI/CD Pipeline / Documentation & Examples (push) Failing after 1s
Clone Tracker (14-day rolling) / Snapshot clones for ruflo ecosystem (push) Failing after 1s
CodeQL Advanced / Analyze (actions) (push) Failing after 1s
CodeQL Advanced / Analyze (javascript-typescript) (push) Failing after 1s
federation-peer-rust / stable-noop (push) Failing after 1s
metaharness-ci / score (push) Failing after 1s
metaharness-ci / router-compat (push) Failing after 0s
metaharness-ci / similarity-tests (push) Failing after 0s
no-agentbbs-smoke / smoke-without-agentbbs (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (windows-latest) (push) Has been skipped
codex-integration-audit / Codex integration audit (push) Failing after 1s
helpers-manifest-guard / guard (push) Failing after 1s
🔗 Cross-Agent Integration Tests / 🤝 Agent Coordination Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🧠 Memory Sharing Integration (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🛡️ Fault Tolerance Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / ⚡ Performance Integration Tests (push) Has been skipped
metaharness-ci / mcp-scan (push) Failing after 1s
metaharness-ci / eject-dryrun (push) Failing after 1s
metaharness-ci / metaharness-real-data (push) Failing after 0s
no-cli-optdep-bloat-2561 / guard (push) Failing after 1s
no-metaharness-smoke / smoke-without-metaharness (push) Failing after 1s
no-phantom-agentic-flow-subpath / guard (push) Failing after 1s
🔄 Automated Rollback Manager / 🚨 Failure Detection (push) Failing after 1s
V3 CI/CD Pipeline / Plugin hooks smoke / ubuntu-latest / Node 22 (push) Failing after 1s
V3 CI/CD Pipeline / ruflo-graph-intelligence build + test smoke (#2044, ADR-123) (push) Failing after 1s
CVE Audit Gate / Audit root (critical-blocking) (push) Failing after 2s
cost-tracker-smoke / smoke (push) Failing after 3s
oia-audit-weekly / audit (push) Failing after 2s
ruflo-agent-smoke / ruflo-agent structural smoke (push) Failing after 1s
📊 Status Badges Update / 📊 Update Status Badges (push) Failing after 1s
V3 CI/CD Pipeline / Static regression guards (#2267 YAML + (push) Failing after 1s
V3 CI/CD Pipeline / Test V3 Packages (push) Failing after 0s
V3 CI/CD Pipeline / agent_execute provider routing smoke (#2042) (push) Failing after 0s
CVE Audit Gate / Audit v3 (critical-blocking) (push) Failing after 1s
federation-peer-rust / stable-native (push) Failing after 2s
🔗 Cross-Agent Integration Tests / 🚀 Integration Test Setup (push) Failing after 2s
neural-trader-smoke / runtime-smoke (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (macos-latest) (push) Has been skipped
V3 CI/CD Pipeline / Build V3 (ubuntu-latest) (push) Has been skipped
V3 CI/CD Pipeline / Type Check V3 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 24 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 22 (push) Failing after 2s
V3 CI/CD Pipeline / browser rvf create flag smoke (#2015) (push) Failing after 0s
V3 CI/CD Pipeline / Dependency review (#2046) (push) Has been skipped
V3 CI/CD Pipeline / Supply-chain audit (#2046) (push) Failing after 0s
V3 CI/CD Pipeline / witness marker drift smoke (#2021) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader portfolio CG smoke (#2068, ADR-126 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader backtest signing smoke (#2068, ADR-126 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / kg-extract type-import classification smoke (#2049) (push) Failing after 0s
V3 CI/CD Pipeline / witness verify precondition smoke (#1880) (push) Failing after 2s
V3 CI/CD Pipeline / neural-trader pipeline risk-gate smoke (#2068, ADR-126 Phase 5) (push) Failing after 0s
V3 CI/CD Pipeline / neural-trader feature attribution smoke (#2068, ADR-126 Phase 6) (push) Failing after 0s
V3 CI/CD Pipeline / plugin-registry signature verification smoke (#1922, CWE-347) (push) Failing after 4s
V3 CI/CD Pipeline / memory stats legacy-DB smoke (#2120) (push) Failing after 4s
V3 CI/CD Pipeline / github deprecated actions smoke (#2089, ADR-127 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / graph query + pathfinder smoke (ADR-130 P2+P5) (push) Has been skipped
V3 CI/CD Pipeline / graph trajectory hooks smoke (ADR-130 P3) (push) Has been skipped
V3 CI/CD Pipeline / graph plugin adapter smoke (ADR-130 P4) (push) Has been skipped
V3 CI/CD Pipeline / graph benchmark (ADR-130 P6) (push) Has been skipped
V3 CI/CD Pipeline / statusline generator delegation smoke (#2195) (push) Failing after 1s
V3 CI/CD Pipeline / wizard init regression guard (#2206 (push) Failing after 1s
V3 CI/CD Pipeline / memory no-stray-db smoke (ADR-125 P7) (push) Failing after 1s
V3 CI/CD Pipeline / github-safe injection smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github actions pin smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github attribution opt-in smoke (#2089, ADR-127 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / pre-bash hook safety smoke (#2017) (push) Failing after 1s
V3 CI/CD Pipeline / Memory import smoke / ubuntu-latest (push) Failing after 0s
V3 CI/CD Pipeline / MCP protocol smoke / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / ruvllm WASM auto-init smoke (#2086) (push) Failing after 4s
V3 CI/CD Pipeline / MCP paired-tool round-trip smoke (#1889) (push) Failing after 1s
V3 CI/CD Pipeline / Plugin package install-safety (#1902/#1903/#1904) (push) Failing after 1s
V3 CI/CD Pipeline / Tool description discoverability (ADR-112) (push) Failing after 3s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (22) (push) Failing after 1s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (24) (push) Failing after 1s
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Vector-index dimension audit (#1947) (push) Failing after 0s
V3 CI/CD Pipeline / Hook-command install safety (#1921) (push) Failing after 1s
V3 CI/CD Pipeline / ToolOutputGuardrail smoke (ADR-131, (push) Failing after 1s
V3 CI/CD Pipeline / init-bundle invariants smoke (#2095, ADR-128 Phase 5) (push) Failing after 1s
V3 CI/CD Pipeline / wasm provider bridge smoke (ADR-129 P1) (push) Failing after 2s
V3 CI/CD Pipeline / wasm gallery CRUD smoke (ADR-129 P3) (push) Failing after 1s
V3 CI/CD Pipeline / wasm plugin bridge smoke (ADR-129 P4) (push) Failing after 0s
V3 CI/CD Pipeline / wasm compose smoke (ADR-129 P2) (push) Failing after 4s
V3 CI/CD Pipeline / graph schema smoke (ADR-130 P1) (push) Failing after 0s
Validate Marketplace / validate (push) Failing after 1s
🔍 Verification Pipeline / 🚀 Setup Verification (push) Failing after 1s
🔍 Verification Pipeline / 🛡️ Security Verification (push) Has been skipped
🔍 Verification Pipeline / 📝 Code Quality (push) Has been skipped
🔍 Verification Pipeline / 🧪 Test Verification (${{ matrix.os }}, Node ${{ matrix.node }}) (push) Has been skipped
🔍 Verification Pipeline / 🏗️ Build Verification (push) Has been skipped
🔍 Verification Pipeline / 📚 Documentation Verification (push) Has been skipped
CVE Audit Gate / High-severity report (warn only) (push) Has been cancelled
🔄 Automated Rollback Manager / 🔄 Execute Rollback (push) Has been cancelled
🔄 Automated Rollback Manager / ✅ Post-Rollback Verification (push) Has been cancelled
🔄 Automated Rollback Manager / 📊 Rollback Monitoring (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / windows-latest (push) Has been cancelled
🔄 Automated Rollback Manager / ⏳ Manual Rollback Approval (push) Has been cancelled
V3 CI/CD Pipeline / MCP protocol smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Memory import smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / ubuntu-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Publish to npm (alpha) (push) Has been cancelled
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / macos-latest / Node 22 (push) Has been cancelled
V3 CI/CD Pipeline / Plugin hooks smoke / macos-latest / Node 22 (push) Has been cancelled
CI/CD Pipeline / Deploy & Release (push) Has been cancelled
CI/CD Pipeline / CI Status (push) Has been cancelled
🔗 Cross-Agent Integration Tests / 📊 Integration Test Report (push) Has been cancelled
🔄 Automated Rollback Manager / 🔍 Pre-Rollback Validation (push) Has been cancelled
🔍 Verification Pipeline / ⚡ Performance Verification (push) Has been cancelled
🔍 Verification Pipeline / 📊 Verification Report (push) Has been cancelled
312 lines
12 KiB
TypeScript
312 lines
12 KiB
TypeScript
/**
|
||
* Tests for AtomicBbsRoomBudgetTracker — ADR-164.1.
|
||
*
|
||
* Implements Tests 1, 2, 3, 6 from §9. Test 4 (mid-transaction crash) is
|
||
* covered by a synchronous throw-during-reserve scenario; Test 5 (clock-skew
|
||
* backward) is intentionally skipped as it requires system-clock control
|
||
* beyond what the test harness exposes — see ADR-164.1 §9 Test 5 for the
|
||
* design intent.
|
||
*
|
||
* Uses better-sqlite3 in-memory mode for speed.
|
||
*/
|
||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||
import { createRequire } from 'node:module';
|
||
|
||
import {
|
||
AtomicBbsRoomBudgetTracker,
|
||
clampReservationExpiry,
|
||
RESERVATION_EXPIRY_FLOOR_MS,
|
||
RESERVATION_EXPIRY_CEILING_MS,
|
||
createAtomicTrackerIfEnabled,
|
||
type BudgetAuditSink,
|
||
type SqliteDatabase,
|
||
} from '../src/business-pods/bbs-budget-tracker.js';
|
||
|
||
// Resolve better-sqlite3 via CJS require so tests work even when ESM
|
||
// resolution from this nested test depth has trouble locating the
|
||
// hoisted v3-level dependency.
|
||
const cjsRequire = createRequire(import.meta.url);
|
||
type DatabaseCtor = new (filename: string) => SqliteDatabase;
|
||
|
||
function openDb(): SqliteDatabase {
|
||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||
const Database = cjsRequire('better-sqlite3') as DatabaseCtor;
|
||
return new Database(':memory:');
|
||
}
|
||
|
||
let db: SqliteDatabase;
|
||
let auditEvents: Array<{ type: string; payload: Record<string, unknown> }>;
|
||
let audit: BudgetAuditSink;
|
||
|
||
beforeEach(() => {
|
||
db = openDb();
|
||
auditEvents = [];
|
||
audit = {
|
||
emit: (type, payload) => { auditEvents.push({ type, payload }); },
|
||
};
|
||
});
|
||
|
||
afterEach(() => {
|
||
try { db.close(); } catch { /* noop */ }
|
||
});
|
||
|
||
// ---------- clamp ---------------------------------------------------------
|
||
|
||
describe('clampReservationExpiry', () => {
|
||
it('clamps small values up to the 5s floor', () => {
|
||
expect(clampReservationExpiry(1000)).toBe(RESERVATION_EXPIRY_FLOOR_MS);
|
||
});
|
||
it('clamps large values down to the 5min ceiling', () => {
|
||
expect(clampReservationExpiry(60 * 60 * 1000)).toBe(RESERVATION_EXPIRY_CEILING_MS);
|
||
});
|
||
it('passes through values within bounds', () => {
|
||
expect(clampReservationExpiry(30_000)).toBe(30_000);
|
||
});
|
||
});
|
||
|
||
// ---------- Test 1: serial reserves under a cap (drop-in for §9 Test 1) --
|
||
|
||
describe('Test 1 — serial reserves under a $1.00 cap', () => {
|
||
it('exactly 20 of 100 callers reserving $0.05 succeed; 80 BUDGET_EXCEEDED; total = $1.00', () => {
|
||
const tracker = new AtomicBbsRoomBudgetTracker({ db, audit, defaultExpiryMs: 60_000 });
|
||
tracker.registerRoom('sales-test', 1.0);
|
||
|
||
let ok = 0;
|
||
let exceeded = 0;
|
||
for (let i = 0; i < 100; i++) {
|
||
const r = tracker.reserve('sales-test', `caller-${i}`, 0.05);
|
||
if (r.ok) ok++;
|
||
else if (r.error === 'BUDGET_EXCEEDED') exceeded++;
|
||
}
|
||
expect(ok).toBe(20);
|
||
expect(exceeded).toBe(80);
|
||
|
||
const rows = tracker.listReservations('sales-test').filter((r) => r.state === 'reserved');
|
||
expect(rows.length).toBe(20);
|
||
const sum = rows.reduce((acc, r) => acc + Number(r.estimated_usd), 0);
|
||
expect(sum).toBeCloseTo(1.0, 6);
|
||
});
|
||
});
|
||
|
||
// ---------- Test 2: block → release → allow 10 of 50 ---------------------
|
||
|
||
describe('Test 2 — release frees budget for subsequent callers', () => {
|
||
it('first $0.50 reserve blocks 50 callers; release frees 10 of next 50', () => {
|
||
const tracker = new AtomicBbsRoomBudgetTracker({ db, audit, defaultExpiryMs: 60_000 });
|
||
tracker.registerRoom('sales-test2', 1.0);
|
||
|
||
const initial = tracker.reserve('sales-test2', 'initial', 0.5);
|
||
expect(initial.ok).toBe(true);
|
||
if (!initial.ok) throw new Error('unreachable');
|
||
const reservationIdA = initial.reservationId;
|
||
|
||
// Step B: 50 reserves of $0.05 — all should fail (0.50 + 0.05 = 0.55 OK
|
||
// for the first call, but cumulative reserved + 0.05 must exceed cap to fail).
|
||
// 0.50 reserved + 50 × 0.05 = 0.50 + 2.50 = 3.00 way over. Each individual
|
||
// call sees 0.50 + N×0.05 already reserved, so callers 1-10 succeed and
|
||
// 11-50 fail (0.50 reserved + 0.50 in step B = 1.00 cap reached after 10).
|
||
let phaseBOk = 0;
|
||
let phaseBFail = 0;
|
||
for (let i = 0; i < 50; i++) {
|
||
const r = tracker.reserve('sales-test2', `phaseB-${i}`, 0.05);
|
||
if (r.ok) phaseBOk++;
|
||
else phaseBFail++;
|
||
}
|
||
expect(phaseBOk).toBe(10);
|
||
expect(phaseBFail).toBe(40);
|
||
|
||
// Step C: release the initial $0.50.
|
||
const rel = tracker.release(reservationIdA);
|
||
expect(rel.ok).toBe(true);
|
||
|
||
// Step D: another 50 — should see exactly 10 succeed (0.50 freed → 10 × 0.05).
|
||
let phaseDOk = 0;
|
||
let phaseDFail = 0;
|
||
for (let i = 0; i < 50; i++) {
|
||
const r = tracker.reserve('sales-test2', `phaseD-${i}`, 0.05);
|
||
if (r.ok) phaseDOk++;
|
||
else phaseDFail++;
|
||
}
|
||
expect(phaseDOk).toBe(10);
|
||
expect(phaseDFail).toBe(40);
|
||
});
|
||
});
|
||
|
||
// ---------- Test 3: expiry frees budget after sweepExpired() --------------
|
||
|
||
describe('Test 3 — expiry frees budget for next caller', () => {
|
||
it('after sweepExpired() an expired reservation no longer blocks new reserves', () => {
|
||
let mockNow = 1_000_000;
|
||
const tracker = new AtomicBbsRoomBudgetTracker({
|
||
db, audit,
|
||
defaultExpiryMs: 5_000, // floor — clamp goes to 5000ms
|
||
clock: () => mockNow,
|
||
});
|
||
tracker.registerRoom('hr-test', 0.10);
|
||
|
||
const a = tracker.reserve('hr-test', 'agent-1', 0.10);
|
||
expect(a.ok).toBe(true);
|
||
|
||
// Step B: should be BUDGET_EXCEEDED (live reservation occupies full cap).
|
||
const b = tracker.reserve('hr-test', 'agent-2', 0.05);
|
||
expect(b.ok).toBe(false);
|
||
if (b.ok) throw new Error('unreachable');
|
||
expect(b.error).toBe('BUDGET_EXCEEDED');
|
||
|
||
// Step C: advance clock past expiry, then sweep.
|
||
mockNow += 6_000;
|
||
const swept = tracker.sweepExpired();
|
||
expect(swept).toBe(1);
|
||
|
||
// Step D: next reserve succeeds (the expired row is excluded from totals).
|
||
const d = tracker.reserve('hr-test', 'agent-2', 0.05);
|
||
expect(d.ok).toBe(true);
|
||
});
|
||
|
||
it('expired reservation is excluded from gate check BEFORE sweep (window query check)', () => {
|
||
let mockNow = 2_000_000;
|
||
const tracker = new AtomicBbsRoomBudgetTracker({
|
||
db, audit,
|
||
defaultExpiryMs: 5_000,
|
||
clock: () => mockNow,
|
||
});
|
||
tracker.registerRoom('window-test', 0.10);
|
||
|
||
const a = tracker.reserve('window-test', 'agent-1', 0.10);
|
||
expect(a.ok).toBe(true);
|
||
|
||
// Advance past expiry but DO NOT call sweepExpired().
|
||
mockNow += 6_000;
|
||
|
||
// Per ADR-164.1 §7.2 "phantom debt intentional bias" — even unswepped
|
||
// expired rows must be excluded from the reserve() gate check.
|
||
const b = tracker.reserve('window-test', 'agent-2', 0.05);
|
||
expect(b.ok).toBe(true);
|
||
});
|
||
});
|
||
|
||
// ---------- Test 6: COMMIT_AFTER_EXPIRY closes Expired Commit Leak --------
|
||
|
||
describe('Test 6 — COMMIT_AFTER_EXPIRY captures the real spend (§8.1 fix)', () => {
|
||
it('late commit records spend, marks committed_post_expiry, emits audit', () => {
|
||
let mockNow = 3_000_000;
|
||
const tracker = new AtomicBbsRoomBudgetTracker({
|
||
db, audit,
|
||
defaultExpiryMs: 5_000,
|
||
clock: () => mockNow,
|
||
});
|
||
tracker.registerRoom('commit-late', 1.0);
|
||
|
||
const r = tracker.reserve('commit-late', 'agent-1', 0.30);
|
||
expect(r.ok).toBe(true);
|
||
if (!r.ok) throw new Error('unreachable');
|
||
const reservationId = r.reservationId;
|
||
|
||
// Advance past expiry — reservation is now expired but not committed.
|
||
mockNow += 6_000;
|
||
|
||
// Late commit: should succeed with warned: 'COMMIT_AFTER_EXPIRY'.
|
||
const c = tracker.commit(reservationId, 0.30);
|
||
expect(c.ok).toBe(true);
|
||
if (!c.ok) throw new Error('unreachable');
|
||
expect('warned' in c && c.warned).toBe('COMMIT_AFTER_EXPIRY');
|
||
if ('warned' in c) {
|
||
expect(c.finalRemaining).toBeCloseTo(0.70, 6);
|
||
}
|
||
|
||
// Audit emitted the high-priority envelope.
|
||
const evt = auditEvents.find((e) => e.type === 'reservation.committed_post_expiry');
|
||
expect(evt).toBeDefined();
|
||
expect(evt!.payload.reservationId).toBe(reservationId);
|
||
|
||
// The post-expiry commit counts against the budget.
|
||
const next = tracker.reserve('commit-late', 'agent-2', 0.70);
|
||
expect(next.ok).toBe(true);
|
||
|
||
// Now fully spent — even a tiny additional reserve is BUDGET_EXCEEDED.
|
||
const tiny = tracker.reserve('commit-late', 'agent-3', 0.01);
|
||
expect(tiny.ok).toBe(false);
|
||
if (tiny.ok) throw new Error('unreachable');
|
||
expect(tiny.error).toBe('BUDGET_EXCEEDED');
|
||
});
|
||
|
||
it('happy-path commit (within window) returns committed:true without warning', () => {
|
||
const tracker = new AtomicBbsRoomBudgetTracker({ db, audit, defaultExpiryMs: 60_000 });
|
||
tracker.registerRoom('commit-clean', 1.0);
|
||
const r = tracker.reserve('commit-clean', 'agent-1', 0.20);
|
||
expect(r.ok).toBe(true);
|
||
if (!r.ok) throw new Error('unreachable');
|
||
|
||
const c = tracker.commit(r.reservationId, 0.18);
|
||
expect(c.ok).toBe(true);
|
||
if (!c.ok) throw new Error('unreachable');
|
||
expect('committed' in c && c.committed).toBe(true);
|
||
if ('committed' in c) {
|
||
expect(c.finalRemaining).toBeCloseTo(0.82, 6);
|
||
}
|
||
});
|
||
|
||
it('double-commit returns ALREADY_FINALIZED', () => {
|
||
const tracker = new AtomicBbsRoomBudgetTracker({ db, audit, defaultExpiryMs: 60_000 });
|
||
tracker.registerRoom('double-commit', 1.0);
|
||
const r = tracker.reserve('double-commit', 'agent-1', 0.10);
|
||
if (!r.ok) throw new Error('expected ok');
|
||
expect(tracker.commit(r.reservationId, 0.10).ok).toBe(true);
|
||
const second = tracker.commit(r.reservationId, 0.05);
|
||
expect(second.ok).toBe(false);
|
||
if (second.ok) throw new Error('unreachable');
|
||
expect(second.error).toBe('ALREADY_FINALIZED');
|
||
});
|
||
|
||
it('commit on unknown id returns NOT_FOUND', () => {
|
||
const tracker = new AtomicBbsRoomBudgetTracker({ db, audit });
|
||
const c = tracker.commit('does-not-exist', 0.10);
|
||
expect(c.ok).toBe(false);
|
||
if (c.ok) throw new Error('unreachable');
|
||
expect(c.error).toBe('NOT_FOUND');
|
||
});
|
||
|
||
it('release on committed reservation returns ALREADY_FINALIZED', () => {
|
||
const tracker = new AtomicBbsRoomBudgetTracker({ db, audit, defaultExpiryMs: 60_000 });
|
||
tracker.registerRoom('release-blocked', 1.0);
|
||
const r = tracker.reserve('release-blocked', 'agent-1', 0.10);
|
||
if (!r.ok) throw new Error('expected ok');
|
||
expect(tracker.commit(r.reservationId, 0.10).ok).toBe(true);
|
||
const rel = tracker.release(r.reservationId);
|
||
expect(rel.ok).toBe(false);
|
||
if (rel.ok) throw new Error('unreachable');
|
||
expect(rel.error).toBe('ALREADY_FINALIZED');
|
||
});
|
||
|
||
it('reserve on unknown room returns ROOM_NOT_FOUND', () => {
|
||
const tracker = new AtomicBbsRoomBudgetTracker({ db, audit });
|
||
const r = tracker.reserve('no-such-room', 'agent-1', 0.10);
|
||
expect(r.ok).toBe(false);
|
||
if (r.ok) throw new Error('unreachable');
|
||
expect(r.error).toBe('ROOM_NOT_FOUND');
|
||
});
|
||
});
|
||
|
||
// ---------- feature-flag factory ------------------------------------------
|
||
|
||
describe('createAtomicTrackerIfEnabled feature flag', () => {
|
||
it('returns null when CLAUDE_FLOW_BBS_ATOMIC_BUDGET is unset', () => {
|
||
const prev = process.env.CLAUDE_FLOW_BBS_ATOMIC_BUDGET;
|
||
delete process.env.CLAUDE_FLOW_BBS_ATOMIC_BUDGET;
|
||
expect(createAtomicTrackerIfEnabled({ db })).toBeNull();
|
||
if (prev !== undefined) process.env.CLAUDE_FLOW_BBS_ATOMIC_BUDGET = prev;
|
||
});
|
||
|
||
it('returns the atomic tracker when flag is set to "1"', () => {
|
||
const prev = process.env.CLAUDE_FLOW_BBS_ATOMIC_BUDGET;
|
||
process.env.CLAUDE_FLOW_BBS_ATOMIC_BUDGET = '1';
|
||
try {
|
||
const tr = createAtomicTrackerIfEnabled({ db });
|
||
expect(tr).toBeInstanceOf(AtomicBbsRoomBudgetTracker);
|
||
} finally {
|
||
if (prev === undefined) delete process.env.CLAUDE_FLOW_BBS_ATOMIC_BUDGET;
|
||
else process.env.CLAUDE_FLOW_BBS_ATOMIC_BUDGET = prev;
|
||
}
|
||
});
|
||
});
|