<?php
declare(strict_types=1);
require __DIR__ . '/read6_safe2_contract.php';

function verify(bool $ok, string $message): void
{
    if (!$ok) throw new RuntimeException($message);
}

$setting = ['aifaceview' => '', 'aifaceset' => '', 'apifacedefault' => '',
    'withdrawRecord' => '', 'details' => '', 'recharge' => '',
    'orderDetail' => '', 'face_type' => 1, 'zhct_host' => ''];
$body = json_encode(['status' => 200, 'data' => ['setting' => $setting]]);
$result = read6_safe2_contract('setting', 200, $body);
verify($result['state'] === 'shape_ok' && $result['shape']['setting']['face_type'] === 'integer',
    'setting nine-key shape');
verify(!isset($result['data']), 'never expose raw setting URLs');
verify(read6_safe2_contract('setting', 200,
    json_encode(['status' => 200, 'data' => ['setting' => []]]))['state'] === 'contract_red',
    'missing setting keys must fail');
$list = ['current_page' => 1, 'data' => [], 'last_page' => 1,
    'per_page' => 50, 'total' => 0];
$result = read6_safe2_contract('cashout_list', 200,
    json_encode(['status' => 200, 'data' => ['list' => $list]]));
verify($result['state'] === 'shape_ok' && $result['branch'] === 'empty_only',
    'empty list is not positive');
$list['data'][] = ['id' => 1, 'order_no' => 'test', 'create_time' => 1,
    'money' => '1.00', 'pay_status' => 1];
$list['total'] = 1;
$result = read6_safe2_contract('cashout_list', 200,
    json_encode(['status' => 200, 'data' => ['list' => $list]]));
verify($result['state'] === 'shape_ok' && $result['branch'] === 'nonempty_candidate',
    'nonempty list needs source reconciliation');
verify(read6_safe2_contract('cashout_list', 200, '{invalid')['state'] === 'contract_red',
    'invalid JSON must fail');
verify(read6_safe2_contract('cashout_list', 500, '')['state'] === 'contract_red',
    'HTTP error must fail');
echo "read6 safe2 contract tests: 7 green\n";
