flow-capture-stress.test.mjs
93 lines 3.6 KB
Raw
sha256:8915fe406161f95c1681f9469375e7bae5b28c884f00bedbdef65e4b0cd0738d docs(flow): commit FLOW-V0-SPEC.md hygiene for 7A-INT merge Human 13 hours ago
1 /**
2 * Tier 4 — STRESS: caps, concurrent propose, list truncation.
3 *
4 * @see lib/flow/flow-capture.mjs
5 */
6 import { describe, it, beforeEach, afterEach } from 'node:test';
7 import assert from 'node:assert/strict';
8 import fs from 'node:fs';
9 import path from 'node:path';
10 import { fileURLToPath } from 'node:url';
11 import {
12 handleFlowCaptureObserveRequest,
13 handleFlowCaptureListRequest,
14 handleFlowCaptureProposeRequest,
15 MAX_SESSION_SIGNAL_REFS,
16 MAX_CANDIDATE_SUMMARIES,
17 FLOW_CAPTURE_PER_SESSION_CAP,
18 } from '../lib/flow/flow-capture.mjs';
19 import { upsertCandidate } from '../lib/flow/flow-store.mjs';
20 import { createProposal } from '../hub/proposals-store.mjs';
21 import { validSessionMeta, makeCandidateRecord } from './fixtures/flow/capture-helpers.mjs';
22 import { emptyStarterDir } from './fixtures/flow/authoring-helpers.mjs';
23
24 const __dirname = path.dirname(fileURLToPath(import.meta.url));
25 const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-flow-capture-stress');
26 const visible = new Set(['personal', 'project', 'org']);
27
28 describe('Flow capture — stress', () => {
29 const dataDir = path.join(tmpRoot, 'data');
30 const vaultId = 'default';
31
32 beforeEach(() => {
33 fs.rmSync(tmpRoot, { recursive: true, force: true });
34 fs.mkdirSync(dataDir, { recursive: true });
35 process.env.FLOW_CAPTURE_DETECTION_ENABLED = '1';
36 process.env.FLOW_CAPTURE_WRITES_ENABLED = '1';
37 });
38 afterEach(() => {
39 fs.rmSync(tmpRoot, { recursive: true, force: true });
40 delete process.env.FLOW_CAPTURE_DETECTION_ENABLED;
41 delete process.env.FLOW_CAPTURE_WRITES_ENABLED;
42 });
43
44 it('rejects observe at MAX_SESSION_SIGNAL_REFS + 1', () => {
45 const refs = Array.from({ length: MAX_SESSION_SIGNAL_REFS + 1 }, (_, i) => `flow_x#${i + 1}`);
46 const r = handleFlowCaptureObserveRequest({
47 dataDir, vaultId, visibleScopes: visible, sessionMeta: validSessionMeta({ step_sequence_refs: refs }),
48 });
49 assert.equal(r.ok, false);
50 assert.equal(r.code, 'FLOW_CAPTURE_SIGNAL_MALFORMED');
51 });
52
53 it('enforces per-session cap on new candidates', () => {
54 const meta = validSessionMeta({
55 observed_counts: {
56 repetition: 4,
57 re_explanation: 4,
58 repeated_correction: 4,
59 review_debt: 2,
60 },
61 });
62 const r = handleFlowCaptureObserveRequest({ dataDir, vaultId, visibleScopes: visible, sessionMeta: meta });
63 assert.equal(r.ok, true);
64 assert.ok(r.payload.returned_count <= FLOW_CAPTURE_PER_SESSION_CAP);
65 });
66
67 it('list at MAX_CANDIDATE_SUMMARIES sets truncated', () => {
68 for (let i = 0; i < MAX_CANDIDATE_SUMMARIES + 5; i += 1) {
69 upsertCandidate(
70 dataDir,
71 vaultId,
72 makeCandidateRecord({ candidate_id: `cand_${String(i).padStart(4, '0')}` }),
73 );
74 }
75 const r = handleFlowCaptureListRequest({ dataDir, vaultId, visibleScopes: visible, limit: MAX_CANDIDATE_SUMMARIES });
76 assert.equal(r.payload.candidates.length, MAX_CANDIDATE_SUMMARIES);
77 assert.equal(r.payload.truncated, true);
78 });
79
80 it('concurrent propose on same candidate — exactly one succeeds', () => {
81 upsertCandidate(dataDir, vaultId, makeCandidateRecord({ candidate_id: 'cand_concurrent' }));
82 const starterDir = emptyStarterDir(dataDir);
83 const results = Array.from({ length: 5 }, () =>
84 handleFlowCaptureProposeRequest({
85 dataDir, vaultId, visibleScopes: visible, candidateId: 'cand_concurrent', confirmedScope: 'personal', intent: 'x', createProposal, starterDir,
86 }),
87 );
88 const okCount = results.filter((r) => r.ok).length;
89 assert.equal(okCount, 1);
90 const conflict = results.filter((r) => !r.ok && r.code === 'FLOW_CANDIDATE_NOT_PROMOTABLE');
91 assert.ok(conflict.length >= 1);
92 });
93 });
File History 1 commit
sha256:8915fe406161f95c1681f9469375e7bae5b28c884f00bedbdef65e4b0cd0738d docs(flow): commit FLOW-V0-SPEC.md hygiene for 7A-INT merge Human 13 hours ago