Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/backend/drizzle/0001_tearful_the_fallen.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `downloads` ADD `attempts` integer DEFAULT 0 NOT NULL;
195 changes: 195 additions & 0 deletions apps/backend/drizzle/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
{
"version": "6",
"dialect": "sqlite",
"id": "0e170273-951e-4b46-b8be-8d77d6188596",
"prevId": "7511a5bb-12b1-4836-8099-498542b3d20d",
"tables": {
"downloads": {
"name": "downloads",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"torrent_filename": {
"name": "torrent_filename",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"peer_id": {
"name": "peer_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"peer_name": {
"name": "peer_name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"item_id": {
"name": "item_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"filename": {
"name": "filename",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"dest_path": {
"name": "dest_path",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"part_path": {
"name": "part_path",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"release_size": {
"name": "release_size",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"release_json": {
"name": "release_json",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expected_bytes": {
"name": "expected_bytes",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expected_bytes_source": {
"name": "expected_bytes_source",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expected_bytes_mismatch": {
"name": "expected_bytes_mismatch",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
},
"downloaded_bytes": {
"name": "downloaded_bytes",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"attempts": {
"name": "attempts",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"status": {
"name": "status",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"started_at": {
"name": "started_at",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"updated_at": {
"name": "updated_at",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"completed_at": {
"name": "completed_at",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"error": {
"name": "error",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"downloads_status_idx": {
"name": "downloads_status_idx",
"columns": [
"status"
],
"isUnique": false
},
"downloads_updated_at_idx": {
"name": "downloads_updated_at_idx",
"columns": [
"updated_at"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {
"downloads_status_check": {
"name": "downloads_status_check",
"value": "\"downloads\".\"status\" in ('downloading', 'completed', 'failed', 'import_queued')"
},
"downloads_expected_bytes_source_check": {
"name": "downloads_expected_bytes_source_check",
"value": "\"downloads\".\"expected_bytes_source\" is null or \"downloads\".\"expected_bytes_source\" = 'content_length'"
}
}
}
},
"views": {},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
7 changes: 7 additions & 0 deletions apps/backend/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1780589033291,
"tag": "0000_mysterious_vin_gonzales",
"breakpoints": true
},
{
"idx": 1,
"version": "6",
"when": 1780707145431,
"tag": "0001_tearful_the_fallen",
"breakpoints": true
}
]
}
18 changes: 18 additions & 0 deletions apps/backend/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,22 @@ describe('appConfig parsing', () => {
})
expect(result.success).toBe(false)
})

test('defaults the downloads hardening knobs', () => {
const parsed = AppConfig.parse({
downloads: { watchPath: '/w', completedPath: '/c' },
})
expect(parsed.downloads).toMatchObject({
maxConcurrentDownloads: 3,
maxDownloadAttempts: 5,
retryBaseDelayMs: 1000,
retryMaxDelayMs: 60_000,
})
})

test('respects an explicit maxConcurrentDownloads and rejects non-positive values', () => {
const parsed = AppConfig.parse({ downloads: { watchPath: '/w', completedPath: '/c', maxConcurrentDownloads: 8 } })
expect(parsed.downloads?.maxConcurrentDownloads).toBe(8)
expect(AppConfig.safeParse({ downloads: { watchPath: '/w', completedPath: '/c', maxConcurrentDownloads: 0 } }).success).toBe(false)
})
})
61 changes: 61 additions & 0 deletions apps/backend/src/__tests__/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,65 @@ describe('DownloadsRepository', () => {
expect(stale.error).toContain('stale')
handle.close()
})

test('increments attempts and records a resume reset', async () => {
const handle = await openDatabase({ appConfigPath: join(tempDir, 'config.jsonc') })
const repository = new DownloadsRepository(handle.db)
const created = repository.create({
torrentFilename: 'movie.torrent',
peerId: 'peer-1',
peerName: 'Friend Jack',
itemId: 'movie:1',
filename: release.filename,
destPath: join(tempDir, release.filename),
partPath: join(tempDir, `${release.filename}.part`),
releaseSize: release.size,
release,
})

expect(repository.incrementAttempts(created.id)).toBe(1)
expect(repository.incrementAttempts(created.id)).toBe(2)
repository.updateProgress(created.id, 40)
repository.markResumeReset(created.id)

const row = repository.get(created.id)!
expect(row.attempts).toBe(2)
expect(row.downloadedBytes).toBe(0)
expect(row.status).toBe('downloading')
expect(row.error).toContain('resume validation failed')
handle.close()
})

test('lists stale downloading rows without mutating them', async () => {
const handle = await openDatabase({ appConfigPath: join(tempDir, 'config.jsonc') })
const repository = new DownloadsRepository(handle.db)
const a = repository.create({
torrentFilename: 'a.torrent',
peerId: 'peer-1',
peerName: 'Friend Jack',
itemId: 'movie:1',
filename: 'A.mkv',
destPath: join(tempDir, 'A.mkv'),
partPath: join(tempDir, 'A.mkv.part'),
releaseSize: 10,
release,
})
const b = repository.create({
torrentFilename: 'b.torrent',
peerId: 'peer-1',
peerName: 'Friend Jack',
itemId: 'movie:2',
filename: 'B.mkv',
destPath: join(tempDir, 'B.mkv'),
partPath: join(tempDir, 'B.mkv.part'),
releaseSize: 10,
release,
})
repository.markCompleted(b.id, 10)

const stale = repository.listStaleDownloads()
expect(stale.map(r => r.id)).toEqual([a.id])
expect(repository.get(a.id)?.status).toBe('downloading')
handle.close()
})
})
6 changes: 3 additions & 3 deletions apps/backend/src/__tests__/downloads-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { AppConfig } from '../lib/config'
import type { Envs } from '../lib/envs'
import type { Release } from '../lib/release'
import { mkdtemp, rm } from 'node:fs/promises'
Expand All @@ -7,6 +6,7 @@ import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import { getApp } from '../app'
import { openDatabase } from '../database/connection'
import { AppConfig } from '../lib/config'
import { DownloadsRepository } from '../modules/downloads/downloads.repository'

const envs: Envs = {
Expand All @@ -20,12 +20,12 @@ const envs: Envs = {
NODE_ENV: 'test',
}

const config: AppConfig = {
const config = AppConfig.parse({
jack: { baseUrl: 'http://localhost:3000', apiKey: 'test-api-key' },
downloads: { watchPath: '/tmp/watch', completedPath: '/tmp/completed' },
servers: [],
peers: [],
}
})

const release: Release = {
id: 'remote:movie:1',
Expand Down
Loading
Loading