<?php
// Runs on staging. Asserts that enabling Valkey bumps getUserResourceUsage by memory_mb.
require_once '/docker/whp/web/configs/config.php';
require_once '/docker/whp/web/libs/mysqlmgmt.php';
require_once '/docker/whp/web/libs/valkey-mgmt.php';

$username = $argv[1] ?? 'valkey-qa-user';
$m = new mysqlmgmt();
$db = $m->getMySQLConnection();

$before = $m->getUserResourceUsage($username)['memory_used'] ?? 0;
echo "memory_used before: $before\n";

$stmt = $db->prepare("INSERT INTO whp.customer_valkey (username, enabled, memory_mb, mode, eviction_policy, network_name, container_name)
    VALUES (?, 1, 77, 'cache', 'allkeys-lru', CONCAT('cust-', ?, '-valkey-net'), CONCAT('valkey-cust-', ?))
    ON DUPLICATE KEY UPDATE enabled=1, memory_mb=77");
$stmt->execute([$username, $username, $username]);

$after = $m->getUserResourceUsage($username)['memory_used'] ?? 0;
echo "memory_used after: $after\n";

$db->prepare("DELETE FROM whp.customer_valkey WHERE username = ?")->execute([$username]);

if ($after - $before !== 77) { fwrite(STDERR, "FAIL: expected +77, got " . ($after - $before) . "\n"); exit(1); }
echo "PASS\n";
