Skip to content

Commit 1da3f49

Browse files
test: cover worktree disk pressure gate (#840)
Co-authored-by: homeboy-ci[bot] <266378653+homeboy-ci[bot]@users.noreply.github.com>
1 parent e51d5d9 commit 1da3f49

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

tests/worktree-add-lifecycle.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ function is_wp_error( mixed $value ): bool {
4242
return $value instanceof WP_Error;
4343
}
4444

45+
$GLOBALS['datamachine_code_test_filters'] = array();
4546
function apply_filters( string $hook_name, mixed $value, mixed ...$args ): mixed {
47+
$callback = $GLOBALS['datamachine_code_test_filters'][ $hook_name ] ?? null;
48+
if ( is_callable($callback) ) {
49+
return $callback($value, ...$args);
50+
}
51+
4652
return $value;
4753
}
4854

@@ -232,10 +238,31 @@ function create_primary_checkout( string $workspace_root ): void {
232238
$GLOBALS['wpdb'] = $wpdb;
233239

234240
$workspace = new Workspace();
241+
$GLOBALS['datamachine_code_test_filters']['datamachine_worktree_disk_budget_thresholds'] = static function ( array $thresholds ) use ( $workspace_root ): array {
242+
$free = disk_free_space($workspace_root);
243+
assert_true(false !== $free, 'fixture workspace free space is not measurable');
244+
$thresholds['refuse_free_bytes'] = (int) $free + 1;
245+
$thresholds['warn_free_bytes'] = (int) $free + 1;
246+
$thresholds['refuse_free_percent'] = 0.0;
247+
$thresholds['warn_free_percent'] = 0.0;
248+
return $thresholds;
249+
};
250+
$refused = $workspace->worktree_add('homeboy', 'audit-primitives-disk-refused', 'origin/main', false, false, false, false, false);
251+
unset($GLOBALS['datamachine_code_test_filters']['datamachine_worktree_disk_budget_thresholds']);
252+
assert_true(is_wp_error($refused), 'disk pressure below the hard floor reported success');
253+
assert_true('worktree_disk_budget_exceeded' === $refused->get_error_code(), 'unexpected disk pressure refusal error code');
254+
$refusal_data = (array) $refused->get_error_data();
255+
$disk_budget = (array) ( $refusal_data['disk_budget'] ?? array() );
256+
assert_true('refused' === ( $disk_budget['status'] ?? '' ), 'disk pressure refusal did not include refused budget status');
257+
assert_true(isset($disk_budget['free_bytes'], $disk_budget['effective_refuse_bytes']), 'disk pressure refusal must include exact free and required bytes');
258+
assert_true(str_contains($refused->get_error_message(), 'studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --dry-run --limit=25'), 'disk pressure refusal must include the next cleanup command');
259+
assert_true(! is_dir($workspace_root . '/homeboy@audit-primitives-disk-refused'), 'disk pressure refusal left a worktree directory behind');
260+
235261
$result = $workspace->worktree_add('homeboy', 'audit-primitives-20260616', 'origin/main', false, false, false, false, true);
236262
assert_true(! is_wp_error($result), is_wp_error($result) ? $result->get_error_message() : 'worktree_add failed');
237263
assert_true(is_dir($result['path']), 'successful worktree_add path is not accessible');
238264
assert_true(isset($wpdb->rows['homeboy@audit-primitives-20260616']), 'successful worktree_add was not persisted');
265+
assert_true('refused' !== ( $result['disk_budget']['status'] ?? '' ), 'normal worktree_add should pass the disk budget gate without hard refusal');
239266

240267
$show = $workspace->show_repo('homeboy@audit-primitives-20260616');
241268
assert_true(! is_wp_error($show), 'persisted worktree is not visible to show_repo');

tests/worktree-disk-budget.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ function assert_true( bool $condition, string $message ): void {
5050
assert_true('studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --limit=25' === $bounded['apply_command'], 'bounded cleanup apply command is missing');
5151
assert_true(str_contains($bounded['apply_note'], 'may skip rows'), 'bounded cleanup apply note must explain dirty/unpushed revalidation can block removal');
5252

53+
$healthy = WorktreeDiskBudget::evaluate(
54+
array(
55+
'workspace_path' => '/tmp/dmc-test-workspace',
56+
'free_bytes' => 40 * $gib,
57+
'total_bytes' => 100 * $gib,
58+
'worktree_count' => 12,
59+
),
60+
array(
61+
'warn_free_bytes' => 20 * $gib,
62+
'refuse_free_bytes' => 10 * $gib,
63+
'warn_free_percent' => 15.0,
64+
'refuse_free_percent' => 10.0,
65+
'warn_worktree_count' => 100,
66+
)
67+
);
68+
69+
assert_true('ok' === $healthy['status'], 'healthy free space should pass the worktree disk budget gate');
70+
assert_true(array() === $healthy['warnings'], 'healthy free space should not emit disk budget warnings');
71+
5372
fwrite(STDOUT, "worktree-disk-budget ok\n");
5473
} catch (Throwable $e) {
5574
fwrite(STDERR, $e->getMessage() . "\n");

0 commit comments

Comments
 (0)