stackchain-dashboard/tests/test_voice_issue_capture.py
timmy 3b4bb618dd
All checks were successful
CI / lint (pull_request) Successful in 2m14s
CI / build-release (pull_request) Successful in 7s
CI / browser-journey (pull_request) Successful in 1m10s
CI / release-candidate (pull_request) Has been skipped
feat: add voice-to-draft mobile capture (Closes #929)
2026-08-16 02:02:24 +00:00

201 lines
8.3 KiB
Python

import json
import subprocess
from pathlib import Path
from src.frontend_bundle import build_frontend
VOICE_CAPTURE = Path(__file__).parents[1] / "frontend" / "voice-issue-capture.js"
FRONTEND = VOICE_CAPTURE.parent
def run_node(script: str):
completed = subprocess.run(
["node", "-e", script], capture_output=True, text=True
)
assert completed.returncode == 0, completed.stderr
return json.loads(completed.stdout)
def test_transcript_maps_first_sentence_to_title_and_remainder_to_note_with_field_limits():
script = f"""
const {{mapVoiceTranscript}} = require({json.dumps(str(VOICE_CAPTURE))});
const mapped = mapVoiceTranscript(' Lift outage on level four. Elevator B is trapped. ');
const bounded = mapVoiceTranscript('x'.repeat(300) + '. ' + 'y'.repeat(10100));
process.stdout.write(JSON.stringify({{mapped, titleLength:bounded.title.length, bodyLength:bounded.body.length}}));
"""
assert run_node(script) == {
"mapped": {
"title": "Lift outage on level four.",
"body": "Elevator B is trapped.",
},
"titleLength": 255,
"bodyLength": 10000,
}
def test_explicit_tap_starts_recognition_and_final_transcript_waits_for_review():
script = f"""
const {{createVoiceIssueCapture}} = require({json.dumps(str(VOICE_CAPTURE))});
class Element {{
constructor() {{ this.hidden=false; this.value=''; this.textContent=''; this.listeners={{}}; }}
addEventListener(name, callback) {{ this.listeners[name]=callback; }}
click() {{ this.listeners.click?.({{currentTarget:this}}); }}
dispatchEvent() {{}}
}}
let instance;
class Recognition {{
constructor() {{ instance=this; this.started=0; }}
start() {{ this.started += 1; }}
stop() {{}}
abort() {{}}
}}
const elements = Object.fromEntries(
['root','start','stop','review','transcript','append','replace','status','title','body'].map(key=>[key,new Element()])
);
elements.review.hidden=true;
const controller = createVoiceIssueCapture({{Recognition,elements}});
const before = {{started:Boolean(instance), rootHidden:elements.root.hidden}};
elements.start.click();
instance.onresult({{results:[Object.assign([{{transcript:'Lift outage. Elevator B is trapped.'}}], {{isFinal:true}})]}});
process.stdout.write(JSON.stringify({{
supported:controller.supported, before, started:instance.started,
listening:elements.status.textContent, transcript:elements.transcript.value,
reviewHidden:elements.review.hidden, title:elements.title.value, body:elements.body.value,
appendLabel:elements.append.textContent, replaceHidden:elements.replace.hidden
}}));
"""
assert run_node(script) == {
"supported": True,
"before": {"started": False, "rootHidden": False},
"started": 1,
"listening": "Transcript ready. Review it before using it.",
"transcript": "Lift outage. Elevator B is trapped.",
"reviewHidden": False,
"title": "",
"body": "",
"appendLabel": "Use transcript",
"replaceHidden": True,
}
def test_review_requires_explicit_append_or_replace_when_typed_content_exists():
script = f"""
const {{createVoiceIssueCapture}} = require({json.dumps(str(VOICE_CAPTURE))});
class Element {{
constructor() {{ this.hidden=false; this.value=''; this.textContent=''; this.listeners={{}}; this.events=[]; }}
addEventListener(name, callback) {{ this.listeners[name]=callback; }}
click() {{ this.listeners.click?.({{currentTarget:this}}); }}
dispatchEvent(event) {{ this.events.push(event.type); }}
}}
function setup() {{
let instance;
class Recognition {{
constructor() {{ instance=this; }} start() {{}} stop() {{}} abort() {{}}
}}
const elements=Object.fromEntries(
['root','start','stop','review','transcript','append','replace','status','title','body'].map(key=>[key,new Element()])
);
elements.title.value='Existing title'; elements.body.value='Existing note'; elements.review.hidden=true;
createVoiceIssueCapture({{Recognition,elements, createEvent:name=>({{type:name}})}});
elements.start.click();
instance.onresult({{results:[Object.assign([{{transcript:'Lift outage. Elevator B is trapped.'}}], {{isFinal:true}})]}});
return elements;
}}
const appended=setup(); appended.append.click();
const replaced=setup(); replaced.replace.click();
process.stdout.write(JSON.stringify({{
appended:{{title:appended.title.value,body:appended.body.value,events:[appended.title.events,appended.body.events]}},
replaced:{{title:replaced.title.value,body:replaced.body.value,events:[replaced.title.events,replaced.body.events]}}
}}));
"""
assert run_node(script) == {
"appended": {
"title": "Existing title",
"body": "Existing note\n\nLift outage.\n\nElevator B is trapped.",
"events": [["input"], ["input"]],
},
"replaced": {
"title": "Lift outage.",
"body": "Elevator B is trapped.",
"events": [["input"], ["input"]],
},
}
def test_unsupported_browser_keeps_keyboard_flow_and_exposes_no_voice_control():
script = f"""
const {{createVoiceIssueCapture}} = require({json.dumps(str(VOICE_CAPTURE))});
const element=()=>({{hidden:false,textContent:'',addEventListener(){{}}}});
const elements=Object.fromEntries(
['root','start','stop','review','transcript','append','replace','status','title','body'].map(key=>[key,element()])
);
const controller=createVoiceIssueCapture({{Recognition:null,elements}});
process.stdout.write(JSON.stringify({{
supported:controller.supported, hidden:elements.root.hidden, guidance:elements.status.textContent
}}));
"""
assert run_node(script) == {
"supported": False,
"hidden": True,
"guidance": "Voice capture is unavailable; type the title and note instead.",
}
def test_stop_error_and_sheet_close_release_recognition_without_losing_transcript():
script = f"""
const {{createVoiceIssueCapture}} = require({json.dumps(str(VOICE_CAPTURE))});
class Element {{
constructor() {{ this.hidden=false; this.value=''; this.textContent=''; this.listeners={{}}; }}
addEventListener(name, callback) {{ this.listeners[name]=callback; }}
click() {{ this.listeners.click?.({{currentTarget:this}}); }} dispatchEvent() {{}}
}}
let instance;
class Recognition {{
constructor() {{ instance=this; this.stops=0; this.aborts=0; }}
start() {{}} stop() {{ this.stops+=1; }} abort() {{ this.aborts+=1; }}
}}
const elements=Object.fromEntries(
['root','start','stop','review','transcript','append','replace','status','title','body'].map(key=>[key,new Element()])
);
const controller=createVoiceIssueCapture({{Recognition,elements}});
elements.start.click();
instance.onresult({{results:[Object.assign([{{transcript:'Partial final transcript.'}}], {{isFinal:true}})]}});
elements.stop.click();
const afterStop=elements.status.textContent;
instance.onerror({{error:'not-allowed'}});
const afterError=elements.status.textContent;
controller.cancel();
process.stdout.write(JSON.stringify({{
stops:instance.stops, aborts:instance.aborts, afterStop, afterError,
transcript:elements.transcript.value, reviewHidden:elements.review.hidden
}}));
"""
assert run_node(script) == {
"stops": 1,
"aborts": 1,
"afterStop": "Finishing transcript…",
"afterError": "Microphone permission was denied. Your draft and transcript are unchanged.",
"transcript": "Partial final transcript.",
"reviewHidden": False,
}
def test_mobile_new_sheet_ships_accessible_voice_review_inside_issue_capture_feature():
build = build_frontend(FRONTEND)
html = build.dashboard_html
capture_bundle = build.feature_bundles["issue-capture"].runtime_bytes
dashboard = (FRONTEND / "dashboard.js").read_text()
assert 'id="voice-issue-capture"' in html
assert 'id="start-voice-issue-capture"' in html
assert 'id="stop-voice-issue-capture"' in html
assert 'id="voice-issue-transcript"' in html
assert 'id="voice-issue-status" class="small" aria-live="polite"' in html
assert 'id="append-voice-issue-transcript"' in html
assert 'id="replace-with-voice-issue-transcript"' in html
assert b"function createVoiceIssueCapture" in capture_bundle
assert b"function createVoiceIssueCapture" not in build.runtime_bytes
assert "window.SpeechRecognition || window.webkitSpeechRecognition" in dashboard
assert "voiceIssueCapture.cancel();" in dashboard