|
| 1 | +/** Tests for incident-response helpers — branch naming (must satisfy the spawn route's pattern) and the diagnostic task content. */ |
| 2 | +import { describe, it, expect, vi, afterEach } from 'vitest' |
| 3 | +import { buildIncidentTask, incidentBranchName, type BreachPayload } from './incident-response.js' |
| 4 | +import { runHttpProbe } from './deployment-monitor.js' |
| 5 | +import type { DeploymentSample } from './deployment-monitor.js' |
| 6 | + |
| 7 | +/** The branchName validation pattern from orchestrator-session-router. */ |
| 8 | +const BRANCH_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9/_.-]*$/ |
| 9 | + |
| 10 | +const NOW = new Date('2026-08-30T14:30:00.000Z') |
| 11 | + |
| 12 | +const payload: BreachPayload = { |
| 13 | + deploymentId: 'codekin-prod', |
| 14 | + deploymentName: 'Codekin Production', |
| 15 | + repoPath: '/srv/repos/codekin', |
| 16 | + probeKey: 'codekin-prod::http:https://app.example.com/health', |
| 17 | + probeType: 'http', |
| 18 | + breaches: ['http 502'], |
| 19 | + metrics: { status: 502, latencyMs: 120 }, |
| 20 | +} |
| 21 | + |
| 22 | +describe('incidentBranchName', () => { |
| 23 | + it('produces a branch that passes the spawn route validation', () => { |
| 24 | + expect(incidentBranchName('codekin-prod', NOW)).toMatch(BRANCH_PATTERN) |
| 25 | + expect(incidentBranchName('My Weird/ID!!', NOW)).toMatch(BRANCH_PATTERN) |
| 26 | + expect(incidentBranchName('---', NOW)).toMatch(BRANCH_PATTERN) |
| 27 | + }) |
| 28 | + |
| 29 | + it('embeds the deployment and timestamp for uniqueness', () => { |
| 30 | + expect(incidentBranchName('codekin-prod', NOW)).toBe('incident/codekin-prod-202608301430') |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +describe('buildIncidentTask', () => { |
| 35 | + const sample: DeploymentSample = { |
| 36 | + id: 1, deploymentId: 'codekin-prod', probeKey: payload.probeKey, probeType: 'http', |
| 37 | + ok: false, breaches: ['http 502'], metrics: { status: 502 }, createdAt: NOW.toISOString(), |
| 38 | + } |
| 39 | + |
| 40 | + it('carries the breach evidence, report path, and operational hard constraints', () => { |
| 41 | + const task = buildIncidentTask(payload, [sample], NOW) |
| 42 | + expect(task).toContain('Codekin Production') |
| 43 | + expect(task).toContain('http 502') |
| 44 | + expect(task).toContain('.codekin/reports/incidents/2026-08-30_codekin-prod.md') |
| 45 | + expect(task).toContain('Do not restart services') |
| 46 | + expect(task).toContain('BREACHED (http 502)') |
| 47 | + }) |
| 48 | + |
| 49 | + it('handles missing history gracefully', () => { |
| 50 | + const task = buildIncidentTask(payload, [], NOW) |
| 51 | + expect(task).toContain('(no history available)') |
| 52 | + }) |
| 53 | +}) |
| 54 | + |
| 55 | +describe('runHttpProbe security headers', () => { |
| 56 | + afterEach(() => { |
| 57 | + vi.unstubAllGlobals() |
| 58 | + }) |
| 59 | + |
| 60 | + function stubFetch(headers: Record<string, string>) { |
| 61 | + vi.stubGlobal('fetch', vi.fn(async () => ({ |
| 62 | + status: 200, |
| 63 | + headers: { get: (name: string) => headers[name.toLowerCase()] ?? null }, |
| 64 | + }))) |
| 65 | + } |
| 66 | + |
| 67 | + it('breaches on missing HSTS/CSP when checkHeaders is set', async () => { |
| 68 | + stubFetch({}) |
| 69 | + const result = await runHttpProbe({ type: 'http', url: 'https://x.test/', checkHeaders: true }) |
| 70 | + expect(result.ok).toBe(false) |
| 71 | + expect(result.breaches).toEqual([ |
| 72 | + 'missing Strict-Transport-Security header', |
| 73 | + 'missing Content-Security-Policy header', |
| 74 | + ]) |
| 75 | + }) |
| 76 | + |
| 77 | + it('passes when both headers are present, recording them as metrics', async () => { |
| 78 | + stubFetch({ 'strict-transport-security': 'max-age=63072000', 'content-security-policy': "default-src 'self'" }) |
| 79 | + const result = await runHttpProbe({ type: 'http', url: 'https://x.test/', checkHeaders: true }) |
| 80 | + expect(result.ok).toBe(true) |
| 81 | + expect(result.metrics).toMatchObject({ hsts: 1, csp: 1 }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('does not check headers without the opt-in', async () => { |
| 85 | + stubFetch({}) |
| 86 | + const result = await runHttpProbe({ type: 'http', url: 'https://x.test/' }) |
| 87 | + expect(result.ok).toBe(true) |
| 88 | + }) |
| 89 | +}) |
0 commit comments