Commit 233bcb1f authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Make Discovery's mic listen continuous instead of a fixed 5-second clip

Press to start, press again to stop - identification is retried
periodically against the growing recording while listening, and
listening stops automatically the instant a match is found (matching
stops immediately to control API cost; it never keeps checking in the
background after success). Also renames the failure message to "We
could not detect this song".
parent ded2404d
{
"title": "Discovery",
"aria.listenIdle": "Listen for what’s playing",
"aria.listening": "Listening, {{countdown}} seconds left",
"status.listening": "Listening…",
"aria.listening": "Listening, {{elapsed}} seconds, tap to stop",
"status.listening": "Listening… tap to stop",
"status.identifying": "Identifying…",
"status.idleHint": "Tap to identify what's playing - listens for 5 seconds",
"status.idleHint": "Tap to start listening - stops automatically once it finds a match",
"fileDrop.label": "Drop a short clip of what's playing to identify it",
"message.detected": "Detected: {{title}} - {{artist}}",
"message.notIdentified": "Could not identify that clip.",
......
{
"title": "کشف",
"aria.listenIdle": "شنیدن آنچه پخش می‌شود",
"aria.listening": "در حال شنیدن، {{countdown}} ثانیه باقی مانده",
"status.listening": "در حال شنیدن…",
"aria.listening": "در حال شنیدن، {{elapsed}} ثانیه، برای توقف ضربه بزن",
"status.listening": "در حال شنیدن… برای توقف ضربه بزن",
"status.identifying": "در حال شناسایی…",
"status.idleHint": "برای شناسایی آنچه پخش می‌شود ضربه بزن - به مدت ۵ ثانیه می‌شنود",
"status.idleHint": "برای شروع شنیدن ضربه بزن - با پیدا شدن آهنگ خودکار متوقف می‌شود",
"fileDrop.label": "یک کلیپ کوتاه از آنچه پخش می‌شود رها کن تا شناسایی شود",
"message.detected": "شناسایی شد: {{title}} - {{artist}}",
"message.notIdentified": "این کلیپ شناسایی نشد.",
......
......@@ -22,12 +22,13 @@ const SONG = {
library_song_id: null,
}
/** Stands in for the real MediaRecorder - jsdom has no audio capture support at all, so this
* is what makes the "record 5 seconds automatically" flow testable without a browser. start()
* fires one synchronous data chunk (a real recorder would stream several over time, but the
* component only cares that *something* arrives before stop()); stop() is driven by the
* component's own countdown, not by the test calling it directly - mirrors how a real 5-second
* auto-stop actually happens. */
/** Stands in for the real MediaRecorder - jsdom has no audio capture support at all, so this is
* what makes the "listen continuously, periodically re-check, stop on match" flow testable
* without a browser. start() and requestData() both fire a synchronous data chunk (a real
* recorder streams several over time and requestData() forces an early flush mid-recording -
* the component only cares that *something* arrives each time); stop() is driven by the
* component itself (either the periodic check finding a match, or the test calling the button
* again), never by a fixed timer the fake owns. */
class FakeMediaRecorder {
static isTypeSupported(type: string) {
return type === 'audio/webm'
......@@ -41,6 +42,9 @@ class FakeMediaRecorder {
start() {
this.ondataavailable?.({ data: new Blob(['fake-audio-bytes']) })
}
requestData() {
this.ondataavailable?.({ data: new Blob(['fake-audio-bytes']) })
}
stop() {
this.onstop?.()
}
......@@ -114,7 +118,7 @@ describe('Discovery', () => {
})
})
it('records 5 seconds automatically, shows a listening state, and submits the clip', async () => {
it('listens continuously, periodically re-checks, and stops automatically once a match is found', async () => {
mockMicSupport()
const listenSpy = vi.spyOn(discoveryApi, 'listen').mockResolvedValue({
status: 'success',
......@@ -127,7 +131,7 @@ describe('Discovery', () => {
const button = await screen.findByLabelText(/Listen for what/)
// Fake timers have to be active *before* the click, since that's what starts the
// component's countdown setInterval - switching to fake timers afterward wouldn't take
// component's elapsed-time setInterval - switching to fake timers afterward wouldn't take
// over a real interval already running. fireEvent (not userEvent) deliberately - userEvent's
// own internal delay handling doesn't play well with fake timers here, and a plain click
// event is all this needs: React calls the onClick handler synchronously either way.
......@@ -137,10 +141,18 @@ describe('Discovery', () => {
fireEvent.click(button)
})
expect(screen.getByLabelText(/Listening, 5 seconds left/)).toBeInTheDocument()
expect(screen.getByLabelText(/Listening, 0 seconds/)).toBeInTheDocument()
// No check yet before the first 5-second mark - still just recording.
await act(async () => {
await vi.advanceTimersByTimeAsync(5000)
await vi.advanceTimersByTimeAsync(4000)
})
expect(listenSpy).not.toHaveBeenCalled()
// The 5-second mark triggers the first periodic check, which this test's mock resolves
// as a match - recording should stop itself rather than waiting for a 6th second.
await act(async () => {
await vi.advanceTimersByTimeAsync(1000)
})
} finally {
vi.useRealTimers()
......@@ -158,6 +170,80 @@ describe('Discovery', () => {
expect(await screen.findByLabelText(/Listen for what/)).toBeInTheDocument()
})
it('keeps listening past a miss and only stops once a later check succeeds', async () => {
mockMicSupport()
const listenSpy = vi
.spyOn(discoveryApi, 'listen')
.mockResolvedValueOnce({ status: 'error', message: 'We could not detect this song' })
.mockResolvedValueOnce({
status: 'success',
detected: { title: 'Found Song', artist: 'Found Artist', confidence: null, source: null },
discovery_id: 42,
library_song_id: null,
})
renderDiscovery()
const button = await screen.findByLabelText(/Listen for what/)
vi.useFakeTimers()
try {
await act(async () => {
fireEvent.click(button)
})
// First check (5s) misses - the UI stays silent and keeps listening, not a disruptive
// "no match" message every cycle.
await act(async () => {
await vi.advanceTimersByTimeAsync(5000)
})
expect(listenSpy).toHaveBeenCalledTimes(1)
expect(screen.getByLabelText(/Listening,/)).toBeInTheDocument()
expect(screen.queryByText(/We could not detect this song/)).not.toBeInTheDocument()
// Second check (10s) finds it.
await act(async () => {
await vi.advanceTimersByTimeAsync(5000)
})
} finally {
vi.useRealTimers()
}
expect(listenSpy).toHaveBeenCalledTimes(2)
await waitFor(() => {
expect(screen.getByText('Detected: Found Song - Found Artist')).toBeInTheDocument()
})
})
it('stops immediately when the listen button is tapped again, and reports a miss', async () => {
mockMicSupport()
const listenSpy = vi.spyOn(discoveryApi, 'listen').mockResolvedValue({ status: 'error', message: 'We could not detect this song' })
renderDiscovery()
const startButton = await screen.findByLabelText(/Listen for what/)
vi.useFakeTimers()
try {
await act(async () => {
fireEvent.click(startButton)
})
await act(async () => {
await vi.advanceTimersByTimeAsync(2000)
})
// Manual stop before any periodic check has even fired (2s < the 5s interval) - this is
// the only identify call that happens, and since it misses, the user needs to actually be
// told (unlike a background miss while still recording).
const stopButton = screen.getByLabelText(/Listening,/)
await act(async () => {
fireEvent.click(stopButton)
})
} finally {
vi.useRealTimers()
}
expect(listenSpy).toHaveBeenCalledTimes(1)
expect(await screen.findByText('We could not detect this song')).toBeInTheDocument()
expect(await screen.findByLabelText(/Listen for what/)).toBeInTheDocument()
})
it('releases the microphone once recording stops', async () => {
const { stopTrack } = mockMicSupport()
vi.spyOn(discoveryApi, 'listen').mockResolvedValue({ status: 'success', detected: { title: '', artist: '', confidence: null, source: null }, discovery_id: 1, library_song_id: null })
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment