@build-astron-co/nimbus 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +628 -0
- package/bin/nimbus +38 -0
- package/package.json +80 -0
- package/src/__tests__/app.test.ts +76 -0
- package/src/__tests__/audit.test.ts +877 -0
- package/src/__tests__/circuit-breaker.test.ts +116 -0
- package/src/__tests__/cli-run.test.ts +115 -0
- package/src/__tests__/context-manager.test.ts +502 -0
- package/src/__tests__/context.test.ts +242 -0
- package/src/__tests__/enterprise.test.ts +401 -0
- package/src/__tests__/generator.test.ts +433 -0
- package/src/__tests__/hooks.test.ts +582 -0
- package/src/__tests__/init.test.ts +436 -0
- package/src/__tests__/intent-parser.test.ts +229 -0
- package/src/__tests__/llm-router.test.ts +209 -0
- package/src/__tests__/lsp.test.ts +293 -0
- package/src/__tests__/modes.test.ts +336 -0
- package/src/__tests__/permissions.test.ts +338 -0
- package/src/__tests__/serve.test.ts +275 -0
- package/src/__tests__/sessions.test.ts +227 -0
- package/src/__tests__/sharing.test.ts +288 -0
- package/src/__tests__/snapshots.test.ts +581 -0
- package/src/__tests__/state-db.test.ts +334 -0
- package/src/__tests__/stream-with-tools.test.ts +732 -0
- package/src/__tests__/subagents.test.ts +176 -0
- package/src/__tests__/system-prompt.test.ts +169 -0
- package/src/__tests__/tool-converter.test.ts +256 -0
- package/src/__tests__/tool-schemas.test.ts +397 -0
- package/src/__tests__/tools.test.ts +143 -0
- package/src/__tests__/version.test.ts +49 -0
- package/src/agent/compaction-agent.ts +227 -0
- package/src/agent/context-manager.ts +435 -0
- package/src/agent/context.ts +427 -0
- package/src/agent/deploy-preview.ts +426 -0
- package/src/agent/index.ts +68 -0
- package/src/agent/loop.ts +717 -0
- package/src/agent/modes.ts +429 -0
- package/src/agent/permissions.ts +466 -0
- package/src/agent/subagents/base.ts +116 -0
- package/src/agent/subagents/cost.ts +51 -0
- package/src/agent/subagents/explore.ts +42 -0
- package/src/agent/subagents/general.ts +54 -0
- package/src/agent/subagents/index.ts +102 -0
- package/src/agent/subagents/infra.ts +59 -0
- package/src/agent/subagents/security.ts +69 -0
- package/src/agent/system-prompt.ts +436 -0
- package/src/app.ts +122 -0
- package/src/audit/activity-log.ts +290 -0
- package/src/audit/compliance-checker.ts +540 -0
- package/src/audit/cost-tracker.ts +318 -0
- package/src/audit/index.ts +23 -0
- package/src/audit/security-scanner.ts +596 -0
- package/src/auth/guard.ts +75 -0
- package/src/auth/index.ts +56 -0
- package/src/auth/oauth.ts +455 -0
- package/src/auth/providers.ts +470 -0
- package/src/auth/sso.ts +113 -0
- package/src/auth/store.ts +505 -0
- package/src/auth/types.ts +187 -0
- package/src/build.ts +141 -0
- package/src/cli/index.ts +16 -0
- package/src/cli/init.ts +854 -0
- package/src/cli/openapi-spec.ts +356 -0
- package/src/cli/run.ts +237 -0
- package/src/cli/serve-auth.ts +80 -0
- package/src/cli/serve.ts +462 -0
- package/src/cli/web.ts +67 -0
- package/src/cli.ts +1417 -0
- package/src/clients/core-engine-client.ts +227 -0
- package/src/clients/enterprise-client.ts +334 -0
- package/src/clients/generator-client.ts +351 -0
- package/src/clients/git-client.ts +627 -0
- package/src/clients/github-client.ts +410 -0
- package/src/clients/helm-client.ts +504 -0
- package/src/clients/index.ts +80 -0
- package/src/clients/k8s-client.ts +497 -0
- package/src/clients/llm-client.ts +161 -0
- package/src/clients/rest-client.ts +130 -0
- package/src/clients/service-discovery.ts +33 -0
- package/src/clients/terraform-client.ts +482 -0
- package/src/clients/tools-client.ts +1843 -0
- package/src/clients/ws-client.ts +115 -0
- package/src/commands/analyze/index.ts +352 -0
- package/src/commands/apply/helm.ts +473 -0
- package/src/commands/apply/index.ts +213 -0
- package/src/commands/apply/k8s.ts +454 -0
- package/src/commands/apply/terraform.ts +582 -0
- package/src/commands/ask.ts +167 -0
- package/src/commands/audit/index.ts +238 -0
- package/src/commands/auth-cloud.ts +294 -0
- package/src/commands/auth-list.ts +134 -0
- package/src/commands/auth-profile.ts +121 -0
- package/src/commands/auth-status.ts +141 -0
- package/src/commands/aws/ec2.ts +501 -0
- package/src/commands/aws/iam.ts +397 -0
- package/src/commands/aws/index.ts +133 -0
- package/src/commands/aws/lambda.ts +396 -0
- package/src/commands/aws/rds.ts +439 -0
- package/src/commands/aws/s3.ts +439 -0
- package/src/commands/aws/vpc.ts +393 -0
- package/src/commands/aws-discover.ts +649 -0
- package/src/commands/aws-terraform.ts +805 -0
- package/src/commands/azure/aks.ts +376 -0
- package/src/commands/azure/functions.ts +253 -0
- package/src/commands/azure/index.ts +116 -0
- package/src/commands/azure/storage.ts +478 -0
- package/src/commands/azure/vm.ts +355 -0
- package/src/commands/billing/index.ts +256 -0
- package/src/commands/chat.ts +314 -0
- package/src/commands/config.ts +346 -0
- package/src/commands/cost/cloud-cost-estimator.ts +266 -0
- package/src/commands/cost/estimator.ts +79 -0
- package/src/commands/cost/index.ts +594 -0
- package/src/commands/cost/parsers/terraform.ts +273 -0
- package/src/commands/cost/parsers/types.ts +25 -0
- package/src/commands/cost/pricing/aws.ts +544 -0
- package/src/commands/cost/pricing/azure.ts +499 -0
- package/src/commands/cost/pricing/gcp.ts +396 -0
- package/src/commands/cost/pricing/index.ts +40 -0
- package/src/commands/demo.ts +250 -0
- package/src/commands/doctor.ts +794 -0
- package/src/commands/drift/index.ts +439 -0
- package/src/commands/explain.ts +277 -0
- package/src/commands/feedback.ts +389 -0
- package/src/commands/fix.ts +324 -0
- package/src/commands/fs/index.ts +402 -0
- package/src/commands/gcp/compute.ts +325 -0
- package/src/commands/gcp/functions.ts +271 -0
- package/src/commands/gcp/gke.ts +438 -0
- package/src/commands/gcp/iam.ts +344 -0
- package/src/commands/gcp/index.ts +129 -0
- package/src/commands/gcp/storage.ts +284 -0
- package/src/commands/generate-helm.ts +1249 -0
- package/src/commands/generate-k8s.ts +1560 -0
- package/src/commands/generate-terraform.ts +1460 -0
- package/src/commands/gh/index.ts +863 -0
- package/src/commands/git/index.ts +1343 -0
- package/src/commands/helm/index.ts +1126 -0
- package/src/commands/help.ts +539 -0
- package/src/commands/history.ts +142 -0
- package/src/commands/import.ts +868 -0
- package/src/commands/index.ts +367 -0
- package/src/commands/init.ts +1046 -0
- package/src/commands/k8s/index.ts +1137 -0
- package/src/commands/login.ts +631 -0
- package/src/commands/logout.ts +83 -0
- package/src/commands/onboarding.ts +228 -0
- package/src/commands/plan/display.ts +279 -0
- package/src/commands/plan/index.ts +599 -0
- package/src/commands/preview.ts +452 -0
- package/src/commands/questionnaire.ts +1270 -0
- package/src/commands/resume.ts +55 -0
- package/src/commands/team/index.ts +346 -0
- package/src/commands/template.ts +232 -0
- package/src/commands/tf/index.ts +1034 -0
- package/src/commands/upgrade.ts +550 -0
- package/src/commands/usage/index.ts +134 -0
- package/src/commands/version.ts +170 -0
- package/src/compat/index.ts +2 -0
- package/src/compat/runtime.ts +12 -0
- package/src/compat/sqlite.ts +107 -0
- package/src/config/index.ts +17 -0
- package/src/config/manager.ts +530 -0
- package/src/config/safety-policy.ts +358 -0
- package/src/config/schema.ts +125 -0
- package/src/config/types.ts +527 -0
- package/src/context/context-db.ts +199 -0
- package/src/demo/index.ts +349 -0
- package/src/demo/scenarios/full-journey.ts +229 -0
- package/src/demo/scenarios/getting-started.ts +127 -0
- package/src/demo/scenarios/helm-release.ts +341 -0
- package/src/demo/scenarios/k8s-deployment.ts +194 -0
- package/src/demo/scenarios/terraform-vpc.ts +170 -0
- package/src/demo/types.ts +92 -0
- package/src/engine/cost-estimator.ts +438 -0
- package/src/engine/diagram-generator.ts +256 -0
- package/src/engine/drift-detector.ts +902 -0
- package/src/engine/executor.ts +1035 -0
- package/src/engine/index.ts +76 -0
- package/src/engine/orchestrator.ts +636 -0
- package/src/engine/planner.ts +720 -0
- package/src/engine/safety.ts +743 -0
- package/src/engine/verifier.ts +770 -0
- package/src/enterprise/audit.ts +348 -0
- package/src/enterprise/auth.ts +270 -0
- package/src/enterprise/billing.ts +822 -0
- package/src/enterprise/index.ts +17 -0
- package/src/enterprise/teams.ts +443 -0
- package/src/generator/best-practices.ts +1608 -0
- package/src/generator/helm.ts +630 -0
- package/src/generator/index.ts +37 -0
- package/src/generator/intent-parser.ts +514 -0
- package/src/generator/kubernetes.ts +976 -0
- package/src/generator/terraform.ts +1867 -0
- package/src/history/index.ts +8 -0
- package/src/history/manager.ts +322 -0
- package/src/history/types.ts +34 -0
- package/src/hooks/config.ts +432 -0
- package/src/hooks/engine.ts +391 -0
- package/src/hooks/index.ts +4 -0
- package/src/llm/auth-bridge.ts +198 -0
- package/src/llm/circuit-breaker.ts +140 -0
- package/src/llm/config-loader.ts +201 -0
- package/src/llm/cost-calculator.ts +171 -0
- package/src/llm/index.ts +8 -0
- package/src/llm/model-aliases.ts +115 -0
- package/src/llm/provider-registry.ts +63 -0
- package/src/llm/providers/anthropic.ts +433 -0
- package/src/llm/providers/bedrock.ts +477 -0
- package/src/llm/providers/google.ts +405 -0
- package/src/llm/providers/ollama.ts +767 -0
- package/src/llm/providers/openai-compatible.ts +340 -0
- package/src/llm/providers/openai.ts +328 -0
- package/src/llm/providers/openrouter.ts +338 -0
- package/src/llm/router.ts +1035 -0
- package/src/llm/types.ts +232 -0
- package/src/lsp/client.ts +298 -0
- package/src/lsp/languages.ts +116 -0
- package/src/lsp/manager.ts +278 -0
- package/src/mcp/client.ts +402 -0
- package/src/mcp/index.ts +5 -0
- package/src/mcp/manager.ts +133 -0
- package/src/nimbus.ts +214 -0
- package/src/plugins/index.ts +27 -0
- package/src/plugins/loader.ts +334 -0
- package/src/plugins/manager.ts +376 -0
- package/src/plugins/types.ts +284 -0
- package/src/scanners/cicd-scanner.ts +258 -0
- package/src/scanners/cloud-scanner.ts +466 -0
- package/src/scanners/framework-scanner.ts +469 -0
- package/src/scanners/iac-scanner.ts +388 -0
- package/src/scanners/index.ts +539 -0
- package/src/scanners/language-scanner.ts +276 -0
- package/src/scanners/package-manager-scanner.ts +277 -0
- package/src/scanners/types.ts +172 -0
- package/src/sessions/manager.ts +365 -0
- package/src/sessions/types.ts +44 -0
- package/src/sharing/sync.ts +296 -0
- package/src/sharing/viewer.ts +97 -0
- package/src/snapshots/index.ts +2 -0
- package/src/snapshots/manager.ts +530 -0
- package/src/state/artifacts.ts +147 -0
- package/src/state/audit.ts +137 -0
- package/src/state/billing.ts +240 -0
- package/src/state/checkpoints.ts +117 -0
- package/src/state/config.ts +67 -0
- package/src/state/conversations.ts +14 -0
- package/src/state/credentials.ts +154 -0
- package/src/state/db.ts +58 -0
- package/src/state/index.ts +26 -0
- package/src/state/messages.ts +115 -0
- package/src/state/projects.ts +123 -0
- package/src/state/schema.ts +236 -0
- package/src/state/sessions.ts +147 -0
- package/src/state/teams.ts +200 -0
- package/src/telemetry.ts +108 -0
- package/src/tools/aws-ops.ts +952 -0
- package/src/tools/azure-ops.ts +579 -0
- package/src/tools/file-ops.ts +593 -0
- package/src/tools/gcp-ops.ts +625 -0
- package/src/tools/git-ops.ts +773 -0
- package/src/tools/github-ops.ts +799 -0
- package/src/tools/helm-ops.ts +943 -0
- package/src/tools/index.ts +17 -0
- package/src/tools/k8s-ops.ts +819 -0
- package/src/tools/schemas/converter.ts +184 -0
- package/src/tools/schemas/devops.ts +612 -0
- package/src/tools/schemas/index.ts +73 -0
- package/src/tools/schemas/standard.ts +1144 -0
- package/src/tools/schemas/types.ts +705 -0
- package/src/tools/terraform-ops.ts +862 -0
- package/src/types/ambient.d.ts +193 -0
- package/src/types/config.ts +83 -0
- package/src/types/drift.ts +116 -0
- package/src/types/enterprise.ts +335 -0
- package/src/types/index.ts +20 -0
- package/src/types/plan.ts +44 -0
- package/src/types/request.ts +65 -0
- package/src/types/response.ts +54 -0
- package/src/types/service.ts +51 -0
- package/src/ui/App.tsx +997 -0
- package/src/ui/DeployPreview.tsx +169 -0
- package/src/ui/Header.tsx +68 -0
- package/src/ui/InputBox.tsx +350 -0
- package/src/ui/MessageList.tsx +585 -0
- package/src/ui/PermissionPrompt.tsx +151 -0
- package/src/ui/StatusBar.tsx +158 -0
- package/src/ui/ToolCallDisplay.tsx +409 -0
- package/src/ui/chat-ui.ts +853 -0
- package/src/ui/index.ts +33 -0
- package/src/ui/ink/index.ts +711 -0
- package/src/ui/streaming.ts +176 -0
- package/src/ui/types.ts +57 -0
- package/src/utils/analytics.ts +72 -0
- package/src/utils/cost-warning.ts +27 -0
- package/src/utils/env.ts +46 -0
- package/src/utils/errors.ts +69 -0
- package/src/utils/event-bus.ts +38 -0
- package/src/utils/index.ts +24 -0
- package/src/utils/logger.ts +171 -0
- package/src/utils/rate-limiter.ts +121 -0
- package/src/utils/service-auth.ts +49 -0
- package/src/utils/validation.ts +53 -0
- package/src/version.ts +4 -0
- package/src/watcher/index.ts +163 -0
- package/src/wizard/approval.ts +383 -0
- package/src/wizard/index.ts +25 -0
- package/src/wizard/prompts.ts +338 -0
- package/src/wizard/types.ts +171 -0
- package/src/wizard/ui.ts +556 -0
- package/src/wizard/wizard.ts +304 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,877 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the Nimbus audit modules:
|
|
3
|
+
* - Security Scanner
|
|
4
|
+
* - Compliance Checker
|
|
5
|
+
* - Cost Tracker
|
|
6
|
+
* - Activity Log
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
|
|
10
|
+
import * as fs from 'node:fs';
|
|
11
|
+
import * as path from 'node:path';
|
|
12
|
+
import * as os from 'node:os';
|
|
13
|
+
|
|
14
|
+
import { scanSecurity, formatFindings, type SecurityFinding } from '../audit/security-scanner';
|
|
15
|
+
import { checkCompliance, generateScorecard, type Framework } from '../audit/compliance-checker';
|
|
16
|
+
import { CostTracker } from '../audit/cost-tracker';
|
|
17
|
+
import { ActivityLog } from '../audit/activity-log';
|
|
18
|
+
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Shared helpers
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
function makeTmpDir(): string {
|
|
24
|
+
return fs.mkdtempSync(path.join(os.tmpdir(), 'nimbus-audit-test-'));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function removeTmpDir(dir: string): void {
|
|
28
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ============================================================================
|
|
32
|
+
// Security Scanner
|
|
33
|
+
// ============================================================================
|
|
34
|
+
|
|
35
|
+
describe('Security Scanner', () => {
|
|
36
|
+
let tmpDir: string;
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
tmpDir = makeTmpDir();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
removeTmpDir(tmpDir);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// ---- scanSecurity() ----
|
|
47
|
+
|
|
48
|
+
test('scanSecurity() finds hardcoded API keys', async () => {
|
|
49
|
+
fs.writeFileSync(path.join(tmpDir, 'config.ts'), 'const API_KEY = "sk-1234567890abcdef";\n');
|
|
50
|
+
|
|
51
|
+
const result = await scanSecurity({ dir: tmpDir });
|
|
52
|
+
|
|
53
|
+
expect(result.findings.length).toBeGreaterThanOrEqual(1);
|
|
54
|
+
const finding = result.findings.find(f => f.id.startsWith('SEC-001'));
|
|
55
|
+
expect(finding).toBeDefined();
|
|
56
|
+
expect(finding!.severity).toBe('CRITICAL');
|
|
57
|
+
expect(finding!.title).toContain('API key');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('scanSecurity() finds hardcoded passwords', async () => {
|
|
61
|
+
fs.writeFileSync(path.join(tmpDir, 'db.ts'), 'const password = "supersecret123";\n');
|
|
62
|
+
|
|
63
|
+
const result = await scanSecurity({ dir: tmpDir });
|
|
64
|
+
|
|
65
|
+
expect(result.findings.length).toBeGreaterThanOrEqual(1);
|
|
66
|
+
const finding = result.findings.find(f => f.id.startsWith('SEC-002'));
|
|
67
|
+
expect(finding).toBeDefined();
|
|
68
|
+
expect(finding!.severity).toBe('CRITICAL');
|
|
69
|
+
expect(finding!.title).toContain('password');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('scanSecurity() finds open security groups (0.0.0.0/0 in .tf files)', async () => {
|
|
73
|
+
fs.writeFileSync(path.join(tmpDir, 'main.tf'), 'cidr_blocks = ["0.0.0.0/0"]\n');
|
|
74
|
+
|
|
75
|
+
const result = await scanSecurity({ dir: tmpDir });
|
|
76
|
+
|
|
77
|
+
expect(result.findings.length).toBeGreaterThanOrEqual(1);
|
|
78
|
+
const finding = result.findings.find(f => f.id.startsWith('TF-001'));
|
|
79
|
+
expect(finding).toBeDefined();
|
|
80
|
+
expect(finding!.severity).toBe('HIGH');
|
|
81
|
+
expect(finding!.title).toContain('Open security group');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('scanSecurity() finds public S3 buckets', async () => {
|
|
85
|
+
fs.writeFileSync(path.join(tmpDir, 's3.tf'), 'acl = "public-read"\n');
|
|
86
|
+
|
|
87
|
+
const result = await scanSecurity({ dir: tmpDir });
|
|
88
|
+
|
|
89
|
+
expect(result.findings.length).toBeGreaterThanOrEqual(1);
|
|
90
|
+
const finding = result.findings.find(f => f.id.startsWith('TF-002'));
|
|
91
|
+
expect(finding).toBeDefined();
|
|
92
|
+
expect(finding!.severity).toBe('HIGH');
|
|
93
|
+
expect(finding!.title).toContain('Public S3 bucket');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('scanSecurity() returns no findings for clean code', async () => {
|
|
97
|
+
fs.writeFileSync(
|
|
98
|
+
path.join(tmpDir, 'clean.ts'),
|
|
99
|
+
[
|
|
100
|
+
'const apiKey = process.env.API_KEY;',
|
|
101
|
+
'const password = process.env.DB_PASSWORD;',
|
|
102
|
+
'export function greet(name: string): string {',
|
|
103
|
+
' return `Hello, ${name}`;',
|
|
104
|
+
'}',
|
|
105
|
+
].join('\n')
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const result = await scanSecurity({ dir: tmpDir });
|
|
109
|
+
|
|
110
|
+
expect(result.findings.length).toBe(0);
|
|
111
|
+
expect(result.scannedFiles).toBe(1);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('scanSecurity() skips node_modules', async () => {
|
|
115
|
+
const nodeModulesDir = path.join(tmpDir, 'node_modules', 'some-pkg');
|
|
116
|
+
fs.mkdirSync(nodeModulesDir, { recursive: true });
|
|
117
|
+
fs.writeFileSync(
|
|
118
|
+
path.join(nodeModulesDir, 'index.ts'),
|
|
119
|
+
'const API_KEY = "sk-should-be-ignored-abcde";\n'
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
// Also put a clean file in the root so scannedFiles > 0 in root but
|
|
123
|
+
// node_modules should be excluded entirely
|
|
124
|
+
fs.writeFileSync(path.join(tmpDir, 'index.ts'), 'export const x = 1;\n');
|
|
125
|
+
|
|
126
|
+
const result = await scanSecurity({ dir: tmpDir });
|
|
127
|
+
|
|
128
|
+
expect(result.scannedFiles).toBe(1); // only root index.ts
|
|
129
|
+
expect(result.findings.length).toBe(0);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ---- formatFindings() ----
|
|
133
|
+
|
|
134
|
+
test('formatFindings() produces readable output', () => {
|
|
135
|
+
const findings: SecurityFinding[] = [
|
|
136
|
+
{
|
|
137
|
+
id: 'SEC-001-abcd1234',
|
|
138
|
+
severity: 'CRITICAL',
|
|
139
|
+
title: 'Hardcoded API key or secret',
|
|
140
|
+
description: 'A potential API key is hardcoded.',
|
|
141
|
+
file: 'config.ts',
|
|
142
|
+
line: 3,
|
|
143
|
+
recommendation: 'Move to environment variables.',
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: 'TF-001-efgh5678',
|
|
147
|
+
severity: 'HIGH',
|
|
148
|
+
title: 'Open security group (0.0.0.0/0)',
|
|
149
|
+
description: 'Security group allows traffic from any IP.',
|
|
150
|
+
file: 'main.tf',
|
|
151
|
+
line: 10,
|
|
152
|
+
recommendation: 'Restrict CIDR blocks.',
|
|
153
|
+
},
|
|
154
|
+
];
|
|
155
|
+
|
|
156
|
+
const output = formatFindings(findings);
|
|
157
|
+
|
|
158
|
+
expect(output).toContain('Security Scan Report');
|
|
159
|
+
expect(output).toContain('2 finding(s)');
|
|
160
|
+
expect(output).toContain('[CRITICAL]');
|
|
161
|
+
expect(output).toContain('[HIGH]');
|
|
162
|
+
expect(output).toContain('config.ts:3');
|
|
163
|
+
expect(output).toContain('main.tf:10');
|
|
164
|
+
expect(output).toContain('Recommendation:');
|
|
165
|
+
expect(output).toContain('1 critical');
|
|
166
|
+
expect(output).toContain('1 high');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('formatFindings() returns clean message for empty findings', () => {
|
|
170
|
+
const output = formatFindings([]);
|
|
171
|
+
expect(output).toBe('No security issues found.');
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// ============================================================================
|
|
176
|
+
// Compliance Checker
|
|
177
|
+
// ============================================================================
|
|
178
|
+
|
|
179
|
+
describe('Compliance Checker', () => {
|
|
180
|
+
let tmpDir: string;
|
|
181
|
+
|
|
182
|
+
beforeEach(() => {
|
|
183
|
+
tmpDir = makeTmpDir();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
afterEach(() => {
|
|
187
|
+
removeTmpDir(tmpDir);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('checkCompliance() returns reports for all requested frameworks', async () => {
|
|
191
|
+
// Create a minimal .tf file so controls are evaluated (not skipped)
|
|
192
|
+
fs.writeFileSync(path.join(tmpDir, 'main.tf'), 'provider "aws" {\n region = "us-east-1"\n}\n');
|
|
193
|
+
|
|
194
|
+
const frameworks: Framework[] = ['SOC2', 'HIPAA', 'PCI'];
|
|
195
|
+
const reports = await checkCompliance({ dir: tmpDir, frameworks });
|
|
196
|
+
|
|
197
|
+
expect(reports.length).toBe(3);
|
|
198
|
+
expect(reports.map(r => r.framework)).toEqual(['SOC2', 'HIPAA', 'PCI']);
|
|
199
|
+
|
|
200
|
+
for (const report of reports) {
|
|
201
|
+
expect(report.controls.length).toBeGreaterThan(0);
|
|
202
|
+
expect(report.timestamp).toBeInstanceOf(Date);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('checkCompliance() passes encryption check when encryption is configured', async () => {
|
|
207
|
+
fs.writeFileSync(
|
|
208
|
+
path.join(tmpDir, 'storage.tf'),
|
|
209
|
+
[
|
|
210
|
+
'resource "aws_s3_bucket" "data" {',
|
|
211
|
+
' bucket = "my-bucket"',
|
|
212
|
+
'',
|
|
213
|
+
' server_side_encryption_configuration {',
|
|
214
|
+
' rule {',
|
|
215
|
+
' apply_server_side_encryption_by_default {',
|
|
216
|
+
' sse_algorithm = "AES256"',
|
|
217
|
+
' }',
|
|
218
|
+
' }',
|
|
219
|
+
' }',
|
|
220
|
+
'}',
|
|
221
|
+
].join('\n')
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const reports = await checkCompliance({
|
|
225
|
+
dir: tmpDir,
|
|
226
|
+
frameworks: ['SOC2'],
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const soc2 = reports[0];
|
|
230
|
+
// SOC2-003 is the "Encryption at rest" control
|
|
231
|
+
const encryptionControl = soc2.controls.find(c => c.id === 'SOC2-003');
|
|
232
|
+
expect(encryptionControl).toBeDefined();
|
|
233
|
+
expect(encryptionControl!.status).toBe('pass');
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test('checkCompliance() fails encryption check when missing', async () => {
|
|
237
|
+
fs.writeFileSync(
|
|
238
|
+
path.join(tmpDir, 'storage.tf'),
|
|
239
|
+
['resource "aws_s3_bucket" "data" {', ' bucket = "my-bucket"', '}'].join('\n')
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
const reports = await checkCompliance({
|
|
243
|
+
dir: tmpDir,
|
|
244
|
+
frameworks: ['SOC2'],
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const soc2 = reports[0];
|
|
248
|
+
const encryptionControl = soc2.controls.find(c => c.id === 'SOC2-003');
|
|
249
|
+
expect(encryptionControl).toBeDefined();
|
|
250
|
+
expect(encryptionControl!.status).toBe('fail');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('checkCompliance() returns correct score', async () => {
|
|
254
|
+
// Create .tf content that satisfies some SOC2 controls but not others.
|
|
255
|
+
// SOC2 has 5 controls. We provide content matching logging (SOC2-001)
|
|
256
|
+
// and access controls (SOC2-002) and network security groups (SOC2-005).
|
|
257
|
+
fs.writeFileSync(
|
|
258
|
+
path.join(tmpDir, 'infra.tf'),
|
|
259
|
+
[
|
|
260
|
+
'resource "aws_cloudtrail" "main" {}',
|
|
261
|
+
'resource "aws_iam_role" "admin" {}',
|
|
262
|
+
'resource "aws_security_group" "web" {}',
|
|
263
|
+
].join('\n')
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
const reports = await checkCompliance({
|
|
267
|
+
dir: tmpDir,
|
|
268
|
+
frameworks: ['SOC2'],
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const soc2 = reports[0];
|
|
272
|
+
// 3 pass, 2 fail, 0 warn => score = round(3/5 * 100) = 60
|
|
273
|
+
expect(soc2.passCount).toBe(3);
|
|
274
|
+
expect(soc2.failCount).toBe(2);
|
|
275
|
+
expect(soc2.score).toBe(60);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test('generateScorecard() produces formatted output', async () => {
|
|
279
|
+
fs.writeFileSync(path.join(tmpDir, 'main.tf'), 'resource "aws_cloudtrail" "main" {}\n');
|
|
280
|
+
|
|
281
|
+
const reports = await checkCompliance({
|
|
282
|
+
dir: tmpDir,
|
|
283
|
+
frameworks: ['SOC2'],
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
const scorecard = generateScorecard(reports);
|
|
287
|
+
|
|
288
|
+
expect(scorecard).toContain('Compliance Scorecard');
|
|
289
|
+
expect(scorecard).toContain('SOC2');
|
|
290
|
+
expect(scorecard).toContain('[PASS]');
|
|
291
|
+
expect(scorecard).toContain('[FAIL]');
|
|
292
|
+
expect(scorecard).toContain('Overall:');
|
|
293
|
+
expect(scorecard).toContain('compliant');
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test('generateScorecard() handles empty reports', () => {
|
|
297
|
+
const scorecard = generateScorecard([]);
|
|
298
|
+
expect(scorecard).toBe('No compliance reports to display.');
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
// ============================================================================
|
|
303
|
+
// Cost Tracker
|
|
304
|
+
// ============================================================================
|
|
305
|
+
|
|
306
|
+
describe('Cost Tracker', () => {
|
|
307
|
+
let tracker: CostTracker;
|
|
308
|
+
|
|
309
|
+
beforeEach(() => {
|
|
310
|
+
tracker = new CostTracker();
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test('recordLLMCost() adds an entry', () => {
|
|
314
|
+
const entry = tracker.recordLLMCost({
|
|
315
|
+
sessionId: 'session-1',
|
|
316
|
+
model: 'claude-sonnet-4-20250514',
|
|
317
|
+
inputTokens: 1000,
|
|
318
|
+
outputTokens: 500,
|
|
319
|
+
costUSD: 0.015,
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
expect(entry.id).toBeDefined();
|
|
323
|
+
expect(entry.category).toBe('llm');
|
|
324
|
+
expect(entry.amount).toBe(0.015);
|
|
325
|
+
expect(entry.model).toBe('claude-sonnet-4-20250514');
|
|
326
|
+
expect(entry.inputTokens).toBe(1000);
|
|
327
|
+
expect(entry.outputTokens).toBe(500);
|
|
328
|
+
expect(entry.sessionId).toBe('session-1');
|
|
329
|
+
expect(entry.timestamp).toBeInstanceOf(Date);
|
|
330
|
+
expect(entry.description).toContain('claude-sonnet-4-20250514');
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test('recordInfraCost() adds an entry', () => {
|
|
334
|
+
const entry = tracker.recordInfraCost({
|
|
335
|
+
sessionId: 'session-2',
|
|
336
|
+
description: 'Added t3.medium EC2 instance',
|
|
337
|
+
monthlyCost: 30.37,
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
expect(entry.id).toBeDefined();
|
|
341
|
+
expect(entry.category).toBe('infra');
|
|
342
|
+
expect(entry.amount).toBe(30.37);
|
|
343
|
+
expect(entry.sessionId).toBe('session-2');
|
|
344
|
+
expect(entry.description).toBe('Added t3.medium EC2 instance');
|
|
345
|
+
expect(entry.inputTokens).toBeUndefined();
|
|
346
|
+
expect(entry.outputTokens).toBeUndefined();
|
|
347
|
+
expect(entry.model).toBeUndefined();
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
test('getSummary() calculates correct totals', () => {
|
|
351
|
+
tracker.recordLLMCost({
|
|
352
|
+
sessionId: 's1',
|
|
353
|
+
model: 'claude-sonnet-4-20250514',
|
|
354
|
+
inputTokens: 100,
|
|
355
|
+
outputTokens: 50,
|
|
356
|
+
costUSD: 0.01,
|
|
357
|
+
});
|
|
358
|
+
tracker.recordLLMCost({
|
|
359
|
+
sessionId: 's1',
|
|
360
|
+
model: 'claude-sonnet-4-20250514',
|
|
361
|
+
inputTokens: 200,
|
|
362
|
+
outputTokens: 100,
|
|
363
|
+
costUSD: 0.02,
|
|
364
|
+
});
|
|
365
|
+
tracker.recordInfraCost({
|
|
366
|
+
sessionId: 's1',
|
|
367
|
+
description: 'RDS',
|
|
368
|
+
monthlyCost: 50.0,
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
const summary = tracker.getSummary();
|
|
372
|
+
|
|
373
|
+
expect(summary.totalCost).toBeCloseTo(50.03, 6);
|
|
374
|
+
expect(summary.llmCost).toBeCloseTo(0.03, 6);
|
|
375
|
+
expect(summary.infraCost).toBeCloseTo(50.0, 6);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
test('getSummary() groups by session', () => {
|
|
379
|
+
tracker.recordLLMCost({
|
|
380
|
+
sessionId: 'session-a',
|
|
381
|
+
model: 'gpt-4',
|
|
382
|
+
inputTokens: 100,
|
|
383
|
+
outputTokens: 50,
|
|
384
|
+
costUSD: 0.01,
|
|
385
|
+
});
|
|
386
|
+
tracker.recordLLMCost({
|
|
387
|
+
sessionId: 'session-b',
|
|
388
|
+
model: 'gpt-4',
|
|
389
|
+
inputTokens: 200,
|
|
390
|
+
outputTokens: 100,
|
|
391
|
+
costUSD: 0.02,
|
|
392
|
+
});
|
|
393
|
+
tracker.recordInfraCost({
|
|
394
|
+
sessionId: 'session-a',
|
|
395
|
+
description: 'EC2',
|
|
396
|
+
monthlyCost: 10.0,
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
const summary = tracker.getSummary();
|
|
400
|
+
|
|
401
|
+
expect(summary.entriesBySession.size).toBe(2);
|
|
402
|
+
expect(summary.entriesBySession.get('session-a')!.length).toBe(2);
|
|
403
|
+
expect(summary.entriesBySession.get('session-b')!.length).toBe(1);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
test('getSummary() calculates daily costs', () => {
|
|
407
|
+
// All entries created with new Date(), so they all belong to today
|
|
408
|
+
tracker.recordLLMCost({
|
|
409
|
+
sessionId: 's1',
|
|
410
|
+
model: 'claude',
|
|
411
|
+
inputTokens: 100,
|
|
412
|
+
outputTokens: 50,
|
|
413
|
+
costUSD: 0.01,
|
|
414
|
+
});
|
|
415
|
+
tracker.recordLLMCost({
|
|
416
|
+
sessionId: 's1',
|
|
417
|
+
model: 'claude',
|
|
418
|
+
inputTokens: 200,
|
|
419
|
+
outputTokens: 100,
|
|
420
|
+
costUSD: 0.02,
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
const summary = tracker.getSummary();
|
|
424
|
+
|
|
425
|
+
expect(summary.dailyCosts.length).toBe(1);
|
|
426
|
+
// Today's date in YYYY-MM-DD
|
|
427
|
+
const today = new Date();
|
|
428
|
+
const y = today.getFullYear();
|
|
429
|
+
const m = String(today.getMonth() + 1).padStart(2, '0');
|
|
430
|
+
const d = String(today.getDate()).padStart(2, '0');
|
|
431
|
+
expect(summary.dailyCosts[0].date).toBe(`${y}-${m}-${d}`);
|
|
432
|
+
expect(summary.dailyCosts[0].amount).toBeCloseTo(0.03, 6);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
test('getSummary() calculates monthly projection', () => {
|
|
436
|
+
tracker.recordLLMCost({
|
|
437
|
+
sessionId: 's1',
|
|
438
|
+
model: 'claude',
|
|
439
|
+
inputTokens: 100,
|
|
440
|
+
outputTokens: 50,
|
|
441
|
+
costUSD: 1.0,
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
const summary = tracker.getSummary();
|
|
445
|
+
|
|
446
|
+
// Single day of data: daily average = 1.0, projection = 1.0 * 30 = 30.0
|
|
447
|
+
expect(summary.monthlyProjection).toBeCloseTo(30.0, 6);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test('getEntries() filters by session', () => {
|
|
451
|
+
tracker.recordLLMCost({
|
|
452
|
+
sessionId: 'alpha',
|
|
453
|
+
model: 'claude',
|
|
454
|
+
inputTokens: 100,
|
|
455
|
+
outputTokens: 50,
|
|
456
|
+
costUSD: 0.01,
|
|
457
|
+
});
|
|
458
|
+
tracker.recordInfraCost({
|
|
459
|
+
sessionId: 'beta',
|
|
460
|
+
description: 'Lambda',
|
|
461
|
+
monthlyCost: 5.0,
|
|
462
|
+
});
|
|
463
|
+
tracker.recordLLMCost({
|
|
464
|
+
sessionId: 'alpha',
|
|
465
|
+
model: 'claude',
|
|
466
|
+
inputTokens: 200,
|
|
467
|
+
outputTokens: 100,
|
|
468
|
+
costUSD: 0.02,
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
const alphaEntries = tracker.getEntries('alpha');
|
|
472
|
+
expect(alphaEntries.length).toBe(2);
|
|
473
|
+
expect(alphaEntries.every(e => e.sessionId === 'alpha')).toBe(true);
|
|
474
|
+
|
|
475
|
+
const betaEntries = tracker.getEntries('beta');
|
|
476
|
+
expect(betaEntries.length).toBe(1);
|
|
477
|
+
expect(betaEntries[0].category).toBe('infra');
|
|
478
|
+
|
|
479
|
+
// All entries when no filter
|
|
480
|
+
const allEntries = tracker.getEntries();
|
|
481
|
+
expect(allEntries.length).toBe(3);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
test('formatSummary() produces readable output', () => {
|
|
485
|
+
tracker.recordLLMCost({
|
|
486
|
+
sessionId: 'demo',
|
|
487
|
+
model: 'claude-sonnet-4-20250514',
|
|
488
|
+
inputTokens: 1500,
|
|
489
|
+
outputTokens: 800,
|
|
490
|
+
costUSD: 0.0165,
|
|
491
|
+
});
|
|
492
|
+
tracker.recordInfraCost({
|
|
493
|
+
sessionId: 'demo',
|
|
494
|
+
description: 'RDS t3.micro',
|
|
495
|
+
monthlyCost: 15.33,
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
const summary = tracker.getSummary();
|
|
499
|
+
const output = tracker.formatSummary(summary);
|
|
500
|
+
|
|
501
|
+
expect(output).toContain('Cost Summary');
|
|
502
|
+
expect(output).toContain('Total Cost:');
|
|
503
|
+
expect(output).toContain('LLM Cost:');
|
|
504
|
+
expect(output).toContain('Infra Cost:');
|
|
505
|
+
expect(output).toContain('Monthly Estimate:');
|
|
506
|
+
expect(output).toContain('Per-Session Breakdown:');
|
|
507
|
+
expect(output).toContain('demo');
|
|
508
|
+
expect(output).toContain('Daily Costs:');
|
|
509
|
+
expect(output).toContain('Projected monthly cost:');
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
// ============================================================================
|
|
514
|
+
// Activity Log
|
|
515
|
+
// ============================================================================
|
|
516
|
+
|
|
517
|
+
describe('Activity Log', () => {
|
|
518
|
+
let log: ActivityLog;
|
|
519
|
+
|
|
520
|
+
beforeEach(() => {
|
|
521
|
+
log = new ActivityLog();
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
test('log() creates entry with auto-generated ID', () => {
|
|
525
|
+
const entry = log.log({
|
|
526
|
+
timestamp: new Date(),
|
|
527
|
+
sessionId: 'session-1',
|
|
528
|
+
toolName: 'terraform_plan',
|
|
529
|
+
toolInput: { dir: '/infra' },
|
|
530
|
+
result: { output: 'Plan: 3 to add', isError: false },
|
|
531
|
+
duration: 4200,
|
|
532
|
+
mode: 'plan',
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
expect(entry.id).toBeDefined();
|
|
536
|
+
expect(typeof entry.id).toBe('string');
|
|
537
|
+
expect(entry.id.length).toBeGreaterThan(0);
|
|
538
|
+
expect(entry.toolName).toBe('terraform_plan');
|
|
539
|
+
expect(entry.sessionId).toBe('session-1');
|
|
540
|
+
expect(entry.duration).toBe(4200);
|
|
541
|
+
expect(entry.mode).toBe('plan');
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
test('query() returns all entries with no filter', () => {
|
|
545
|
+
log.log({
|
|
546
|
+
timestamp: new Date(),
|
|
547
|
+
sessionId: 's1',
|
|
548
|
+
toolName: 'file_read',
|
|
549
|
+
toolInput: { path: '/a.ts' },
|
|
550
|
+
result: { output: 'ok', isError: false },
|
|
551
|
+
duration: 50,
|
|
552
|
+
mode: 'build',
|
|
553
|
+
});
|
|
554
|
+
log.log({
|
|
555
|
+
timestamp: new Date(),
|
|
556
|
+
sessionId: 's1',
|
|
557
|
+
toolName: 'git_commit',
|
|
558
|
+
toolInput: { message: 'fix' },
|
|
559
|
+
result: { output: 'committed', isError: false },
|
|
560
|
+
duration: 300,
|
|
561
|
+
mode: 'build',
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
const results = log.query();
|
|
565
|
+
|
|
566
|
+
expect(results.length).toBe(2);
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
test('query() filters by toolName', () => {
|
|
570
|
+
log.log({
|
|
571
|
+
timestamp: new Date(),
|
|
572
|
+
sessionId: 's1',
|
|
573
|
+
toolName: 'file_read',
|
|
574
|
+
toolInput: {},
|
|
575
|
+
result: { output: 'ok', isError: false },
|
|
576
|
+
duration: 10,
|
|
577
|
+
mode: 'build',
|
|
578
|
+
});
|
|
579
|
+
log.log({
|
|
580
|
+
timestamp: new Date(),
|
|
581
|
+
sessionId: 's1',
|
|
582
|
+
toolName: 'terraform_plan',
|
|
583
|
+
toolInput: {},
|
|
584
|
+
result: { output: 'ok', isError: false },
|
|
585
|
+
duration: 3000,
|
|
586
|
+
mode: 'plan',
|
|
587
|
+
});
|
|
588
|
+
log.log({
|
|
589
|
+
timestamp: new Date(),
|
|
590
|
+
sessionId: 's1',
|
|
591
|
+
toolName: 'file_read',
|
|
592
|
+
toolInput: {},
|
|
593
|
+
result: { output: 'ok', isError: false },
|
|
594
|
+
duration: 15,
|
|
595
|
+
mode: 'build',
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
const results = log.query({ toolName: 'file_read' });
|
|
599
|
+
|
|
600
|
+
expect(results.length).toBe(2);
|
|
601
|
+
expect(results.every(e => e.toolName === 'file_read')).toBe(true);
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
test('query() filters by date range', () => {
|
|
605
|
+
const jan1 = new Date('2025-01-01T00:00:00Z');
|
|
606
|
+
const feb1 = new Date('2025-02-01T00:00:00Z');
|
|
607
|
+
const mar1 = new Date('2025-03-01T00:00:00Z');
|
|
608
|
+
|
|
609
|
+
log.log({
|
|
610
|
+
timestamp: jan1,
|
|
611
|
+
sessionId: 's1',
|
|
612
|
+
toolName: 'tool_a',
|
|
613
|
+
toolInput: {},
|
|
614
|
+
result: { output: '', isError: false },
|
|
615
|
+
duration: 100,
|
|
616
|
+
mode: 'build',
|
|
617
|
+
});
|
|
618
|
+
log.log({
|
|
619
|
+
timestamp: feb1,
|
|
620
|
+
sessionId: 's1',
|
|
621
|
+
toolName: 'tool_b',
|
|
622
|
+
toolInput: {},
|
|
623
|
+
result: { output: '', isError: false },
|
|
624
|
+
duration: 100,
|
|
625
|
+
mode: 'build',
|
|
626
|
+
});
|
|
627
|
+
log.log({
|
|
628
|
+
timestamp: mar1,
|
|
629
|
+
sessionId: 's1',
|
|
630
|
+
toolName: 'tool_c',
|
|
631
|
+
toolInput: {},
|
|
632
|
+
result: { output: '', isError: false },
|
|
633
|
+
duration: 100,
|
|
634
|
+
mode: 'build',
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
const results = log.query({
|
|
638
|
+
since: new Date('2025-01-15T00:00:00Z'),
|
|
639
|
+
until: new Date('2025-02-15T00:00:00Z'),
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
expect(results.length).toBe(1);
|
|
643
|
+
expect(results[0].toolName).toBe('tool_b');
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
test('query() filters by sessionId', () => {
|
|
647
|
+
log.log({
|
|
648
|
+
timestamp: new Date(),
|
|
649
|
+
sessionId: 'alpha',
|
|
650
|
+
toolName: 'file_read',
|
|
651
|
+
toolInput: {},
|
|
652
|
+
result: { output: '', isError: false },
|
|
653
|
+
duration: 10,
|
|
654
|
+
mode: 'build',
|
|
655
|
+
});
|
|
656
|
+
log.log({
|
|
657
|
+
timestamp: new Date(),
|
|
658
|
+
sessionId: 'beta',
|
|
659
|
+
toolName: 'file_write',
|
|
660
|
+
toolInput: {},
|
|
661
|
+
result: { output: '', isError: false },
|
|
662
|
+
duration: 20,
|
|
663
|
+
mode: 'build',
|
|
664
|
+
});
|
|
665
|
+
log.log({
|
|
666
|
+
timestamp: new Date(),
|
|
667
|
+
sessionId: 'alpha',
|
|
668
|
+
toolName: 'git_status',
|
|
669
|
+
toolInput: {},
|
|
670
|
+
result: { output: '', isError: false },
|
|
671
|
+
duration: 30,
|
|
672
|
+
mode: 'build',
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
const results = log.query({ sessionId: 'alpha' });
|
|
676
|
+
|
|
677
|
+
expect(results.length).toBe(2);
|
|
678
|
+
expect(results.every(e => e.sessionId === 'alpha')).toBe(true);
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
test('query() filters by error status', () => {
|
|
682
|
+
log.log({
|
|
683
|
+
timestamp: new Date(),
|
|
684
|
+
sessionId: 's1',
|
|
685
|
+
toolName: 'terraform_apply',
|
|
686
|
+
toolInput: {},
|
|
687
|
+
result: { output: 'Error: access denied', isError: true },
|
|
688
|
+
duration: 5000,
|
|
689
|
+
mode: 'deploy',
|
|
690
|
+
});
|
|
691
|
+
log.log({
|
|
692
|
+
timestamp: new Date(),
|
|
693
|
+
sessionId: 's1',
|
|
694
|
+
toolName: 'file_read',
|
|
695
|
+
toolInput: {},
|
|
696
|
+
result: { output: 'ok', isError: false },
|
|
697
|
+
duration: 10,
|
|
698
|
+
mode: 'build',
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
const errors = log.query({ isError: true });
|
|
702
|
+
expect(errors.length).toBe(1);
|
|
703
|
+
expect(errors[0].toolName).toBe('terraform_apply');
|
|
704
|
+
|
|
705
|
+
const successes = log.query({ isError: false });
|
|
706
|
+
expect(successes.length).toBe(1);
|
|
707
|
+
expect(successes[0].toolName).toBe('file_read');
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
test('query() respects limit', () => {
|
|
711
|
+
for (let i = 0; i < 10; i++) {
|
|
712
|
+
log.log({
|
|
713
|
+
timestamp: new Date(Date.now() + i * 1000),
|
|
714
|
+
sessionId: 's1',
|
|
715
|
+
toolName: `tool_${i}`,
|
|
716
|
+
toolInput: {},
|
|
717
|
+
result: { output: '', isError: false },
|
|
718
|
+
duration: 100,
|
|
719
|
+
mode: 'build',
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
const results = log.query({ limit: 3 });
|
|
724
|
+
|
|
725
|
+
expect(results.length).toBe(3);
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
test('getStats() returns correct totals', () => {
|
|
729
|
+
log.log({
|
|
730
|
+
timestamp: new Date(),
|
|
731
|
+
sessionId: 's1',
|
|
732
|
+
toolName: 'file_read',
|
|
733
|
+
toolInput: {},
|
|
734
|
+
result: { output: '', isError: false },
|
|
735
|
+
duration: 100,
|
|
736
|
+
mode: 'build',
|
|
737
|
+
});
|
|
738
|
+
log.log({
|
|
739
|
+
timestamp: new Date(),
|
|
740
|
+
sessionId: 's1',
|
|
741
|
+
toolName: 'terraform_plan',
|
|
742
|
+
toolInput: {},
|
|
743
|
+
result: { output: 'Error', isError: true },
|
|
744
|
+
duration: 5000,
|
|
745
|
+
mode: 'plan',
|
|
746
|
+
});
|
|
747
|
+
log.log({
|
|
748
|
+
timestamp: new Date(),
|
|
749
|
+
sessionId: 's1',
|
|
750
|
+
toolName: 'file_read',
|
|
751
|
+
toolInput: {},
|
|
752
|
+
result: { output: '', isError: false },
|
|
753
|
+
duration: 200,
|
|
754
|
+
mode: 'build',
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
const stats = log.getStats();
|
|
758
|
+
|
|
759
|
+
expect(stats.totalCalls).toBe(3);
|
|
760
|
+
expect(stats.errorCount).toBe(1);
|
|
761
|
+
// avg = (100 + 5000 + 200) / 3 = 1766.666... => round = 1767
|
|
762
|
+
expect(stats.avgDuration).toBe(1767);
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
test('getStats() returns tool breakdown', () => {
|
|
766
|
+
log.log({
|
|
767
|
+
timestamp: new Date(),
|
|
768
|
+
sessionId: 's1',
|
|
769
|
+
toolName: 'file_read',
|
|
770
|
+
toolInput: {},
|
|
771
|
+
result: { output: '', isError: false },
|
|
772
|
+
duration: 10,
|
|
773
|
+
mode: 'build',
|
|
774
|
+
});
|
|
775
|
+
log.log({
|
|
776
|
+
timestamp: new Date(),
|
|
777
|
+
sessionId: 's1',
|
|
778
|
+
toolName: 'file_read',
|
|
779
|
+
toolInput: {},
|
|
780
|
+
result: { output: '', isError: false },
|
|
781
|
+
duration: 10,
|
|
782
|
+
mode: 'build',
|
|
783
|
+
});
|
|
784
|
+
log.log({
|
|
785
|
+
timestamp: new Date(),
|
|
786
|
+
sessionId: 's1',
|
|
787
|
+
toolName: 'terraform_plan',
|
|
788
|
+
toolInput: {},
|
|
789
|
+
result: { output: '', isError: false },
|
|
790
|
+
duration: 3000,
|
|
791
|
+
mode: 'plan',
|
|
792
|
+
});
|
|
793
|
+
log.log({
|
|
794
|
+
timestamp: new Date(),
|
|
795
|
+
sessionId: 's1',
|
|
796
|
+
toolName: 'git_commit',
|
|
797
|
+
toolInput: {},
|
|
798
|
+
result: { output: '', isError: false },
|
|
799
|
+
duration: 500,
|
|
800
|
+
mode: 'build',
|
|
801
|
+
});
|
|
802
|
+
|
|
803
|
+
const stats = log.getStats();
|
|
804
|
+
|
|
805
|
+
expect(stats.toolBreakdown['file_read']).toBe(2);
|
|
806
|
+
expect(stats.toolBreakdown['terraform_plan']).toBe(1);
|
|
807
|
+
expect(stats.toolBreakdown['git_commit']).toBe(1);
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
test('formatLog() produces readable output', () => {
|
|
811
|
+
const now = new Date('2025-06-15T10:30:00Z');
|
|
812
|
+
log.log({
|
|
813
|
+
timestamp: now,
|
|
814
|
+
sessionId: 's1',
|
|
815
|
+
toolName: 'terraform_plan',
|
|
816
|
+
toolInput: { dir: '/infra' },
|
|
817
|
+
result: { output: 'Plan: 3 to add', isError: false },
|
|
818
|
+
duration: 4200,
|
|
819
|
+
mode: 'plan',
|
|
820
|
+
});
|
|
821
|
+
log.log({
|
|
822
|
+
timestamp: new Date(now.getTime() + 60000),
|
|
823
|
+
sessionId: 's1',
|
|
824
|
+
toolName: 'terraform_apply',
|
|
825
|
+
toolInput: { dir: '/infra' },
|
|
826
|
+
result: { output: 'Error: access denied', isError: true },
|
|
827
|
+
duration: 1200,
|
|
828
|
+
mode: 'deploy',
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
const entries = log.query();
|
|
832
|
+
const output = log.formatLog(entries);
|
|
833
|
+
|
|
834
|
+
expect(output).toContain('Activity Log');
|
|
835
|
+
expect(output).toContain('2 entries');
|
|
836
|
+
expect(output).toContain('terraform_plan');
|
|
837
|
+
expect(output).toContain('terraform_apply');
|
|
838
|
+
expect(output).toContain('[OK]');
|
|
839
|
+
expect(output).toContain('[ERROR]');
|
|
840
|
+
expect(output).toContain('[plan]');
|
|
841
|
+
expect(output).toContain('[deploy]');
|
|
842
|
+
expect(output).toContain('1 errors');
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
test('formatLog() handles empty entries', () => {
|
|
846
|
+
const output = log.formatLog([]);
|
|
847
|
+
expect(output).toBe('No activity entries to display.');
|
|
848
|
+
});
|
|
849
|
+
|
|
850
|
+
test('clear() removes all entries', () => {
|
|
851
|
+
log.log({
|
|
852
|
+
timestamp: new Date(),
|
|
853
|
+
sessionId: 's1',
|
|
854
|
+
toolName: 'file_read',
|
|
855
|
+
toolInput: {},
|
|
856
|
+
result: { output: '', isError: false },
|
|
857
|
+
duration: 10,
|
|
858
|
+
mode: 'build',
|
|
859
|
+
});
|
|
860
|
+
log.log({
|
|
861
|
+
timestamp: new Date(),
|
|
862
|
+
sessionId: 's1',
|
|
863
|
+
toolName: 'git_status',
|
|
864
|
+
toolInput: {},
|
|
865
|
+
result: { output: '', isError: false },
|
|
866
|
+
duration: 50,
|
|
867
|
+
mode: 'build',
|
|
868
|
+
});
|
|
869
|
+
|
|
870
|
+
expect(log.query().length).toBe(2);
|
|
871
|
+
|
|
872
|
+
log.clear();
|
|
873
|
+
|
|
874
|
+
expect(log.query().length).toBe(0);
|
|
875
|
+
expect(log.getStats().totalCalls).toBe(0);
|
|
876
|
+
});
|
|
877
|
+
});
|