Files
api/services/nginx/app/tests/Unit/XLVask/XLVaskUsageRouteContractTest.php
T
Jeppe B 6d888a455d Automate XL-Vask invoice-period resolution (#340)
Deploy the revision-aware XL-Vask import and guarded autopilot infrastructure. Automatic actions remain fail-closed pending production readiness, calibration, dry-run, and canary gates.
2026-08-03 15:33:55 +02:00

164 lines
7.5 KiB
PHP

<?php
it('exposes direct linked order metadata on XL Vask usage order rows', function (): void {
$route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');
expect($route)->not->toBeFalse();
$route = (string)$route;
expect($route)
->toContain('$linked_order_ids_by_wash_id = []')
->and($route)->toContain('array_key_exists($wash_id, $linked_order_ids_by_wash_id)')
->and($route)->toContain('selectByWashId($wash_id)')
->and($route)->toContain("'linked_order_id' => \$linked_order_id")
->and($route)->toContain("'usage_log_id' => \$id");
});
it('does not execute XL Vask usage automation while listing usage order rows', function (): void {
$route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');
expect($route)->not->toBeFalse();
$route = (string)$route;
expect($route)
->toContain('$automation = $automation_service->readAutomationStateByUsageLogId($id, $log);')
->and($route)->not->toContain('$automation = $automation_service->evaluateUsageLogRow($log, (int)$user->id, true);')
->and($route)->toContain("requirePermission('manage_xlvask_usage_automation')");
expect($route)
->toContain("if (\$allowedHallIds === [])")
->toContain("No XL Vask hall scope is available', 403")
->not->toContain('__no_authorized_xlvask_hall__');
});
it('returns cached amount summaries on XL Vask usage order rows without widening the usage-log object payload', function (): void {
$route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');
expect($route)->not->toBeFalse();
$route = (string)$route;
expect($route)
->toContain('$amount_summary = $xlvask_usage_logs->getAmountSummaryReadOnly($log)')
->and($route)->toContain('$usage_log_payload = array_intersect_key($log, array_flip([')
->and($route)->toContain('$tmp->setProperties($usage_log_payload)')
->and($route)->toContain("\$tmp_res['order']['total_net_amount'] = \$amount_summary['total_net_amount']")
->and($route)->toContain("\$tmp_res['order']['xlvask_primary_product_name'] = \$amount_summary['primary_product_name']")
->and($route)->toContain("\$tmp_res['order']['xlvask_amount_cached'] = \$amount_summary['cached']");
});
it('adapts the legacy XL Vask usage import route to a scoped queued autopilot run', function (): void {
$route = file_get_contents(WD . '/routes/moduleXLVaskRoute.php');
$automation = file_get_contents(WD . '/classes/xlvask_automation_service.php');
expect($route)
->not->toBeFalse()
->and($automation)->not->toBeFalse();
$route = (string)$route;
$automation = (string)$automation;
expect($route)
->toContain("getParameter('dateFrom')")
->toContain("getParameter('dateTo')")
->toContain("'replacement' => '/modules/xlvask/services/usage/autopilot-runs'")
->toContain("'forceRefetch' => true")
->not->toContain('runPending($dateFrom, $dateTo, [], 100, null)')
->and($automation)->toContain("STR_TO_DATE(REPLACE(SUBSTRING(StartTime, 1, 19), 'T', ' '), '%Y-%m-%d %H:%i:%s')");
});
it('exposes additive XL Vask autopilot run and summary routes', function (): void {
$route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');
expect($route)->not->toBeFalse();
$route = (string)$route;
expect($route)
->toContain("get('/modules/xlvask/services/usage/orders/summary'")
->toContain("post('/modules/xlvask/services/usage/autopilot-runs'")
->toContain("get('/modules/xlvask/services/usage/autopilot-runs/{id}'")
->toContain('(new xlvask_autopilot_service())->getSummary(')
->toContain('(new xlvask_autopilot_service())->createRun(')
->toContain('(new xlvask_autopilot_service())->getRun(')
->toContain('self::allowedHallIdsForUser($user)')
->toContain('], 202);');
});
it('routes legacy automation entry points through the durable queue and preview lifecycle', function (): void {
$route = (string)file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');
expect($route)
->toContain("'mode' => 'execute'")
->toContain("'mode' => 'dry_run'")
->toContain('Use the server-generated automation decision preview and apply endpoints.')
->not->toContain('(new xlvask_automation_service())->runPending(')
->not->toContain('(new xlvask_automation_service())->evaluateUsageLogById(');
});
it('returns revision and resolution state on XL Vask usage order rows', function (): void {
$route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');
expect($route)->not->toBeFalse();
$route = (string)$route;
expect($route)
->toContain("'source_hash' => \$log['source_hash'] ?? null")
->toContain("'source_revision' => \$log['source_revision'] ?? null")
->toContain("'import_state' => \$log['import_state'] ?? 'unchanged'")
->toContain("'resolution_state' => \$log['resolution_state'] ?? 'needs_review'")
->toContain("'certainty' => \$log['certainty'] ?? 'none'")
->toContain("'planned_action' => \$log['planned_action'] ?? 'none'")
->toContain("'expected_version' => isset(\$log['expected_version']) ? (int)\$log['expected_version'] : 1")
->toContain("'automation' => \$automation");
});
it('documents XL Vask autopilot summary and run APIs in OpenAPI', function (): void {
$openApi = file_get_contents(WD . '/openapi.yaml');
expect($openApi)->not->toBeFalse();
$openApi = (string)$openApi;
expect($openApi)
->toContain('/modules/xlvask/services/usage/orders/summary:')
->toContain('operationId: summarizeXlvaskUsageAutomation')
->toContain('/modules/xlvask/services/usage/autopilot-runs:')
->toContain('operationId: createXlvaskUsageAutopilotRun')
->toContain('/modules/xlvask/services/usage/autopilot-runs/{id}:')
->toContain('operationId: getXlvaskUsageAutopilotRun')
->toContain('/modules/xlvask/services/usage/automation/decisions/preview:')
->toContain('operationId: previewXlvaskUsageAutomationDecision')
->toContain('/modules/xlvask/services/usage/automation/decisions/apply:')
->toContain('operationId: applyXlvaskUsageAutomationDecision')
->toContain('/modules/xlvask/services/usage/automation/admin/readiness:')
->toContain('operationId: adjudicateXlvaskCalibrationLabel')
->toContain('operationId: generateXlvaskCalibrationArtifact')
->toContain('operationId: activateXlvaskCalibrationArtifact')
->toContain('operationId: activateXlvaskWashIdUniqueness');
});
it('wires preview-bound bulk decisions through the transactional autopilot service', function (): void {
$route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');
$autopilot = file_get_contents(WD . '/classes/xlvask_autopilot_service.php');
expect($route)
->not->toBeFalse()
->and($autopilot)->not->toBeFalse();
$route = (string)$route;
$autopilot = (string)$autopilot;
expect($route)
->toContain("post('/modules/xlvask/services/usage/automation/decisions/preview'")
->toContain("post('/modules/xlvask/services/usage/automation/decisions/apply'")
->toContain('createDecisionPreview(')
->toContain('applyDecision(')
->and($autopilot)->toContain("SELECT * FROM xlvask_automation_decision_previews WHERE id = '")
->toContain('FOR UPDATE')
->toContain('applyBoundDecisionWithinTransaction(')
->toContain('expected_version')
->toContain('source_hash');
});