diff --git a/Cargo.lock b/Cargo.lock index a5fc9cd6b30..399a03610c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4300,7 +4300,6 @@ dependencies = [ "moq-audio", "moq-auth", "moq-hls", - "moq-msf", "moq-mux", "moq-relay", "moq-rtc", @@ -4317,11 +4316,13 @@ dependencies = [ "serde", "serde_json", "tempfile", + "terminal_size", "tokio", "tower-http 0.7.1", "tracing", "url", "usage-rs", + "vt100", "winit", ] @@ -8625,6 +8626,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "terminal_size" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" +dependencies = [ + "rustix 1.1.5", + "windows-sys 0.61.2", +] + [[package]] name = "textwrap" version = "0.16.4" @@ -9571,6 +9582,27 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "vt100" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "054ff75fb8fa83e609e685106df4faeffdf3a735d3c74ebce97ec557d5d36fd9" +dependencies = [ + "itoa", + "unicode-width", + "vte", +] + +[[package]] +name = "vte" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5924018406ce0063cd67f8e008104968b74b563ee1b85dde3ed1f7cb87d3dbd" +dependencies = [ + "arrayvec", + "memchr", +] + [[package]] name = "walkdir" version = "2.5.0" diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 9d86fa27231..830328b03e8 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -24,7 +24,7 @@ Docker; see [Install](/setup/install). | `export` | `rtmp`, `srt`, `rtc` | Serve plays (`--listen`) or push to a remote (`--connect`). | | `play` | | Decode and play in a native window with sound. | | `transcode` | | Publish a just-in-time rendition ladder next to a broadcast. | -| `ls` | `[prefix]` | List the broadcasts live on a relay. | +| `announced` | `[prefix]` | Follow the broadcasts announced on a relay. | | `fetch` | `` | Write one group of a track to stdout. | | `auth` | | Generate, sign, and verify relay JWTs. | | `devices` | | List capture sources and their ids. | @@ -35,7 +35,7 @@ Docker; see [Install](/setup/install). moq import [options] moq export [options] moq play [options] -moq ls [prefix] [options] +moq announced [prefix] [options] moq fetch [options] ``` @@ -287,28 +287,30 @@ heights and bitrates must then increase strictly together. Duplicate heights or bitrates, inverted rankings, and zero-sized or zero-bitrate rungs are rejected before connecting. -## List +## Announced ```bash -moq --connect https://relay.example.com/anon ls -moq ... ls room --follow --json +moq --connect https://relay.example.com/anon announced +moq ... announced room --json ``` -Lists the broadcasts live on a relay over MoQ, with the session's own auth: the -counterpart of the relay's HTTP `/announced/`. It prints one path per -line, relative to the `--connect` path, and exits once the relay has sent -everything live under `prefix`. `--follow` keeps running: it prints `+ path` -for each live broadcast, then `+ path` and `- path` as broadcasts come and go, -and exits non-zero if the session ends. `--json` prints -`{"path": "room/alice", "active": true}` per line instead, in either mode. - -Like `/announced`, it lists announced prefixes, which by convention are -broadcast paths. A new route to a path already live prints nothing. A name -starting with `.` stays hidden unless `prefix` names it. `ls` only dials +Follows the broadcasts announced on a relay over MoQ, with the session's own +auth: the live counterpart of the relay's HTTP `/announced/`. Paths are +relative to the `--connect` path. On a terminal it shows what is announced +under `prefix` right now, redrawn as broadcasts start and end, and a list +taller than the terminal ends in a count of the rest. Piped, it prints +`+ path` for each broadcast already announced, then `+ path` and `- path` as +broadcasts come and go. `--json` prints `{"path": "room/alice", "active": true}` +per line instead, on a terminal or not. It runs until interrupted, and exits +non-zero if the session ends. + +Like `/announced`, it follows announced prefixes, which by convention are +broadcast paths. A new route to a path already announced prints nothing. A name +starting with `.` stays hidden unless `prefix` names it. `announced` only dials `--connect`, and refuses any other MoQ-side flag. -[Inspect a relay](/bin/inspect) walks through `ls` and `fetch` next to their -`curl` equivalents, including reading the relay's stats. +[Inspect a relay](/bin/inspect) walks through `announced` and `fetch` next to +their `curl` equivalents, including reading the relay's stats. ## Fetch @@ -441,7 +443,7 @@ way. Only `ts` can mark the restart, so the other formats refuse `--linger`. ## Debugging `RUST_LOG=debug` prints the negotiated version and every subscription. -`moq --connect ls`, or `curl http://relay:4443/announced`, confirms the +`moq --connect announced`, or `curl http://relay:4443/announced`, confirms the relay is reachable and shows what it holds; see [Inspect a relay](/bin/inspect). Connection refused means UDP isn't getting through; certificate errors on a dev relay want `--connect-tls-insecure` or the `http://` fingerprint flow. diff --git a/doc/bin/inspect.md b/doc/bin/inspect.md index f83a55b4a33..d2e3420e8dd 100644 --- a/doc/bin/inspect.md +++ b/doc/bin/inspect.md @@ -12,21 +12,22 @@ with the session's own auth, and a `curl` against the relay's | Question | MoQ | HTTP | | --- | --- | --- | -| What is live? | `moq --connect / ls [prefix]` | `GET /announced/` | -| What changes? | `moq ... ls --follow` | (none) | +| What is live? | `moq --connect / announced [prefix]` | `GET /announced/` | +| What changes? | `moq ... announced [prefix] \| cat` | (none) | | What is in a group? | `moq ... --broadcast fetch ` | `GET /fetch///` | ## What is live ```bash -moq --connect http://localhost:4443/anon ls +moq --connect http://localhost:4443/anon announced curl http://localhost:4443/announced/anon ``` -Both print one broadcast path per line, relative to the path in the URL, and -both use that path for auth, so a token rooted at `rooms/123` lists only its -room. `moq ls room` narrows the listing to one prefix, and `--json` prints -`{"path": "room/alice", "active": true}` per line instead. +Both name broadcasts by path, relative to the path in the URL, and both use +that path for auth, so a token rooted at `rooms/123` lists only its room. +`curl` prints a snapshot, one path per line. `moq announced` keeps running and +redraws the list in place as broadcasts start and end, until you stop it. +`moq announced room` narrows either to one prefix. Both list announced prefixes, which by convention are broadcast paths. A segment starting with `.` stays hidden unless the URL path or `prefix` names @@ -35,13 +36,14 @@ it, which keeps the relay's own `.stats` out of the listing. ## What changes ```bash -moq --connect http://localhost:4443/anon ls --follow +moq --connect http://localhost:4443/anon announced | cat ``` -`--follow` prints `+ path` for each live broadcast, then `+ path` and `- path` -as broadcasts come and go, until you stop it. It exits non-zero if the session -ends, so a script can tell a lost relay from a quiet one. `curl` has no -equivalent: `/announced` is a snapshot. +Piped, `moq announced` prints `+ path` for each broadcast already announced, +then `+ path` and `- path` as broadcasts come and go, and `--json` prints +`{"path": "room/alice", "active": true}` per line instead. It exits non-zero if +the session ends, so a script can tell a lost relay from a quiet one. `curl` +has no equivalent: `/announced` is a snapshot. ## What is in a group @@ -66,7 +68,7 @@ counters as ordinary broadcasts under `.stats`, so the same tools read them. Dial the `.stats` path, which the token must cover: ```bash -moq --connect http://localhost:4443/.stats ls +moq --connect http://localhost:4443/.stats announced moq --connect http://localhost:4443/.stats --broadcast node/local/host fetch publisher.json | jq curl http://localhost:4443/fetch/.stats/node/local/host/publisher.json | jq ``` diff --git a/doc/bin/relay/http.md b/doc/bin/relay/http.md index 8f24e2c33ca..7975a39e29e 100644 --- a/doc/bin/relay/http.md +++ b/doc/bin/relay/http.md @@ -26,7 +26,7 @@ The announcement listing names each announced route by the prefix it covers; by convention a publisher announces each broadcast's exact path, so the list reads as broadcast names. -`moq ls` and `moq fetch` answer the same questions over MoQ; see +`moq announced` and `moq fetch` answer the same questions over MoQ; see [Inspect a relay](/bin/inspect). A relay configured with more than one certificate has no single fingerprint to diff --git a/quest/m0/broadcast-epoch/hop-removal.md b/quest/m0/broadcast-epoch/hop-removal.md index 2e98108b4f9..154ad2e17ef 100644 --- a/quest/m0/broadcast-epoch/hop-removal.md +++ b/quest/m0/broadcast-epoch/hop-removal.md @@ -32,7 +32,7 @@ publisher's hop chain. and "the first entry identifies the endpoint that originated the route". In `drafts/draft-lcurley-moq-cluster.md`: the unknown-publisher stamping text, the relay behavior that stamps, and the changelog bullets. -- CLI: `rs/moq-cli/src/{args,complete,fetch,ls}.rs`. Relays keep `cluster.id` +- CLI: `rs/moq-cli/src/{args,complete,fetch,announced}.rs`. Relays keep `cluster.id` as their node id. - Docs: `doc/bin/cli.md` ("Redundant publishers" uses `--epoch`), the migration row in `doc/setup/upgrade.md`, the stamping paragraph in diff --git a/quest/m1/README.md b/quest/m1/README.md index fd242f0f822..09e1b9647a5 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -48,7 +48,6 @@ blocks. The quests that gated m0 lines moved under them. - [moq-bot may push workflow changes](/quest/m1/bot-workflows-permission.md) - condition: the maintainer grants moq-bot's GitHub App the `workflows` permission, so back-merges carrying workflow changes go through - [Binding audio delay](/quest/m1/binding-surface.md) - moq-ffi and every wrapper configure and observe audio playout delay - [moq-net owns its transport seam](/quest/m1/transport-seam.md) - moq-net names only its own transport traits, so a web-transport-trait or qmux major bump is a patch for it -- [moq announced](/quest/m1/cli-announced.md) - `moq ls` becomes a follow-only live view named `moq announced`, with local-only shell completion - [fMP4 init from the catalog](/quest/m1/fmp4-catalog-init.md) - fMP4 export writes avc3/hev1 entries from the catalog, so an Annex-B H.264 or H.265 init no longer waits for the first keyframe - [Delete the live marker](/quest/m1/announce-live-removal.md) - announce streams yield only route events; the `Live` marker and its hold machinery are gone - [Browser close code](/quest/m1/browser-close-code.md) - a playwright case proves the page reads a relay's close code and reason, on every web-transport backend diff --git a/quest/m1/announce-live-removal.md b/quest/m1/announce-live-removal.md index 3a57b69c209..a5d232b8105 100644 --- a/quest/m1/announce-live-removal.md +++ b/quest/m1/announce-live-removal.md @@ -14,7 +14,7 @@ Decided 2026-09-29 by the maintainer, after walking through a page-load fix, a Live/Offline toggle, and a per-session `live()`: - The marker answers "has the initial list arrived?" Only one-shot listing - (`moq ls`, shell completion) uses it, and no app does: room, watch, and the + (`moq ls`, shell completion, both since removed) used it, and no app does: room, watch, and the demo skip it. An origin merges many sessions and local publishers, so "caught up" there needs aggregation across connections that start, fail, and reconnect independently. That produced the page-load race, the special @@ -45,7 +45,3 @@ Guidance: Public API: removes `AnnounceEvent::Live` / `{ kind: "live" }` / `MoqAnnounceEvent::Live` and the binding aliases. Wire: none. - -## Required - -- [moq announced](/quest/m1/cli-announced.md) - the CLI stops reading the marker first diff --git a/quest/m1/cli-announced.md b/quest/m1/cli-announced.md deleted file mode 100644 index 39653bc162f..00000000000 --- a/quest/m1/cli-announced.md +++ /dev/null @@ -1,41 +0,0 @@ -# [M] moq announced follows announcements live - -## Goal - -`moq ls` becomes `moq announced`, which only follows. On a terminal it -redraws a list of what is announced right now, updating on each start and -end. When piped, or with `--json`, it prints one `+`/`-` line per event, as -`moq ls --follow` does today. `moq ls` and its one-shot mode are gone. Shell -completion is local-only: flags, subcommands, and local capture devices; -nothing dials a relay or reads a catalog. Nothing in `rs/moq-cli` reads the -announce `Live` marker afterwards. - -## Plan - -Decided 2026-09-29 by the maintainer: - -- Follow-only. A one-shot listing needs to know when the initial set is - complete, which is the only thing the `Live` marker is used for. No - customer needs a one-shot listing, so it waits until one does. Why: - [Delete the live marker](/quest/m1/announce-live-removal.md) removes that - marker because an origin fed by many sessions can't answer "caught up" - honestly. -- The name says what it shows: announcements, as they happen. -- No `ls` alias: it becomes an unknown command, per the no-compat-shim rule. -- No network completion. Shell completion (`rs/moq-cli/src/complete.rs`) - drops the `BROADCAST` completer, which waits on `Live` today and would - otherwise need a timeout, and the catalog rendition completers - (`--video-name`, `--audio-name`), along with whatever exists only to let a - completer dial. Why: completion stays fast and works offline. Local - capture-device completers stay. -- The live view is plain terminal redraw. Prefer a maintained crate if the - redraw grows beyond a few lines. Keep the event-line output byte-for-byte - compatible with today's `--follow` output so scripts only change the - command name. - -Update every invocation: `doc/bin/cli.md`, `doc/bin/inspect.md`, -`doc/bin/relay/http.md`, `doc/concept/moq-lite.md`, demo recipes, and -anything else a repo-wide search for `moq ls` finds. Check them against -`--help`. - -Public API: moq-cli command rename, which is breaking for scripts. Wire: none. diff --git a/rs/moq-cli/Cargo.toml b/rs/moq-cli/Cargo.toml index b3eff382fcf..a74b9ff2d61 100644 --- a/rs/moq-cli/Cargo.toml +++ b/rs/moq-cli/Cargo.toml @@ -113,6 +113,7 @@ reqwest = { workspace = true, features = ["rustls", "json"] } rustls = { version = "0.23", features = ["aws-lc-rs"], default-features = false } serde = { workspace = true } serde_json = { workspace = true } +terminal_size = "0.4.4" tokio = { workspace = true, features = ["full"] } tower-http = { workspace = true } tracing = { workspace = true } @@ -124,11 +125,7 @@ winit = { version = "0.30.13", optional = true } sd-notify = { workspace = true } [dev-dependencies] -# Authors an MSF-only catalog, which is the one shape that tells a completer reading -# the wrong catalog track apart from one reading the right one: `moq-mux`'s producer -# publishes hang and MSF from the same source, so both answer for an ordinary broadcast. -moq-msf = { path = "../moq-msf" } -# `test_relay` stands up a real relay for the `fetch` tests to read through. +# `test_relay` stands up a real relay for the `fetch` and `announced` tests to read through. moq-relay = { path = "../moq-relay", default-features = false, features = ["test-support"] } # Writes the PAT and PMTs, CRCs included, of the two-program input `import ts --program all` splits. mpeg2ts = "0.6.0" @@ -136,6 +133,8 @@ tempfile = { workspace = true } # `test-util` enables `tokio::time::pause` so the TS round-trip test simulates the # exporter drain timeouts instantly instead of waiting on the wall clock. tokio = { workspace = true, features = ["test-util"] } +# Plays the `announced` view's frames on a virtual terminal, scrolling included. +vt100 = "0.16.2" [[bench]] name = "play_buffer" diff --git a/rs/moq-cli/src/announced.rs b/rs/moq-cli/src/announced.rs new file mode 100644 index 00000000000..eafe3508e98 --- /dev/null +++ b/rs/moq-cli/src/announced.rs @@ -0,0 +1,413 @@ +//! `moq announced`: follow the broadcasts announced on a relay, the MoQ +//! counterpart of the relay's HTTP `/announced/`. + +use std::collections::BTreeSet; +use std::io::IsTerminal; + +use anyhow::Context; +use hang::moq_net::{self, announce::Event}; +use tokio::io::{AsyncWrite, AsyncWriteExt}; + +use crate::args::MoqSide; + +/// Follow the broadcasts announced on a relay as they start and end. +#[derive(usage::Args, Clone)] +#[usage(unknown_flags = "error", args_override_self = false)] +pub struct Args { + /// Only follow paths under this prefix. + pub prefix: Option, + + /// Print each start and end as a line of JSON. + #[usage(long)] + pub json: bool, +} + +/// One `--json` line. +#[derive(serde::Serialize)] +struct Line<'a> { + path: &'a str, + active: bool, +} + +/// How each start and end is written. +enum Output { + /// Redraw the announced set in place, for a person watching a terminal. + View(View), + /// `+ path` / `- path`, one line per event. + Lines, + /// One [`Line`] per event. + Json, +} + +/// Follow what is announced under the prefix `args` names, writing to stdout until +/// the session ends. +pub async fn run(moq: MoqSide, args: Args, net: crate::Net) -> anyhow::Result<()> { + let output = match args.json { + true => Output::Json, + false if std::io::stdout().is_terminal() => Output::View(View::default()), + false => Output::Lines, + }; + follow(&moq, args.prefix.as_deref(), &net, output, &mut tokio::io::stdout()).await +} + +async fn follow( + moq: &MoqSide, + prefix: Option<&str>, + net: &crate::Net, + mut output: Output, + out: &mut (impl AsyncWrite + Unpin), +) -> anyhow::Result<()> { + let url = moq + .client + .url + .clone() + .context("`announced` dials a relay: pass --connect ")?; + let prefix = prefix.unwrap_or_default(); + + // Scoped to the prefix, so the session only asks the relay for what is followed. + let pattern = moq_net::Pattern::subtree(prefix).with_context(|| format!("invalid prefix `{prefix}`"))?; + let origin = moq_tokio::origin::spawn() + .scope("", &moq_net::Patterns::from(pattern)) + .with_context(|| format!("failed to scope to `{prefix}`"))?; + + // Subscribe-only: this session reads and never publishes. + let client = net + .client(moq.client.clone())? + .with_subscriber(origin.clone()) + .with_reconnect(false); + let connection = client.connect(url).established().await.context("failed to connect")?; + + let mut announced = origin.consume().announced(); + + loop { + // Checked first: a closing session retracts every route, which is not news. + let event = tokio::select! { + biased; + closed = connection.closed() => { + closed?; + anyhow::bail!("connection closed"); + } + event = announced.next() => event.context("announcements ended")?, + }; + + let (announce, active) = match event { + Event::Start(announce) => (announce, true), + Event::End(announce) => (announce, false), + // An `Update` is a new route for a path already announced, which changes + // nothing shown; nothing else names a path. + _ => continue, + }; + + let path = announce.prefix.as_str(); + let text = match &mut output { + Output::View(view) => match view.apply(path, active, rows()) { + Some(frame) => frame, + None => continue, + }, + Output::Lines => format!("{} {path}\n", if active { '+' } else { '-' }), + Output::Json => format!("{}\n", serde_json::to_string(&Line { path, active })?), + }; + out.write_all(text.as_bytes()).await?; + out.flush().await?; + } +} + +/// The terminal's height, read on each frame so a resize takes effect on the next change. +fn rows() -> usize { + terminal_size::terminal_size().map_or(usize::MAX, |(_, height)| height.0.into()) +} + +/// The announced set, drawn below the cursor and redrawn in place on each change. +#[derive(Default)] +struct View { + announced: BTreeSet, + /// Lines the last frame drew, which the next one moves back over. + drawn: usize, +} + +impl View { + /// Apply one start or end, returning the frame that redraws the set when it changed. + /// + /// The frame fits in `rows`, the terminal's height, because the cursor cannot move + /// back up to a line that scrolled off the top: a longer set ends in a count of + /// the paths left out. Autowrap is off while drawing for the same reason, so a + /// path wider than the terminal is cut off rather than wrapped. + fn apply(&mut self, path: &str, active: bool, rows: usize) -> Option { + let changed = match active { + true => self.announced.insert(path.to_owned()), + false => self.announced.remove(path), + }; + if !changed { + return None; + } + + let mut frame = String::from("\x1b[?7l"); + if self.drawn > 0 { + // To the start of the first line drawn, then erase everything below it. + frame.push_str(&format!("\x1b[{}F\x1b[J", self.drawn)); + } + // The cursor rests on the line below the frame, which takes a row too. + let fits = rows.saturating_sub(1); + let shown = match self.announced.len() > fits { + true => fits.saturating_sub(1), + false => self.announced.len(), + }; + for path in self.announced.iter().take(shown) { + frame.push_str(path); + frame.push('\n'); + } + self.drawn = shown; + let hidden = self.announced.len() - shown; + if hidden > 0 { + frame.push_str(&format!("({hidden} more)\n")); + self.drawn += 1; + } + frame.push_str("\x1b[?7h"); + Some(frame) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::args::{Command, Invocation}; + use crate::test_env::EnvGuard; + use std::time::Duration; + use tokio::io::{AsyncBufReadExt, BufReader, DuplexStream, Lines}; + + const TIMEOUT: Duration = Duration::from_secs(10); + const ENV: &[&str] = &["MOQ_CONNECT", "MOQ_HOP", "MOQ_BROADCAST"]; + + /// A running relay holding `demo/a`, `demo/b`, and `other/c` from one publisher. + struct Fixture { + /// `--connect` and the TLS pin for the relay's generated certificate. + connect: [String; 4], + /// Stops the relay, which ends every session on it. + shutdown: moq_relay::shutdown::Trigger, + /// The published broadcasts by path; dropping one retracts it. + broadcasts: Vec<(&'static str, moq_net::broadcast::Producer)>, + _publisher: moq_tokio::Connection, + } + + impl Fixture { + async fn new() -> Self { + let _ = moq_tokio::crypto::install_default(); + let fixture = moq_relay::test_relay().await.expect("test relay"); + let ready = fixture.relay.ready(); + let shutdown = fixture.relay.shutdown_trigger().clone(); + let relay = fixture.relay.cluster().origin.consume(); + tokio::spawn(fixture.relay.run()); + ready.wait().await.expect("relay ready"); + + let connect = [ + "--connect".to_string(), + fixture.url.to_string(), + "--connect-tls-fingerprint".to_string(), + fixture.fingerprint.clone(), + ]; + + let origin = moq_tokio::origin::spawn(); + let broadcasts = ["demo/a", "demo/b", "other/c"] + .into_iter() + .map(|path| { + let broadcast = origin.create_broadcast(path).expect("broadcast"); + broadcast.announce(Default::default()).expect("announce"); + (path, broadcast) + }) + .collect::>(); + + let (moq, _) = parse(&connect, &[]); + let publisher = net() + .client(moq.client.clone()) + .expect("client") + .with_publisher(origin.consume()) + .with_reconnect(false) + .connect(fixture.url.clone()) + .established() + .await + .expect("publisher connects"); + + // Every broadcast is on the relay before anything follows it. + for (path, _) in &broadcasts { + tokio::time::timeout(TIMEOUT, relay.routed_broadcast(*path)) + .await + .expect("relay routes the broadcast") + .expect("broadcast"); + } + + Self { + connect, + shutdown, + broadcasts, + _publisher: publisher, + } + } + + /// Start `moq announced ` as if piped, returning its lines as + /// they come and the running task. + fn follow( + &self, + args: &[&str], + ) -> ( + Lines>, + tokio::task::JoinHandle>, + ) { + let (moq, args) = parse(&self.connect, args); + let output = match args.json { + true => Output::Json, + false => Output::Lines, + }; + let (mut writer, reader) = tokio::io::duplex(4096); + let task = + tokio::spawn(async move { follow(&moq, args.prefix.as_deref(), &net(), output, &mut writer).await }); + (BufReader::new(reader).lines(), task) + } + + /// Unpublish `path`. + fn retract(&mut self, path: &str) { + self.broadcasts.retain(|(held, _)| *held != path); + } + } + + /// The next line printed. + async fn next(lines: &mut Lines>) -> String { + tokio::time::timeout(TIMEOUT, lines.next_line()) + .await + .expect("a line in time") + .expect("read") + .expect("a line") + } + + /// Parse an `announced` invocation the way `main` does. + fn parse(connect: &[String], args: &[&str]) -> (MoqSide, Args) { + let argv = ["moq"] + .into_iter() + .chain(connect.iter().map(String::as_str)) + .chain(["announced"]) + .chain(args.iter().copied()); + let mut cli = Invocation::try_parse_from(argv).expect("parse"); + cli.dial_only("announced", &[]).expect("only the dial"); + match cli.stages.remove(0) { + Command::Announced(args) => (cli.moq, args), + _ => unreachable!("parsed an announced"), + } + } + + fn net() -> crate::Net { + crate::Net { + quic: Default::default(), + #[cfg(feature = "iroh")] + iroh: None, + } + } + + /// What is already announced as `+` lines, then a `-` when a publisher leaves. + #[tokio::test] + async fn prints_the_replay_then_changes() { + let _env = EnvGuard::clear(ENV); + let mut fixture = Fixture::new().await; + + let (mut lines, task) = fixture.follow(&["demo"]); + let mut replay = vec![next(&mut lines).await, next(&mut lines).await]; + replay.sort(); + assert_eq!(replay, ["+ demo/a", "+ demo/b"]); + + fixture.retract("demo/a"); + assert_eq!(next(&mut lines).await, "- demo/a"); + assert!(!task.is_finished(), "runs until interrupted"); + task.abort(); + } + + /// Each `--json` line is `{"path", "active"}`. + #[tokio::test] + async fn json_lines_parse() { + let _env = EnvGuard::clear(ENV); + let mut fixture = Fixture::new().await; + + let (mut lines, task) = fixture.follow(&["other", "--json"]); + let line: serde_json::Value = serde_json::from_str(&next(&mut lines).await).expect("a JSON line"); + assert_eq!(line, serde_json::json!({"path": "other/c", "active": true})); + fixture.retract("other/c"); + let line: serde_json::Value = serde_json::from_str(&next(&mut lines).await).expect("a JSON line"); + assert_eq!(line, serde_json::json!({"path": "other/c", "active": false})); + task.abort(); + } + + /// It fails once the relay goes away rather than waiting forever. + #[tokio::test] + async fn fails_when_the_session_ends() { + let _env = EnvGuard::clear(ENV); + let fixture = Fixture::new().await; + + let (mut lines, task) = fixture.follow(&["other"]); + assert_eq!(next(&mut lines).await, "+ other/c"); + + fixture.shutdown.start(); + let result = tokio::time::timeout(TIMEOUT, task) + .await + .expect("exits once the session ends") + .expect("task"); + result.expect_err("a lost session is an error"); + } + + /// Each change redraws the whole set over the previous frame; a no-op draws nothing. + #[test] + fn the_view_redraws_in_place() { + let mut view = View::default(); + assert_eq!(view.apply("b", true, 24).unwrap(), "\x1b[?7lb\n\x1b[?7h"); + assert_eq!( + view.apply("a", true, 24).unwrap(), + "\x1b[?7l\x1b[1F\x1b[Ja\nb\n\x1b[?7h" + ); + assert_eq!(view.apply("a", true, 24), None, "already shown"); + assert_eq!(view.apply("c", false, 24), None, "never shown"); + assert_eq!(view.apply("b", false, 24).unwrap(), "\x1b[?7l\x1b[2F\x1b[Ja\n\x1b[?7h"); + assert_eq!(view.apply("a", false, 24).unwrap(), "\x1b[?7l\x1b[1F\x1b[J\x1b[?7h"); + assert_eq!(view.apply("a", true, 24).unwrap(), "\x1b[?7la\n\x1b[?7h"); + } + + /// A set taller than the terminal ends in a count, and stays at the top of the + /// screen through changes and a shrink rather than scrolling away. + #[test] + fn the_view_fits_the_terminal() { + let mut term = vt100::Parser::new(4, 20, 0); + let mut view = View::default(); + // What a tty does to each `\n` on its way to the screen. + let draw = |term: &mut vt100::Parser, frame: String| term.process(frame.replace('\n', "\r\n").as_bytes()); + + for path in ["a", "b", "c", "d", "e", "f"] { + draw(&mut term, view.apply(path, true, 4).unwrap()); + } + assert_eq!(term.screen().contents(), "a\nb\n(4 more)"); + + draw(&mut term, view.apply("a", false, 4).unwrap()); + assert_eq!(term.screen().contents(), "b\nc\n(3 more)"); + + term.screen_mut().set_size(3, 20); + draw(&mut term, view.apply("b", false, 3).unwrap()); + assert_eq!(term.screen().contents(), "c\n(3 more)"); + + for path in ["c", "d"] { + draw(&mut term, view.apply(path, false, 3).unwrap()); + } + assert_eq!(term.screen().contents(), "e\nf"); + } + + /// `announced` follows a prefix, so a `--broadcast` is refused rather than ignored. + #[test] + fn only_connect_is_accepted() { + let _env = EnvGuard::clear(ENV); + let cli = Invocation::try_parse_from(["moq", "--connect", "http://relay", "--broadcast", "demo", "announced"]) + .expect("parse"); + let err = cli.dial_only("announced", &[]).unwrap_err().to_string(); + assert!(err.contains("--broadcast"), "{err}"); + } + + /// The one-shot listing is gone, with no alias or flag left behind for it. + #[test] + fn ls_and_follow_are_unknown() { + let _env = EnvGuard::clear(ENV); + assert!(Invocation::try_parse_from(["moq", "--connect", "http://relay", "ls"]).is_err()); + assert!(Invocation::try_parse_from(["moq", "--connect", "http://relay", "announced", "--follow"]).is_err()); + } +} diff --git a/rs/moq-cli/src/args.rs b/rs/moq-cli/src/args.rs index 34050ace331..ce86586fcca 100644 --- a/rs/moq-cli/src/args.rs +++ b/rs/moq-cli/src/args.rs @@ -2,7 +2,7 @@ //! //! Grammar: `moq [-- ]...`, where a stage is //! ` [endpoint opts]`, plus `moq play` for -//! native playback, `moq ls [prefix]` to list what is live, and +//! native playback, `moq announced [prefix]` to follow what is announced, and //! `moq fetch ` to read one group. //! //! - The MoQ side (`--connect`, the `--listen*` transport binds, `--cluster-lan`, @@ -158,8 +158,8 @@ impl std::error::Error for ParseError {} impl Invocation { /// Parse the process arguments, exiting with Usage's rendered message on error. /// - /// Async because a completion request is answered first, and some completers - /// dial the relay the line already names (see [`crate::complete`]). + /// Async because a completion request is answered first, and the capture + /// completers enumerate devices asynchronously (see [`crate::complete`]). pub async fn parse() -> Self { let args: Vec = std::env::args_os().collect(); // `#[usage(completion)]` installs the `__complete_word__` interception in the @@ -318,7 +318,7 @@ impl Invocation { // Only `import` and `export` share an Origin. The rest own the process: `play` // drives a window on the main thread, `transcode` builds its own Origin, `fetch` - // and `ls` open their own session, and `auth` / `devices` never touch the network at all. + // and `announced` open their own session, and `auth` / `devices` never touch the network at all. if let Some(command) = self.stages.iter().find(|command| !command.is_stageable()) { anyhow::bail!( "`{}` must be the only verb; it can't share a process with another `--` stage", @@ -352,15 +352,6 @@ fn parse_error( ParseError::new(kind, moq_tokio::cli::answer(spec, root, argv, err).message()) } -/// Whether [`MoqSide::from_argv`] lets the environment fill what the words left out. -#[derive(Clone, Copy, Eq, PartialEq)] -pub(crate) enum Environment { - /// Apply the `MOQ_*` variables, as an ordinary parse does. - Read, - /// Read only what the words say, which is what "the user asked for this" means. - Ignore, -} - /// The MoQ attachment: a relay dial, a server listener, a LAN mesh, or any /// combination. /// @@ -537,32 +528,6 @@ impl MoqSide { !(self.auth.public.is_empty() && self.auth.public_subscribe.is_empty() && self.auth.public_publish.is_empty()) } - /// Build a [`MoqSide`] from one chunk of a command line, leniently. - /// - /// Stops at the first thing the grammar cannot take, because the caller is looking - /// at a half-typed line being completed. Whatever was understood before that point - /// is the answer. - pub(crate) fn from_argv(argv: &[&OsStr], environment: Environment) -> Option { - use usage::spec::CommandArgs; - - let mut partial = ::start(); - let mut parser = usage::Parser::new(Cli::command(), argv); - while let Some(event) = parser.next_event() { - match event { - Ok(event) => { - ::apply(&mut partial, &event); - } - Err(_) => break, - } - } - - if environment == Environment::Read { - ::apply_env(&mut partial); - } - ::apply_defaults(&mut partial); - ::build(partial).ok() - } - /// The MoQ-side flags one chunk of a command line typed, each once, in order. /// /// A flattened config's flags sit in this struct's table under the keys that @@ -602,8 +567,8 @@ pub enum Command { /// The released spelling of [`Self::Export`]. #[usage(hide = true)] Subscribe(Export), - /// List the broadcasts live on a relay. - Ls(crate::ls::Args), + /// Follow the broadcasts announced on a relay as they start and end. + Announced(crate::announced::Args), /// Write one group of a track to stdout. Fetch(crate::fetch::Args), /// Play a broadcast in a native window and speaker. @@ -663,7 +628,7 @@ impl Command { match self { Self::Import(_) | Self::Publish(_) => "import", Self::Export(_) | Self::Subscribe(_) => "export", - Self::Ls(_) => "ls", + Self::Announced(_) => "announced", Self::Fetch(_) => "fetch", #[cfg(feature = "play")] Self::Play(_) => "play", @@ -1687,6 +1652,27 @@ mod tests { assert!(err.to_string().contains("subscribe -> export"), "{err}"); } + /// An exported `MOQ_CONNECT` configures a MoQ side but does not ask for one, so a + /// local verb still runs in a shell that exports a relay for its usual publishing. + #[test] + fn the_environment_cannot_ask_for_a_moq_side() { + let url = "https://relay.example.com"; + let _env = crate::test_env::EnvGuard::set(&[("MOQ_CONNECT", url)]); + + let ambient = Invocation::try_parse_from(["moq", "auth", "generate"]).expect("parse"); + assert!( + ambient.moq.client.url.is_some(), + "the resolved side should still pick the variable up" + ); + assert!( + ambient.reject("auth").is_ok(), + "an exported MOQ_CONNECT was treated as a request" + ); + + let typed = Invocation::try_parse_from(["moq", "--connect", url, "auth", "generate"]).expect("parse"); + assert!(typed.reject("auth").is_err(), "a typed --connect stopped being refused"); + } + #[test] fn auth_verb() { let cli = Invocation::try_parse_from(["moq", "auth", "generate", "--algorithm", "ES256"]).unwrap(); diff --git a/rs/moq-cli/src/complete.rs b/rs/moq-cli/src/complete.rs index e4346c81c9f..9ce0390fe89 100644 --- a/rs/moq-cli/src/complete.rs +++ b/rs/moq-cli/src/complete.rs @@ -4,76 +4,61 @@ //! Two halves. The plumbing decides whether a cursor belongs to the root grammar //! or to a `--`-separated stage, because this binary splits argv itself (see //! [`crate::args`]) and Usage's own interception only knows the root. The -//! completers answer the values that are only knowable at the prompt: the capture -//! sources this machine has, and the broadcasts and renditions the relay on the -//! line is carrying. +//! completers answer the capture sources this machine has. +//! +//! Completion is local: nothing here dials a relay or reads a catalog, so a +//! keystroke stays fast and works offline. //! //! A completion is not a command anybody ran, so nothing here reports a failure. -//! An unreachable relay, a refused session, or a budget that runs out all mean -//! "no candidates": a message in the prompt would be worse than a short list. +//! A source that cannot be listed, or a budget that runs out, means "no +//! candidates": a message in the prompt would be worse than a short list. -use std::cell::RefCell; -use std::collections::BTreeSet; -use std::ffi::{OsStr, OsString}; -// Only the capture completers name a future by type; the rest are `async` blocks. +use std::ffi::OsString; +// Only the capture completers name a future by type. #[cfg(feature = "capture")] use std::future::Future; use std::time::Duration; use anyhow::Context; -use hang::moq_net; -use moq_mux::catalog::{CatalogFormat, Stream}; use tokio::time::{Instant, timeout_at}; -use usage::complete::{Candidate, CompleteCtx, CompletionFuture, CompletionOverlay, CompletionRequest, Shell, render}; -use usage::spec::{CommandArgs, ValueEnum}; +#[cfg(feature = "capture")] +use usage::complete::{Candidate, CompleteCtx, CompletionFuture}; +use usage::complete::{CompletionOverlay, CompletionRequest, Shell, render}; -use crate::args::{Cli, Environment, Export, MoqSide, Stage}; -use crate::subscribe::CatalogFormatArg; +use crate::args::{Cli, Stage}; -/// The wall-clock budget one network-backed completer gets, handshake included. +/// The wall-clock budget one capture completer gets. /// -/// A ceiling on the whole exchange rather than a timeout per step: whatever has -/// arrived when it expires is the answer. Tab is pressed between keystrokes, so a -/// completer that outlives the user's patience is a shell that appears to hang, -/// and half a list now beats the whole list later. +/// Tab is pressed between keystrokes, so a completer that outlives the user's +/// patience is a shell that appears to hang, and no list now beats a list later. +#[cfg(feature = "capture")] const BUDGET: Duration = Duration::from_millis(500); /// The ceiling on a whole completion request, whatever it is answering. /// /// A backstop, not the working budget: every completer bounds its own lookup by -/// [`BUDGET`], and this is deliberately looser so that theirs always fires first and -/// their partial answer survives. It exists so a completer added later cannot hang a -/// prompt by forgetting to bound itself, and so work that ignores cancellation -/// (a blocking device enumeration) still cannot hold the answer back. +/// its budget, and this is deliberately looser so that theirs always fires first. +/// It exists so a completer added later cannot hang a prompt by forgetting to bound +/// itself, and so work that ignores cancellation (a blocking device enumeration) +/// still cannot hold the answer back. const CEILING: Duration = Duration::from_millis(1_500); -/// The completers every build has, keyed by the *value* name they answer for. -/// -/// The value name, not the flag: that is what Usage matches an overlay on, and the -/// derive spells it in screaming snake case (`--video-name `), so a key -/// of `video-name` silently never fires. `every_overlay_matches_only_what_it_answers_for` -/// is what keeps this table honest. -/// -/// [`CommandSelector::Any`](usage::spec::CommandSelector::Any) throughout: every one -/// of these names means the same thing wherever it appears, and `--broadcast` -/// deliberately appears both before the verb and on a stage. -static NETWORK: &[CompletionOverlay<'static>] = &[ - CompletionOverlay::async_any("BROADCAST", broadcasts), - CompletionOverlay::async_any("VIDEO_NAME", video_names), - CompletionOverlay::async_any("AUDIO_NAME", audio_names), -]; - -/// The command whose source flags [`CAPTURE`] answers for. +/// The command whose source flags [`OVERLAYS`] answers for. #[cfg(feature = "capture")] const CAPTURE_PATH: &str = "import capture"; -/// The completers for `import capture`'s sources, which only that feature declares. +/// Every completer this build has, keyed by the *value* name it answers for. +/// +/// The value name, not the flag: that is what Usage matches an overlay on, and the +/// derive spells it in screaming snake case (`--camera `). +/// `every_overlay_matches_its_command` keeps this table honest. /// -/// Scoped to the one command, unlike [`NETWORK`]: these names are generic enough to -/// collide. `export hls --window ` is a playlist window, and an `Any` -/// overlay answered it with this machine's macOS window ids. +/// Scoped to `import capture`, the one command that declares these sources: the +/// names are generic enough to collide. `export hls --window ` is a +/// playlist window, and an unscoped overlay answered it with this machine's macOS +/// window ids. #[cfg(feature = "capture")] -static CAPTURE: &[CompletionOverlay<'static>] = &[ +static OVERLAYS: &[CompletionOverlay<'static>] = &[ CompletionOverlay::asynchronous(CAPTURE_PATH, "CAMERA", cameras), CompletionOverlay::asynchronous(CAPTURE_PATH, "DISPLAY", displays), CompletionOverlay::asynchronous(CAPTURE_PATH, "WINDOW", windows), @@ -81,49 +66,9 @@ static CAPTURE: &[CompletionOverlay<'static>] = &[ CompletionOverlay::asynchronous(CAPTURE_PATH, "MICROPHONE", microphones), ]; -/// See [`CAPTURE`]. +/// See [`OVERLAYS`]. #[cfg(not(feature = "capture"))] -static CAPTURE: &[CompletionOverlay<'static>] = &[]; - -/// Every completer this build has. One allocation on the completion path only. -fn overlays() -> Vec> { - NETWORK.iter().chain(CAPTURE).copied().collect() -} - -thread_local! { - /// The process-wide MoQ flags of the line being completed. - /// - /// A completer is a bare `fn` in a table Usage owns, so it cannot capture the - /// request; and a cursor past a `--` is answered against the stage grammar, - /// whose chunk has no `--connect` in it by construction. Parsing the globals - /// once, here, is what lets a completer in either grammar reach them. - /// - /// Thread-local rather than a process global because this is per-request state: - /// [`answer`]'s future is `!Send` (Usage's completion futures are), so it can - /// only ever be driven on the thread that started it. - static GLOBALS: RefCell> = const { RefCell::new(None) }; -} - -/// Holds [`GLOBALS`] for one request and clears it on the way out. -struct Globals; - -impl Globals { - fn set(side: Option) -> Self { - GLOBALS.with_borrow_mut(|slot| *slot = side); - Self - } - - /// The globals this request parsed, or `None` when the line has none yet. - fn get() -> Option { - GLOBALS.with_borrow(Clone::clone) - } -} - -impl Drop for Globals { - fn drop(&mut self) { - GLOBALS.with_borrow_mut(|slot| *slot = None); - } -} +static OVERLAYS: &[CompletionOverlay<'static>] = &[]; /// Answer a shell's completion request, against the grammar the cursor is in. /// @@ -136,9 +81,6 @@ impl Drop for Globals { /// [`Stage`]. pub async fn answer(argv: &[OsString]) -> Option { let request = CompletionRequest::parse(argv)?; - // Dropped at the end of this call, so a second request cannot read the first's. - let _globals = Globals::set(globals(&request)); - let overlays = overlays(); // Everything past the cursor says nothing about the word being completed. let words = request.split.walked(); @@ -152,7 +94,7 @@ pub async fn answer(argv: &[OsString]) -> Option { // Whatever happens below, the shell gets an answer. `render` of an empty result // is a well-formed "no candidates", which is the right thing to say when a // lookup has outlived the keystroke that asked for it. - let answer = timeout_at(Instant::now() + CEILING, complete(&request, staged, &overlays)) + let answer = timeout_at(Instant::now() + CEILING, complete(&request, staged)) .await .unwrap_or_default(); @@ -160,17 +102,13 @@ pub async fn answer(argv: &[OsString]) -> Option { } /// Answer one request against the grammar its cursor is in. -async fn complete<'a>( - request: &CompletionRequest, - staged: Option, - overlays: &'a [CompletionOverlay<'a>], -) -> usage::complete::Completions<'a> { +async fn complete(request: &CompletionRequest, staged: Option) -> usage::complete::Completions<'static> { let words = request.split.walked(); match staged { None => { Cli::app() .completion_app() - .completions(overlays) + .completions(OVERLAYS) .complete_request(request) .await } @@ -184,90 +122,13 @@ async fn complete<'a>( request.split.words = chunk; Stage::app() .completion_app() - .completions(overlays) + .completions(OVERLAYS) .complete_request(&request) .await } } } -/// The process-wide MoQ flags, as far as the words before the cursor go. -/// -/// Read from the first chunk, which is the only one that can carry them, and -/// through the real tables rather than by scanning for `--connect`: what a completer -/// dials has to be what an invocation would have dialed, TLS roots included. -/// -/// Built twice, because the environment is allowed to configure the dial but not to -/// authorize it. `--connect` has an env var (`MOQ_CONNECT`), so a single pass would -/// let an exported one turn a keystroke into a session with a relay the user never -/// typed, and that URL can carry a `?jwt=` credential. The first pass never reads the -/// environment and its `--connect` is the gate; the second is the one that dials, so -/// everything else an invocation would have picked up still applies. -fn globals(request: &CompletionRequest) -> Option { - let chunk = request.split.argv().split(|word| word == "--").next()?; - let argv: Vec<&OsStr> = chunk.iter().map(OsStr::new).collect(); - - let typed = MoqSide::from_argv(&argv, Environment::Ignore)?; - let mut side = MoqSide::from_argv(&argv, Environment::Read)?; - if typed.client.url.is_none() { - side.client.url = None; - } - Some(side) -} - -/// `T`'s own flags, as far as the words the cursor's command was given go. -/// -/// `None` when the cursor is not inside `T`. A stage's own `--broadcast` and -/// `--catalog-format` override the process-wide ones, so a rendition completer has -/// to read the command it was typed in rather than the globals alone. -fn partial(ctx: &CompleteCtx<'_>) -> Option { - let (command, words) = ctx.command_for(T::COMMAND)?; - let argv: Vec<&OsStr> = words.iter().map(OsStr::new).collect(); - - let mut partial = T::start(); - let mut parser = usage::Parser::new(command, &argv); - while let Some(event) = parser.next_event() { - match event { - Ok(event) => { - T::apply(&mut partial, &event); - } - // A line being completed is unfinished by definition, so an error means the - // grammar ran out here; the partial holds what was understood before that. - Err(_) => break, - } - } - Some(partial) -} - -/// One raw partial value as text, or `None` when it was never given. -fn given(value: &Option>) -> Option<&str> { - std::str::from_utf8(value.as_deref()?).ok() -} - -/// One raw partial value read back through its `ValueEnum`. -fn choice(value: &Option>) -> Option { - T::from_choice(given(value)?) -} - -/// The catalog format the command under the cursor named, if it named one. -/// -/// `export` and `play` each declare their own `--catalog-format`, and the cursor is -/// inside exactly one of them. Reading only `export`'s would leave a -/// `play --catalog-format msf` completer subscribing to the Hang track that -/// invocation is never going to read. -fn catalog_format(ctx: &CompleteCtx<'_>, export: Option<&::Partial>) -> Option { - let named = export.and_then(|export| choice::(&export.catalog_format)); - - #[cfg(feature = "play")] - let named = named.or_else(|| { - partial::(ctx).and_then(|play| choice::(&play.catalog_format)) - }); - #[cfg(not(feature = "play"))] - let _ = ctx; - - named.map(Into::into) -} - // ------------------------------------------------------------------ script /// `Usage` adapter for [`Shell`], which is a foreign type and so can't derive @@ -451,175 +312,11 @@ fn microphones(_ctx: CompleteCtx<'_>) -> CompletionFuture<'static> { }) } -// ------------------------------------------------------------------ network - -/// Complete `--broadcast` from what the relay on the line announces. -/// -/// Offered on an `import` as well as an `export`, even though an import is naming a -/// broadcast it is about to publish rather than one that exists: a redundant (1+1) -/// publisher deliberately reuses the name, and seeing what is already there is how -/// you avoid colliding with it by accident. -fn broadcasts(_ctx: CompleteCtx<'_>) -> CompletionFuture<'static> { - Box::pin(async move { - let Some(side) = Globals::get() else { - return Vec::new(); - }; - let deadline = Instant::now() + BUDGET; - let Some((origin, connection)) = dial(&side, deadline).await else { - return Vec::new(); - }; - - // Announce and unannounce arrive as separate updates for the same path, so - // this tracks a set rather than appending: a broadcast that ends while the - // sweep is running is one the user cannot name by the time they press enter. - let mut announced = origin.consume().announced(); - let mut live = BTreeSet::new(); - // The marker says the relay's whole set has arrived; the budget only bounds - // a relay that is slow to send it. - while let Ok(Some(event)) = timeout_at(deadline, announced.next()).await { - // The root broadcast is the connection path itself, which an unset - // `--broadcast` already names; there is no word to insert for it. - match event { - moq_net::announce::Event::Start(announce) | moq_net::announce::Event::Update(announce) => { - if !announce.prefix.is_empty() { - live.insert(announce.prefix.to_string()); - } - } - moq_net::announce::Event::End(announce) => { - live.remove(announce.prefix.as_str()); - } - moq_net::announce::Event::Live => break, - } - } - drop(connection); - - live.into_iter().map(Candidate::new).collect() - }) -} - -/// Complete `--video-name` from the broadcast's catalog. -fn video_names(ctx: CompleteCtx<'_>) -> CompletionFuture<'_> { - Box::pin(async move { - let Some(catalog) = renditions(&ctx).await else { - return Vec::new(); - }; - catalog - .video - .renditions - .iter() - .map(|(name, config)| { - let size = match (config.coded_width, config.coded_height) { - (Some(width), Some(height)) => format!(" {width}x{height}"), - _ => String::new(), - }; - Candidate::described(name.clone(), format!("{}{size}", config.codec)) - }) - .collect() - }) -} - -/// Complete `--audio-name` from the broadcast's catalog. -fn audio_names(ctx: CompleteCtx<'_>) -> CompletionFuture<'_> { - Box::pin(async move { - let Some(catalog) = renditions(&ctx).await else { - return Vec::new(); - }; - catalog - .audio - .renditions - .iter() - .map(|(name, config)| { - Candidate::described( - name.clone(), - format!("{} {} Hz {}ch", config.codec, config.sample_rate, config.channel_count), - ) - }) - .collect() - }) -} - -/// Dial the relay and read one catalog snapshot for the broadcast on the line. -async fn renditions(ctx: &CompleteCtx<'_>) -> Option { - let side = Globals::get()?; - let export = partial::(ctx); - let export = export.as_ref(); - - // A stage's own `--broadcast` wins over the process-wide one, exactly as it does - // for the invocation this line is on its way to becoming. `play` declares no - // `--broadcast`, so there it is the global or nothing. - let path = export - .and_then(|export| given(&export.broadcast)) - .or(side.broadcast.as_deref()) - .unwrap_or_default() - .to_string(); - - let format = catalog_format(ctx, export) - .or_else(|| CatalogFormat::detect(&path)) - .unwrap_or_default(); - - let deadline = Instant::now() + BUDGET; - let (origin, connection) = dial(&side, deadline).await?; - let catalog = timeout_at(deadline, catalog(&origin, &path, format)) - .await - .ok() - .flatten(); - drop(connection); - catalog -} - -/// Subscribe to a broadcast's catalog and return its first snapshot. -async fn catalog( - origin: &moq_net::origin::Producer, - path: &str, - format: CatalogFormat, -) -> Option { - // Wait for a covering route rather than asking on the spot: the announcement is - // still in flight right after connecting, and an immediate request reports a live - // broadcast as unroutable. - let consumer = origin.consume(); - consumer.routed(path).await?; - - let mut stream = moq_mux::Source::new(consumer, path).catalog(format).await.ok()?; - stream.next().await.ok()? -} - -/// Open a throwaway subscribe-only session to the relay `--connect` names. -/// -/// The Hop ID is fresh and random rather than the pinned `--hop`: this -/// session is not the publisher the user is about to start, and a shared id is -/// what tells a relay two sessions carry the same content. -async fn dial(side: &MoqSide, deadline: Instant) -> Option<(moq_net::origin::Producer, moq_tokio::Connection)> { - let url = side.client.url.clone()?; - let origin = moq_tokio::origin::spawn(); - - // Building the client reads the TLS material off disk synchronously, so it goes on - // the blocking pool and under the deadline like everything else: a `--connect-tls-root` - // on a stalled mount (or a FIFO) would otherwise hang the prompt before the first - // timeout is even entered. Dropping the timeout cannot cancel the read, but it does - // let the process answer and exit. - // - // No iroh endpoint: binding one is more setup than a keystroke should pay for, so an - // `iroh://` peer completes nothing rather than completing slowly. - let (connect, quic) = (side.client.clone(), side.quic.clone()); - let client = timeout_at(deadline, tokio::task::spawn_blocking(move || connect.init(quic))) - .await - .ok()? - .ok()? - .ok()? - .with_subscriber(origin.clone()) - .with_reconnect(false); - - let connection = timeout_at(deadline, client.connect(url).established()) - .await - .ok()? - .ok()?; - Some((origin, connection)) -} - #[cfg(test)] mod tests { use super::*; use crate::test_env::EnvGuard; + use usage::spec::ValueEnum; /// Answer a whole line, with the cursor at its end. async fn complete(line: &str) -> Vec { @@ -637,25 +334,6 @@ mod tests { .collect() } - /// A relay serving `origin`, and the `--connect` flags that reach it. - /// - /// Self-signed, so the line has to say `--connect-tls-insecure`. That is also the - /// point: the completer builds its client from the same flags the invocation would - /// have, so a line that can connect completes and one that cannot does not. - fn relay(origin: &moq_net::origin::Producer) -> String { - let _ = moq_tokio::crypto::install_default(); - - let mut config = moq_tokio::listen::Config::default(); - config.bind = Some("127.0.0.1:0".parse().unwrap()); - config.tls.generate = vec!["localhost".to_string()]; - - let server = config.init(Default::default()).expect("failed to bind listener"); - let port = server.local_addr().expect("no local addr").port(); - tokio::spawn(server.serve_publish(origin.consume())); - - format!("--connect moqt://127.0.0.1:{port} --connect-tls-insecure") - } - /// A cursor in a later stage is completed against the stage grammar. /// /// The root spec is the globals plus the first stage, so answering a later chunk @@ -684,32 +362,15 @@ mod tests { assert!(complete("moq import fmp4 --").await.is_empty()); } - /// Every overlay names a value that only the commands it is meant for declare. + /// Every overlay is scoped to a command that declares the value it answers for. /// - /// Two ways this goes wrong, and neither is visible at runtime. A renamed field - /// leaves an overlay matching nothing, and a completer that never runs looks - /// exactly like one that found nothing. A *new* flag that happens to reuse the - /// name captures an unrelated value: `export hls --window ` was answered - /// with this machine's macOS window ids until the capture overlays were scoped. + /// A renamed field leaves an overlay matching nothing, and a completer that never + /// runs looks exactly like one that found nothing. An unscoped overlay captures any + /// flag that happens to reuse the name: `export hls --window ` was + /// answered with this machine's macOS window ids until the capture overlays were + /// scoped. #[test] - fn every_overlay_matches_only_what_it_answers_for() { - // Where each `Any`-scoped value name is allowed to appear. These are the names - // whose meaning really is the same wherever they are written: a broadcast before - // the verb and on a stage are one relay path, and a rendition is a rendition. - // Anything scoped to a command is checked against that command instead. - // - // Built rather than declared, because which commands exist depends on the build: - // `play` is a feature, and the root itself is the empty path. - let renditions = match cfg!(feature = "play") { - true => vec!["export", "play"], - false => vec!["export"], - }; - let shared = [ - ("BROADCAST", vec!["", "import", "export"]), - ("VIDEO_NAME", renditions.clone()), - ("AUDIO_NAME", renditions), - ]; - + fn every_overlay_matches_its_command() { /// Every command path that declares a value by this name. fn declaring(command: &usage::spec::CommandMeta<'_>, value: &str, at: &str, found: &mut Vec) { if command.hide { @@ -735,96 +396,54 @@ mod tests { } } - for overlay in overlays() { + for overlay in OVERLAYS { + let usage::spec::CommandSelector::Path(path) = overlay.command else { + panic!( + "`{}` is answered wherever it appears; scope it to its command", + overlay.value + ); + }; let mut found = Vec::new(); declaring(Cli::spec().root, overlay.value, "", &mut found); assert!( - !found.is_empty(), - "no flag or argument takes a value named `{}`, so its completer never runs", - overlay.value - ); - - // A scoped overlay only fires on its own command, so a name reused elsewhere - // is harmless; an `Any` one answers everywhere the name appears. - let Some((_, allowed)) = shared.iter().find(|(name, _)| *name == overlay.value) else { - continue; - }; - found.sort(); - let mut allowed: Vec = allowed.iter().map(|path| path.to_string()).collect(); - allowed.sort(); - assert_eq!( - found, allowed, - "`{}` is answered everywhere it appears, and the set of commands declaring it changed", + found.iter().any(|at| at == path), + "`{path}` takes no value named `{}`, so its completer never runs", overlay.value ); } } - /// The environment may configure a MoQ side, but it may not ask for one. + /// Completion never dials, even with a reachable relay on the line. /// - /// Two consumers of that distinction, tested together because both need the - /// variable set and this module is where every test holds the lock for it. - /// Completion must not turn a keystroke into a session with a relay the user never - /// typed (that URL can carry a `?jwt=`), and a local verb must not refuse to run - /// because the shell exports a relay for the publishing it usually does. + /// A keystroke must stay fast and work offline, and a dial is neither. The relay + /// announces a broadcast, so a completer that still dialed would name it. #[tokio::test] - async fn the_environment_cannot_ask_for_a_moq_side() { + async fn completion_never_dials() { + let _env = EnvGuard::clear(&["MOQ_CONNECT"]); + let _ = moq_tokio::crypto::install_default(); + let origin = moq_tokio::origin::spawn(); let _alpha = origin.create_broadcast("alpha").expect("alpha"); _alpha.announce(Default::default()).expect("alpha"); - let connect = relay(&origin); - - // The same reachable relay, named only by the environment. - let url = connect.split_whitespace().nth(1).expect("a --connect url").to_string(); - let _env = EnvGuard::set(&[("MOQ_CONNECT", &url)]); - - assert!( - complete("moq --connect-tls-insecure --broadcast ").await.is_empty(), - "MOQ_CONNECT authorized a dial the line never asked for" - ); - - // The same relay named on the line still completes, so the gate is the URL's - // source and not the dial itself. - assert_eq!( - complete(&format!("moq {connect} --broadcast ")).await, - ["alpha"], - "a typed --connect stopped working" - ); - - // The other reader of the typed view: `moq auth` / `devices` / `completion` - // refuse a MoQ side, and an exported variable is not one being asked for. - let ambient = crate::args::Invocation::try_parse_from(["moq", "auth", "generate"]).expect("parse"); - assert!( - ambient.moq.client.url.is_some(), - "the resolved side should still pick the variable up" - ); - assert!( - ambient.reject("auth").is_ok(), - "an exported MOQ_CONNECT was treated as a request" - ); - let typed = - crate::args::Invocation::try_parse_from(["moq", "--connect", &url, "auth", "generate"]).expect("parse"); - assert!(typed.reject("auth").is_err(), "a typed --connect stopped being refused"); - } + let mut config = moq_tokio::listen::Config::default(); + config.bind = Some("127.0.0.1:0".parse().unwrap()); + config.tls.generate = vec!["localhost".to_string()]; + let server = config.init(Default::default()).expect("failed to bind listener"); + let port = server.local_addr().expect("no local addr").port(); + tokio::spawn(server.serve_publish(origin.consume())); + let connect = format!("--connect moqt://127.0.0.1:{port} --connect-tls-insecure"); - /// A completer that needs the network answers nothing when the line names no relay. - /// - /// The gate is the whole reason tab-completion may dial at all: without a - /// `--connect` on the line there is nothing to ask, and a keystroke must not open a - /// connection the user did not name. An overlay that runs and finds nothing still - /// suppresses the shell's path fallback, so an empty answer here is also evidence - /// that the completer fired at all. - #[tokio::test] - async fn no_relay_on_the_line_means_no_dial() { - let _env = EnvGuard::clear(&["MOQ_CONNECT"]); for line in [ - "moq --broadcast ", - "moq export --broadcast ", - "moq export --video-name ", - "moq export --audio-name ", + format!("moq {connect} --broadcast "), + format!("moq {connect} import fmp4 -- export --broadcast "), + format!("moq {connect} --broadcast alpha export --video-name "), ] { - assert!(complete(line).await.is_empty(), "{line:?} completed without a relay"); + let found = complete(&line).await; + assert!( + !found.iter().any(|candidate| candidate == "alpha"), + "{line:?} read the relay: {found:?}" + ); } } @@ -856,118 +475,4 @@ mod tests { ); assert!(script.contains("__complete_word__"), "the script asks nothing"); } - - /// `--broadcast` is answered from what the relay on the line announces. - #[tokio::test] - async fn a_relay_on_the_line_answers_broadcast() { - let _env = EnvGuard::clear(&["MOQ_CONNECT"]); - let origin = moq_tokio::origin::spawn(); - let _alpha = origin.create_broadcast("alpha").expect("alpha"); - _alpha.announce(Default::default()).expect("alpha"); - let _nested = origin.create_broadcast("room/beta").expect("beta"); - _nested.announce(Default::default()).expect("beta"); - - let connect = relay(&origin); - let found = complete(&format!("moq {connect} --broadcast ")).await; - assert!(found.contains(&"alpha".to_string()), "{found:?}"); - assert!(found.contains(&"room/beta".to_string()), "{found:?}"); - - // A cursor past a `--` is answered against the stage grammar, whose chunk holds - // no `--connect`: the completer still has to reach the relay the line named - // before the separator. - let staged = complete(&format!("moq {connect} import fmp4 -- export --broadcast ")).await; - assert_eq!(staged, found, "a later stage lost the relay the globals named"); - } - - /// `--video-name` and `--audio-name` are answered from the catalog of the - /// broadcast the stage names, which overrides the process-wide one. - #[tokio::test] - async fn a_stage_broadcast_picks_the_catalog_to_read() { - let _env = EnvGuard::clear(&["MOQ_CONNECT"]); - use hang::catalog::{AudioCodec, AudioConfig, H264, VideoConfig}; - - let origin = moq_tokio::origin::spawn(); - - // Two broadcasts with different renditions, so a completer reading the wrong - // one fails loudly instead of matching by luck. - let mut keep = Vec::new(); - for (path, video, audio) in [("wanted", "hd", "stereo"), ("other", "sd", "mono")] { - let mut broadcast = origin.create_broadcast(path).expect("broadcast"); - broadcast.announce(Default::default()).expect("broadcast"); - let mut catalog = - moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).expect("catalog"); - let mut edit = catalog.modify().unwrap(); - edit.video.renditions.insert( - video.to_string(), - VideoConfig::new(H264 { - profile: 0x42, - constraints: 0, - level: 0x1e, - inline: false, - }), - ); - edit.audio - .renditions - .insert(audio.to_string(), AudioConfig::new(AudioCodec::Opus, 48_000, 2)); - edit.commit().expect("publish the catalog"); - keep.push((broadcast, catalog)); - } - - // The global names `other`; the stage overrides it, exactly as the invocation - // this line is on its way to becoming would. - let connect = relay(&origin); - let line = format!("moq {connect} --broadcast other export --broadcast wanted"); - - assert_eq!(complete(&format!("{line} --video-name ")).await, ["hd"]); - assert_eq!(complete(&format!("{line} --audio-name ")).await, ["stereo"]); - } - - /// The `--catalog-format` on the line decides which catalog track is read. - /// - /// `export` and `play` each declare their own, and reading only `export`'s left a - /// `play --catalog-format msf` completer subscribing to a Hang track that - /// invocation is never going to read. The broadcast here publishes MSF and nothing - /// else, which is the one shape that tells the two apart: `moq-mux`'s catalog - /// producer emits hang and MSF from the same source, so an ordinary broadcast - /// answers either way and hides the bug. - #[tokio::test] - async fn the_catalog_format_on_the_line_is_honored() { - let _env = EnvGuard::clear(&["MOQ_CONNECT"]); - let origin = moq_tokio::origin::spawn(); - let broadcast = origin.create_broadcast("room").expect("broadcast"); - broadcast.announce(Default::default()).expect("broadcast"); - - let track = broadcast - .create_track(moq_msf::DEFAULT_NAME, moq_net::track::Info::default()) - .expect("msf track"); - let mut msf = moq_msf::Track::new("hd", moq_msf::Packaging::Loc); - msf.role = Some(moq_msf::Role::Video); - // A video track without one is a hard error in the MSF reader, not a skip. - msf.codec = Some("avc1.42001e".to_string()); - let catalog = moq_msf::Catalog::new(vec![msf]).to_json().expect("msf json"); - let mut group = track.append_group().expect("group"); - group.write_frame(moq_net::Timestamp::now(), catalog).expect("frame"); - - let connect = relay(&origin); - let line = format!("moq {connect} --broadcast room"); - - // Nothing publishes a Hang catalog here, so the default finds no renditions. - assert!(complete(&format!("{line} export --video-name ")).await.is_empty()); - assert_eq!( - complete(&format!("{line} export --catalog-format msf --video-name ")).await, - ["hd"], - "export ignored its own --catalog-format" - ); - - // `play` declares a `--catalog-format` of its own, on a different command. - #[cfg(feature = "play")] - { - assert!(complete(&format!("{line} play --video-name ")).await.is_empty()); - assert_eq!( - complete(&format!("{line} play --catalog-format msf --video-name ")).await, - ["hd"], - "play ignored its own --catalog-format" - ); - } - } } diff --git a/rs/moq-cli/src/ls.rs b/rs/moq-cli/src/ls.rs deleted file mode 100644 index 93f3736bc3f..00000000000 --- a/rs/moq-cli/src/ls.rs +++ /dev/null @@ -1,338 +0,0 @@ -//! `moq ls`: list the broadcasts live on a relay, the MoQ counterpart of the -//! relay's HTTP `/announced/`. - -use std::collections::BTreeSet; - -use anyhow::Context; -use hang::moq_net::{self, announce::Event}; -use tokio::io::{AsyncWrite, AsyncWriteExt}; - -use crate::args::MoqSide; - -/// List the broadcasts live on a relay. -#[derive(usage::Args, Clone)] -#[usage(unknown_flags = "error", args_override_self = false)] -pub struct Args { - /// Only list paths under this prefix. - pub prefix: Option, - - /// Keep running, printing `+ path` and `- path` as broadcasts come and go. - #[usage(long)] - pub follow: bool, - - /// Print one JSON object per line instead of the bare paths. - #[usage(long)] - pub json: bool, -} - -/// One `--json` line. -#[derive(serde::Serialize)] -struct Line<'a> { - path: &'a str, - active: bool, -} - -/// List what is live under the prefix `args` names and write it to stdout. -pub async fn run(moq: MoqSide, args: Args, net: crate::Net) -> anyhow::Result<()> { - list(&moq, &args, &net, &mut tokio::io::stdout()).await -} - -async fn list(moq: &MoqSide, args: &Args, net: &crate::Net, out: &mut (impl AsyncWrite + Unpin)) -> anyhow::Result<()> { - let url = moq - .client - .url - .clone() - .context("`ls` dials a relay: pass --connect ")?; - let prefix = args.prefix.as_deref().unwrap_or_default(); - - // Scoped to the prefix, so the session only asks the relay for what is listed. - let pattern = moq_net::Pattern::subtree(prefix).with_context(|| format!("invalid prefix `{prefix}`"))?; - let origin = moq_tokio::origin::spawn() - .scope("", &moq_net::Patterns::from(pattern)) - .with_context(|| format!("failed to scope to `{prefix}`"))?; - - // Subscribe-only: this session reads and never publishes. - let client = net - .client(moq.client.clone())? - .with_subscriber(origin.clone()) - .with_reconnect(false); - let connection = client.connect(url).established().await.context("failed to connect")?; - - // Registered once the session has started, so `Live` waits for the relay's initial set. - let mut announced = origin.consume().announced(); - let mut live = BTreeSet::new(); - - loop { - // Checked first: a closing session retracts every route, which is not news. - let event = tokio::select! { - biased; - closed = connection.closed() => { - closed?; - anyhow::bail!("connection closed"); - } - event = announced.next() => event.context("announcements ended")?, - }; - - let (announce, active) = match event { - Event::Start(announce) => (announce, true), - Event::End(announce) => (announce, false), - // A new route for a path already live changes nothing listed. - Event::Update(_) => continue, - Event::Live if args.follow => continue, - Event::Live => break, - }; - - let path = announce.prefix.as_str(); - if args.follow { - let line = match args.json { - true => json(path, active)?, - false => format!("{} {path}", if active { '+' } else { '-' }), - }; - out.write_all(format!("{line}\n").as_bytes()).await?; - out.flush().await?; - } else if active { - live.insert(path.to_owned()); - } else { - live.remove(path); - } - } - - for path in &live { - let line = match args.json { - true => json(path, true)?, - false => path.clone(), - }; - out.write_all(format!("{line}\n").as_bytes()).await?; - } - out.flush().await?; - Ok(()) -} - -fn json(path: &str, active: bool) -> serde_json::Result { - serde_json::to_string(&Line { path, active }) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::args::{Command, Invocation}; - use crate::test_env::EnvGuard; - use std::time::Duration; - use tokio::io::{AsyncBufReadExt, BufReader, DuplexStream, Lines}; - - const TIMEOUT: Duration = Duration::from_secs(10); - const ENV: &[&str] = &["MOQ_CONNECT", "MOQ_HOP", "MOQ_BROADCAST"]; - - /// A running relay holding `demo/a`, `demo/b`, and `other/c` from one publisher. - struct Fixture { - /// `--connect` and the TLS pin for the relay's generated certificate. - connect: [String; 4], - /// Stops the relay, which ends every session on it. - shutdown: moq_relay::shutdown::Trigger, - /// The published broadcasts by path; dropping one retracts it. - broadcasts: Vec<(&'static str, moq_net::broadcast::Producer)>, - _publisher: moq_tokio::Connection, - } - - impl Fixture { - async fn new() -> Self { - let _ = moq_tokio::crypto::install_default(); - let fixture = moq_relay::test_relay().await.expect("test relay"); - let ready = fixture.relay.ready(); - let shutdown = fixture.relay.shutdown_trigger().clone(); - let relay = fixture.relay.cluster().origin.consume(); - tokio::spawn(fixture.relay.run()); - ready.wait().await.expect("relay ready"); - - let connect = [ - "--connect".to_string(), - fixture.url.to_string(), - "--connect-tls-fingerprint".to_string(), - fixture.fingerprint.clone(), - ]; - - let origin = moq_tokio::origin::spawn(); - let broadcasts = ["demo/a", "demo/b", "other/c"] - .into_iter() - .map(|path| { - let broadcast = origin.create_broadcast(path).expect("broadcast"); - broadcast.announce(Default::default()).expect("announce"); - (path, broadcast) - }) - .collect::>(); - - let (moq, _) = parse(&connect, &[]); - let publisher = net() - .client(moq.client.clone()) - .expect("client") - .with_publisher(origin.consume()) - .with_reconnect(false) - .connect(fixture.url.clone()) - .established() - .await - .expect("publisher connects"); - - // Every broadcast is on the relay before anything lists it. - for (path, _) in &broadcasts { - tokio::time::timeout(TIMEOUT, relay.routed_broadcast(*path)) - .await - .expect("relay routes the broadcast") - .expect("broadcast"); - } - - Self { - connect, - shutdown, - broadcasts, - _publisher: publisher, - } - } - - /// Run `moq ls ` to completion, returning what it wrote. - async fn ls(&self, args: &[&str]) -> String { - let (moq, args) = parse(&self.connect, args); - let mut out = Vec::new(); - tokio::time::timeout(TIMEOUT, list(&moq, &args, &net(), &mut out)) - .await - .expect("ls exits once caught up") - .expect("ls"); - String::from_utf8(out).expect("UTF-8") - } - - /// Start `moq ls --follow `, returning its lines as they come - /// and the running task. - fn follow( - &self, - args: &[&str], - ) -> ( - Lines>, - tokio::task::JoinHandle>, - ) { - let args = ["--follow"].iter().chain(args).copied().collect::>(); - let (moq, args) = parse(&self.connect, &args); - let (mut writer, reader) = tokio::io::duplex(4096); - let task = tokio::spawn(async move { list(&moq, &args, &net(), &mut writer).await }); - (BufReader::new(reader).lines(), task) - } - - /// Unpublish `path`. - fn retract(&mut self, path: &str) { - self.broadcasts.retain(|(held, _)| *held != path); - } - } - - /// The next line `--follow` prints. - async fn next(lines: &mut Lines>) -> String { - tokio::time::timeout(TIMEOUT, lines.next_line()) - .await - .expect("a line in time") - .expect("read") - .expect("a line") - } - - /// Parse an ls invocation the way `main` does. - fn parse(connect: &[String], args: &[&str]) -> (MoqSide, Args) { - let argv = ["moq"] - .into_iter() - .chain(connect.iter().map(String::as_str)) - .chain(["ls"]) - .chain(args.iter().copied()); - let mut cli = Invocation::try_parse_from(argv).expect("parse"); - cli.dial_only("ls", &[]).expect("only the dial"); - match cli.stages.remove(0) { - Command::Ls(args) => (cli.moq, args), - _ => unreachable!("parsed an ls"), - } - } - - fn net() -> crate::Net { - crate::Net { - quic: Default::default(), - #[cfg(feature = "iroh")] - iroh: None, - } - } - - /// Exactly the announced set under the prefix, one path per line. - #[tokio::test] - async fn prints_the_live_set_and_exits() { - let _env = EnvGuard::clear(ENV); - let fixture = Fixture::new().await; - - assert_eq!(fixture.ls(&[]).await, "demo/a\ndemo/b\nother/c\n"); - assert_eq!(fixture.ls(&["demo"]).await, "demo/a\ndemo/b\n"); - assert_eq!(fixture.ls(&["nothing"]).await, ""); - } - - /// The initial replay as `+` lines, then a `-` when a publisher leaves. - #[tokio::test] - async fn follow_prints_the_replay_then_changes() { - let _env = EnvGuard::clear(ENV); - let mut fixture = Fixture::new().await; - - let (mut lines, task) = fixture.follow(&["demo"]); - let mut replay = vec![next(&mut lines).await, next(&mut lines).await]; - replay.sort(); - assert_eq!(replay, ["+ demo/a", "+ demo/b"]); - - fixture.retract("demo/a"); - assert_eq!(next(&mut lines).await, "- demo/a"); - assert!(!task.is_finished(), "follow runs until interrupted"); - task.abort(); - } - - /// Each `--json` line is `{"path", "active"}`, in either mode. - #[tokio::test] - async fn json_lines_parse() { - let _env = EnvGuard::clear(ENV); - let mut fixture = Fixture::new().await; - - let lines = fixture.ls(&["demo", "--json"]).await; - let lines: Vec = lines - .lines() - .map(|line| serde_json::from_str(line).expect("a JSON line")) - .collect(); - assert_eq!( - lines, - [ - serde_json::json!({"path": "demo/a", "active": true}), - serde_json::json!({"path": "demo/b", "active": true}), - ] - ); - - let (mut lines, task) = fixture.follow(&["other", "--json"]); - let line: serde_json::Value = serde_json::from_str(&next(&mut lines).await).expect("a JSON line"); - assert_eq!(line, serde_json::json!({"path": "other/c", "active": true})); - fixture.retract("other/c"); - let line: serde_json::Value = serde_json::from_str(&next(&mut lines).await).expect("a JSON line"); - assert_eq!(line, serde_json::json!({"path": "other/c", "active": false})); - task.abort(); - } - - /// `--follow` fails once the relay goes away rather than waiting forever. - #[tokio::test] - async fn follow_fails_when_the_session_ends() { - let _env = EnvGuard::clear(ENV); - let fixture = Fixture::new().await; - - let (mut lines, task) = fixture.follow(&["other"]); - assert_eq!(next(&mut lines).await, "+ other/c"); - - fixture.shutdown.start(); - let result = tokio::time::timeout(TIMEOUT, task) - .await - .expect("follow exits") - .expect("task"); - result.expect_err("a lost session is an error"); - } - - /// `ls` lists a prefix, so a `--broadcast` is refused rather than ignored. - #[test] - fn only_connect_is_accepted() { - let _env = EnvGuard::clear(ENV); - let cli = Invocation::try_parse_from(["moq", "--connect", "http://relay", "--broadcast", "demo", "ls"]) - .expect("parse"); - let err = cli.dial_only("ls", &[]).unwrap_err().to_string(); - assert!(err.contains("--broadcast"), "{err}"); - } -} diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index a4394934b87..1044b44aa62 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -4,6 +4,7 @@ //! grammar; this module orchestrates the shared Origin and spawns the MoQ side //! plus every stage's endpoint. +mod announced; mod args; mod auth; mod complete; @@ -12,7 +13,6 @@ mod devices; mod duration; mod fetch; mod hls; -mod ls; mod moq; mod play; mod publish; @@ -318,12 +318,12 @@ async fn main() -> anyhow::Result<()> { } } - // `fetch` and `ls` only dial, so an ambient listener or cluster setting they + // `fetch` and `announced` only dial, so an ambient listener or cluster setting they // never use is not validated either. if let [Command::Fetch(_)] = stages.as_slice() { cli.dial_only("fetch", &["--broadcast"])?; - } else if let [Command::Ls(_)] = stages.as_slice() { - cli.dial_only("ls", &[])?; + } else if let [Command::Announced(_)] = stages.as_slice() { + cli.dial_only("announced", &[])?; } else { cli.moq.validate()?; } @@ -345,7 +345,7 @@ async fn main() -> anyhow::Result<()> { if stages.len() == 1 && !stages[0].is_stageable() { match stages.remove(0) { Command::Fetch(args) => return fetch::run(cli.moq, args, net).await, - Command::Ls(args) => return ls::run(cli.moq, args, net).await, + Command::Announced(args) => return announced::run(cli.moq, args, net).await, #[cfg(feature = "play")] Command::Play(args) => return run_play(cli.moq, args, net).await, #[cfg(feature = "transcode")]