Files
javis_bot/app/tests/worker_protocol.rs
javis-bot a70ccda37e feat(app): answer registered chat channels in chat
- chatChannels[].answer: wakeWord (default) or everything. Chat uses
  call.chatWakeWords (else the voice wake words) with the shared
  designated-people switches.
- The brain's answer (reply via chat, not spoken) is posted as a Discord
  reply to the message, pinging nobody; failed or empty answers post
  nothing. Bots, channels with sending off and stops are not answered.
- Chat runs beside the voice conversation and never takes its turn.
- Core: send_chat_reply, chat_answer and the ReplyText event; the worker
  client's reply takes via.
- Specs (conversation, discord core) and the roadmap updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 22:36:43 +09:00

151 lines
5.8 KiB
Rust

//! The worker client and playback against the real Python voice worker server with fake engines, so
//! both sides of the protocol are checked together. Needs a Python with the worker's requirements:
//! JARVIS_VOICE_PYTHON=<venv python> cargo test --test worker_protocol
use std::path::Path;
use std::time::Duration;
use jarvis_app::playback::{ClipSink, Playback, PlaybackEvent};
use jarvis_app::worker_client::{WorkerClient, WorkerClientOptions, WorkerEvent};
use jarvis_app::worker_process::free_port;
use serde_json::json;
use tokio::io::{AsyncBufReadExt, BufReader};
const TOKEN: &str = "protocol-test-token";
/// 20 ms of a 48 kHz stereo tone whose amplitude and pitch (x1000 Hz) are `level`.
fn frame(level: f64) -> Vec<i16> {
(0..960)
.flat_map(|i| {
let v = (level * (2.0 * std::f64::consts::PI * level * 1000.0 * i as f64 / 48000.0).sin() * 32767.0) as i16;
[v, v]
})
.collect()
}
#[derive(Default)]
struct Sink(Vec<usize>);
impl ClipSink for Sink {
fn play(&mut self, pcm: Vec<u8>) {
self.0.push(pcm.len());
}
fn stop(&mut self) {}
}
#[tokio::test]
async fn audio_and_tts_round_trip_through_the_python_server() {
let Ok(python) = std::env::var("JARVIS_VOICE_PYTHON") else {
eprintln!("skipped: set JARVIS_VOICE_PYTHON");
return;
};
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let port = free_port().unwrap();
let mut child = tokio::process::Command::new(python)
.arg(root.join("tests/fixtures/fake_engine_worker.py"))
.arg(port.to_string())
.env("PYTHONPATH", root.join("../src"))
.env("JARVIS_VOICE_TOKEN", TOKEN)
.stdout(std::process::Stdio::piped())
.kill_on_drop(true)
.spawn()
.unwrap();
let mut lines = BufReader::new(child.stdout.take().unwrap()).lines();
while let Some(l) = lines.next_line().await.unwrap() {
if l.starts_with("READY") {
break;
}
}
let mut o = WorkerClientOptions::new(format!("ws://127.0.0.1:{port}"), TOKEN);
o.stt = json!({"partials": false, "hotwords": ["자비스"]}).as_object().unwrap().clone();
let (c, mut rx) = WorkerClient::new(o);
c.start();
let ok = c.wait_ready(Duration::from_secs(5)).await.expect("ready");
assert!(ok.stt_ready && ok.tts_ready);
assert_eq!(ok.stt["hotwords"], json!(["자비스"]));
// Two Discord-format speakers; the second leaves mid-utterance and still gets its final.
let (a, b) = ("111111111111111111", "222222222222222222");
for _ in 0..30 {
c.send_audio(a, &frame(0.9));
c.send_audio(b, &frame(0.6));
}
c.speaking_end(a);
c.speaker_leave(b);
let mut finals = std::collections::HashMap::new();
tokio::time::timeout(Duration::from_secs(5), async {
while finals.len() < 2 {
if let Some(WorkerEvent::Final(f)) = rx.recv().await {
finals.insert(f.speaker.clone(), f);
}
}
})
.await
.expect("finals");
assert_eq!((finals[a].text.as_str(), finals[a].end_reason.as_str(), finals[a].utt), ("level 0.9", "speaking_end", 1));
assert_eq!((finals[b].text.as_str(), finals[b].end_reason.as_str()), ("level 0.6", "speaking_end"));
// TTS sentences play in order through Playback; 10 samples per character, stereo, 2 bytes each.
let mut p = Playback::new(c.clone(), Sink::default());
p.say("첫 번째 문장입니다. 두 번째 문장은 조금 더 깁니다.", false);
let end = tokio::time::timeout(Duration::from_secs(5), async {
loop {
match rx.recv().await.unwrap() {
WorkerEvent::TtsAudio { id, seq, pcm } => {
p.audio(&id, seq, pcm);
p.clip_ended();
}
WorkerEvent::TtsDone(d) => p.done(&d),
_ => {}
}
if let Some(PlaybackEvent::End(e)) = p.take_events().into_iter().find(|e| matches!(e, PlaybackEvent::End(_))) {
return e;
}
}
})
.await
.expect("speech end");
assert_eq!((end.played, end.cancelled, end.failed), (2, false, false));
// A spoken brain answer plays under the adopted id, like a TTS request.
let id = p.adopt();
assert!(c.reply(&id, 42, "자비스 안녕", "voice", true));
let end = tokio::time::timeout(Duration::from_secs(5), async {
loop {
match rx.recv().await.unwrap() {
WorkerEvent::TtsAudio { id, seq, pcm } => {
p.audio(&id, seq, pcm);
p.clip_ended();
}
WorkerEvent::TtsDone(d) => p.done(&d),
_ => {}
}
if let Some(PlaybackEvent::End(e)) = p.take_events().into_iter().find(|e| matches!(e, PlaybackEvent::End(_))) {
return e;
}
}
})
.await
.expect("answer end");
assert_eq!((end.id.as_str(), end.played, end.failed), (id.as_str(), 2, false));
// The two judgements come back under their ids.
assert!(c.addressed("q1", 42, "불 켜 줘"));
assert!(c.rank("k1", &[("1".into(), 42, "a".into()), ("2".into(), 43, "b".into())]));
let (mut addressed, mut ranked) = (None, None);
tokio::time::timeout(Duration::from_secs(5), async {
while addressed.is_none() || ranked.is_none() {
match rx.recv().await.unwrap() {
WorkerEvent::Addressed { id, addressed: a } => addressed = Some((id, a)),
WorkerEvent::Ranked { id, order } => ranked = Some((id, order)),
_ => {}
}
}
})
.await
.expect("judgements");
assert_eq!(addressed, Some(("q1".to_string(), true)));
assert_eq!(ranked, Some(("k1".to_string(), vec!["2".to_string(), "1".to_string()])));
c.close();
let _ = child.kill().await;
}