-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use hot marbles for speaker tests #2815
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
23f4b7d
Refactor the speaker detection logic into observeSpeaker and add tests
hughns 7f2a7b0
Extra test cases and clean up
hughns a587956
Make distinctUntilChanged part of the observable itself
hughns 585eb6f
More suggestions from code review
hughns b6e90b2
Use hot marbles for speaker tests
hughns 35b716b
Only feed speaking mocks to observables that ask for IsSpeakingChanged
hughns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
|
||
SPDX-License-Identifier: AGPL-3.0-only | ||
Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
import { describe, test } from "vitest"; | ||
|
||
import { withTestScheduler } from "../utils/test"; | ||
import { observeSpeaker } from "./observeSpeaker"; | ||
|
||
const yesNo = { | ||
y: true, | ||
n: false, | ||
}; | ||
|
||
describe("observeSpeaker", () => { | ||
describe("does not activate", () => { | ||
const expectedOutputMarbles = "n"; | ||
test("starts correctly", () => { | ||
// should default to false when no input is given | ||
const speakingInputMarbles = ""; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
|
||
test("after no speaking", () => { | ||
const speakingInputMarbles = "n"; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
|
||
test("with speaking for 1ms", () => { | ||
const speakingInputMarbles = "y n"; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
|
||
test("with speaking for 999ms", () => { | ||
const speakingInputMarbles = "y 999ms n"; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
|
||
test("with speaking intermittently", () => { | ||
const speakingInputMarbles = | ||
"y 199ms n 199ms y 199ms n 199ms y 199ms n 199ms y 199ms n 199ms y 199ms n 199ms y 199ms n 199ms y 199ms n 199ms y 199ms n"; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
|
||
test("with consecutive speaking then stops speaking", () => { | ||
const speakingInputMarbles = "y y y y y y y y y y n"; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("activates", () => { | ||
test("after 1s", () => { | ||
// this will active after 1s as no `n` follows it: | ||
const speakingInputMarbles = " y"; | ||
const expectedOutputMarbles = "n 999ms y"; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
|
||
test("speaking for 1001ms activates for 60s", () => { | ||
const speakingInputMarbles = " y 1s n "; | ||
const expectedOutputMarbles = "n 999ms y 60s n"; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
|
||
test("speaking for 5s activates for 64s", () => { | ||
const speakingInputMarbles = " y 5s n "; | ||
const expectedOutputMarbles = "n 999ms y 64s n"; | ||
withTestScheduler(({ hot, expectObservable }) => { | ||
expectObservable(observeSpeaker(hot(speakingInputMarbles, yesNo))).toBe( | ||
expectedOutputMarbles, | ||
yesNo, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
|
||
SPDX-License-Identifier: AGPL-3.0-only | ||
Please see LICENSE in the repository root for full details. | ||
*/ | ||
import { | ||
Observable, | ||
audit, | ||
merge, | ||
timer, | ||
filter, | ||
startWith, | ||
distinctUntilChanged, | ||
} from "rxjs"; | ||
|
||
/** | ||
* Require 1 second of continuous speaking to become a speaker, and 60 second of | ||
* continuous silence to stop being considered a speaker | ||
*/ | ||
export function observeSpeaker( | ||
isSpeakingObservable: Observable<boolean>, | ||
): Observable<boolean> { | ||
const distinct = isSpeakingObservable.pipe(distinctUntilChanged()); | ||
|
||
return distinct.pipe( | ||
// Either change to the new value after the timer or re-emit the same value if it toggles back | ||
// (audit will return the latest (toggled back) value) before the timeout. | ||
audit((s) => | ||
merge(timer(s ? 1000 : 60000), distinct.pipe(filter((s1) => s1 !== s))), | ||
), | ||
// Filter the re-emissions (marked as: | ) that happen if we toggle quickly (<1s) from false->true->false|->.. | ||
startWith(false), | ||
distinctUntilChanged(), | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that's what was happening. Nice catch