TenantAtlas/apps/platform/tests/Feature/Operations/ProductTelemetryOperationStartCaptureTest.php
ahmido 6053d87b99
Some checks failed
Main Confidence / confidence (push) Failing after 48s
feat: implement product usage adoption telemetry (#281)
## Summary
- implement spec 243 product usage adoption telemetry end-to-end
- add bounded product usage event capture, aggregation, retention pruning, and system dashboard KPIs
- add unit and feature coverage for telemetry capture, authorization, retention, privacy, and dashboard window behavior

## Validation
- ran focused Pest test suites for telemetry and system dashboard behavior
- ran Laravel Pint formatting
- verified the system dashboard telemetry widget in the integrated browser

## Notes
- branch: `243-product-usage-adoption-telemetry`
- target: `dev`

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #281
2026-04-26 20:52:38 +00:00

73 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\ProductUsageEvent;
use App\Models\Tenant;
use App\Services\OperationRunService;
use App\Support\OperationRunType;
use App\Support\ProductTelemetry\ProductUsageEventCatalog;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('records telemetry for user-initiated tenant-bound operation starts only once per created run', function (): void {
$tenant = Tenant::factory()->create();
[$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'operator');
$service = app(OperationRunService::class);
$run = $service->ensureRun(
tenant: $tenant,
type: OperationRunType::ReviewPackGenerate->value,
inputs: [
'include_pii' => true,
'include_operations' => true,
],
initiator: $user,
);
$sameRun = $service->ensureRun(
tenant: $tenant,
type: OperationRunType::ReviewPackGenerate->value,
inputs: [
'include_pii' => true,
'include_operations' => true,
],
initiator: $user,
);
expect($sameRun->getKey())->toBe($run->getKey())
->and(ProductUsageEvent::query()->count())->toBe(1);
$event = ProductUsageEvent::query()->sole();
$serializedEvent = json_encode($event->toArray(), JSON_THROW_ON_ERROR);
expect($event->event_name)->toBe(ProductUsageEventCatalog::OPERATIONS_STARTED)
->and($event->tenant_id)->toBe((int) $tenant->getKey())
->and($event->workspace_id)->toBe((int) $tenant->workspace_id)
->and($event->user_id)->toBe((int) $user->getKey())
->and($event->subject_type)->toBe('operation_run')
->and($event->subject_id)->toBe((string) $run->getKey())
->and($event->metadata)->toBe([
'operation_type' => OperationRunType::ReviewPackGenerate->value,
])
->and($serializedEvent)->not->toContain('@')
->and($serializedEvent)->not->toContain($user->name)
->and($serializedEvent)->not->toContain((string) $tenant->name);
});
it('does not record telemetry for system-initiated operation starts', function (): void {
$tenant = Tenant::factory()->create();
app(OperationRunService::class)->ensureRun(
tenant: $tenant,
type: OperationRunType::ReviewPackGenerate->value,
inputs: [
'include_pii' => true,
],
initiator: null,
);
expect(ProductUsageEvent::query()->count())->toBe(0);
});