test: AssertsToolErrors helper for tool error assertions (T3)

tests/Support/AssertsToolErrors (used by TestCase): assertToolError($result, code)
checks the WP_Error code lit_<code> and the code of the client JSON and returns the
payload; clientError() decodes the payload; errorFields() returns the fields and
asserts they exist. Replaces the private clientError() decoders of
DiviWriteAbilitiesTest/DiviReplaceTextAbilityTest, 216 single-line
assertSame('lit_…', …->get_error_code()) assertions, 24 get_error_data()['fields']
digs and two json_decode(message) reads. ErrorsTest keeps json_decode: it tests
the format itself. Tests 1404 -> 1409 (AssertsToolErrorsTest); no test has fewer
assertions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-26 17:55:35 +02:00
co-authored by Claude Opus 5.5
parent 72eeaa46ad
commit 60290c85a5
50 changed files with 337 additions and 256 deletions
@@ -107,12 +107,6 @@ final class DiviReplaceTextAbilityTest extends TestCase
return $block === null ? null : AttrMap::desktop($block->attrs(), $path);
}
/** @return array<string, mixed> */
private static function clientError(WP_Error $error): array
{
return (array) json_decode($error->get_error_message(), true);
}
public function testDryRunListsHitsWithTreeRevAndChangesNothing(): void
{
$ability = $this->ability();
@@ -177,13 +171,13 @@ final class DiviReplaceTextAbilityTest extends TestCase
{
$ability = $this->ability();
self::assertSame('lit_validation_failed', $ability->execute(['search' => 'Termin', 'replace' => 'X', 'dry_run' => false])->get_error_code());
self::assertSame('lit_validation_failed', $ability->execute([
self::assertToolError($ability->execute(['search' => 'Termin', 'replace' => 'X', 'dry_run' => false]), 'validation_failed');
self::assertToolError($ability->execute([
'search' => 'Termin', 'replace' => 'X', 'dry_run' => false, 'confirm' => true, 'targets' => [['post_id' => 7]],
])->get_error_code());
self::assertSame('lit_confirm_required', $ability->execute([
]), 'validation_failed');
self::assertToolError($ability->execute([
'search' => 'Termin', 'replace' => 'X', 'dry_run' => false, 'targets' => [['post_id' => 7, 'tree_rev' => 'abc']],
])->get_error_code());
]), 'confirm_required');
self::assertSame([], $this->saved);
}
@@ -225,9 +219,9 @@ final class DiviReplaceTextAbilityTest extends TestCase
$short = $ability->execute(['search' => 'T', 'replace' => 'X']);
$reference = $ability->execute(['search' => 'Termin', 'replace' => self::REF, 'dry_run' => false, 'confirm' => true, 'targets' => [['post_id' => 7, 'tree_rev' => 'x']]]);
self::assertSame('lit_validation_failed', $short->get_error_code());
self::assertToolError($short, 'validation_failed');
self::assertArrayHasKey('search', self::clientError($short)['fields']);
self::assertSame('lit_validation_failed', $reference->get_error_code());
self::assertToolError($reference, 'validation_failed');
self::assertArrayHasKey('replace', self::clientError($reference)['fields']);
self::assertSame([], $this->queries);
self::assertSame([], $this->saved);
@@ -357,7 +351,7 @@ final class DiviReplaceTextAbilityTest extends TestCase
self::assertSame([], $this->saved);
if ($result instanceof WP_Error) {
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertToolError($result, 'validation_failed');
self::assertInstanceOf(WP_Error::class, $dry);
return;
@@ -454,7 +448,7 @@ final class DiviReplaceTextAbilityTest extends TestCase
foreach (['', "Ter\0min", "Ter\x0Bmin"] as $search) {
$error = $ability->execute(['search' => $search, 'replace' => 'X']);
self::assertSame('lit_validation_failed', $error->get_error_code(), json_encode($search));
self::assertToolError($error, 'validation_failed', json_encode($search));
self::assertArrayHasKey('search', self::clientError($error)['fields']);
}
@@ -478,7 +472,7 @@ final class DiviReplaceTextAbilityTest extends TestCase
self::assertStringContainsString('LIMIT 3 OFFSET 0', $this->queries[0]);
self::assertStringContainsString('LIMIT 3 OFFSET 2', $this->queries[1]);
self::assertIsString($second['next_cursor'], 'the fake query returns three rows again');
self::assertSame('lit_validation_failed', $invalid->get_error_code());
self::assertToolError($invalid, 'validation_failed');
self::assertStringContainsString('LIMIT 101 OFFSET 0', $this->queries[2]);
self::assertCount(3, $capped['matches']);
}