-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_stream_manager_fixes.ts
More file actions
208 lines (166 loc) · 5.85 KB
/
test_stream_manager_fixes.ts
File metadata and controls
208 lines (166 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { StreamManager, StreamState, StreamFailureType } from './src/server/stream_manager.ts';
import { StreamInstance } from './src/types/stream_instance.js';
import { Config } from './src/types/stream.js';
import { TwitchService } from './src/server/services/twitch.js';
import { HolodexService } from './src/server/services/holodex.js';
import { PlayerService } from './src/server/services/player.js';
import { logger } from './src/server/services/logger.js';
// Mock services for testing
class MockTwitchService extends TwitchService {
constructor() {
super('', '', []);
}
async getStreams() {
return [];
}
async getVTuberStreams() {
return [];
}
async getJapaneseStreams() {
return [];
}
}
class MockHolodexService extends HolodexService {
constructor(apiKey: string, filters: any[], favoriteChannelIds: any[]) {
super(apiKey, filters, favoriteChannelIds);
}
async getLiveStreams() {
return [];
}
}
class MockPlayerService extends PlayerService {
constructor(config: Config) {
super(config);
}
async startStream() {
return { success: true, screen: 1, message: 'Stream started' };
}
async stopStream() {
return true;
}
isStreamHealthy() {
return true;
}
getStartupCooldown() {
return 0;
}
onStreamEnd(callback: (data: any) => void) {
// Mock event listener
}
onStreamError(callback: (data: any) => void) {
// Mock event listener
}
onStreamOutput(callback: (data: any) => void) {
// Mock event listener
}
sendCommandToScreen(screen: number, command: string) {
// Mock implementation
}
sendCommandToAll(command: string) {
// Mock implementation
}
getActiveStreams() {
return [];
}
disableScreen(screen: number) {
// Mock implementation
}
enableScreen(screen: number) {
// Mock implementation
}
handleLuaMessage(screen: number, type: string, data: Record<string, unknown>) {
// Mock implementation
}
}
// Create a mock config
const mockConfig: Config = {
streams: [],
organizations: [],
favoriteChannels: {
holodex: {},
twitch: {},
youtube: {}
},
holodex: {
apiKey: ''
},
twitch: {
clientId: '',
clientSecret: '',
streamersFile: ''
},
filters: {
filters: []
},
player: {
preferStreamlink: false,
defaultQuality: 'best',
defaultVolume: 50,
windowMaximized: false,
maxStreams: 10,
autoStart: false,
screens: []
},
mpv: {}
};
// Test the fixes
async function testStreamManagerFixes() {
console.log('Testing StreamManager fixes...');
const twitchService = new MockTwitchService();
const holodexService = new MockHolodexService('', [], []);
const playerService = new MockPlayerService(mockConfig);
const streamManager = new StreamManager(
mockConfig,
holodexService,
twitchService,
playerService
);
// Test 1: Verify that stuck starting timer is properly set up and cancelled
console.log('\n1. Testing stuck starting timer setup and cancellation...');
// Simulate transitioning to STARTING state
await streamManager['setScreenState'](1, StreamState.STARTING);
// Check if timer was set up
const hasTimer = streamManager['stuckStartingTimers'].has(1);
console.log(`Timer set up for screen 1: ${hasTimer}`);
// Simulate transitioning to PLAYING state (should cancel timer)
await streamManager['setScreenState'](1, StreamState.PLAYING);
// Check if timer was cancelled
const hasTimerAfterPlay = streamManager['stuckStartingTimers'].has(1);
console.log(`Timer cancelled after PLAYING transition: ${!hasTimerAfterPlay}`);
// Test 2: Verify failure classification
console.log('\n2. Testing failure classification...');
// Test manual stop
const manualStopFailure = streamManager['classifyStreamFailure'](true, 0);
console.log(`Manual stop classified as: ${manualStopFailure}`);
console.log(`Manual stop should be marked as watched: ${streamManager['shouldMarkStreamAsWatched'](manualStopFailure, 0)}`);
// Test short playback (timeout)
const shortPlaybackFailure = streamManager['classifyStreamFailure'](false, 3);
console.log(`Short playback (<5s) classified as: ${shortPlaybackFailure}`);
console.log(`Short playback should be marked as watched: ${streamManager['shouldMarkStreamAsWatched'](shortPlaybackFailure, 3)}`);
// Test longer playback (natural end)
const longPlaybackFailure = streamManager['classifyStreamFailure'](false, 15);
console.log(`Longer playback (>5s) classified as: ${longPlaybackFailure}`);
console.log(`Longer playback should be marked as watched: ${streamManager['shouldMarkStreamAsWatched'](longPlaybackFailure, 15)}`);
// Test 3: Verify stuck starting timer cancellation
console.log('\n3. Testing stuck starting timer cancellation...');
// Set up a timer
streamManager['setupStuckStartingTimer'](2);
console.log(`Timer set up for screen 2: ${streamManager['stuckStartingTimers'].has(2)}`);
// Cancel the timer
streamManager['cancelStuckStartingTimer'](2);
console.log(`Timer cancelled for screen 2: ${!streamManager['stuckStartingTimers'].has(2)}`);
// Test 4: Verify state transitions properly cancel stuck starting timers
console.log('\n4. Testing state transitions cancel stuck starting timers...');
// Set up a timer again
streamManager['setupStuckStartingTimer'](3);
console.log(`Timer set up for screen 3: ${streamManager['stuckStartingTimers'].has(3)}`);
// Transition from STARTING to PLAYING (should cancel timer)
await streamManager['setScreenState'](3, StreamState.STARTING);
await streamManager['setScreenState'](3, StreamState.PLAYING);
console.log(`Timer cancelled after STARTING->PLAYING transition: ${!streamManager['stuckStartingTimers'].has(3)}`);
console.log('\nAll tests completed successfully!');
// Cleanup
await streamManager.cleanup();
}
// Run the tests
testStreamManagerFixes().catch(console.error);