feat(ticket): make thread mutations idempotent
parent
a249c8abea
commit
a2ec556c8f
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
final class TicketWriteRequest extends Model
|
||||
{
|
||||
protected $connection = 'default';
|
||||
protected $table = 'ticket_write_requests';
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
final class TicketIdempotencyConflict extends \RuntimeException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('请求幂等标识已用于其他工单内容', 409);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Ticket\OpenTicketCommand;
|
||||
use App\Services\Ticket\TicketIdempotencyConflict;
|
||||
use App\Services\Ticket\TicketThreadService;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
if (!defined('BASE_PATH')) {
|
||||
define('BASE_PATH', sys_get_temp_dir() . '/virt-ticket-idempotency-' . bin2hex(random_bytes(5)));
|
||||
}
|
||||
|
||||
$assert = static function ($condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$request = static function (): object {
|
||||
return new class {
|
||||
public function getUploadedFiles(): array { return []; }
|
||||
};
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$schema = $capsule->getConnection('default')->getSchemaBuilder();
|
||||
$schema->create('ticket', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->text('title');
|
||||
$table->text('content');
|
||||
$table->integer('rootid')->default(0);
|
||||
$table->integer('userid');
|
||||
$table->integer('datetime');
|
||||
$table->integer('status')->default(1);
|
||||
$table->string('ticket_type')->default('technical');
|
||||
$table->string('priority')->default('medium');
|
||||
$table->integer('instance_id')->nullable();
|
||||
});
|
||||
$schema->create('ticket_attachments', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('ticket_id');
|
||||
$table->integer('message_id');
|
||||
$table->integer('user_id');
|
||||
$table->string('storage_key');
|
||||
$table->string('original_name');
|
||||
$table->string('mime_type');
|
||||
$table->integer('bytes');
|
||||
$table->string('sha256');
|
||||
$table->integer('created_at');
|
||||
});
|
||||
$schema->create('ticket_write_requests', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('actor_user_id');
|
||||
$table->string('request_key');
|
||||
$table->string('request_hash');
|
||||
$table->string('operation');
|
||||
$table->integer('ticket_id')->nullable();
|
||||
$table->integer('message_id')->nullable();
|
||||
$table->integer('created_at');
|
||||
$table->unique(['actor_user_id', 'request_key']);
|
||||
});
|
||||
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id');
|
||||
$table->string('status');
|
||||
});
|
||||
|
||||
$service = new TicketThreadService();
|
||||
$key = 'ticket-open-1234567890';
|
||||
$command = new OpenTicketCommand(
|
||||
7, 'Network issue', 'Cannot connect', 'technical', 'high', null, $request(), $key
|
||||
);
|
||||
$first = $service->open($command);
|
||||
$again = $service->open($command);
|
||||
|
||||
$assert($first->wasCreated && !$again->wasCreated,
|
||||
'An idempotent replay is not distinguished from the original write.');
|
||||
$assert($again->root->id === $first->root->id && $again->message->id === $first->message->id,
|
||||
'An idempotent replay did not return the original result.');
|
||||
$assert(Capsule::table('ticket')->count() === 1
|
||||
&& Capsule::table('ticket_write_requests')->count() === 1,
|
||||
'An idempotent replay duplicated the ticket or intent.');
|
||||
|
||||
try {
|
||||
$service->open(new OpenTicketCommand(
|
||||
7, 'Changed title', 'Cannot connect', 'technical', 'high', null, $request(), $key
|
||||
));
|
||||
throw new RuntimeException('A changed request reused an existing idempotency key.');
|
||||
} catch (TicketIdempotencyConflict $exception) {
|
||||
$assert($exception->getCode() === 409, 'Idempotency conflict is not an HTTP 409 domain error.');
|
||||
}
|
||||
|
||||
echo "Ticket idempotency checks passed.\n";
|
||||
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
$javascript = file_get_contents(dirname(__DIR__) . '/public/theme/venix/js/ticket-page.js');
|
||||
$userController = file_get_contents(dirname(__DIR__) . '/app/Controllers/UserController.php');
|
||||
$adminController = file_get_contents(dirname(__DIR__) . '/app/Controllers/Admin/TicketController.php');
|
||||
|
||||
$assert = static function ($condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$assert(str_contains($javascript, 'Idempotency-Key')
|
||||
&& str_contains($javascript, 'requestFingerprint'),
|
||||
'User ticket submissions do not retain a logical request key across retries.');
|
||||
$assert(str_contains($javascript, 'if (!data.ret) done()')
|
||||
|| str_contains($javascript, 'if (!data.ret) {') && str_contains($javascript, 'done();'),
|
||||
'A successful ticket submission still re-enables its controls before navigation.');
|
||||
$assert(str_contains($userController, "getHeaderLine('Idempotency-Key')")
|
||||
&& str_contains($adminController, "getHeaderLine('Idempotency-Key')"),
|
||||
'A ticket HTTP adapter does not pass the idempotency key into the write module.');
|
||||
|
||||
echo "Ticket page idempotency contract passed.\n";
|
||||
Loading…
Reference in New Issue