@@ -7,57 +7,50 @@ Please see LICENSE files in the repository root for full details.
7
7
8
8
import { type Mocked } from "jest-mock" ;
9
9
import { type MatrixEvent , type Room } from "matrix-js-sdk/src/matrix" ;
10
- import { SimpleObservable } from "matrix-widget-api" ;
11
10
12
11
import { PlaybackQueue } from "../../../src/audio/PlaybackQueue" ;
13
- import { PlaybackState , type Playback } from "../../../src/audio/Playback" ;
14
- import { MockEventEmitter } from "../../test-utils" ;
12
+ import { type Playback , PlaybackState } from "../../../src/audio/Playback" ;
15
13
import { UPDATE_EVENT } from "../../../src/stores/AsyncStore" ;
14
+ import { MockedPlayback } from "./MockedPlayback" ;
16
15
17
16
describe ( "PlaybackQueue" , ( ) => {
18
17
let playbackQueue : PlaybackQueue ;
18
+ let mockRoom : Mocked < Room > ;
19
19
20
20
beforeEach ( ( ) => {
21
- const mockRoom = {
21
+ mockRoom = {
22
22
getMember : jest . fn ( ) ,
23
23
} as unknown as Mocked < Room > ;
24
24
playbackQueue = new PlaybackQueue ( mockRoom ) ;
25
25
} ) ;
26
26
27
- it ( "does not call skipTo on playback if clock advances to 0s" , ( ) => {
27
+ it . each ( [
28
+ [ PlaybackState . Playing , true ] ,
29
+ [ PlaybackState . Paused , true ] ,
30
+ [ PlaybackState . Preparing , false ] ,
31
+ [ PlaybackState . Decoding , false ] ,
32
+ [ PlaybackState . Stopped , false ] ,
33
+ ] ) ( "should save (or not) the clock PlayBackState=%s expected=%s" , ( playbackState , expected ) => {
28
34
const mockEvent = {
29
35
getId : jest . fn ( ) . mockReturnValue ( "$foo:bar" ) ,
30
36
} as unknown as Mocked < MatrixEvent > ;
31
- const mockPlayback = new MockEventEmitter ( {
32
- clockInfo : {
33
- liveData : new SimpleObservable < number [ ] > ( ) ,
34
- } ,
35
- skipTo : jest . fn ( ) ,
36
- } ) as unknown as Mocked < Playback > ;
37
+ const mockPlayback = new MockedPlayback ( playbackState , 0 , 0 ) as unknown as Mocked < Playback > ;
37
38
38
39
// Enqueue
39
40
playbackQueue . unsortedEnqueue ( mockEvent , mockPlayback ) ;
40
41
41
42
// Emit our clockInfo of 0, which will playbackQueue to save the state.
42
- mockPlayback . clockInfo . liveData . update ( [ 0 ] ) ;
43
-
44
- // Fire an update event to say that we have stopped.
45
- // Note that Playback really emits an UPDATE_EVENT whenever state changes, the types are lies.
46
- mockPlayback . emit ( UPDATE_EVENT as any , PlaybackState . Stopped ) ;
43
+ mockPlayback . clockInfo . liveData . update ( [ 1 ] ) ;
47
44
48
- expect ( mockPlayback . skipTo ) . not . toHaveBeenCalled ( ) ;
45
+ // @ts -ignore
46
+ expect ( playbackQueue . clockStates . has ( mockEvent . getId ( ) ! ) ) . toBe ( expected ) ;
49
47
} ) ;
50
48
51
- it ( "does call skipTo on playback if clock advances to 0s " , ( ) => {
49
+ it ( "does call skipTo on playback if clock advances to 1s " , ( ) => {
52
50
const mockEvent = {
53
51
getId : jest . fn ( ) . mockReturnValue ( "$foo:bar" ) ,
54
52
} as unknown as Mocked < MatrixEvent > ;
55
- const mockPlayback = new MockEventEmitter ( {
56
- clockInfo : {
57
- liveData : new SimpleObservable < number [ ] > ( ) ,
58
- } ,
59
- skipTo : jest . fn ( ) ,
60
- } ) as unknown as Mocked < Playback > ;
53
+ const mockPlayback = new MockedPlayback ( PlaybackState . Playing , 0 , 0 ) as unknown as Mocked < Playback > ;
61
54
62
55
// Enqueue
63
56
playbackQueue . unsortedEnqueue ( mockEvent , mockPlayback ) ;
@@ -71,4 +64,24 @@ describe("PlaybackQueue", () => {
71
64
72
65
expect ( mockPlayback . skipTo ) . toHaveBeenCalledWith ( 1 ) ;
73
66
} ) ;
67
+
68
+ it ( "should ignore the nullish clock state when loading" , ( ) => {
69
+ const clockStates = new Map ( [
70
+ [ "a" , 1 ] ,
71
+ [ "b" , null ] ,
72
+ [ "c" , 3 ] ,
73
+ ] ) ;
74
+ localStorage . setItem (
75
+ `mx_voice_message_clocks_${ mockRoom . roomId } ` ,
76
+ JSON . stringify ( Array . from ( clockStates . entries ( ) ) ) ,
77
+ ) ;
78
+ playbackQueue = new PlaybackQueue ( mockRoom ) ;
79
+
80
+ // @ts -ignore
81
+ expect ( playbackQueue . clockStates . has ( "a" ) ) . toBe ( true ) ;
82
+ // @ts -ignore
83
+ expect ( playbackQueue . clockStates . has ( "b" ) ) . toBe ( false ) ;
84
+ // @ts -ignore
85
+ expect ( playbackQueue . clockStates . has ( "c" ) ) . toBe ( true ) ;
86
+ } ) ;
74
87
} ) ;
0 commit comments