File size: 6,219 Bytes
a8f4b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { DatabaseSync } from 'node:sqlite';
import { test } from 'node:test';
import { GovernanceService } from '../../governance-fabric/src/index.mjs';
import { validateFrontierPromotion } from '../../logic-fabric/dist/index.js';
import { ArchiveService, CanonService, ConsolidationService, FrontierService, PromotionService, SnapshotService } from '../dist/index.js';

function setup() {
  const db = new DatabaseSync(':memory:');
  db.exec(readFileSync(new URL('../../storage-fabric/sqlite/schema.sql', import.meta.url), 'utf8'));
  db.prepare("INSERT INTO projects(project_id, name) VALUES ('p1', 'Project One')").run();
  return db;
}

test('promotion requires both logic and governance and commits canon plus snapshot atomically', async () => {
  const db = setup();
  try {
    const frontier = new FrontierService(db);
    const item = frontier.create({ id: 'f1', projectId: 'p1', title: 'Reviewed practice', description: 'Use the reviewed practice.' });
    let governanceCalled = false;
    const promotion = new PromotionService(db, {
      logic: { validatePromotion: ({ frontierItem }) => ({ valid: Boolean(frontierItem.title) }) },
      governance: { review: () => { governanceCalled = true; return { approved: true, approvalId: 'approval-1' }; } }
    });
    const result = await promotion.promote(item.id, { requestedBy: 'reviewer-1' });
    assert.equal(governanceCalled, true);
    assert.equal(result.canonEntry.entry_id, item.id);
    assert.equal(frontier.get(item.id).status, 'adopted');
    assert.equal(new SnapshotService(db).get(result.snapshot.id).state.canonEntries[0].entry_id, item.id);
  } finally {
    db.close();
  }
});

test('rejected governance leaves frontier unchanged and creates no canon entry', async () => {
  const db = setup();
  try {
    const frontier = new FrontierService(db);
    const item = frontier.create({ id: 'f-reject', projectId: 'p1', title: 'Unapproved practice' });
    const promotion = new PromotionService(db, {
      logic: { validatePromotion: () => true },
      governance: { review: () => ({ approved: false, reason: 'approval required' }) }
    });
    await assert.rejects(promotion.promote(item.id), /approval required/);
    assert.equal(frontier.get(item.id).status, 'candidate');
    assert.equal(new CanonService(db).get(item.id), null);
  } finally {
    db.close();
  }
});

test('Logic validation and a persisted Governance approval gate canon promotion', async () => {
  const db = setup();
  try {
    const frontier = new FrontierService(db);
    const item = frontier.create({ id: 'f-governed', projectId: 'p1', title: 'Governed practice', description: 'Supported by reviewed evidence.' });
    const governance = new GovernanceService(db);
    const promotion = new PromotionService(db, {
      logic: { validatePromotion: ({ frontierItem, projectId }) => validateFrontierPromotion(frontierItem, projectId) },
      governance
    });
    let reviewId;
    await assert.rejects(promotion.promote(item.id), /Review ID:/);
    reviewId = governance.list('p1', 'pending')[0].id;
    governance.decide(reviewId, { decision: 'approved', reviewedBy: 'project-owner', reason: 'Evidence and impact reviewed.' });
    const result = await promotion.promote(item.id, { requestedBy: 'operations-agent' });
    const snapshots = new SnapshotService(db);
    const captured = snapshots.get(result.snapshot.id);
    assert.equal(result.canonEntry.entry_id, item.id);
    assert.equal(captured.state.governanceReviews[0].decision, 'approved');
    db.prepare('DELETE FROM governance_reviews').run();
    snapshots.restore(result.snapshot.id);
    assert.equal(governance.get(reviewId).decision, 'approved');
  } finally {
    db.close();
  }
});

test('promotion rolls back canon and Frontier changes when snapshot capture fails', async () => {
  const db = setup();
  try {
    const frontier = new FrontierService(db);
    const item = frontier.create({ id: 'f-snapshot-fail', projectId: 'p1', title: 'Atomic practice', description: 'A complete practice.' });
    const promotion = new PromotionService(db, {
      logic: { validatePromotion: () => true },
      governance: { review: () => ({ approved: true, approvalId: 'approved' }) }
    });
    db.exec(`CREATE TRIGGER fail_snapshot_insert BEFORE INSERT ON snapshots BEGIN SELECT RAISE(ABORT, 'snapshot write failed'); END;`);
    await assert.rejects(promotion.promote(item.id), /snapshot write failed/);
    assert.equal(frontier.get(item.id).status, 'candidate');
    assert.equal(new CanonService(db).get(item.id), null);
    assert.equal(db.prepare("SELECT count(*) AS n FROM project_events WHERE event_type = 'frontier.promoted'").get().n, 0);
  } finally {
    db.close();
  }
});

test('consolidation prepares a frontier candidate tied to same-project active canon', () => {
  const db = setup();
  try {
    const canon = new CanonService(db);
    const source = canon.create({ id: 'c1', projectId: 'p1', title: 'Source principle', body: 'Keep provenance.', status: 'active' });
    const candidate = new ConsolidationService(db).prepare({
      projectId: 'p1', title: 'Consolidated principle', body: 'Preserve evidence and provenance.', sourceEntryIds: [source.id]
    });
    assert.equal(candidate.status, 'candidate');
    assert.deepEqual(candidate.metadata.sourceEntryIds, [source.id]);
    assert.throws(() => new ConsolidationService(db).prepare({
      projectId: 'p1', title: 'Bad consolidation', body: 'No source', sourceEntryIds: ['missing']
    }), /active canon entry/);
  } finally {
    db.close();
  }
});

test('archives and restores checksummed payloads', () => {
  const db = setup();
  try {
    const archives = new ArchiveService(db);
    const record = archives.archive({ projectId: 'p1', resourceType: 'document', resourceId: 'doc-1', payload: { text: 'retained' } });
    assert.deepEqual(archives.restore(record.id), { text: 'retained' });
    db.prepare('UPDATE archive_records SET payload_json = ? WHERE archive_id = ?').run('{"text":"changed"}', record.id);
    assert.throws(() => archives.get(record.id), /checksum validation/);
  } finally {
    db.close();
  }
});