File size: 6,750 Bytes
95f4e64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node

const tape = require('tape')
const request = require('supertest')
const AddonClient = require('stremio-addon-client')
const { addonBuilder, serveHTTP, getRouter, publishToCentral } = require('../')

const PORT = 5000

let addonUrl
let addonServer

const manifest = {
	id: 'org.myexampleaddon',
	version: '1.0.0',
	description: 'not so simple',

	name: 'simple example',

	logo: `http://localhost:${PORT}/static/imgs/logo.png`,
	background: `http://localhost:${PORT}/static/imgs/background.jpg`,

	resources: ['stream'],
	types: ['movie'],

	catalogs: [{ type: 'movie', id: 'test' }],
}

tape('try to create an addon with an invalid manifest', function(t) {
	try { new addonBuilder(null) }
	catch(e) {
		t.ok(e.message, 'invalid manifest')
		t.end()
	}
})

tape('try to create an addon with an invalid manifest: linter', function(t) {
	try { new addonBuilder({ name: 'something' }) }
	catch(e) {
		t.equal(e.message, 'manifest.id must be a string')
		t.end()
	}
})

tape('try to create an addon with an unspecified resource', function(t) {
	try { new addonBuilder(manifest).defineMetaHandler(function() { }).getInterface() }
	catch(e) {
		t.equal(e.message, 'manifest.resources does not contain: meta')
		t.end()
	}
})


tape('create an addon and get the router', function(t) {
	// getRouter requires all handlers to be defined
	var addon = new addonBuilder(manifest)
		.defineCatalogHandler(() => Promise.resolve())
		.defineStreamHandler(() => Promise.resolve())
	t.ok(getRouter(addon.getInterface()), 'can get router')
	t.end()
})

tape('create an addon and expose on HTTP with serveHTTP()', function(t) {
	const builder = new addonBuilder(manifest)
		.defineCatalogHandler(() => Promise.resolve({ metas: [] }))
		// NOTE: we're not supposed to mirror back the `args`, but we're doing it for easier testing
		.defineStreamHandler((args) => Promise.resolve({ streams: [], args }))

	serveHTTP(builder.getInterface(), { port: PORT, cacheMaxAge: 3600 }).then(function(h) {
		t.ok(h.url, 'has url')
		t.ok(h.url.endsWith('manifest.json'), 'url ends with manifest.json')

		t.ok(h.server, 'has h.server')

		addonUrl = h.url
		addonServer = h.server

		request(addonServer)
			.get('/manifest.json')
			.expect(200)
			.end((err, res) => {
				t.error(err, 'request error')
				t.error(res.error, 'response error')
				t.equal(res.ok, true, 'has response status 200')
				t.equal(res.status, 200, 'has response status ok')
				t.equal(res.headers['cache-control'], 'max-age=3600, public', 'cache headers are correct')
				t.end()
			})

	})
})

tape('try to serve a directory that does not exist', function (t) {
	var addon = new addonBuilder(manifest)
		.defineCatalogHandler(() => Promise.resolve())
		.defineStreamHandler(() => Promise.resolve())
	try {
		serveHTTP(addon.getInterface(), { static: '/notexist' })
	} catch (e) {
		t.equal(e.message, 'directory to serve does not exist')
		t.end()
	}
})

tape('try to serve a directory', function (t) {
	var addon = new addonBuilder(manifest)
		.defineCatalogHandler(() => Promise.resolve())
		.defineStreamHandler(() => Promise.resolve())

	serveHTTP(addon.getInterface(), { static: '/docs' }).then(function (h) {
		request(h.server)
			.get('/docs/README.md')
			.expect(200)
			.end((err, res) => {
				h.server.close()
				t.error(err, 'request error')
				t.error(res.error, 'response error')
				t.equal(res.ok, true, 'has response status 200')
				t.equal(res.status, 200, 'has response status ok')
				t.equal(res.type, 'text/markdown', 'is a valid markdown document')
				t.end()
			})
	})
})

// Test the homepage of the addon
tape('should return a valid html document', function (t) {
	request(addonServer)
		.get('/')
		.expect(200)
		.end((err, res) => {
			t.error(err, 'request error')
			t.error(res.error, 'response error')
			t.equal(res.ok, true, 'has response status ok')
			t.equal(res.status, 200, 'has response status 200')
			t.notEqual(res.text, undefined, 'is not undefined')
			t.equal(res.type, 'text/html', 'is a valid html document')
			t.end()
		})
})


tape('initialize an addon client for the addon', function(t) {
	AddonClient.detectFromURL(addonUrl)
		.then(function(resp) {
			t.ok(resp.addon, 'has addon')

			t.ok(resp.addon.manifest, 'has manifest')
			// NOTE: this is an important design principle - immutability on the manifest object
			t.deepEquals(resp.addon.manifest, manifest, 'addon.manifest is immutable')

			const addonClient = resp.addon
			return addonClient.get('stream', 'channel', '11')
				.then(function(resp) {
					t.ok(resp.streams, 'has streams')
					t.deepEqual(resp.args, { type: 'channel', id: '11', extra: {}, config: {} }, 'args parsed right')
					return addonClient.get('stream', 'channel', '11', { search: 'foobar' })
				})
				.then(function(resp) {
					t.ok(resp.streams, 'has streams')
					t.deepEqual(resp.args, { type: 'channel', id: '11', extra: { search: 'foobar' }, config: {} }, 'args parsed right')
				})
		})
		.then(() => t.end())
		.catch(function(err) {
			t.error(err, 'error on addonClient')
			t.end()
		})
})

tape('getInterface: define a stream handler on the addon and test it', function(t) {
	const addon = new addonBuilder(manifest)
		.defineCatalogHandler(() => Promise.resolve({ metas: [] }))
		.defineStreamHandler(function(args) {
			t.equals(args.type, 'channel', 'args.type is right')
			t.equals(args.id, '11', 'args.id is right')
			t.deepEquals(args.extra, {}, 'args.extra is empty')
			return Promise.resolve({streams:[]})
		})
	const addonInterface = addon.getInterface()
	t.ok(addonInterface.manifest, 'interface has manifest')
	addonInterface.manifest.name += 'foobar'
	t.equal(addonInterface.manifest.name, manifest.name, 'interface.manifest is immutable')
	addonInterface.get('stream', 'channel', '11')
		.then(r => {
			t.ok(r.streams, 'response has streams')
		})
		.then(() => t.end())
		.catch(function(err) {
			t.error(err, 'error on addonClient stream request')
			t.end()
		})
})

tape('defining the same handler throws', function(t) {
	const addon = new addonBuilder(manifest)
		.defineCatalogHandler(() => Promise.resolve({ metas: [] }))
		.defineStreamHandler(() => Promise.resolve({ streams: [] }))
	try {
		addon.defineStreamHandler(() => Promise.resolve())
	} catch(e) {
		t.ok(e, 'has exception')
		t.equal(e.message, 'handler for stream already defined')
		t.end()
	}
})

// publishToCentral publishes to the API
tape('publishToCentral', function(t) {
	publishToCentral('https://cinemeta.strem.io/manifest.json')
		.then(function(resp) {
			t.equal(resp.success, true, 'can announce')
			t.end()
		})
		.catch(function(err) {
			t.error(err)
			t.end()
		})
})

tape.onFinish(function() {
	// cause the server is still listening
	addonServer.close()
})