-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_focus_shield.ts
More file actions
79 lines (67 loc) · 2.64 KB
/
Copy pathtest_focus_shield.ts
File metadata and controls
79 lines (67 loc) · 2.64 KB
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
/**
* Copyright (c) 2026 DietCode Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* [TEST] 🛡️ Focus Shield (Context Lockdown) Verification
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import { FocusShield } from './src/core/task/FocusShield';
import { FileSystemAdapter } from './src/infrastructure/FileSystemAdapter';
async function testFocusShield() {
console.log('🛡️ Starting Focus Shield Protocol Verification...');
const adapter = new FileSystemAdapter();
const shield = FocusShield.getInstance();
const testFileAllowed = path.resolve(process.cwd(), 'allowed_file.ts');
const testFileBlocked = path.resolve(process.cwd(), 'blocked_file.ts');
fs.writeFileSync(testFileAllowed, '// [LAYER: CORE]\nexport const allowed = 1;');
fs.writeFileSync(testFileBlocked, '// [LAYER: CORE]\nexport const blocked = 1;');
try {
// 1. Before activation, everything should be allowed
console.log('[1] Verifying access before shield activation...');
adapter.readFile(testFileBlocked);
console.log('✅ Access allowed (Inactive): PASS');
// 2. Activate shield with only the allowed file
console.log('\n[2] Activating Focus Shield with "allowed_file.ts"...');
shield.activate([testFileAllowed]);
// 3. Test allowed access
console.log('[3] Verifying access to allowed file...');
const content = adapter.readFile(testFileAllowed);
if (!content) throw new Error('Failed to read allowed file');
console.log('✅ Access allowed (Active): PASS');
// 4. Test blocked access
console.log('\n[4] Verifying access to blocked file (Expect Violation)...');
try {
adapter.readFile(testFileBlocked);
throw new Error('❌ FocusShield ERROR: Blocked file was accessed!');
} catch (err: unknown) {
if (err instanceof Error && err.message.includes('Sovereign Scope Violation')) {
console.log('✅ Access blocked: PASS');
} else {
throw err;
}
}
// 5. Deactivate shield
console.log('\n[5] Deactivating Focus Shield...');
shield.deactivate();
adapter.readFile(testFileBlocked);
console.log('✅ Access restored: PASS');
} finally {
// Cleanup
try {
fs.unlinkSync(testFileAllowed);
} catch (e) { /* ignore cleanup error */ }
try {
fs.unlinkSync(testFileBlocked);
} catch (e) { /* ignore cleanup error */ }
}
console.log('\n✨ FOCUS SHIELD PROTOCOL VERIFIED ✨');
}
testFocusShield().catch((err) => {
console.error('\n❌ FOCUS SHIELD VERIFICATION FAILED');
console.error(err);
process.exit(1);
});