Files
vdm/cli/tests/client_test.cpp
samiandClaude Sonnet 5 824fa481bb daemon: event.* fan-out (D5) + category.list/queue.list (D3) — GUI-ready
The two items aimed at pointing GUI at a real veloxd instead of mockd.

rpc/event_hub — per-subscription fan-out shared by both transports.
subscribe() registers a connection with no interest; set_filter()
(session.subscribe, replaces not adds) turns on event kinds and an
optional per-task id filter; publish() delivers a pre-built
notification to every matching subscriber. session.subscribe on both
UdsServer and WsServer now does the real thing — registers/updates a
subscription, tears it down in close_conn.

sched/scheduler — the on_engine_state hook now actually publishes:
- transition() is the one place a task's row changes state; it reads
  the store's own prior row for previousState (authoritative
  regardless of engine/scheduler timing), writes the error columns,
  and — when a hub is supplied — publishes event.task.state with
  {taskId, state, previousState, summary, error}. Wired into every
  transition: scheduler-driven (admission -> probing, resume ->
  connecting, pause) and engine-reported (on_engine_state).
- progress_snapshot(): one row per task the engine is tracking
  (EnginePort::progress(), a new interface method backed by
  DownloadHandle::progress()), plus a store side-effect
  (Tasks::update_progress) so download.list/get stay current between
  state transitions. Returns rows; does NOT publish itself — batching
  into one array message is the caller's job, per the schema's
  x-maxRateHz: 4 and AGENT-DAEMON.md item 5 ("one message per task per
  tick burns a core"). main.cpp's 250 ms timerfd is that caller: one
  event.task.progress per tick, only when there's something to say.

dispatcher::on_download_add now publishes event.task.added (schema:
"summary is always present so a client can insert the row without a
follow-up download.get").

store/categories, store/queues — the two D3 handlers GUI's panels
call. category.list projects the six seeded built-ins; queue.list
derives taskIds from tasks.queue_id/queue_position (Queue's own schema
note: a queue's stored row never carries membership, download.update
/ queue.reorder do).

Verified live end to end against tools/testserver: a subscribed client
sees event.task.added on add, then the full event.task.state sequence
(queued -> probing -> connecting -> downloading -> assembling ->
verifying -> complete) with correct previousState at every step, and
real category.list / queue.list results.

Tests: event_hub (filter-by-kind, filter-by-task-id, replace-not-add,
unsubscribe), store_categories_queues, plus new sched_scheduler cases
for event.task.state publishing and progress_snapshot's store
side-effect. 38 daemon/cli tests green; sched_scheduler / event_hub /
ws_server / uds_roundtrip TSan-clean.

deferrals.md: D5 mostly closed (event.task.removed and the
still-unpublished events wait on their owning D3 handlers); D3 down to
the remaining download.* verbs, rules/settings/limiter/schedule,
queue mutation, category mutation, grabber, media.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Upd9WhG9oppieig5nRDLig
2026-09-11 07:19:11 +04:00

133 lines
4.3 KiB
C++

// The CLI's Client against a real in-process daemon RPC server.
#include <sys/stat.h>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <thread>
#include "check.hpp"
#include "client.hpp"
#include "rpc/dispatcher.hpp"
#include "rpc/event_loop.hpp"
#include "rpc/event_hub.hpp"
#include "rpc/uds_server.hpp"
#include "store/migrations.hpp"
#include "store/settings.hpp"
#include "store/sqlite.hpp"
namespace rpc = velox::daemon::rpc;
static std::string g_allowed_root;
using velox::cli::CallError;
using velox::cli::Client;
void run() {
// connect() with no daemon -> a kConnect error, not a crash.
{
::setenv("XDG_RUNTIME_DIR", "/tmp/velox-cli-test-nonexistent-xyz", 1);
Client c;
auto err = c.connect();
CHECK(err.has_value());
if (err) CHECK_EQ(err->code, CallError::kConnect);
}
// Point XDG_RUNTIME_DIR at a fresh temp dir; the client derives
// <XDG_RUNTIME_DIR>/velox/velox.sock and the server binds exactly that.
char tmpl[] = "/tmp/velox-cli-test-XXXXXX";
const char* xdg = ::mkdtemp(tmpl);
CHECK(xdg != nullptr);
if (xdg == nullptr) return;
::setenv("XDG_RUNTIME_DIR", xdg, 1);
const std::string velox_dir = std::string(xdg) + "/velox";
::mkdir(velox_dir.c_str(), 0700);
const std::string server_sock = velox_dir + "/velox.sock";
rpc::EventLoop loop;
auto db = velox::daemon::store::Db::open(":memory:");
CHECK(db.has_value());
if (!db) return;
CHECK(velox::daemon::store::migrate_to_head(*db).has_value());
char root_tmpl[] = "/tmp/velox-cli-root-XXXXXX";
g_allowed_root = ::mkdtemp(root_tmpl);
CHECK(!g_allowed_root.empty());
{
velox::daemon::store::Settings settings(*db);
CHECK(settings.set_raw("saveTo.allowedRoots",
"[\"" + g_allowed_root + "\"]").has_value());
CHECK(settings.set_raw("saveTo.defaultDir",
"\"" + g_allowed_root + "\"").has_value());
}
rpc::EventHub hub;
rpc::VeloxDispatcher dispatcher(*db, hub);
rpc::UdsServer server(loop, dispatcher, hub, server_sock);
const auto ec = server.start();
CHECK(!ec);
if (ec) return;
std::thread th([&loop] { loop.run(); });
{
Client c;
auto err = c.connect();
CHECK(!err.has_value());
if (err) {
loop.stop();
th.join();
return;
}
CHECK_EQ(c.hello_result().value("transport", ""), std::string("uds"));
auto ls = c.call("download.list", nlohmann::json::object());
CHECK(ls.has_value());
if (ls) {
CHECK_EQ(ls->value("total", -1), 0);
CHECK(ls->at("items").is_array());
}
// download.add outside every allowed root -> -32011, original saveDir echoed.
auto bad = c.call("download.add",
{{"url", "https://example.com/x"}, {"saveDir", "/etc"}});
CHECK(!bad.has_value());
if (!bad) {
CHECK_EQ(bad.error().code, -32011);
CHECK_EQ(bad.error().data.value("path", ""), std::string("/etc"));
}
// download.add into an allowed root -> a task id that then shows up in the list.
auto ok = c.call(
"download.add",
{{"url", "https://example.com/movie.mp4"}, {"saveDir", g_allowed_root}});
CHECK(ok.has_value());
std::string task_id;
if (ok) {
task_id = ok->value("taskId", "");
CHECK(!task_id.empty());
CHECK_EQ(ok->value("state", ""), std::string("queued"));
}
auto ls2 = c.call("download.list", nlohmann::json::object());
CHECK(ls2.has_value());
if (ls2) {
CHECK_EQ(ls2->value("total", -1), 1);
CHECK_EQ(ls2->at("items").at(0).value("filename", ""), std::string("movie.mp4"));
}
auto detail = c.call("download.get", {{"taskId", task_id}});
CHECK(detail.has_value());
if (detail) CHECK_EQ(detail->at("summary").value("taskId", ""), task_id);
auto missing =
c.call("download.get", {{"taskId", "00000000-0000-4000-8000-000000000000"}});
CHECK(!missing.has_value());
if (!missing) CHECK_EQ(missing.error().code, -32010);
}
loop.stop();
th.join();
::unlink(server_sock.c_str());
}
TEST_MAIN()