-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExitCodePolicyTest.php
More file actions
302 lines (262 loc) · 12.4 KB
/
ExitCodePolicyTest.php
File metadata and controls
302 lines (262 loc) · 12.4 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
declare(strict_types=1);
namespace Tests\Commands;
use DurableWorkflow\Cli\Commands\BaseCommand;
use DurableWorkflow\Cli\Support\CompatibilityDiagnostics;
use DurableWorkflow\Cli\Support\CompatibilityException;
use DurableWorkflow\Cli\Support\ControlPlaneRequestContract;
use DurableWorkflow\Cli\Support\ExitCode;
use DurableWorkflow\Cli\Support\NetworkException;
use DurableWorkflow\Cli\Support\ResolvedConnection;
use DurableWorkflow\Cli\Support\ServerClient;
use DurableWorkflow\Cli\Support\ServerHttpException;
use DurableWorkflow\Cli\Support\TimeoutException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class ExitCodePolicyTest extends TestCase
{
/**
* @dataProvider httpStatusCases
*/
public function test_http_status_maps_to_expected_exit_code(int $status, int $expected): void
{
self::assertSame($expected, ExitCode::fromHttpStatus($status));
}
/**
* @return iterable<string, array{int, int}>
*/
public static function httpStatusCases(): iterable
{
yield '401 unauthorized → AUTH' => [401, ExitCode::AUTH];
yield '403 forbidden → AUTH' => [403, ExitCode::AUTH];
yield '404 not found → NOT_FOUND' => [404, ExitCode::NOT_FOUND];
yield '408 request timeout → TIMEOUT' => [408, ExitCode::TIMEOUT];
yield '400 bad request → INVALID' => [400, ExitCode::INVALID];
yield '422 unprocessable → INVALID' => [422, ExitCode::INVALID];
yield '500 server error → SERVER' => [500, ExitCode::SERVER];
yield '503 service unavailable → SERVER' => [503, ExitCode::SERVER];
yield '200 ok → FAILURE (non-error status)' => [200, ExitCode::FAILURE];
}
public function test_base_command_translates_network_exception_to_network_exit_code(): void
{
$command = new ThrowingBaseCommand(new NetworkException('Connection refused'));
$tester = new CommandTester($command);
self::assertSame(ExitCode::NETWORK, $tester->execute([]));
self::assertStringContainsString('Connection refused', $tester->getDisplay());
self::assertStringContainsString('Next steps:', $tester->getDisplay());
self::assertStringContainsString('dw doctor --output=json', $tester->getDisplay());
}
public function test_base_command_translates_timeout_exception_to_timeout_exit_code(): void
{
$command = new ThrowingBaseCommand(new TimeoutException('deadline exceeded'));
$tester = new CommandTester($command);
self::assertSame(ExitCode::TIMEOUT, $tester->execute([]));
self::assertStringContainsString('deadline exceeded', $tester->getDisplay());
}
public function test_base_command_translates_auth_http_status_to_auth_exit_code(): void
{
$command = new ThrowingBaseCommand(new ServerHttpException('Unauthorized', 401));
$tester = new CommandTester($command);
self::assertSame(ExitCode::AUTH, $tester->execute([]));
self::assertStringContainsString('Unauthorized', $tester->getDisplay());
self::assertStringContainsString('Check the selected environment, auth token source, and namespace permissions.', $tester->getDisplay());
}
public function test_base_command_translates_not_found_http_status_to_not_found_exit_code(): void
{
$command = new ThrowingBaseCommand(new ServerHttpException('Workflow not found', 404));
$tester = new CommandTester($command);
self::assertSame(ExitCode::NOT_FOUND, $tester->execute([]));
self::assertStringContainsString('dw throwing --help', $tester->getDisplay());
}
public function test_base_command_translates_server_http_status_to_server_exit_code(): void
{
$command = new ThrowingBaseCommand(new ServerHttpException('internal', 502));
$tester = new CommandTester($command);
self::assertSame(ExitCode::SERVER, $tester->execute([]));
self::assertStringContainsString('dw server:health --output=json', $tester->getDisplay());
}
public function test_base_command_translates_validation_http_status_to_invalid_exit_code(): void
{
$command = new ThrowingBaseCommand(new ServerHttpException('bad input', 422));
$tester = new CommandTester($command);
self::assertSame(ExitCode::INVALID, $tester->execute([]));
self::assertStringContainsString('Inspect the request options', $tester->getDisplay());
}
public function test_base_command_translates_unexpected_throwable_to_failure_exit_code(): void
{
$command = new ThrowingBaseCommand(new \RuntimeException('boom'));
$tester = new CommandTester($command);
self::assertSame(ExitCode::FAILURE, $tester->execute([]));
self::assertStringContainsString('boom', $tester->getDisplay());
}
public function test_base_command_emits_structured_compatibility_error(): void
{
$diagnostic = [
'cli_version' => '0.1.5',
'server_version' => '0.2.221',
'compatibility_window' => 'cli >=0.1,<1.0',
'next_step' => CompatibilityDiagnostics::NEXT_STEP,
'detail' => 'unsupported control_plane.version [3]; dw 0.1.5 sends control_plane.version 2.',
];
$command = new ThrowingBaseCommand(new CompatibilityException(
CompatibilityDiagnostics::failureMessage($diagnostic),
$diagnostic,
));
$tester = new CommandTester($command);
self::assertSame(ExitCode::COMPATIBILITY, $tester->execute([
'--output' => 'json',
]));
$envelope = json_decode(trim($tester->getDisplay()), true, 512, JSON_THROW_ON_ERROR);
self::assertSame(ExitCode::COMPATIBILITY, $envelope['exit_code']);
self::assertSame('0.1.5', $envelope['compatibility']['cli_version'] ?? null);
self::assertSame('0.2.221', $envelope['compatibility']['server_version'] ?? null);
self::assertSame('cli >=0.1,<1.0', $envelope['compatibility']['compatibility_window'] ?? null);
self::assertSame('compatibility.unsupported', $envelope['recommendations'][0]['id'] ?? null);
self::assertStringContainsString('Next step:', $envelope['error']);
}
public function test_base_command_refuses_before_mutation_when_compatibility_preflight_fails(): void
{
$previousCliVersion = getenv('DW_CLI_VERSION');
putenv('DW_CLI_VERSION=0.1.5');
try {
$requests = [];
$http = new MockHttpClient(
static function (string $method, string $url, array $options) use (&$requests): MockResponse {
$requests[] = [$method, $url];
if (str_ends_with($url, '/api/cluster/info')) {
return new MockResponse(json_encode([
'version' => '0.2.221',
'control_plane' => [
'version' => ServerClient::CONTROL_PLANE_VERSION,
'request_contract' => [
'schema' => ControlPlaneRequestContract::SCHEMA,
'version' => ControlPlaneRequestContract::VERSION,
'operations' => [],
],
],
'client_compatibility' => [
'clients' => [
'cli' => [
'supported_versions' => '>=0.2,<1.0',
],
],
],
], JSON_THROW_ON_ERROR), [
'http_code' => 200,
]);
}
self::fail('Mutation request should not be sent after compatibility preflight fails.');
},
'http://example.test',
);
$command = new PreflightMutatingCommand($http);
$tester = new CommandTester($command);
self::assertSame(ExitCode::COMPATIBILITY, $tester->execute([
'--output' => 'json',
]));
self::assertCount(1, $requests);
self::assertSame('GET', $requests[0][0]);
self::assertStringEndsWith('/api/cluster/info', $requests[0][1]);
$envelope = json_decode(trim($tester->getDisplay()), true, 512, JSON_THROW_ON_ERROR);
self::assertSame('0.1.5', $envelope['compatibility']['cli_version'] ?? null);
self::assertSame('0.2.221', $envelope['compatibility']['server_version'] ?? null);
self::assertSame(
'cli >=0.2,<1.0; control-plane version 2',
$envelope['compatibility']['compatibility_window'] ?? null,
);
} finally {
if (is_string($previousCliVersion)) {
putenv('DW_CLI_VERSION='.$previousCliVersion);
} else {
putenv('DW_CLI_VERSION');
}
}
}
public function test_exit_codes_remain_distinct(): void
{
$values = [
ExitCode::SUCCESS,
ExitCode::FAILURE,
ExitCode::INVALID,
ExitCode::NETWORK,
ExitCode::AUTH,
ExitCode::NOT_FOUND,
ExitCode::SERVER,
ExitCode::TIMEOUT,
ExitCode::COMPATIBILITY,
];
self::assertCount(count($values), array_unique($values), 'exit codes must remain distinct');
}
public function test_readme_documents_compatibility_exit_contract(): void
{
$readme = file_get_contents(dirname(__DIR__, 2).'/README.md');
self::assertIsString($readme, 'README.md must be readable.');
self::assertStringContainsString('| 8 | `COMPATIBILITY` |', $readme);
self::assertStringContainsString('exits with `COMPATIBILITY` (`8`)', $readme);
self::assertStringContainsString('"exit_code": 8', $readme);
self::assertStringContainsString('"cli_version": "0.1.73"', $readme);
self::assertStringContainsString('"server_version": "0.2.221"', $readme);
self::assertStringContainsString(
'"compatibility_window": "cli >=0.1,<1.0; control-plane version 2; worker protocol same-major <= 1.0"',
$readme,
);
self::assertStringContainsString(
'"next_step": "Upgrade dw, pin dw to a supported release, or connect to a compatible server."',
$readme,
);
self::assertStringContainsString('missing control_plane.request_contract', $readme);
self::assertStringNotContainsString('Upgrade the server or use a compatible CLI version.', $readme);
}
public function test_symfony_canonical_codes_preserved(): void
{
self::assertSame(Command::SUCCESS, ExitCode::SUCCESS);
self::assertSame(Command::FAILURE, ExitCode::FAILURE);
self::assertSame(Command::INVALID, ExitCode::INVALID);
}
}
class ThrowingBaseCommand extends BaseCommand
{
public function __construct(private readonly \Throwable $toThrow)
{
parent::__construct('throwing');
}
protected function client(InputInterface $input, ?float $timeout = null): ServerClient
{
throw new \LogicException('not used');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
throw $this->toThrow;
}
}
class PreflightMutatingCommand extends BaseCommand
{
public function __construct(private readonly HttpClientInterface $http)
{
parent::__construct('workflow:start');
}
protected function makeClient(ResolvedConnection $resolved, ?float $timeout = null): ServerClient
{
return new ServerClient(
baseUrl: $resolved->server,
token: $resolved->token,
namespace: $resolved->namespace,
tlsVerify: $resolved->tlsVerify,
http: $this->http,
timeout: $timeout,
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->client($input)->post('/workflows', [
'workflow_type' => 'preflight-test',
]);
return Command::SUCCESS;
}
}