-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathdeployment-error-capture.property.test.ts
More file actions
156 lines (137 loc) · 6.06 KB
/
Copy pathdeployment-error-capture.property.test.ts
File metadata and controls
156 lines (137 loc) · 6.06 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Property 23 — Deployment Error Capture
*
* REQUIREMENT:
* For any deployment failure at any stage, error logs should be captured
* with full context and displayed to the user.
*
* WHAT THIS TEST SPECIFIES:
* When the deployment pipeline encounters an error in any of its stages
* (generating, creating_repo, pushing_code, or deploying), the system MUST:
* 1. Update the deployment status to 'failed'.
* 2. Persist the specific error message in the deployment record.
* 3. Create a log entry in 'deployment_logs' with:
* - Level set to 'error'
* - Correct stage identifier
* - Error message
* - Metadata containing the correlationId for tracing.
*
* Validates: Design document Property 23.
*/
import * as fc from 'fast-check';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { DeploymentPipelineService } from './deployment-pipeline.service';
import type { CustomizationConfig, DeploymentStatusType } from '@craft/types';
// ── Mocks ────────────────────────────────────────────────────────────────────
const mockSupabase = {
from: vi.fn().mockReturnThis(),
insert: vi.fn().mockResolvedValue({ error: null }),
update: vi.fn().mockReturnThis(),
eq: vi.fn().mockResolvedValue({ error: null }),
select: vi.fn().mockReturnThis(),
single: vi.fn().mockResolvedValue({ data: { category: 'dex' }, error: null }),
};
vi.mock('@/lib/supabase/server', () => ({
createClient: () => mockSupabase,
}));
vi.mock('@/lib/api/logger', () => ({
createLogger: () => ({
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
}),
}));
describe('Property 23 — Deployment Error Capture (Property Test)', () => {
let service: DeploymentPipelineService;
// Sub-service mocks
const mockGenerator = { generate: vi.fn() };
const mockGitHub = { createRepository: vi.fn() };
const mockGitHubPush = { pushGeneratedCode: vi.fn() };
const mockVercel = { createProject: vi.fn(), triggerDeployment: vi.fn() };
beforeEach(() => {
vi.clearAllMocks();
service = new DeploymentPipelineService(
mockGenerator as any,
mockGitHub as any,
mockGitHubPush as any,
mockVercel as any
);
mockGenerator.generate.mockResolvedValue({ success: true, generatedFiles: [] });
mockGitHub.createRepository.mockResolvedValue({
repository: { fullName: 'owner/repo', url: 'https://github.com/owner/repo', defaultBranch: 'main' },
resolvedName: 'repo'
});
mockGitHubPush.pushGeneratedCode.mockResolvedValue({ commitSha: 'abc', fileCount: 0 });
mockVercel.createProject.mockResolvedValue({ id: 'prj_123', name: 'craft-repo' });
mockVercel.triggerDeployment.mockResolvedValue({ deploymentId: 'dep_123', deploymentUrl: 'https://url.com' });
});
const arbStage = fc.constantFrom<DeploymentStatusType>(
'generating',
'creating_repo',
'pushing_code',
'deploying'
);
const arbErrorMessage = fc.string({ minLength: 5, maxLength: 100 });
const arbRequest = fc.record({
userId: fc.uuid(),
templateId: fc.uuid(),
name: fc.string({ minLength: 1 }),
customization: fc.record({
branding: fc.record({ appName: fc.string() }),
features: fc.record({ enableCharts: fc.boolean() }),
stellar: fc.record({ network: fc.constantFrom('mainnet', 'testnet') }),
}) as fc.Arbitrary<CustomizationConfig>,
});
it('Feature: craft-platform, Property 23: should capture errors at any pipeline stage with full context', async () => {
await fc.assert(
fc.asyncProperty(
arbRequest,
arbStage,
arbErrorMessage,
async (request, failedStage, errorMsg) => {
setupFailureAtStage(failedStage, errorMsg, {
mockGenerator,
mockGitHub,
mockGitHubPush,
mockVercel,
});
const result = await service.deploy(request);
expect(result.success).toBe(false);
expect(result.failedStage).toBe(failedStage);
expect(result.errorMessage).toContain(errorMsg);
expect(result.correlationId).toBeDefined();
const deploymentUpdates = mockSupabase.update.mock.calls.filter(
(call) => call[0].status === 'failed'
);
expect(deploymentUpdates.length).toBe(1);
expect(deploymentUpdates[0][0].error_message).toContain(errorMsg);
const logInserts = mockSupabase.insert.mock.calls.filter(
(call) => call[0].level === 'error'
);
expect(logInserts.length).toBe(1);
const errorLog = logInserts[0][0];
expect(errorLog.stage).toBe(failedStage);
expect(errorLog.message).toContain(errorMsg);
expect(errorLog.metadata.correlationId).toBe(result.correlationId);
}
),
{ numRuns: 100 }
);
});
});
function setupFailureAtStage(stage: DeploymentStatusType, message: string, mocks: any) {
switch (stage) {
case 'generating':
mocks.mockGenerator.generate.mockResolvedValue({ success: false, errors: [{ message }], generatedFiles: [] });
break;
case 'creating_repo':
mocks.mockGitHub.createRepository.mockRejectedValue(new Error(message));
break;
case 'pushing_code':
mocks.mockGitHubPush.pushGeneratedCode.mockRejectedValue(new Error(message));
break;
case 'deploying':
mocks.mockVercel.createProject.mockRejectedValue(new Error(message));
break;
}
}