#!/usr/bin/env php
<?php
require_once(__DIR__ . '/../web-files/libs/sitesmith-schema.php');
$pass = 0; $fail = 0;
function ck($desc, $expected_ok, $obj) {
    global $pass, $fail;
    $r = sitesmith_validate_response($obj);
    if ($r['ok'] === $expected_ok) { $pass++; return; }
    $fail++; fprintf(STDERR, "FAIL %s\n", $desc);
}
ck('replace minimal', true, ['type'=>'replace','scope'=>'site','pages'=>[['name'=>'Home','tree'=>['type'=>['resolvedName'=>'Section'],'props'=>['aiName'=>'Hero']]]],'message'=>'ok']);
ck('patch update_props', true, ['type'=>'patch','ops'=>[['op'=>'update_props','node_id'=>'ai-hero-1','props'=>['x'=>1]]],'message'=>'ok']);
ck('ask minimal', true, ['type'=>'ask','question'=>'which one?']);
ck('missing type', false, ['x'=>1]);
ck('replace bad scope', false, ['type'=>'replace','scope'=>'whole','pages'=>[],'message'=>'']);
ck('patch unknown op', false, ['type'=>'patch','ops'=>[['op'=>'splice','node_id'=>'x']],'message'=>'m']);
ck('patch missing nodeid', false, ['type'=>'patch','ops'=>[['op'=>'delete_node']],'message'=>'m']);
ck('ask no question', false, ['type'=>'ask']);
ck('tree missing type', false, ['type'=>'replace','scope'=>'site','pages'=>[['name'=>'H','tree'=>['props'=>[]]]],'message'=>'m']);
echo "$pass passed, $fail failed\n";
exit($fail === 0 ? 0 : 1);
