Skip to content

Commit b3e833d

Browse files
add rollback handling and error collection in tests
Introduce an optional «onRollback» callback to the «TestStep» class, enabling custom rollback logic during test execution. Update the «rollback» method to invoke this callback if provided. Add a new test case to verify proper rollback execution order for failed and completed steps. Ensure rollback errors are collected and validated. Enhances test coverage and ensures consistent behavior during rollback scenarios.
1 parent 20a9f5d commit b3e833d

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

public/dashboard-assistant/modules/installation-manager/domain/entities/installation-progress-manager.test.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ class TestStep extends InstallationAIAssistantStep {
1313
constructor(
1414
name: string,
1515
private readonly successMsg: string = 'ok',
16-
private readonly failureMsg: string = 'fail'
16+
private readonly failureMsg: string = 'fail',
17+
private readonly onRollback?: (
18+
request: InstallAIDashboardAssistantDto,
19+
context: InstallationContext,
20+
error: Error
21+
) => Promise<void> | void
1722
) {
1823
super({ name });
1924
}
@@ -27,11 +32,13 @@ class TestStep extends InstallationAIAssistantStep {
2732
return this.failureMsg;
2833
}
2934
async rollback(
30-
_request: InstallAIDashboardAssistantDto,
31-
_context: InstallationContext,
32-
_error: Error
35+
request: InstallAIDashboardAssistantDto,
36+
context: InstallationContext,
37+
error: Error
3338
): Promise<void> {
34-
// no-op for tests
39+
if (this.onRollback) {
40+
await this.onRollback(request, context, error);
41+
}
3542
}
3643
}
3744

@@ -88,6 +95,41 @@ describe('InstallationProgressManager', () => {
8895
expect(p.getFailedSteps().length).toBe(1);
8996
});
9097

98+
it('invokes rollbacks for failed and completed steps and collects rollback errors', async () => {
99+
const rollbackOrder: string[] = [];
100+
const steps = [
101+
new TestStep('S1', 'ok', 'fail', async () => {
102+
rollbackOrder.push('S1');
103+
}),
104+
new TestStep('S2', 'ok', 'fail', async () => {
105+
rollbackOrder.push('S2');
106+
throw new Error('S2 rollback failed');
107+
}),
108+
new TestStep('S3', 'ok', 'boom', async () => {
109+
rollbackOrder.push('S3');
110+
}),
111+
];
112+
const mgr = new InstallationProgressManager(steps);
113+
114+
await mgr.runStep(steps[0], request, createContext(), async () => {
115+
/* success */
116+
});
117+
await mgr.runStep(steps[1], request, createContext(), async () => {
118+
/* success */
119+
});
120+
121+
await expect(
122+
mgr.runStep(steps[2], request, createContext(), async () => {
123+
throw new Error('boom');
124+
})
125+
).rejects.toThrow('boom');
126+
127+
expect(rollbackOrder).toEqual(['S3', 'S2', 'S1']);
128+
expect(mgr.getRollbackErrors()).toEqual([
129+
{ step: 'S2', message: 'S2 rollback failed' },
130+
]);
131+
});
132+
91133
it('prevents concurrent step execution', async () => {
92134
const steps = [new TestStep('S1')];
93135
const mgr = new InstallationProgressManager(steps);

0 commit comments

Comments
 (0)