A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
test: basic server test get channels
Christian Fei0113011ed107
diff --git a/lib/server.js b/lib/server.jsindex e3825e7..5ec52fe 100644--- a/lib/server.js+++ b/lib/server.js@@ -33,74 +33,78 @@ export function createServer ({repo, connections = [], updateVideos = true} = {}}return http.createServer(async (req, res) => {- const url = new URL(req.url, `http://${req.headers.host}`)-- /* initialize SSE */- if (req.headers.accept && req.headers.accept.indexOf('text/event-stream') >= 0) return handleSSE(res, connections)-- /* api */- if (url.pathname === '/api/channels' && req.method === 'POST')- return addChannelHandler(req, res, repo, connections)- if (url.pathname === '/api/channels' && req.method === 'DELETE')- return deleteChannelHandler(req, res, repo, connections)- if (url.pathname === '/api/channels' && req.method === 'GET')- return getChannelHandler(req, res, repo, connections)- if (url.pathname === '/api/download-video' && req.method === 'POST')- return downloadVideoHandler(req, res, repo, connections)- if (url.pathname === '/api/summarize-video' && req.method === 'POST')- return summarizeVideoHandler(req, res, repo, connections, llmSettings)- if (url.pathname === '/api/ignore-video' && req.method === 'POST')- return ignoreVideoHandler(req, res, repo, connections)- if (url.pathname === '/api/delete-video' && req.method === 'POST')- return deleteVideoHandler(req, res, repo, connections)- if (url.pathname === '/api/videos' && req.method === 'GET')- return allVideosHandler(req, res, repo, connections)- if (url.pathname === '/api/video-quality' && req.method === 'GET')- return getVideoQualityHandler(req, res, repo, connections)- if (url.pathname === '/api/video-quality' && req.method === 'POST')- return setVideoQualityHandler(req, res, repo, connections)- if (url.pathname === '/api/disk-usage' && req.method === 'GET')- return diskUsageHandler(req, res, repo, connections)- if (url.pathname === '/api/reclaim-disk-space' && req.method === 'POST')- return reclaimDiskSpaceHandler(req, res, repo, connections)- if (url.pathname.match(/\/videos\/.*/) && req.method === 'GET')- return watchVideoHandler(req, res, repo)- if (url.pathname.match(/\/captions\/.*/) && req.method === 'GET')- return captionsHandler(req, res, repo)-- /* client */- if (url.pathname === '/main.css')- return fileHandler('client/main.css', 'text/css')(req, res)- if (url.pathname === '/normalize.css')- return fileHandler('client/normalize.css', 'text/css')(req, res)- if (url.pathname === '/main.js')- return fileHandler('client/main.js', 'application/javascript')(req, res)- if (url.pathname === '/lib/store.js')- return fileHandler('client/lib/store.js', 'application/javascript')(req, res)- if (url.pathname === '/lib/router.js')- return fileHandler('client/lib/router.js', 'application/javascript')(req, res)- if (url.pathname === '/lib/utils.js')- return fileHandler('client/lib/utils.js', 'application/javascript')(req, res)- if (url.pathname === '/components/video-element.js')- return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)- if (url.pathname === '/components/add-channel-form.js')- return fileHandler('client/components/add-channel-form.js', 'application/javascript')(req, res)- if (url.pathname === '/components/manage-channels-form.js')- return fileHandler('client/components/manage-channels-form.js', 'application/javascript')(req, res)- if (url.pathname === '/components/video-quality-form.js')- return fileHandler('client/components/video-quality-form.js', 'application/javascript')(req, res)- if (url.pathname === '/components/manage-disk-space-form.js')- return fileHandler('client/components/manage-disk-space-form.js', 'application/javascript')(req, res)- if (url.pathname === '/components/search-videos.js')- return fileHandler('client/components/search-videos.js', 'application/javascript')(req, res)- if (url.pathname === '/components/channels-list.js')- return fileHandler('client/components/channels-list.js', 'application/javascript')(req, res)- if (url.pathname === '/components/empty-state.js')- return fileHandler('client/components/empty-state.js', 'application/javascript')(req, res)- if (url.pathname === '/components/video-filters.js')- return fileHandler('client/components/video-filters.js', 'application/javascript')(req, res)-- return fileHandler('client/index.html', 'text/html')(req, res)+ try {+ const url = new URL(req.url, `http://${req.headers.host}`)++ /* initialize SSE */+ if (req.headers.accept && req.headers.accept.indexOf('text/event-stream') >= 0) return handleSSE(res, connections)++ /* api */+ if (url.pathname === '/api/channels' && req.method === 'GET')+ return getChannelHandler(req, res, repo, connections)+ if (url.pathname === '/api/channels' && req.method === 'POST')+ return addChannelHandler(req, res, repo, connections)+ if (url.pathname === '/api/channels' && req.method === 'DELETE')+ return deleteChannelHandler(req, res, repo, connections)+ if (url.pathname === '/api/download-video' && req.method === 'POST')+ return downloadVideoHandler(req, res, repo, connections)+ if (url.pathname === '/api/summarize-video' && req.method === 'POST')+ return summarizeVideoHandler(req, res, repo, connections, llmSettings)+ if (url.pathname === '/api/ignore-video' && req.method === 'POST')+ return ignoreVideoHandler(req, res, repo, connections)+ if (url.pathname === '/api/delete-video' && req.method === 'POST')+ return deleteVideoHandler(req, res, repo, connections)+ if (url.pathname === '/api/videos' && req.method === 'GET')+ return allVideosHandler(req, res, repo, connections)+ if (url.pathname === '/api/video-quality' && req.method === 'GET')+ return getVideoQualityHandler(req, res, repo, connections)+ if (url.pathname === '/api/video-quality' && req.method === 'POST')+ return setVideoQualityHandler(req, res, repo, connections)+ if (url.pathname === '/api/disk-usage' && req.method === 'GET')+ return diskUsageHandler(req, res, repo, connections)+ if (url.pathname === '/api/reclaim-disk-space' && req.method === 'POST')+ return reclaimDiskSpaceHandler(req, res, repo, connections)+ if (url.pathname.match(/\/videos\/.*/) && req.method === 'GET')+ return watchVideoHandler(req, res, repo)+ if (url.pathname.match(/\/captions\/.*/) && req.method === 'GET')+ return captionsHandler(req, res, repo)++ /* client */+ if (url.pathname === '/main.css')+ return fileHandler('client/main.css', 'text/css')(req, res)+ if (url.pathname === '/normalize.css')+ return fileHandler('client/normalize.css', 'text/css')(req, res)+ if (url.pathname === '/main.js')+ return fileHandler('client/main.js', 'application/javascript')(req, res)+ if (url.pathname === '/lib/store.js')+ return fileHandler('client/lib/store.js', 'application/javascript')(req, res)+ if (url.pathname === '/lib/router.js')+ return fileHandler('client/lib/router.js', 'application/javascript')(req, res)+ if (url.pathname === '/lib/utils.js')+ return fileHandler('client/lib/utils.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/video-element.js')+ return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/add-channel-form.js')+ return fileHandler('client/components/add-channel-form.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/manage-channels-form.js')+ return fileHandler('client/components/manage-channels-form.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/video-quality-form.js')+ return fileHandler('client/components/video-quality-form.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/manage-disk-space-form.js')+ return fileHandler('client/components/manage-disk-space-form.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/search-videos.js')+ return fileHandler('client/components/search-videos.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/channels-list.js')+ return fileHandler('client/components/channels-list.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/empty-state.js')+ return fileHandler('client/components/empty-state.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/video-filters.js')+ return fileHandler('client/components/video-filters.js', 'application/javascript')(req, res)++ return fileHandler('client/index.html', 'text/html')(req, res)+ } catch (error) {+ console.error(error)+ }})}@@ -167,6 +171,7 @@ async function deleteChannelHandler(req, res, repo, connections = []) {}async function getChannelHandler(req, res, repo, connections = []) {+ console.log('getting channels')const channels = repo.getChannels()res.writeHead(200, { 'Content-Type': 'application/json' })return res.end(JSON.stringify(channels))diff --git a/test/server.test.js b/test/server.test.jsnew file mode 100644index 0000000..a34d1ad--- /dev/null+++ b/test/server.test.js@@ -0,0 +1,34 @@+import {test, describe} from 'node:test'+import fs from 'fs'+import assert from 'assert'+import { createServer } from '../lib/server.js'+import Repository from '../lib/repository.js'++if (fs.existsSync('./test/data')) {+ fs.rmSync('./test/data', { recursive: true })+}+const repo = new Repository('./test/data')+test('starts server', async () => {+ const server = createServer({updateVideos: false, repo})+ await new Promise(resolve => server.listen(3001, resolve))+ assert.equal(server.address().port, 3001)+ await new Promise(resolve => server.close(resolve))+})++describe('server', () => {+ let server+ test.before(async () => {+ server = createServer({updateVideos: false, repo})+ await new Promise(resolve => server.listen(3001, resolve))+ }, {timeout: 1000})+ test.after(async () => {+ await new Promise(resolve => server.close(resolve))+ }, {timeout: 1000})++ test('get channels', {timeout: 5000}, async () => {+ const res = await fetch('http://0.0.0.0:3001/api/channels')+ const data = await res.json()+ assert.equal(res.status, 200)+ assert.deepEqual(data, [])+ })+})\ No newline at end of file