-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathjellyfin.test.ts
272 lines (218 loc) · 12.5 KB
/
jellyfin.test.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { loggerTest } from "@foxxmd/logging";
import { assert, expect } from 'chai';
import EventEmitter from "events";
import { describe, it } from 'mocha';
import { JsonPlayObject, PlayMeta, PlayObject } from "../../../core/Atomic.js";
import JellyfinSource from "../../sources/JellyfinSource.js";
import JellyfinApiSource from "../../sources/JellyfinApiSource.js";
import samplePayload from './playbackProgressSample.json';
import validSession from './validSession.json';
import { JellyApiData } from "../../common/infrastructure/config/source/jellyfin.js";
import { generatePlay } from "../utils/PlayTestUtils.js";
import { fakerJA } from "@faker-js/faker";
import {
// @ts-expect-error weird typings?
SessionInfo,
} from "@jellyfin/sdk/lib/generated-client/index.js";
import { PlayerStateDataMaybePlay } from "../../common/infrastructure/Atomic.js";
const dataAsFixture = (data: any): TestFixture => {
return data as TestFixture;
}
interface TestFixture {
data: any
expected: JsonPlayObject
}
const createJfApi = (data: JellyApiData): JellyfinApiSource => {
const jf = new JellyfinApiSource('Test', {
data,
options: {}
}, { localUrl: new URL('http://test'), configDir: 'test', logger: loggerTest, version: 'test' }, new EventEmitter());
jf.libraries = [{name: 'music', paths: ['/data/allmusic'], collectionType: 'music'}];
return jf;
}
const defaultJfApiCreds = {url: 'http://example.com', user: 'MyUser', apiKey: '1234'};
const validPlayerState: PlayerStateDataMaybePlay = {
platformId: ['1234', 'MyUser'],
play: generatePlay({}, {mediaType: 'Audio', user: 'MyUser', deviceId: '1234'})
}
const playWithMeta = (meta: PlayMeta): PlayerStateDataMaybePlay => {
const {user, deviceId} = meta;
const platformId = validPlayerState.platformId;
return {
...validPlayerState,
platformId: [deviceId ?? platformId[0], user ?? platformId[1]],
play: {
...validPlayerState.play,
meta: {
...validPlayerState.play?.meta,
...meta
}
}
}}// ({...validPlayerState, meta: {...validPlayerState.meta, ...meta}});
const nowPlayingSession = (data: object): SessionInfo => ({...validSession, NowPlayingItem: {...validSession.NowPlayingItem, ...data}});
describe('Jellyfin Legacy Source', function() {
describe('Jellyfin Payload Parsing', function () {
it('Should parse PlayProgress payload as PlayObject', async function () {
const fixture = dataAsFixture(samplePayload[0]);
const play = JellyfinSource.formatPlayObj(fixture.data);
assert.equal(play.data.track, fixture.expected.data.track);
assert.equal(play.meta.mediaType, 'Audio');
});
});
describe('Correctly detects events as valid/invalid', function () {
const jfSource = new JellyfinSource('Test', {data: {}}, {localUrl: new URL('http://test'), configDir: 'test', logger: loggerTest, version: 'test'}, new EventEmitter());
it('Should parse PlayProgress with Audio ItemType as valid event', async function () {
const fixture = dataAsFixture(samplePayload[0]);
const play = JellyfinSource.formatPlayObj(fixture.data);
assert.isTrue(jfSource.isValidEvent(play))
await jfSource.destroy();
});
});
});
describe("Jellyfin API Source", function() {
describe('Parses config allow/block correctly', function () {
it('Should parse users, devices, and libraries, and library types as lowercase from config', async function () {
const jf = createJfApi({
usersAllow: ['MyUser', 'AnotherUser'],
usersBlock: ['SomeUser'],
devicesAllow: ['Web Player'],
devicesBlock: ['Bad Player'],
librariesAllow: ['MuSiCoNe'],
librariesBlock: ['MuSiCbAd'],
additionalAllowedLibraryTypes: ['TVShowS'],
...defaultJfApiCreds});
await jf.buildInitData();
expect(jf.usersAllow).to.be.eql(['myuser', 'anotheruser']);
expect(jf.usersBlock).to.be.eql(['someuser']);
expect(jf.devicesAllow).to.be.eql(['web player']);
expect(jf.devicesBlock).to.be.eql(['bad player']);
expect(jf.librariesAllow).to.be.eql(['musicone']);
expect(jf.librariesBlock).to.be.eql(['musicbad']);
expect(jf.allowedLibraryTypes).to.be.eql(['music','tvshows']);
await jf.destroy();
});
it('Should include authenticating user as allowed when no others are set', async function () {
const jf = createJfApi({...defaultJfApiCreds});
await jf.buildInitData();
expect(jf.usersAllow).to.be.eql(['myuser']);
await jf.destroy();
});
it('Should set allowed users to empty array (allow all) when usersAllow is true', async function () {
const jf = createJfApi({...defaultJfApiCreds, usersAllow: true});
await jf.buildInitData();
expect(jf.usersAllow).to.be.empty;
await jf.destroy();
});
it('Should set allowed users to empty array (allow all) when usersAllow is an array with only one value equal to true', async function () {
const jf = createJfApi({...defaultJfApiCreds, usersAllow: ['true']});
await jf.buildInitData();
expect(jf.usersAllow).to.be.empty;
await jf.destroy();
});
});
describe('Correctly detects activity as valid/invalid', function() {
describe('Filters from Configuration', function() {
it('Should allow activity based on user allow', async function () {
const jf = createJfApi({...defaultJfApiCreds});
await jf.buildInitData();
expect(jf.isActivityValid(playWithMeta({user: 'SomeOtherUser'}), validSession)).to.not.be.true;
expect(jf.isActivityValid(validPlayerState, validSession)).to.be.true;
expect(jf.isActivityValid(playWithMeta({user: 'myuser'}), validSession)).to.be.true;
await jf.destroy();
});
it('Should disallow activity based on user block', async function () {
const jf = createJfApi({...defaultJfApiCreds, usersBlock: ['BadUser']});
await jf.buildInitData();
expect(jf.isActivityValid(playWithMeta({user: 'BadUser'}), validSession)).to.not.be.true;
expect(jf.isActivityValid(validPlayerState, validSession)).to.be.true;
expect(jf.isActivityValid(playWithMeta({user: 'myuser'}), validSession)).to.be.true;
await jf.destroy();
});
it('Should allow activity based on devices allow', async function () {
const jf = createJfApi({...defaultJfApiCreds, devicesAllow: ['WebPlayer']});
await jf.buildInitData();
expect(jf.isActivityValid(validPlayerState, validSession)).to.not.be.true;
expect(jf.isActivityValid(playWithMeta({deviceId: 'WebPlayer'}), validSession)).to.be.true;
await jf.destroy();
});
it('Should disallow activity based on devices block', async function () {
const jf = createJfApi({...defaultJfApiCreds, devicesBlock: ['WebPlayer']});
await jf.buildInitData();
expect(jf.isActivityValid(validPlayerState, validSession)).to.be.true;
expect(jf.isActivityValid(playWithMeta({deviceId: 'WebPlayer'}), validSession)).to.not.be.true;
await jf.destroy();
});
it('Should allow activity based on additional libraries typed allowed', async function () {
const jf = createJfApi({...defaultJfApiCreds, additionalAllowedLibraryTypes: ['musicvideos']});
await jf.buildInitData();
jf.libraries.push({name: 'CoolVideos', paths: ['/data/someOtherFolder'], collectionType: 'musicvideos'});
expect(jf.isActivityValid(validPlayerState, nowPlayingSession({Path: '/data/someOtherFolder/myMusic.mp3'}))).to.be.true;
await jf.destroy();
});
it('Should allow activity based on libraries allow', async function () {
const jf = createJfApi({...defaultJfApiCreds, librariesAllow: ['music']});
await jf.buildInitData();
expect(jf.isActivityValid(validPlayerState, validSession)).to.be.true;
expect(jf.isActivityValid(validPlayerState, nowPlayingSession({Path: '/data/someOtherFolder/myMusic.mp3'}))).to.not.be.true;
await jf.destroy();
});
it('Should allow activity based on libraries allow and override library type restriction', async function () {
const jf = createJfApi({...defaultJfApiCreds, librariesAllow: ['CoolVideos','music']});
await jf.buildInitData();
jf.libraries.push({name: 'CoolVideos', paths: ['/data/someOtherFolder'], collectionType: 'musicvideos'});
expect(jf.isActivityValid(validPlayerState, validSession)).to.be.true;
expect(jf.isActivityValid(validPlayerState, nowPlayingSession({Path: '/data/someOtherFolder/myMusic.mp3'}))).to.be.true;
await jf.destroy();
});
it('Should disallow activity based on libraries block', async function () {
const jf = createJfApi({...defaultJfApiCreds, librariesBlock: ['music']});
await jf.buildInitData();
jf.libraries.push({name: 'CoolMusic', paths: ['/data/someOtherFolder'], collectionType: 'music'});
expect(jf.isActivityValid(validPlayerState, validSession)).to.not.be.true;
expect(jf.isActivityValid(validPlayerState, nowPlayingSession({Path: '/data/someOtherFolder/myMusic.mp3'}))).to.be.true;
await jf.destroy();
});
});
describe('Detection by Session/Media/Library Type', function() {
it('Should allow activity with valid MediaType and valid Library', async function () {
const jf = createJfApi({...defaultJfApiCreds});
await jf.buildInitData();
expect(jf.isActivityValid(validPlayerState, validSession)).to.be.true;
await jf.destroy();
});
it('Should disallow activity with invalid library type', async function () {
const jf = createJfApi({...defaultJfApiCreds});
await jf.buildInitData();
jf.libraries.push({name: 'CoolVideos', paths: ['/data/someOtherFolder'], collectionType: 'musicvideos'});
expect(jf.isActivityValid(validPlayerState, nowPlayingSession({Path: '/data/someOtherFolder/myMusic.mp3'}))).to.not.be.true;
await jf.destroy();
});
it('Should disallow NowPlayingItem that is not valid Type', async function () {
const jf = createJfApi({...defaultJfApiCreds});
await jf.buildInitData();
expect(jf.isActivityValid(validPlayerState, nowPlayingSession({Type: 'Book'}))).to.not.be.true;
await jf.destroy();
});
it('Should disallow Play that is not valid MediaType', async function () {
const jf = createJfApi({...defaultJfApiCreds});
await jf.buildInitData();
expect(jf.isActivityValid(playWithMeta({mediaType: 'Video'}), validSession)).to.not.be.true;
expect(jf.isActivityValid(playWithMeta({mediaType: 'Unknown'}), validSession)).to.not.be.true;
await jf.destroy();
});
it('Should allow Play with unknown mediaType if specified in options', async function () {
const jf = createJfApi({...defaultJfApiCreds});
jf.config.data.allowUnknown = true;
await jf.buildInitData();
expect(jf.isActivityValid(playWithMeta({mediaType: 'Unknown'}), validSession)).to.be.true;
await jf.destroy();
});
it('Should disallow NowPlayingItem that is a theme song (ExtraType)', async function () {
const jf = createJfApi({...defaultJfApiCreds});
await jf.buildInitData();
expect(jf.isActivityValid(validPlayerState, nowPlayingSession({ExtraType: 'ThemeSong'}))).to.not.be.true;
await jf.destroy();
});
});
});
});