Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

test: repository, move initialization logic to repo

Christian Fei82d713e972c9
diff --git a/index.js b/index.js
index 68a2c5b..baef93e 100644
--- a/index.js
+++ b/index.js
@@ -5,16 +5,8 @@ import { createServer } from './lib/server.js'
async function main ({port = 3000, connections = []} = {}) {
- fs.mkdirSync('./data/videos', { recursive: true })
-
const repo = new Repository()
- fs.readdirSync('./data/videos').forEach(file => {
- if (!file.endsWith('.mp4')) return
- const videoId = file.replace('.mp4', '')
- repo.setVideoDownloaded(videoId)
- })
-
createServer({repo, port, connections})
.listen(port, () => {
console.log(`Server running at http://${os.hostname()}:${port} (or http://localhost:${port})`)
diff --git a/lib/repository.js b/lib/repository.js
index 17952ca..7f293c2 100644
--- a/lib/repository.js
+++ b/lib/repository.js
@@ -1,23 +1,39 @@
import fs from 'fs'
export default class Repository {
- constructor() {
+ constructor(basePath = './data') {
this.channels = []
this.videos = []
+ this.basePath = basePath
this.paths = {
- videos: './data/videos.json',
- channels: './data/channels.json',
+ videos: `${this.basePath}/videos.json`,
+ channels: `${this.basePath}/channels.json`,
+ }
+
+ fs.mkdirSync(`${this.basePath}/videos`, { recursive: true })
+ if (!fs.existsSync(this.paths.videos)) {
+ console.log('initializing ', this.paths.videos)
+ fs.writeFileSync(this.paths.videos, '[]')
+ }
+ if (!fs.existsSync(this.paths.channels)) {
+ console.log('initializing ', this.paths.channels)
+ fs.writeFileSync(this.paths.channels, '[]')
}
-
- if (!fs.existsSync(this.paths.videos)) fs.writeFileSync(this.paths.videos, '[]')
- if (!fs.existsSync(this.paths.channels)) fs.writeFileSync(this.paths.channels, '[]')
try {
this.videos = JSON.parse(fs.readFileSync(this.paths.videos))
+ console.log('read videos', this.videos.length)
this.channels = JSON.parse(fs.readFileSync(this.paths.channels))
+ console.log('read channels', this.channels.length)
} catch (err) {
console.error(err)
}
+
+ fs.readdirSync(`${this.basePath}/videos`).forEach(file => {
+ if (!file.endsWith('.mp4')) return
+ const videoId = file.replace('.mp4', '')
+ this.setVideoDownloaded(videoId)
+ })
}
getChannels () { return this.channels }
@@ -56,10 +72,10 @@ export default class Repository {
this.saveVideos()
}
deleteVideo(id) {
- const files = fs.readdirSync('./data/videos')
+ const files = fs.readdirSync(`${this.basePath}/videos`)
files.forEach(file => {
if (file.startsWith(id)) {
- fs.unlinkSync(`./data/videos/${file}`)
+ fs.unlinkSync(`${this.basePath}/videos/${file}`)
}
})
this.videos = this.videos.map(v => {
diff --git a/test/repository.test.js b/test/repository.test.js
new file mode 100644
index 0000000..746f185
--- /dev/null
+++ b/test/repository.test.js
@@ -0,0 +1,89 @@
+import { test } from 'node:test'
+import fs from 'fs'
+import assert from 'assert';
+import Repository from '../lib/repository.js'
+
+let repo
+test.beforeEach(() => {
+ if (fs.existsSync('./test/data')) {
+ fs.rmSync('./test/data', { recursive: true })
+ }
+ repo = new Repository('./test/data')
+})
+
+test('empty repo provisions data folder', () => {
+ assert.ok(repo)
+ assert.equal(repo.paths.videos, './test/data/videos.json')
+ assert.equal(repo.paths.channels, './test/data/channels.json')
+ assert.equal(repo.getVideos().length, 0)
+ assert.ok(fs.existsSync(repo.paths.videos))
+ assert.ok(fs.existsSync(repo.paths.channels))
+})
+
+test('adds channel', () => {
+ repo.addChannel('veritasium')
+ assert.equal(repo.getChannels().length, 1)
+ assert.equal(repo.channelExists('veritasium'), true)
+})
+
+test('gets channels', () => {
+ repo.addChannel('veritasium')
+ const channels = repo.getChannels()
+ assert.equal(channels.length, 1)
+ assert.equal(channels[0].name, 'veritasium')
+})
+
+test('upserts videos', () => {
+ repo.upsertVideos([{id: '12345', channelName: 'tester', title: 'The Code'}, {id: '67890', channelName: 'programmer', title: 'The Error'}])
+ assert.equal(repo.getVideos().length, 2)
+})
+
+test('gets channel videos', () => {
+ repo.upsertVideos([{id: '12345', channelName: 'tester', title: 'The Code'}, {id: '67890', channelName: 'programmer', title: 'The Error'}])
+ const videos = repo.getChannelVideos('tester')
+ assert.equal(videos.length, 1)
+ assert.equal(videos[0].title, 'The Code')
+})
+
+test('gets single video by id', () => {
+ repo.upsertVideos([{id: '12345', channelName: 'tester', title: 'The Code'}, {id: '67890', channelName: 'programmer', title: 'The Error'}])
+ const video = repo.getVideo('12345')
+ assert.equal(video.title, 'The Code')
+})
+
+test('deletes a video by id and sets downloaded true', () => {
+ repo.upsertVideos([{id: '12345', channelName: 'tester', title: 'The Code'}, {id: '67890', channelName: 'programmer', title: 'The Error'}])
+ repo.deleteVideo('12345')
+ assert.equal(repo.getVideos().length, 2)
+ assert.deepEqual(repo.getVideo('12345'), {id: '12345', channelName: 'tester', title: 'The Code', downloaded: false})
+})
+
+test('toggles ignore video', () => {
+ repo.upsertVideos([{id: '12345', channelName: 'tester', title: 'The Code'}, {id: '67890', channelName: 'programmer', title: 'The Error'}])
+ repo.toggleIgnoreVideo('12345')
+ assert.equal(repo.getVideo('12345').ignored, true)
+})
+
+test('marks video as downloaded', () => {
+ repo.upsertVideos([{id: '12345', channelName: 'tester', title: 'The Code'}, {id: '67890', channelName: 'programmer', title: 'The Error'}])
+ repo.setVideoDownloaded('12345')
+ assert.equal(repo.getVideo('12345').downloaded, true)
+})
+
+test('sets video transcript', () => {
+ repo.upsertVideos([{id: '12345', channelName: 'tester', title: 'The Code'}, {id: '67890', channelName: 'programmer', title: 'The Error'}])
+ repo.setVideoTranscript('12345', 'This is the transcript for The Code')
+ assert.equal(repo.getVideo('12345').transcript, 'This is the transcript for The Code')
+})
+
+test('sets video summary', () => {
+ repo.upsertVideos([{id: '12345', channelName: 'tester', title: 'The Code'}, {id: '67890', channelName: 'programmer', title: 'The Error'}])
+ repo.setVideoSummary('12345', 'This is the summary for The Code')
+ assert.equal(repo.getVideo('12345').summary, 'This is the summary for The Code')
+})
+
+test('patches new video with addedAt', () => {
+ const newVideo = {id: '67890', channelName: 'programmer', title: 'The Error'}
+ const patchedVideo = repo.patchVideo(newVideo)
+ assert.equal(patchedVideo.addedAt, Date.now())
+})
\ No newline at end of file