@eduardbar/drift 1.2.0 → 1.4.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/.gga +50 -0
- package/.github/actions/drift-review/README.md +60 -0
- package/.github/actions/drift-review/action.yml +131 -0
- package/.github/actions/drift-scan/README.md +28 -32
- package/.github/actions/drift-scan/action.yml +78 -14
- package/.github/workflows/publish-vscode.yml +3 -3
- package/.github/workflows/publish.yml +3 -3
- package/.github/workflows/review-pr.yml +94 -9
- package/AGENTS.md +75 -245
- package/CHANGELOG.md +28 -0
- package/README.md +308 -51
- package/ROADMAP.md +6 -5
- package/dist/analyzer.d.ts +2 -2
- package/dist/analyzer.js +420 -159
- package/dist/benchmark.d.ts +2 -0
- package/dist/benchmark.js +204 -0
- package/dist/cli.js +693 -67
- package/dist/config.js +16 -2
- package/dist/diff.js +66 -10
- package/dist/doctor.d.ts +5 -0
- package/dist/doctor.js +133 -0
- package/dist/format.d.ts +17 -0
- package/dist/format.js +45 -0
- package/dist/git.js +12 -0
- package/dist/guard-types.d.ts +57 -0
- package/dist/guard-types.js +2 -0
- package/dist/guard.d.ts +14 -0
- package/dist/guard.js +239 -0
- package/dist/index.d.ts +12 -3
- package/dist/index.js +6 -1
- package/dist/init.d.ts +15 -0
- package/dist/init.js +273 -0
- package/dist/map-cycles.d.ts +2 -0
- package/dist/map-cycles.js +34 -0
- package/dist/map-svg.d.ts +19 -0
- package/dist/map-svg.js +97 -0
- package/dist/map.js +78 -138
- package/dist/metrics.js +70 -55
- package/dist/output-metadata.d.ts +13 -0
- package/dist/output-metadata.js +17 -0
- package/dist/plugins-capabilities.d.ts +4 -0
- package/dist/plugins-capabilities.js +21 -0
- package/dist/plugins-messages.d.ts +10 -0
- package/dist/plugins-messages.js +16 -0
- package/dist/plugins-rules.d.ts +9 -0
- package/dist/plugins-rules.js +137 -0
- package/dist/plugins.d.ts +2 -1
- package/dist/plugins.js +80 -28
- package/dist/printer.js +4 -0
- package/dist/reporter-constants.d.ts +16 -0
- package/dist/reporter-constants.js +39 -0
- package/dist/reporter.d.ts +3 -3
- package/dist/reporter.js +35 -55
- package/dist/review.d.ts +2 -1
- package/dist/review.js +4 -3
- package/dist/rules/comments.js +2 -2
- package/dist/rules/complexity.js +2 -7
- package/dist/rules/nesting.js +3 -13
- package/dist/rules/phase0-basic.js +10 -10
- package/dist/rules/phase3-configurable.js +23 -15
- package/dist/rules/shared.d.ts +2 -0
- package/dist/rules/shared.js +27 -3
- package/dist/saas/constants.d.ts +15 -0
- package/dist/saas/constants.js +48 -0
- package/dist/saas/dashboard.d.ts +8 -0
- package/dist/saas/dashboard.js +132 -0
- package/dist/saas/errors.d.ts +19 -0
- package/dist/saas/errors.js +37 -0
- package/dist/saas/helpers.d.ts +21 -0
- package/dist/saas/helpers.js +110 -0
- package/dist/saas/ingest.d.ts +3 -0
- package/dist/saas/ingest.js +249 -0
- package/dist/saas/organization.d.ts +5 -0
- package/dist/saas/organization.js +82 -0
- package/dist/saas/plan-change.d.ts +10 -0
- package/dist/saas/plan-change.js +15 -0
- package/dist/saas/store.d.ts +21 -0
- package/dist/saas/store.js +159 -0
- package/dist/saas/types.d.ts +191 -0
- package/dist/saas/types.js +2 -0
- package/dist/saas.d.ts +8 -82
- package/dist/saas.js +7 -320
- package/dist/sarif.d.ts +74 -0
- package/dist/sarif.js +122 -0
- package/dist/trust-advanced.d.ts +14 -0
- package/dist/trust-advanced.js +65 -0
- package/dist/trust-kpi-fs.d.ts +3 -0
- package/dist/trust-kpi-fs.js +141 -0
- package/dist/trust-kpi-parse.d.ts +7 -0
- package/dist/trust-kpi-parse.js +186 -0
- package/dist/trust-kpi-types.d.ts +16 -0
- package/dist/trust-kpi-types.js +2 -0
- package/dist/trust-kpi.d.ts +7 -0
- package/dist/trust-kpi.js +185 -0
- package/dist/trust-policy.d.ts +32 -0
- package/dist/trust-policy.js +160 -0
- package/dist/trust-render.d.ts +9 -0
- package/dist/trust-render.js +54 -0
- package/dist/trust-scoring.d.ts +9 -0
- package/dist/trust-scoring.js +208 -0
- package/dist/trust.d.ts +37 -0
- package/dist/trust.js +168 -0
- package/dist/types/app.d.ts +30 -0
- package/dist/types/app.js +2 -0
- package/dist/types/config.d.ts +25 -0
- package/dist/types/config.js +2 -0
- package/dist/types/core.d.ts +100 -0
- package/dist/types/core.js +2 -0
- package/dist/types/diff.d.ts +55 -0
- package/dist/types/diff.js +2 -0
- package/dist/types/plugin.d.ts +41 -0
- package/dist/types/plugin.js +2 -0
- package/dist/types/trust.d.ts +120 -0
- package/dist/types/trust.js +2 -0
- package/dist/types.d.ts +8 -211
- package/docs/PRD.md +187 -109
- package/docs/plugin-contract.md +61 -0
- package/docs/release-notes-draft.md +40 -0
- package/docs/rules-catalog.md +49 -0
- package/docs/trust-core-release-checklist.md +87 -0
- package/package.json +6 -3
- package/packages/vscode-drift/src/code-actions.ts +1 -1
- package/schemas/drift-ai-output.v1.json +162 -0
- package/schemas/drift-report.v1.json +151 -0
- package/schemas/drift-trust.v1.json +131 -0
- package/scripts/smoke-repo.mjs +394 -0
- package/src/analyzer.ts +484 -155
- package/src/benchmark.ts +266 -0
- package/src/cli.ts +840 -85
- package/src/config.ts +19 -2
- package/src/diff.ts +84 -10
- package/src/doctor.ts +173 -0
- package/src/format.ts +81 -0
- package/src/git.ts +16 -0
- package/src/guard-types.ts +64 -0
- package/src/guard.ts +324 -0
- package/src/index.ts +83 -0
- package/src/init.ts +298 -0
- package/src/map-cycles.ts +38 -0
- package/src/map-svg.ts +124 -0
- package/src/map.ts +111 -142
- package/src/metrics.ts +78 -59
- package/src/output-metadata.ts +30 -0
- package/src/plugins-capabilities.ts +36 -0
- package/src/plugins-messages.ts +35 -0
- package/src/plugins-rules.ts +296 -0
- package/src/plugins.ts +148 -27
- package/src/printer.ts +4 -0
- package/src/reporter-constants.ts +46 -0
- package/src/reporter.ts +64 -65
- package/src/review.ts +6 -4
- package/src/rules/comments.ts +2 -2
- package/src/rules/complexity.ts +2 -7
- package/src/rules/nesting.ts +3 -13
- package/src/rules/phase0-basic.ts +11 -12
- package/src/rules/phase3-configurable.ts +39 -26
- package/src/rules/shared.ts +31 -3
- package/src/saas/constants.ts +56 -0
- package/src/saas/dashboard.ts +172 -0
- package/src/saas/errors.ts +45 -0
- package/src/saas/helpers.ts +140 -0
- package/src/saas/ingest.ts +278 -0
- package/src/saas/organization.ts +99 -0
- package/src/saas/plan-change.ts +19 -0
- package/src/saas/store.ts +172 -0
- package/src/saas/types.ts +216 -0
- package/src/saas.ts +49 -433
- package/src/sarif.ts +232 -0
- package/src/trust-advanced.ts +99 -0
- package/src/trust-kpi-fs.ts +169 -0
- package/src/trust-kpi-parse.ts +219 -0
- package/src/trust-kpi-types.ts +19 -0
- package/src/trust-kpi.ts +210 -0
- package/src/trust-policy.ts +246 -0
- package/src/trust-render.ts +61 -0
- package/src/trust-scoring.ts +231 -0
- package/src/trust.ts +260 -0
- package/src/types/app.ts +30 -0
- package/src/types/config.ts +27 -0
- package/src/types/core.ts +105 -0
- package/src/types/diff.ts +61 -0
- package/src/types/plugin.ts +46 -0
- package/src/types/trust.ts +134 -0
- package/src/types.ts +78 -238
- package/tests/cli-sarif.test.ts +92 -0
- package/tests/diff.test.ts +124 -0
- package/tests/format.test.ts +157 -0
- package/tests/new-features.test.ts +80 -1
- package/tests/phase1-init-doctor-guard.test.ts +199 -0
- package/tests/plugins.test.ts +219 -0
- package/tests/rules.test.ts +23 -1
- package/tests/saas-foundation.test.ts +358 -1
- package/tests/sarif.test.ts +160 -0
- package/tests/trust-kpi.test.ts +147 -0
- package/tests/trust.test.ts +602 -0
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
buildTrustReport,
|
|
4
|
+
detectBranchName,
|
|
5
|
+
evaluateTrustGate,
|
|
6
|
+
formatTrustGatePolicyExplanation,
|
|
7
|
+
explainTrustGatePolicy,
|
|
8
|
+
formatTrustJson,
|
|
9
|
+
formatTrustJsonObject,
|
|
10
|
+
formatTrustMarkdown,
|
|
11
|
+
normalizeMergeRiskLevel,
|
|
12
|
+
renderTrustOutput,
|
|
13
|
+
resolveTrustGatePolicy,
|
|
14
|
+
shouldFailByMaxRisk,
|
|
15
|
+
shouldFailTrustGate,
|
|
16
|
+
} from '../src/trust.js'
|
|
17
|
+
import type { DriftConfig, DriftDiff, DriftReport } from '../src/types.js'
|
|
18
|
+
|
|
19
|
+
function createBaseReport(overrides?: Partial<DriftReport>): DriftReport {
|
|
20
|
+
return {
|
|
21
|
+
scannedAt: new Date().toISOString(),
|
|
22
|
+
targetPath: '/tmp/repo',
|
|
23
|
+
files: [],
|
|
24
|
+
totalIssues: 0,
|
|
25
|
+
totalScore: 0,
|
|
26
|
+
totalFiles: 0,
|
|
27
|
+
summary: {
|
|
28
|
+
errors: 0,
|
|
29
|
+
warnings: 0,
|
|
30
|
+
infos: 0,
|
|
31
|
+
byRule: {},
|
|
32
|
+
},
|
|
33
|
+
quality: {
|
|
34
|
+
overall: 100,
|
|
35
|
+
dimensions: {
|
|
36
|
+
architecture: 100,
|
|
37
|
+
complexity: 100,
|
|
38
|
+
'ai-patterns': 100,
|
|
39
|
+
testing: 100,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
maintenanceRisk: {
|
|
43
|
+
score: 0,
|
|
44
|
+
level: 'low',
|
|
45
|
+
hotspots: [],
|
|
46
|
+
signals: {
|
|
47
|
+
highComplexityFiles: 0,
|
|
48
|
+
filesWithoutNearbyTests: 0,
|
|
49
|
+
frequentChangeFiles: 0,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
...overrides,
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe('drift trust baseline', () => {
|
|
57
|
+
it('builds trust report contract with required fields', () => {
|
|
58
|
+
const report = createBaseReport({
|
|
59
|
+
totalIssues: 8,
|
|
60
|
+
totalScore: 52,
|
|
61
|
+
summary: {
|
|
62
|
+
errors: 2,
|
|
63
|
+
warnings: 5,
|
|
64
|
+
infos: 1,
|
|
65
|
+
byRule: {
|
|
66
|
+
'high-complexity': 2,
|
|
67
|
+
'debug-leftover': 3,
|
|
68
|
+
'layer-violation': 1,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
maintenanceRisk: {
|
|
72
|
+
score: 70,
|
|
73
|
+
level: 'high',
|
|
74
|
+
hotspots: [{
|
|
75
|
+
file: '/tmp/repo/src/api/user.ts',
|
|
76
|
+
driftScore: 65,
|
|
77
|
+
complexityIssues: 2,
|
|
78
|
+
hasNearbyTests: false,
|
|
79
|
+
changeFrequency: 9,
|
|
80
|
+
risk: 82,
|
|
81
|
+
reasons: ['high complexity signals'],
|
|
82
|
+
}],
|
|
83
|
+
signals: {
|
|
84
|
+
highComplexityFiles: 1,
|
|
85
|
+
filesWithoutNearbyTests: 1,
|
|
86
|
+
frequentChangeFiles: 1,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
const trust = buildTrustReport(report)
|
|
92
|
+
expect(trust.trust_score).toBeGreaterThanOrEqual(0)
|
|
93
|
+
expect(trust.trust_score).toBeLessThanOrEqual(100)
|
|
94
|
+
expect(['LOW', 'MEDIUM', 'HIGH', 'CRITICAL']).toContain(trust.merge_risk)
|
|
95
|
+
expect(trust.top_reasons.length).toBeGreaterThan(0)
|
|
96
|
+
expect(trust.fix_priorities.length).toBeGreaterThan(0)
|
|
97
|
+
expect(trust.fix_priorities[0]?.rank).toBe(1)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('includes architecture signal in top reasons when violations exist', () => {
|
|
101
|
+
const report = createBaseReport({
|
|
102
|
+
totalScore: 25,
|
|
103
|
+
summary: {
|
|
104
|
+
errors: 1,
|
|
105
|
+
warnings: 1,
|
|
106
|
+
infos: 0,
|
|
107
|
+
byRule: {
|
|
108
|
+
'layer-violation': 1,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
const trust = buildTrustReport(report)
|
|
114
|
+
const reasonLabels = trust.top_reasons.map((reason) => reason.label)
|
|
115
|
+
expect(reasonLabels).toContain('Architecture signals')
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('compares merge risk thresholds for CI gating', () => {
|
|
119
|
+
expect(shouldFailByMaxRisk('CRITICAL', 'HIGH')).toBe(true)
|
|
120
|
+
expect(shouldFailByMaxRisk('HIGH', 'HIGH')).toBe(false)
|
|
121
|
+
expect(shouldFailByMaxRisk('LOW', 'MEDIUM')).toBe(false)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('evaluates trust gate using combined thresholds', () => {
|
|
125
|
+
const trust = buildTrustReport(createBaseReport({ totalScore: 30 }))
|
|
126
|
+
const mediumTrust = { ...trust, trust_score: 65, merge_risk: 'HIGH' as const }
|
|
127
|
+
|
|
128
|
+
expect(shouldFailTrustGate(mediumTrust, { minTrust: 70 })).toBe(true)
|
|
129
|
+
expect(shouldFailTrustGate(mediumTrust, { minTrust: 60 })).toBe(false)
|
|
130
|
+
expect(shouldFailTrustGate(mediumTrust, { maxRisk: 'MEDIUM' })).toBe(true)
|
|
131
|
+
expect(shouldFailTrustGate(mediumTrust, { maxRisk: 'HIGH' })).toBe(false)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('treats disabled trust gate policy as pass-through', () => {
|
|
135
|
+
const trust = buildTrustReport(createBaseReport({ totalScore: 30 }))
|
|
136
|
+
const mediumTrust = { ...trust, trust_score: 65, merge_risk: 'HIGH' as const }
|
|
137
|
+
|
|
138
|
+
expect(shouldFailTrustGate(mediumTrust, { enabled: false, minTrust: 90, maxRisk: 'LOW' })).toBe(false)
|
|
139
|
+
|
|
140
|
+
const evaluation = evaluateTrustGate(mediumTrust, { enabled: false, minTrust: 90, maxRisk: 'LOW' })
|
|
141
|
+
expect(evaluation.shouldFail).toBe(false)
|
|
142
|
+
expect(evaluation.checks.gateDisabled).toBe(true)
|
|
143
|
+
expect(evaluation.reasons).toContain('trust gate disabled by policy')
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('normalizes merge risk level inputs', () => {
|
|
147
|
+
expect(normalizeMergeRiskLevel('low')).toBe('LOW')
|
|
148
|
+
expect(normalizeMergeRiskLevel('MEDIUM')).toBe('MEDIUM')
|
|
149
|
+
expect(normalizeMergeRiskLevel('nope')).toBeUndefined()
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it('formats markdown output for PR comments', () => {
|
|
153
|
+
const report = createBaseReport({
|
|
154
|
+
targetPath: '/tmp/repo',
|
|
155
|
+
totalScore: 22,
|
|
156
|
+
summary: {
|
|
157
|
+
errors: 1,
|
|
158
|
+
warnings: 1,
|
|
159
|
+
infos: 0,
|
|
160
|
+
byRule: {
|
|
161
|
+
'high-complexity': 1,
|
|
162
|
+
'layer-violation': 1,
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
const diff: DriftDiff = {
|
|
168
|
+
baseRef: 'origin/main',
|
|
169
|
+
projectPath: '/tmp/repo',
|
|
170
|
+
scannedAt: new Date().toISOString(),
|
|
171
|
+
files: [
|
|
172
|
+
{
|
|
173
|
+
path: '/tmp/repo/src/a.ts',
|
|
174
|
+
scoreBefore: 10,
|
|
175
|
+
scoreAfter: 16,
|
|
176
|
+
scoreDelta: 6,
|
|
177
|
+
newIssues: [],
|
|
178
|
+
resolvedIssues: [],
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
totalScoreBefore: 18,
|
|
182
|
+
totalScoreAfter: 24,
|
|
183
|
+
totalDelta: 6,
|
|
184
|
+
newIssuesCount: 2,
|
|
185
|
+
resolvedIssuesCount: 1,
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const trust = buildTrustReport(report, { diff })
|
|
189
|
+
const markdown = formatTrustMarkdown(trust)
|
|
190
|
+
|
|
191
|
+
expect(markdown).toContain('## drift trust')
|
|
192
|
+
expect(markdown).toContain('Base ref: `origin/main`')
|
|
193
|
+
expect(markdown).toContain('### Top reasons')
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it('renders selected trust output format deterministically', () => {
|
|
197
|
+
const report = createBaseReport()
|
|
198
|
+
const trust = buildTrustReport(report)
|
|
199
|
+
|
|
200
|
+
expect(renderTrustOutput(trust, { json: true })).toBe(formatTrustJson(trust))
|
|
201
|
+
expect(renderTrustOutput(trust, { markdown: true })).toBe(formatTrustMarkdown(trust))
|
|
202
|
+
expect(renderTrustOutput(trust, { json: true, markdown: true })).toBe(formatTrustJson(trust))
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
it('adds schema metadata in trust JSON output without breaking trust payload', () => {
|
|
206
|
+
const report = createBaseReport({ targetPath: '/tmp/metadata' })
|
|
207
|
+
const trust = buildTrustReport(report)
|
|
208
|
+
|
|
209
|
+
const jsonObject = formatTrustJsonObject(trust)
|
|
210
|
+
expect(jsonObject.$schema).toBe('schemas/drift-trust.v1.json')
|
|
211
|
+
expect(typeof jsonObject.toolVersion).toBe('string')
|
|
212
|
+
expect(jsonObject.toolVersion.length).toBeGreaterThan(0)
|
|
213
|
+
expect(jsonObject.trust_score).toBe(trust.trust_score)
|
|
214
|
+
expect(jsonObject.merge_risk).toBe(trust.merge_risk)
|
|
215
|
+
expect(jsonObject.targetPath).toBe('/tmp/metadata')
|
|
216
|
+
|
|
217
|
+
const parsed = JSON.parse(formatTrustJson(trust)) as Record<string, unknown>
|
|
218
|
+
expect(parsed.$schema).toBe('schemas/drift-trust.v1.json')
|
|
219
|
+
expect(typeof parsed.toolVersion).toBe('string')
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
it('keeps baseline trust contract unchanged when advanced mode is disabled', () => {
|
|
223
|
+
const report = createBaseReport({
|
|
224
|
+
totalScore: 28,
|
|
225
|
+
summary: {
|
|
226
|
+
errors: 1,
|
|
227
|
+
warnings: 2,
|
|
228
|
+
infos: 0,
|
|
229
|
+
byRule: {
|
|
230
|
+
'debug-leftover': 3,
|
|
231
|
+
'high-complexity': 1,
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
const trust = buildTrustReport(report)
|
|
237
|
+
expect(trust.advanced_context).toBeUndefined()
|
|
238
|
+
expect(trust.fix_priorities[0]).not.toHaveProperty('confidence')
|
|
239
|
+
expect(trust.fix_priorities[0]).not.toHaveProperty('explanation')
|
|
240
|
+
expect(trust.fix_priorities[0]).not.toHaveProperty('systemic')
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it('enriches trust report with advanced comparison and metadata from previous trust JSON', () => {
|
|
244
|
+
const report = createBaseReport({
|
|
245
|
+
totalScore: 22,
|
|
246
|
+
summary: {
|
|
247
|
+
errors: 1,
|
|
248
|
+
warnings: 1,
|
|
249
|
+
infos: 0,
|
|
250
|
+
byRule: {
|
|
251
|
+
'layer-violation': 1,
|
|
252
|
+
'debug-leftover': 1,
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
const trust = buildTrustReport(report, {
|
|
258
|
+
advanced: {
|
|
259
|
+
enabled: true,
|
|
260
|
+
previousTrust: {
|
|
261
|
+
trust_score: 60,
|
|
262
|
+
merge_risk: 'HIGH',
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
expect(trust.advanced_context?.comparison?.source).toBe('previous-trust-json')
|
|
268
|
+
expect(typeof trust.advanced_context?.comparison?.trust_delta).toBe('number')
|
|
269
|
+
expect(trust.advanced_context?.team_guidance.length).toBeGreaterThan(0)
|
|
270
|
+
expect(trust.fix_priorities[0]).toHaveProperty('confidence')
|
|
271
|
+
expect(trust.fix_priorities[0]).toHaveProperty('explanation')
|
|
272
|
+
expect(trust.fix_priorities[0]).toHaveProperty('systemic')
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
it('uses snapshot history as historical fallback in advanced mode', () => {
|
|
276
|
+
const report = createBaseReport({ totalScore: 20 })
|
|
277
|
+
const trust = buildTrustReport(report, {
|
|
278
|
+
advanced: {
|
|
279
|
+
enabled: true,
|
|
280
|
+
snapshots: [
|
|
281
|
+
{
|
|
282
|
+
timestamp: '2026-01-01T00:00:00.000Z',
|
|
283
|
+
label: 'baseline',
|
|
284
|
+
score: 25,
|
|
285
|
+
grade: 'MODERATE',
|
|
286
|
+
totalIssues: 6,
|
|
287
|
+
files: 4,
|
|
288
|
+
byRule: {
|
|
289
|
+
'debug-leftover': 2,
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
},
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
expect(trust.advanced_context?.comparison?.source).toBe('snapshot-history')
|
|
297
|
+
expect(trust.advanced_context?.comparison?.snapshot_score_delta).toBe(-5)
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('prioritizes systemic rules first in advanced mode', () => {
|
|
301
|
+
const report = createBaseReport({
|
|
302
|
+
totalScore: 30,
|
|
303
|
+
summary: {
|
|
304
|
+
errors: 1,
|
|
305
|
+
warnings: 2,
|
|
306
|
+
infos: 0,
|
|
307
|
+
byRule: {
|
|
308
|
+
'debug-leftover': 6,
|
|
309
|
+
'layer-violation': 2,
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
const baseline = buildTrustReport(report)
|
|
315
|
+
const advanced = buildTrustReport(report, { advanced: { enabled: true } })
|
|
316
|
+
|
|
317
|
+
expect(baseline.fix_priorities[0]?.rule).toBe('debug-leftover')
|
|
318
|
+
expect(advanced.fix_priorities[0]?.rule).toBe('layer-violation')
|
|
319
|
+
expect(advanced.fix_priorities[0]?.systemic).toBe(true)
|
|
320
|
+
})
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
describe('drift trust branch policy', () => {
|
|
324
|
+
const config: DriftConfig = {
|
|
325
|
+
trustGate: {
|
|
326
|
+
enabled: true,
|
|
327
|
+
minTrust: 45,
|
|
328
|
+
maxRisk: 'HIGH',
|
|
329
|
+
policyPacks: {
|
|
330
|
+
strict: { enabled: true, minTrust: 90, maxRisk: 'LOW' },
|
|
331
|
+
balanced: { minTrust: 60, maxRisk: 'MEDIUM' },
|
|
332
|
+
lenient: { minTrust: 30, maxRisk: 'CRITICAL' },
|
|
333
|
+
},
|
|
334
|
+
presets: [
|
|
335
|
+
{ branch: '*', minTrust: 40, maxRisk: 'CRITICAL' },
|
|
336
|
+
{ branch: 'main', minTrust: 70, maxRisk: 'MEDIUM' },
|
|
337
|
+
{ branch: 'release/*', minTrust: 80, maxRisk: 'LOW' },
|
|
338
|
+
{ branch: 'release/legacy', enabled: false },
|
|
339
|
+
],
|
|
340
|
+
},
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
it('falls back to base policy when branch does not match', () => {
|
|
344
|
+
const policy = resolveTrustGatePolicy(config, 'feature/new-api')
|
|
345
|
+
expect(policy).toMatchObject({ enabled: true, minTrust: 40, maxRisk: 'CRITICAL' })
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
it('prefers exact branch preset over wildcard preset', () => {
|
|
349
|
+
const policy = resolveTrustGatePolicy(config, 'main')
|
|
350
|
+
expect(policy).toMatchObject({ enabled: true, minTrust: 70, maxRisk: 'MEDIUM' })
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
it('prefers more specific wildcard when multiple patterns match', () => {
|
|
354
|
+
const policy = resolveTrustGatePolicy(config, 'release/v1.2.3')
|
|
355
|
+
expect(policy).toMatchObject({ enabled: true, minTrust: 80, maxRisk: 'LOW' })
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
it('applies enabled override from matching branch preset', () => {
|
|
359
|
+
const policy = resolveTrustGatePolicy(config, 'release/legacy')
|
|
360
|
+
expect(policy).toMatchObject({ enabled: false, minTrust: 80, maxRisk: 'LOW' })
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
it('returns empty policy when trust gate config is missing', () => {
|
|
364
|
+
expect(resolveTrustGatePolicy(undefined, 'main')).toEqual({})
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
it('resolves precedence in deterministic order base -> pack -> branch -> overrides', () => {
|
|
368
|
+
const policy = resolveTrustGatePolicy(config, {
|
|
369
|
+
branchName: 'main',
|
|
370
|
+
policyPack: 'strict',
|
|
371
|
+
overrides: { maxRisk: 'CRITICAL' },
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
expect(policy).toMatchObject({ enabled: true, minTrust: 70, maxRisk: 'CRITICAL' })
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
it('reports invalid policy pack and preserves legacy branch behavior', () => {
|
|
378
|
+
const legacy = resolveTrustGatePolicy(config, 'main')
|
|
379
|
+
const explained = explainTrustGatePolicy(config, {
|
|
380
|
+
branchName: 'main',
|
|
381
|
+
policyPack: 'unknown-pack',
|
|
382
|
+
})
|
|
383
|
+
|
|
384
|
+
expect(explained.invalidPolicyPack).toBe('unknown-pack')
|
|
385
|
+
expect(explained.effectivePolicy).toMatchObject(legacy)
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
it('keeps compatibility between branch-only and options signatures', () => {
|
|
389
|
+
const branchOnly = resolveTrustGatePolicy(config, 'release/v1.2.3')
|
|
390
|
+
const optionsBased = resolveTrustGatePolicy(config, { branchName: 'release/v1.2.3' })
|
|
391
|
+
|
|
392
|
+
expect(optionsBased).toEqual(branchOnly)
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
it('explains resolution steps in application order', () => {
|
|
396
|
+
const explained = explainTrustGatePolicy(config, {
|
|
397
|
+
branchName: 'main',
|
|
398
|
+
policyPack: 'balanced',
|
|
399
|
+
overrides: { minTrust: 75 },
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
expect(explained.steps.map((step) => step.source)).toEqual([
|
|
403
|
+
'base',
|
|
404
|
+
'policy-pack',
|
|
405
|
+
'branch-preset',
|
|
406
|
+
'branch-preset',
|
|
407
|
+
'overrides',
|
|
408
|
+
])
|
|
409
|
+
expect(explained.effectivePolicy).toMatchObject({ enabled: true, minTrust: 75, maxRisk: 'MEDIUM' })
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
it('formats policy explanation with layer summary for CLI debug mode', () => {
|
|
413
|
+
const explained = explainTrustGatePolicy(config, {
|
|
414
|
+
branchName: 'main',
|
|
415
|
+
policyPack: 'strict',
|
|
416
|
+
overrides: { maxRisk: 'CRITICAL' },
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
const formatted = formatTrustGatePolicyExplanation(explained)
|
|
420
|
+
expect(formatted).toContain('Trust gate policy resolution:')
|
|
421
|
+
expect(formatted).toContain('base (trustGate)')
|
|
422
|
+
expect(formatted).toContain('policy-pack (strict)')
|
|
423
|
+
expect(formatted).toContain('branch-preset (main)')
|
|
424
|
+
expect(formatted).toContain('overrides (cli)')
|
|
425
|
+
expect(formatted).toContain('effective: enabled=true minTrust=70 maxRisk=CRITICAL')
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
it('detects branch names from CI environment candidates', () => {
|
|
429
|
+
expect(detectBranchName({ GITHUB_HEAD_REF: 'feature/payment', GITHUB_REF_NAME: 'main' })).toBe('feature/payment')
|
|
430
|
+
expect(detectBranchName({ GITHUB_REF_NAME: 'main' })).toBe('main')
|
|
431
|
+
expect(detectBranchName({})).toBeUndefined()
|
|
432
|
+
})
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
describe('drift trust calibration (golden)', () => {
|
|
436
|
+
const scenarios: Array<{
|
|
437
|
+
name: string
|
|
438
|
+
report: DriftReport
|
|
439
|
+
expected: { trust: number; risk: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL' }
|
|
440
|
+
}> = [
|
|
441
|
+
{
|
|
442
|
+
name: 'LOW - clean repo baseline',
|
|
443
|
+
report: createBaseReport(),
|
|
444
|
+
expected: { trust: 100, risk: 'LOW' },
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
name: 'MEDIUM - moderate pressure with one error',
|
|
448
|
+
report: createBaseReport({
|
|
449
|
+
totalScore: 20,
|
|
450
|
+
summary: {
|
|
451
|
+
errors: 1,
|
|
452
|
+
warnings: 2,
|
|
453
|
+
infos: 0,
|
|
454
|
+
byRule: {
|
|
455
|
+
'high-complexity': 1,
|
|
456
|
+
'debug-leftover': 2,
|
|
457
|
+
},
|
|
458
|
+
},
|
|
459
|
+
maintenanceRisk: {
|
|
460
|
+
score: 30,
|
|
461
|
+
level: 'medium',
|
|
462
|
+
hotspots: [],
|
|
463
|
+
signals: {
|
|
464
|
+
highComplexityFiles: 1,
|
|
465
|
+
filesWithoutNearbyTests: 0,
|
|
466
|
+
frequentChangeFiles: 0,
|
|
467
|
+
},
|
|
468
|
+
},
|
|
469
|
+
}),
|
|
470
|
+
expected: { trust: 77, risk: 'MEDIUM' },
|
|
471
|
+
},
|
|
472
|
+
{
|
|
473
|
+
name: 'HIGH - multiple risk vectors without collapse',
|
|
474
|
+
report: createBaseReport({
|
|
475
|
+
totalScore: 30,
|
|
476
|
+
summary: {
|
|
477
|
+
errors: 2,
|
|
478
|
+
warnings: 3,
|
|
479
|
+
infos: 0,
|
|
480
|
+
byRule: {
|
|
481
|
+
'high-complexity': 2,
|
|
482
|
+
'layer-violation': 1,
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
maintenanceRisk: {
|
|
486
|
+
score: 45,
|
|
487
|
+
level: 'medium',
|
|
488
|
+
hotspots: [
|
|
489
|
+
{
|
|
490
|
+
file: '/tmp/repo/src/risk.ts',
|
|
491
|
+
driftScore: 46,
|
|
492
|
+
complexityIssues: 2,
|
|
493
|
+
hasNearbyTests: false,
|
|
494
|
+
changeFrequency: 5,
|
|
495
|
+
risk: 50,
|
|
496
|
+
reasons: ['complexity and no tests'],
|
|
497
|
+
},
|
|
498
|
+
],
|
|
499
|
+
signals: {
|
|
500
|
+
highComplexityFiles: 1,
|
|
501
|
+
filesWithoutNearbyTests: 1,
|
|
502
|
+
frequentChangeFiles: 1,
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
}),
|
|
506
|
+
expected: { trust: 56, risk: 'HIGH' },
|
|
507
|
+
},
|
|
508
|
+
{
|
|
509
|
+
name: 'CRITICAL - broad architecture and hotspot failure',
|
|
510
|
+
report: createBaseReport({
|
|
511
|
+
totalScore: 70,
|
|
512
|
+
summary: {
|
|
513
|
+
errors: 5,
|
|
514
|
+
warnings: 8,
|
|
515
|
+
infos: 2,
|
|
516
|
+
byRule: {
|
|
517
|
+
'high-complexity': 4,
|
|
518
|
+
'layer-violation': 2,
|
|
519
|
+
'circular-dependency': 1,
|
|
520
|
+
'debug-leftover': 3,
|
|
521
|
+
},
|
|
522
|
+
},
|
|
523
|
+
maintenanceRisk: {
|
|
524
|
+
score: 80,
|
|
525
|
+
level: 'critical',
|
|
526
|
+
hotspots: [
|
|
527
|
+
{
|
|
528
|
+
file: '/tmp/repo/src/critical.ts',
|
|
529
|
+
driftScore: 84,
|
|
530
|
+
complexityIssues: 6,
|
|
531
|
+
hasNearbyTests: false,
|
|
532
|
+
changeFrequency: 11,
|
|
533
|
+
risk: 90,
|
|
534
|
+
reasons: ['architecture collapse'],
|
|
535
|
+
},
|
|
536
|
+
],
|
|
537
|
+
signals: {
|
|
538
|
+
highComplexityFiles: 3,
|
|
539
|
+
filesWithoutNearbyTests: 2,
|
|
540
|
+
frequentChangeFiles: 3,
|
|
541
|
+
},
|
|
542
|
+
},
|
|
543
|
+
}),
|
|
544
|
+
expected: { trust: 3, risk: 'CRITICAL' },
|
|
545
|
+
},
|
|
546
|
+
]
|
|
547
|
+
|
|
548
|
+
it.each(scenarios)('$name', ({ report, expected }) => {
|
|
549
|
+
const trust = buildTrustReport(report)
|
|
550
|
+
expect(trust.trust_score).toBe(expected.trust)
|
|
551
|
+
expect(trust.merge_risk).toBe(expected.risk)
|
|
552
|
+
})
|
|
553
|
+
|
|
554
|
+
it('applies deterministic diff penalty when PR regresses', () => {
|
|
555
|
+
const report = createBaseReport({ totalScore: 20 })
|
|
556
|
+
const diff: DriftDiff = {
|
|
557
|
+
baseRef: 'origin/main',
|
|
558
|
+
projectPath: '/tmp/repo',
|
|
559
|
+
scannedAt: new Date().toISOString(),
|
|
560
|
+
files: [],
|
|
561
|
+
totalScoreBefore: 20,
|
|
562
|
+
totalScoreAfter: 30,
|
|
563
|
+
totalDelta: 10,
|
|
564
|
+
newIssuesCount: 3,
|
|
565
|
+
resolvedIssuesCount: 0,
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
const trust = buildTrustReport(report, { diff })
|
|
569
|
+
expect(trust.diff_context).toMatchObject({
|
|
570
|
+
baseRef: 'origin/main',
|
|
571
|
+
status: 'regressed',
|
|
572
|
+
penalty: 29,
|
|
573
|
+
bonus: 0,
|
|
574
|
+
netImpact: 29,
|
|
575
|
+
})
|
|
576
|
+
})
|
|
577
|
+
|
|
578
|
+
it('applies deterministic diff bonus when PR improves', () => {
|
|
579
|
+
const report = createBaseReport({ totalScore: 20 })
|
|
580
|
+
const diff: DriftDiff = {
|
|
581
|
+
baseRef: 'origin/main',
|
|
582
|
+
projectPath: '/tmp/repo',
|
|
583
|
+
scannedAt: new Date().toISOString(),
|
|
584
|
+
files: [],
|
|
585
|
+
totalScoreBefore: 35,
|
|
586
|
+
totalScoreAfter: 20,
|
|
587
|
+
totalDelta: -15,
|
|
588
|
+
newIssuesCount: 0,
|
|
589
|
+
resolvedIssuesCount: 4,
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const trust = buildTrustReport(report, { diff })
|
|
593
|
+
expect(trust.diff_context).toMatchObject({
|
|
594
|
+
baseRef: 'origin/main',
|
|
595
|
+
status: 'improved',
|
|
596
|
+
penalty: 0,
|
|
597
|
+
bonus: 20,
|
|
598
|
+
netImpact: -20,
|
|
599
|
+
})
|
|
600
|
+
expect(trust.trust_score).toBe(100)
|
|
601
|
+
})
|
|
602
|
+
})
|