Files
proffesor-for-testing__agen…/tests/unit/kernel/unified-memory-querycount.test.ts
Dragan Spiridonov fc1e86ad2d fix(security): resolve P0/P1 issues from v3.8.3 QE swarm analysis
P0 fixes:
- Fix command injection (CWE-78) in test-verifier.ts: replace exec()
  with execFile() + ALLOWED_TEST_COMMANDS allowlist
- Unify SQL allowlists: add 5 missing tables to sql-safety.ts, replace
  local duplicate in unified-memory.ts with canonical validateTableName()
- Add validateTableName() to all 7 SQL interpolation sites in
  ruvector/brain-shared.ts

P1 fixes:
- Remove 70 process.exit() calls from learning.ts (45) and hooks.ts (25),
  replacing with return/throw for proper cleanup handler execution
- Move @faker-js/faker from dependencies to devDependencies (~8MB savings)
- Enable bundle minification: CLI 9.8→6.9MB (-30%), MCP 12→7.2MB (-40%)
- Fix no-op verification scripts to perform real checks with non-zero
  exit codes on failure

QE reports:
- Add 12 comprehensive QE analysis reports for v3.8.3 covering code
  quality, security, performance, tests, SFDIPOT, dependencies, API
  contracts, architecture/DDD, accessibility, and brutal honesty audit

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 13:53:54 +00:00

79 lines
2.7 KiB
TypeScript

/**
* Issue N3: UnifiedMemoryManager.queryCount() unit tests
*
* Verifies the queryCount method used by fleet_status to report
* learning metrics from the SQLite database.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import { UnifiedMemoryManager, resetUnifiedMemory } from '../../../src/kernel/unified-memory';
const TEST_DB_DIR = '/tmp/aqe-querycount-test-' + Date.now();
const TEST_DB_PATH = path.join(TEST_DB_DIR, 'memory.db');
describe('UnifiedMemoryManager.queryCount', () => {
let um: UnifiedMemoryManager;
beforeAll(() => {
fs.mkdirSync(TEST_DB_DIR, { recursive: true });
resetUnifiedMemory();
um = new UnifiedMemoryManager();
um.initialize(TEST_DB_PATH);
});
afterAll(() => {
um.close();
resetUnifiedMemory();
try {
fs.rmSync(TEST_DB_DIR, { recursive: true, force: true });
} catch { /* ignore */ }
});
it('should return 0 for empty allowed tables', () => {
// kv_store is in the allowlist and should exist after init
const count = um.queryCount('kv_store');
expect(typeof count).toBe('number');
expect(count).toBeGreaterThanOrEqual(0);
});
it('should throw for disallowed table names', () => {
expect(() => um.queryCount('users')).toThrow('not in the allowlist');
expect(() => um.queryCount('DROP TABLE kv_store')).toThrow('not in the allowlist');
expect(() => um.queryCount('')).toThrow('not in the allowlist');
});
it('should count rows in vectors table', () => {
const count = um.queryCount('vectors');
expect(count).toBeGreaterThanOrEqual(0);
});
it('should allow all expected learning tables', () => {
// Tables guaranteed to exist after basic UnifiedMemoryManager.initialize()
// Note: captured_experiences and experience_applications are created by
// optional middleware (experience-capture, brain-export) — not by core schema.
const expectedTables = [
'qe_patterns', 'qe_trajectories',
'dream_cycles', 'dream_insights',
'concept_nodes', 'concept_edges', 'rl_q_values', 'vectors',
'kv_store', 'qe_pattern_usage',
];
for (const table of expectedTables) {
// Should not throw
expect(() => um.queryCount(table)).not.toThrow();
}
});
it('should reflect data after insertion', async () => {
const before = um.queryCount('kv_store');
// Insert a row via the KV store API
await um.kvSet(`querycount-test-key-${Date.now()}`, 'test-value');
const after = um.queryCount('kv_store');
// Count should increase (may be more than +1 if background hooks also write)
expect(after).toBeGreaterThan(before);
});
});