Repository navigation
Expand file tree
/
Copy pathbehavior.php
More file actions
688 lines (651 loc) · 24.5 KB
/
Copy pathbehavior.php
File metadata and controls
688 lines (651 loc) · 24.5 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
<?php
declare( strict_types=1 );
define( 'ABSPATH', __DIR__ . '/' );
define( 'DAY_IN_SECONDS', 86400 );
define( 'MINUTE_IN_SECONDS', 60 );
define( 'SPACEFAST_WORDPRESS_VERSION', '0.1.0-test' );
$GLOBALS['spacefast_options'] = array();
$GLOBALS['spacefast_scheduled'] = array();
$GLOBALS['spacefast_before_query'] = null;
$GLOBALS['spacefast_post_types'] = array();
final class WP_Post {
public string $post_type;
public string $post_status;
public function __construct( string $post_type, string $post_status ) {
$this->post_type = $post_type;
$this->post_status = $post_status;
}
}
function untrailingslashit( string $value ): string {
return rtrim( $value, "/\\" );
}
function wp_parse_url( string $value ) {
return parse_url( $value );
}
function sanitize_key( string $value ): string {
return preg_replace( '/[^a-z0-9_\\-]/', '', strtolower( $value ) ) ?? '';
}
function get_option( string $name, $default = false ) {
return $GLOBALS['spacefast_options'][ $name ] ?? $default;
}
function update_option( string $name, $value ): bool {
$GLOBALS['spacefast_options'][ $name ] = $value;
return true;
}
function add_option( string $name, $value ): bool {
if ( array_key_exists( $name, $GLOBALS['spacefast_options'] ) ) {
return false;
}
$GLOBALS['spacefast_options'][ $name ] = $value;
return true;
}
function delete_option( string $name ): bool {
unset( $GLOBALS['spacefast_options'][ $name ] );
return true;
}
function wp_clear_scheduled_hook( string $hook ): void {
unset( $GLOBALS['spacefast_scheduled'][ $hook ] );
}
function wp_next_scheduled( string $hook ) {
return $GLOBALS['spacefast_scheduled'][ $hook ] ?? false;
}
function wp_schedule_single_event( int $timestamp, string $hook ): bool {
$GLOBALS['spacefast_scheduled'][ $hook ] = $timestamp;
return true;
}
function wp_unschedule_event( int $timestamp, string $hook ): bool {
if ( ( $GLOBALS['spacefast_scheduled'][ $hook ] ?? null ) === $timestamp ) {
unset( $GLOBALS['spacefast_scheduled'][ $hook ] );
}
return true;
}
function maybe_serialize( $value ): string {
return serialize( $value );
}
function wp_cache_delete(): void {}
function wp_json_encode( $value ): string {
return (string) json_encode( $value, JSON_UNESCAPED_SLASHES );
}
function get_post_type_object( string $post_type ) {
return $GLOBALS['spacefast_post_types'][ $post_type ] ?? null;
}
function wp_generate_uuid4(): string {
return '00000000-0000-4000-8000-000000000001';
}
function wp_generate_password( int $length ): string {
return str_repeat( 'a', $length );
}
function admin_url( string $path = '' ): string {
return 'https://wp.example.test/wp-admin/' . ltrim( $path, '/' );
}
function add_query_arg( array $args, string $url ): string {
// WordPress build_query() leaves values unencoded. This matters when a
// query argument is itself a URL with its own query string.
$pairs = array();
foreach ( $args as $key => $value ) {
$pairs[] = $key . '=' . $value;
}
return $url . ( str_contains( $url, '?' ) ? '&' : '?' ) . implode( '&', $pairs );
}
function home_url(): string {
return 'https://wp.example.test';
}
function is_wp_error( $value ): bool {
return $value instanceof Spacefast_Test_Wp_Error;
}
function wp_remote_retrieve_response_code( array $response ): int {
return (int) $response['response']['code'];
}
function wp_remote_retrieve_body( array $response ): string {
return (string) $response['body'];
}
final class Spacefast_Test_Wp_Error {}
final class Spacefast_Test_Wpdb {
public string $options = 'wp_options';
public function prepare( string $sql, ...$args ): array {
return array( $sql, $args );
}
public function query( array $prepared ): int {
if ( is_callable( $GLOBALS['spacefast_before_query'] ) ) {
$before = $GLOBALS['spacefast_before_query'];
$GLOBALS['spacefast_before_query'] = null;
$before();
}
list( $sql, $args ) = $prepared;
if ( str_starts_with( trim( $sql ), 'UPDATE' ) ) {
list( $next, $option, $expected ) = $args;
$current = $GLOBALS['spacefast_options'][ $option ] ?? false;
if ( maybe_serialize( $current ) !== $expected ) {
return 0;
}
$GLOBALS['spacefast_options'][ $option ] = unserialize( $next );
return 1;
}
list( $option, $expected ) = $args;
$current = $GLOBALS['spacefast_options'][ $option ] ?? false;
if ( maybe_serialize( $current ) !== $expected ) {
return 0;
}
unset( $GLOBALS['spacefast_options'][ $option ] );
return 1;
}
}
$wpdb = new Spacefast_Test_Wpdb();
require_once dirname( __DIR__ ) . '/includes/class-spacefast-sync-state.php';
require_once dirname( __DIR__ ) . '/includes/class-spacefast-settings.php';
require_once dirname( __DIR__ ) . '/includes/class-spacefast-oauth.php';
require_once dirname( __DIR__ ) . '/includes/class-spacefast-client.php';
require_once dirname( __DIR__ ) . '/includes/class-spacefast-static-publisher.php';
require_once dirname( __DIR__ ) . '/includes/class-spacefast-plugin.php';
function check( bool $condition, string $message ): void {
if ( ! $condition ) {
throw new RuntimeException( $message );
}
}
function response( int $status, array $data = array() ): array {
return array(
'response' => array( 'code' => $status ),
'body' => json_encode( array( 'data' => $data ) ),
);
}
function paginated_response( array $data, ?string $next_cursor = null ): array {
return array(
'response' => array( 'code' => 200 ),
'body' => json_encode(
array(
'data' => $data,
'pagination' => array(
'nextCursor' => $next_cursor,
'hasMore' => null !== $next_cursor,
),
)
),
);
}
function raw_response( int $status, array $data = array() ): array {
return array(
'response' => array( 'code' => $status ),
'body' => json_encode( $data ),
);
}
check(
array( 'teams:read', 'spaces:read', 'spaces:write', 'spaces:publish', 'offline_access' ) === Spacefast_OAuth::scopes( Spacefast_Settings::MODE_STATIC ),
'static OAuth asks for Team discovery, Space creation and discovery, publishing, and refresh access'
);
check(
array( 'teams:read', 'spaces:read', 'builds:trigger', 'offline_access' ) === Spacefast_OAuth::scopes( Spacefast_Settings::MODE_HEADLESS ),
'headless OAuth asks only for Team discovery, Space discovery, builds, and refresh access'
);
check(
strlen( Spacefast_OAuth::pkce_challenge( str_repeat( 'v', 64 ) ) ) === 43,
'PKCE challenge uses an unpadded SHA-256 base64url value'
);
$oauth_requests = array();
$oauth_transport = static function ( string $url, array $args ) use ( &$oauth_requests ): array {
$oauth_requests[] = array( $url, $args );
if ( str_ends_with( $url, '/oauth2/register' ) ) {
return raw_response( 201, array( 'client_id' => 'client_wordpress' ) );
}
if ( str_ends_with( $url, '/oauth2/token' ) ) {
return raw_response(
200,
array(
'access_token' => 'oauth_access',
'refresh_token' => 'oauth_refresh',
'expires_in' => 900,
'scope' => 'teams:read spaces:read builds:trigger offline_access',
)
);
}
if ( str_contains( $url, '/v1/teams' ) ) {
return paginated_response( array( array( 'id' => 'team_demo', 'name' => 'Demo Team', 'slug' => 'demo-team' ) ) );
}
if ( str_contains( $url, 'cursor=space%20cursor%2F2' ) ) {
return paginated_response(
array(
array(
'id' => 'spc_second',
'teamId' => 'team_demo',
'teamSlug' => 'demo-team',
'slug' => 'second-space',
'title' => 'Second Space',
'liveUrl' => 'https://second.spacefast.site',
),
)
);
}
return paginated_response(
array(
array(
'id' => 'spc_demo',
'teamId' => 'team_demo',
'teamSlug' => 'demo-team',
'slug' => 'demo-space',
'title' => 'Demo Space',
'liveUrl' => 'https://demo.spacefast.site',
),
),
'space cursor/2'
);
};
$oauth = new Spacefast_OAuth( $oauth_transport );
$authorization = $oauth->begin( Spacefast_Settings::MODE_HEADLESS );
check( true === $authorization['ok'], 'dynamic client registration starts OAuth' );
$registration_body = json_decode( $oauth_requests[0][1]['body'], true );
check( 'none' === $registration_body['token_endpoint_auth_method'], 'registers a public PKCE client' );
check( 'Spacefast for WordPress (wp.example.test)' === $registration_body['client_name'], 'identifies this WordPress installation in Connected Apps' );
check(
array( Spacefast_Settings::api_url() . '/v1' ) === $registration_body['resources'],
'binds tokens to the Spacefast API resource'
);
check( str_contains( (string) $authorization['url'], 'code_challenge_method=S256' ), 'authorization uses PKCE S256' );
$authorization_query = array();
parse_str( (string) parse_url( (string) $authorization['url'], PHP_URL_QUERY ), $authorization_query );
check(
Spacefast_OAuth::callback_url() === ( $authorization_query['redirect_uri'] ?? '' ),
'authorization preserves the complete query-bearing WordPress callback URI'
);
$pending = get_option( Spacefast_OAuth::PENDING_OPTION );
$finished = $oauth->finish( 'authorization-code', (string) $pending['state'] );
check( true === $finished['ok'], 'valid callback exchanges its code and loads Team-scoped choices' );
check( 'oauth_refresh' === Spacefast_Settings::get()['refresh_token'], 'stores the rotating refresh token' );
check( 2 === count( get_option( Spacefast_OAuth::CHOICES_OPTION )['spaces'] ), 'loads every paginated Space choice into WordPress' );
check(
str_contains( implode( ' ', array_column( $oauth_requests, 0 ) ), 'cursor=space%20cursor%2F2' ),
'encodes and follows the opaque Space cursor'
);
check( ! Spacefast_Settings::configured(), 'authorization alone is not presented as Connected' );
$create_requests = array();
$creating_client = new Spacefast_Client(
static function ( string $url, array $args ) use ( &$create_requests ): array {
$create_requests[] = array( $url, $args );
return response(
201,
array(
'space' => array(
'id' => 'spc_created',
'teamId' => 'team_demo',
'slug' => 'spacefast-launchpad',
'title' => 'Spacefast Launchpad',
'liveUrl' => 'https://spacefast-launchpad.view.fast',
),
)
);
}
);
$created_space = $creating_client->create_space( 'team_demo', 'Spacefast Launchpad' );
check( true === $created_space['ok'], 'creates a Space with the authorized OAuth token' );
check( '/v1/spaces' === parse_url( $create_requests[0][0], PHP_URL_PATH ), 'uses the canonical Space creation endpoint' );
check(
array( 'teamId' => 'team_demo', 'title' => 'Spacefast Launchpad' ) === json_decode( $create_requests[0][1]['body'], true ),
'creates the Space in the authorized Team with the WordPress site name'
);
$reauthorization = $oauth->begin( Spacefast_Settings::MODE_HEADLESS );
$reauthorization_pending = get_option( Spacefast_OAuth::PENDING_OPTION );
$reauthorized = $oauth->finish( 'replacement-code', (string) $reauthorization_pending['state'] );
check( true === $reauthorized['ok'], 'reauthorization replaces a connection only after the callback succeeds' );
check(
2 === count( array_filter( $oauth_requests, static fn( array $request ): bool => str_ends_with( $request[0], '/oauth2/revoke' ) ) ),
'reauthorization revokes the replaced refresh and access tokens'
);
Spacefast_Settings::merge( array( 'expires_at' => 1 ) );
$refresh_requests = array();
$refreshing_oauth = new Spacefast_OAuth(
static function ( string $url, array $args ) use ( &$refresh_requests ): array {
$refresh_requests[] = array( $url, $args );
return raw_response(
200,
array(
'access_token' => 'rotated_access',
'refresh_token' => 'rotated_refresh',
'expires_in' => 900,
)
);
}
);
check( 'rotated_access' === $refreshing_oauth->access_token(), 'refreshes before an access token expires' );
check( 'rotated_refresh' === Spacefast_Settings::get()['refresh_token'], 'persists refresh-token rotation' );
$revoke_requests = array();
( new Spacefast_OAuth(
static function ( string $url, array $args ) use ( &$revoke_requests ): array {
$revoke_requests[] = array( $url, $args );
return raw_response( 200 );
}
) )->revoke();
check( 2 === count( $revoke_requests ), 'disconnect can revoke refresh and access tokens remotely' );
$connection = array(
'mode' => Spacefast_Settings::MODE_HEADLESS,
'client_id' => 'client_wordpress',
'access_token' => 'access_secret',
'refresh_token' => 'refresh_secret',
'expires_at' => time() + 3600,
'scope' => 'teams:read spaces:read builds:trigger offline_access',
'team_id' => 'team_demo',
'team_name' => 'Demo Team',
'team_slug' => 'demo-team',
'space_id' => 'spc_demo',
'space_name' => 'Demo Space',
'space_slug' => 'demo-space',
'live_url' => 'https://demo.spacefast.site',
'verified_at' => time(),
);
$state = Spacefast_Sync_State::record_change(
Spacefast_Sync_State::defaults(),
'Post Published',
'event-one'
);
$state = Spacefast_Sync_State::record_change( $state, 'taxonomy', 'event-two' );
check( 2 === $state['desired'], 'increments desired generation' );
check( 'event-two' === $state['event_id'], 'keeps latest stable event id' );
check( array( 'postpublished', 'taxonomy' ) === $state['reasons'], 'coalesces reasons' );
check( 0 < $state['last_change_at'], 'records when the latest public content change happened' );
$state['desired'] = 3;
$ack = Spacefast_Sync_State::acknowledge( $state, 2, 'bld_test' );
check( 2 === $ack['delivered'], 'acknowledges only observed generation' );
check( 'pending' === $ack['last_status'], 'preserves an edit arriving during delivery' );
check( 60 === Spacefast_Sync_State::retry_delay( 1 ), 'starts bounded backoff' );
check( DAY_IN_SECONDS === Spacefast_Sync_State::retry_delay( 99 ), 'caps backoff' );
Spacefast_Sync_State::save(
array_merge(
Spacefast_Sync_State::defaults(),
array(
'desired' => 1,
'event_id' => 'event-one',
)
)
);
$GLOBALS['spacefast_before_query'] = static function (): void {
$current = Spacefast_Sync_State::get();
$current['desired'] = 2;
$current['event_id'] = 'event-two';
Spacefast_Sync_State::save( $current );
};
$cas_ack = Spacefast_Sync_State::mutate(
static fn( array $current ): array => Spacefast_Sync_State::acknowledge(
$current,
1,
'bld_first'
)
);
check( 2 === $cas_ack['desired'], 'CAS preserves a concurrent content generation' );
check( 1 === $cas_ack['delivered'], 'CAS acknowledges only the delivered generation' );
check( 'event-two' === $cas_ack['event_id'], 'CAS preserves the successor event id' );
check( 'pending' === $cas_ack['last_status'], 'CAS leaves the successor pending' );
Spacefast_Sync_State::save( Spacefast_Sync_State::defaults() );
check(
Spacefast_Sync_State::claim_lock( 'spacefast_test_lock', 'owner-a', 100 ),
'first worker claims lock'
);
check(
! Spacefast_Sync_State::claim_lock( 'spacefast_test_lock', 'owner-b', 101 ),
'second worker cannot steal live lock'
);
Spacefast_Sync_State::release_lock( 'spacefast_test_lock', 'owner-b' );
check(
'owner-a' === get_option( 'spacefast_test_lock' )['owner'],
'non-owner cannot release lock'
);
Spacefast_Sync_State::release_lock( 'spacefast_test_lock', 'owner-a' );
check( false === get_option( 'spacefast_test_lock', false ), 'owner releases lock' );
$debounce = new ReflectionMethod( Spacefast_Plugin::class, 'schedule_after_change' );
$changed_at = time();
$GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ] = $changed_at + 10;
$debounce->invoke( null, $changed_at );
check(
$changed_at + MINUTE_IN_SECONDS === $GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ],
'public content waits for a 60-second quiet window'
);
$later_change = $changed_at + 20;
$debounce->invoke( null, $later_change );
check(
$later_change + MINUTE_IN_SECONDS === $GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ],
'a later content change restarts the quiet window'
);
Spacefast_Sync_State::save(
array_merge(
Spacefast_Sync_State::defaults(),
array(
'desired' => 3,
'delivered' => 2,
'event_id' => 'event-reactivate',
)
)
);
Spacefast_Plugin::deactivate();
check(
false === wp_next_scheduled( Spacefast_Sync_State::HOOK ),
'deactivation clears the worker'
);
Spacefast_Plugin::activate();
check(
false !== wp_next_scheduled( Spacefast_Sync_State::HOOK ),
'reactivation recovers pending work'
);
Spacefast_Settings::disconnect();
Spacefast_Settings::merge( $connection );
$reconnected = Spacefast_Sync_State::mutate(
static fn( array $current ): array => Spacefast_Sync_State::record_change(
$current,
'manual',
'event-reconnected'
)
);
check( 1 === $reconnected['desired'], 'reconnect restores durable sync state' );
$GLOBALS['spacefast_post_types']['internal'] = (object) array(
'public' => false,
'show_in_rest' => false,
);
$GLOBALS['spacefast_post_types']['post'] = (object) array(
'public' => true,
'show_in_rest' => true,
);
$before_delete = Spacefast_Sync_State::get();
Spacefast_Plugin::post_deleted( 1, new WP_Post( 'internal', 'publish' ) );
check(
$before_delete['desired'] === Spacefast_Sync_State::get()['desired'],
'ignores deletion of non-public post types'
);
$change_recorded = new ReflectionProperty( Spacefast_Plugin::class, 'change_recorded' );
$change_recorded->setValue( null, false );
Spacefast_Plugin::post_deleted( 2, new WP_Post( 'post', 'publish' ) );
check(
$before_delete['desired'] + 1 === Spacefast_Sync_State::get()['desired'],
'rebuilds after deleting a public REST-visible post'
);
Spacefast_Settings::merge( $connection );
$requests = array();
$transport = static function ( string $url, array $args ) use ( &$requests ) {
$requests[] = array( $url, $args );
if ( 'GET' === $args['method'] ) {
return response( 200, array( 'ready' => true ) );
}
return response( 201, array( 'build' => array( 'id' => 'bld_demo' ) ) );
};
$result = ( new Spacefast_Client( $transport ) )->trigger_build( 'wp-event-123' );
check( true === $result['ok'], 'healthy connection triggers a build' );
check( 2 === count( $requests ), 'verifies the source before the build' );
$build_request = $requests[1][1];
check(
'wp-event-123' === $build_request['headers']['Idempotency-Key'],
'reuses the stable event as idempotency key'
);
$build_body = json_decode( $build_request['body'], true );
check(
'https://wp.example.test' === $build_body['sourceUrl'],
'sends only the configured WordPress source'
);
check(
str_ends_with( $requests[1][0], '/builds/wordpress' ),
'uses the constrained server-side trigger'
);
check(
'Bearer access_secret' === $build_request['headers']['Authorization'],
'authenticates with the current OAuth access token'
);
$mismatch = new Spacefast_Client(
static fn(): array => array(
'response' => array( 'code' => 422 ),
'body' => json_encode(
array(
'type' => 'https://spacefast.com/docs/errors/wordpress_source_mismatch',
'title' => 'WordPress source mismatch',
'status' => 422,
'detail' => 'mismatch',
'code' => 'wordpress_source_mismatch',
)
),
)
);
$mismatch_result = $mismatch->health();
check(
'wordpress_source_mismatch' === $mismatch_result['code'],
'blocks a different WordPress origin'
);
check( false === $mismatch_result['retryable'], 'does not hot-loop configuration errors' );
$network = new Spacefast_Client( static fn(): Spacefast_Test_Wp_Error => new Spacefast_Test_Wp_Error() );
$network_result = $network->health();
check( true === $network_result['retryable'], 'retries transport failures' );
$invalid_receipt_requests = 0;
$invalid_receipt = new Spacefast_Client(
static function ( string $url ) use ( &$invalid_receipt_requests ): array {
++$invalid_receipt_requests;
return 1 === $invalid_receipt_requests
? response( 200, array( 'ready' => true ) )
: response( 204 );
}
);
$invalid_receipt_result = $invalid_receipt->trigger_build( 'wp-event-invalid' );
check( false === $invalid_receipt_result['ok'], 'rejects an empty success response' );
check( true === $invalid_receipt_result['retryable'], 'retries invalid build receipts' );
Spacefast_Settings::merge(
array(
'mode' => Spacefast_Settings::MODE_STATIC,
'scope' => 'teams:read spaces:read spaces:publish offline_access',
'verified_at' => time(),
)
);
check( 'static' === Spacefast_Settings::mode(), 'stores the selected static publishing mode' );
check(
array( 'setup', 'fetch_urls', 'spacefast_publish', 'wrapup' ) === Spacefast_Plugin::simply_static_tasks(
array( 'setup', 'fetch_urls', 'wrapup' ),
'zip'
),
'publishes after export delivery and before cleanup'
);
$archive = sys_get_temp_dir() . '/spacefast-wordpress-static-' . uniqid();
mkdir( $archive );
mkdir( $archive . '/assets' );
file_put_contents( $archive . '/index.html', '<h1>Spacefast</h1>' );
file_put_contents( $archive . '/assets/app.js', 'console.log("spacefast")' );
$manifest = Spacefast_Static_Publisher::manifest( $archive );
check(
array( 'assets/app.js', 'index.html' ) === array_column( $manifest, 'path' ),
'builds a sorted snapshot manifest from generated files'
);
check(
hash_file( 'sha256', $archive . '/index.html' ) === $manifest[1]['sha256'],
'hashes the generated bytes instead of trusting exporter metadata'
);
$static_requests = array();
$static_client = new Spacefast_Client(
static function ( string $url, array $args ) use ( &$static_requests ): array {
$static_requests[] = array( $url, $args );
if ( 'GET' === $args['method'] ) {
return response(
200,
array(
'id' => 'ver_static',
'status' => 'ready',
'isCurrentProduction' => true,
)
);
}
if ( str_contains( $url, '/uploads/resume' ) ) {
return response(
200,
array(
'upload' => array(
'id' => 'upl_static',
'mode' => 'dist',
'expiresAt' => '2030-01-01T00:00:00.000Z',
'summary' => array( 'upload' => 1, 'reused' => 0, 'ignored' => 0 ),
'targets' => array(
array(
'path' => 'assets/app.js',
'method' => 'PUT',
'url' => 'https://uploads.example.test/file-2',
'headers' => array( 'X-Upload' => 'signed-2' ),
),
),
'links' => array( 'resume' => '/resume', 'finalize' => '/finalize' ),
),
)
);
}
return response(
201,
array(
'versionId' => 'ver_static',
'upload' => array(
'id' => 'upl_static',
'mode' => 'dist',
'expiresAt' => '2030-01-01T00:00:00.000Z',
'summary' => array( 'upload' => 2, 'reused' => 0, 'ignored' => 0 ),
'targets' => array(
array(
'path' => 'index.html',
'method' => 'PUT',
'url' => 'https://uploads.example.test/file',
'headers' => array( 'X-Upload' => 'signed' ),
),
),
'links' => array( 'resume' => '/resume', 'finalize' => '/finalize' ),
),
)
);
},
static function ( string $url, string $method, array $headers, string $file ) use ( &$static_requests ): array {
$static_requests[] = array( $url, $method, $headers, $file );
return response( 204 );
}
);
$publish_step = Spacefast_Static_Publisher::step( $archive, $static_client );
check( false === $publish_step['done'], 'uploads a bounded target per background step' );
check( 1 === $publish_step['uploaded'], 'records completed generated-file uploads' );
$publish_done = Spacefast_Static_Publisher::step( $archive, $static_client );
check( false === $publish_done['done'], 'resumes a paged upload after the first instruction page' );
$publish_done = Spacefast_Static_Publisher::step( $archive, $static_client );
check( 2 === $publish_done['uploaded'], 'uploads the file from the resumed instruction page' );
$publish_done = Spacefast_Static_Publisher::step( $archive, $static_client );
check( false === $publish_done['done'], 'waits for Spacefast activation after every target lands' );
$publish_done = Spacefast_Static_Publisher::step( $archive, $static_client );
check( true === $publish_done['done'], 'finishes only after the version is ready' );
check( 'ver_static' === $publish_done['version_id'], 'retains the Spacefast version receipt' );
check( 'live' === $publish_done['status'], 'distinguishes live activation from upload acceptance' );
$create_body = json_decode( $static_requests[0][1]['body'], true );
check( 'snapshot' === $create_body['publishMode'], 'publishes the export as an exact snapshot' );
check( array( 'channel' => 'live' ) === $create_body['finalize'], 'requests live auto-finalize up front' );
check(
str_ends_with( $static_requests[1][3], '/index.html' ),
'uploads only the file named by the opaque server target'
);
check(
str_ends_with( $static_requests[3][3], '/assets/app.js' ),
'uploads the next opaque target after resuming'
);
Spacefast_Sync_State::save(
array_merge( Spacefast_Sync_State::defaults(), array( 'desired' => 1 ) )
);
$static_ack = Spacefast_Sync_State::acknowledge_static(
Spacefast_Sync_State::get(),
'ver_static',
'live'
);
check( 'live' === $static_ack['last_status'], 'records verified live publication separately from builds' );
check( 'ver_static' === $static_ack['last_version_id'], 'keeps the last static version id' );
unlink( $archive . '/assets/app.js' );
rmdir( $archive . '/assets' );
unlink( $archive . '/index.html' );
rmdir( $archive );
Spacefast_Static_Publisher::reset();
fwrite( STDOUT, "Spacefast WordPress behavior tests: PASS\n" );