-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstartup-orchestrator.test.ts
More file actions
187 lines (162 loc) · 5.71 KB
/
Copy pathstartup-orchestrator.test.ts
File metadata and controls
187 lines (162 loc) · 5.71 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { describe, it, expect } from 'vitest';
import type {
StartupOptions,
PluginStartupResult,
HealthStatus,
IStartupOrchestrator,
} from './startup-orchestrator';
import type { Plugin } from './plugin-validator';
describe('Startup Orchestrator Contract', () => {
describe('StartupOptions interface', () => {
it('should allow an empty options object (all optional)', () => {
const options: StartupOptions = {};
expect(options).toBeDefined();
expect(options.timeout).toBeUndefined();
expect(options.rollbackOnFailure).toBeUndefined();
});
it('should allow full options', () => {
const options: StartupOptions = {
timeout: 30000,
rollbackOnFailure: true,
healthCheck: true,
parallel: false,
context: { db: 'postgres' },
};
expect(options.timeout).toBe(30000);
expect(options.rollbackOnFailure).toBe(true);
expect(options.healthCheck).toBe(true);
expect(options.parallel).toBe(false);
expect(options.context).toEqual({ db: 'postgres' });
});
});
describe('HealthStatus interface', () => {
it('should allow a minimal health status', () => {
const status: HealthStatus = {
healthy: true,
timestamp: Date.now(),
};
expect(status.healthy).toBe(true);
expect(status.timestamp).toBeGreaterThan(0);
});
it('should allow a full health status with details', () => {
const status: HealthStatus = {
healthy: false,
timestamp: Date.now(),
details: { connections: 0, maxConnections: 10 },
message: 'No database connections available',
};
expect(status.healthy).toBe(false);
expect(status.message).toBe('No database connections available');
expect(status.details).toHaveProperty('connections');
});
});
describe('PluginStartupResult interface', () => {
it('should represent a successful startup', () => {
const plugin: Plugin = { name: 'auth-plugin', version: '1.0.0' };
const result: PluginStartupResult = {
plugin,
success: true,
duration: 150,
};
expect(result.success).toBe(true);
expect(result.duration).toBe(150);
expect(result.error).toBeUndefined();
});
it('should represent a failed startup with error', () => {
const plugin: Plugin = { name: 'broken-plugin' };
const result: PluginStartupResult = {
plugin,
success: false,
duration: 30000,
error: new Error('Timeout'),
};
expect(result.success).toBe(false);
expect(result.error!.message).toBe('Timeout');
});
it('should include optional health status', () => {
const plugin: Plugin = { name: 'healthy-plugin' };
const result: PluginStartupResult = {
plugin,
success: true,
duration: 50,
health: {
healthy: true,
timestamp: Date.now(),
details: { uptime: 1000 },
},
};
expect(result.health!.healthy).toBe(true);
});
});
describe('IStartupOrchestrator interface', () => {
it('should allow a minimal implementation', () => {
const orchestrator: IStartupOrchestrator = {
orchestrateStartup: async (plugins, _options) => {
return plugins.map((p) => ({
plugin: p,
success: true,
duration: 10,
}));
},
rollback: async (_startedPlugins) => {},
checkHealth: async (_plugin) => ({
healthy: true,
timestamp: Date.now(),
}),
};
expect(typeof orchestrator.orchestrateStartup).toBe('function');
expect(typeof orchestrator.rollback).toBe('function');
expect(typeof orchestrator.checkHealth).toBe('function');
});
it('should orchestrate startup for multiple plugins', async () => {
const plugins: Plugin[] = [
{ name: 'core', version: '1.0.0' },
{ name: 'auth', version: '2.0.0', dependencies: ['core'] },
];
const orchestrator: IStartupOrchestrator = {
orchestrateStartup: async (pluginList, options) => {
return pluginList.map((p) => ({
plugin: p,
success: true,
duration: options.timeout ? 10 : 20,
}));
},
rollback: async () => {},
checkHealth: async () => ({ healthy: true, timestamp: Date.now() }),
};
const results = await orchestrator.orchestrateStartup(plugins, { timeout: 5000 });
expect(results).toHaveLength(2);
expect(results[0].success).toBe(true);
expect(results[1].plugin.name).toBe('auth');
});
it('should support optional startWithTimeout method', async () => {
const orchestrator: IStartupOrchestrator = {
orchestrateStartup: async () => [],
rollback: async () => {},
checkHealth: async () => ({ healthy: true, timestamp: Date.now() }),
startWithTimeout: async (_plugin, _context, _timeoutMs) => {},
};
expect(orchestrator.startWithTimeout).toBeDefined();
await expect(
orchestrator.startWithTimeout!({ name: 'test' }, {}, 5000)
).resolves.toBeUndefined();
});
it('should rollback started plugins on failure', async () => {
const rolledBack: string[] = [];
const orchestrator: IStartupOrchestrator = {
orchestrateStartup: async () => [],
rollback: async (startedPlugins) => {
for (const p of startedPlugins) {
rolledBack.push(p.name);
}
},
checkHealth: async () => ({ healthy: true, timestamp: Date.now() }),
};
await orchestrator.rollback([
{ name: 'plugin-a' },
{ name: 'plugin-b' },
]);
expect(rolledBack).toEqual(['plugin-a', 'plugin-b']);
});
});
});