|
1 | 1 | import type { ArrServerConnector, ManualImportParams } from '../lib/servers/arr/base' |
2 | 2 | import type { DownloadsRepository } from '../modules/downloads/downloads.repository' |
3 | | -import { dirname } from 'node:path' |
| 3 | +import { mkdir, mkdtemp, rm } from 'node:fs/promises' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { dirname, join } from 'node:path' |
4 | 6 | import { Database } from 'bun:sqlite' |
5 | | -import { describe, expect, mock, test } from 'bun:test' |
| 7 | +import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test' |
6 | 8 | import { drizzle } from 'drizzle-orm/bun-sqlite' |
7 | 9 | import { runMigrations } from '../database/connection' |
8 | 10 | import * as schema from '../database/schema' |
@@ -414,3 +416,241 @@ describe('ImportWatcher tracked manual imports', () => { |
414 | 416 | expect(repo.get(row.id)).toMatchObject({ status: 'failed', error: 'episode ids could not be resolved' }) |
415 | 417 | }) |
416 | 418 | }) |
| 419 | + |
| 420 | +// `downloads.unlinkImportedFiles`: once *arr confirms the import, jack's own copy in |
| 421 | +// completedPath is dead weight (*arr hardlinked or copied it into the library), so the |
| 422 | +// watcher drops jack's link. Only ever on a confirmed import — never on a queued, |
| 423 | +// failed, or still-pending row. |
| 424 | +describe('ImportWatcher imported-file cleanup', () => { |
| 425 | + let completedPath: string |
| 426 | + |
| 427 | + beforeEach(async () => { |
| 428 | + completedPath = await mkdtemp(join(tmpdir(), 'jack-unlink-')) |
| 429 | + }) |
| 430 | + |
| 431 | + afterEach(async () => { |
| 432 | + await rm(completedPath, { recursive: true, force: true }) |
| 433 | + }) |
| 434 | + |
| 435 | + // A queued row whose destPath is a real file inside the temp completedPath. |
| 436 | + async function queuedRowWithFile(repo: DownloadsRepository, opts: { name?: string, mode?: 'jack_manual' } = {}) { |
| 437 | + const destPath = join(completedPath, opts.name ?? 'x.mkv') |
| 438 | + await Bun.write(destPath, 'payload') |
| 439 | + const row = repo.create({ |
| 440 | + torrentFilename: 't.torrent', |
| 441 | + peerId: 'peer-1', |
| 442 | + peerName: 'Friend', |
| 443 | + itemId: 'movie:1', |
| 444 | + filename: release.filename, |
| 445 | + destPath, |
| 446 | + partPath: `${destPath}.part`, |
| 447 | + releaseSize: release.size, |
| 448 | + release, |
| 449 | + qbSourceServer: 'My Radarr', |
| 450 | + sourceServerId: 'radarr-1', |
| 451 | + ...(opts.mode === 'jack_manual' |
| 452 | + ? { importMode: 'jack_manual' as const, importTarget: { kind: 'movie' as const, movieId: 42 } } |
| 453 | + : {}), |
| 454 | + }) |
| 455 | + repo.markImportQueued(row.id) |
| 456 | + return row |
| 457 | + } |
| 458 | + |
| 459 | + function cleanupWatcher(repo: DownloadsRepository, servers: ImportWatcherServer[], enabled: () => boolean) { |
| 460 | + return new ImportWatcher(repo, { servers: servers as unknown as ArrServerConnector[] }, 1000, undefined, undefined, { |
| 461 | + enabled, |
| 462 | + completedPath, |
| 463 | + }) |
| 464 | + } |
| 465 | + |
| 466 | + test('unlinks the file once *arr history confirms the import', async () => { |
| 467 | + const repo = makeRepo() |
| 468 | + const row = await queuedRowWithFile(repo) |
| 469 | + const watcher = cleanupWatcher(repo, [{ ...fakeServer('My Radarr', [HASH]), id: 'radarr-1' }], () => true) |
| 470 | + |
| 471 | + expect(await watcher.tick()).toBe(1) |
| 472 | + expect(repo.get(row.id)?.status).toBe('imported') |
| 473 | + expect(await Bun.file(row.destPath).exists()).toBe(false) |
| 474 | + }) |
| 475 | + |
| 476 | + test('unlinks the file once a tracked manual import command completes', async () => { |
| 477 | + const repo = makeRepo() |
| 478 | + const row = await queuedRowWithFile(repo, { mode: 'jack_manual' }) |
| 479 | + const server: ImportWatcherServer = { |
| 480 | + id: 'radarr-1', |
| 481 | + name: 'My Radarr', |
| 482 | + isInitialized: true, |
| 483 | + recentlyImportedDownloadIds: async () => new Set(), |
| 484 | + manualImport: async () => 44, |
| 485 | + manualImportCommandStatus: async () => ({ state: 'completed' }), |
| 486 | + } |
| 487 | + const watcher = cleanupWatcher(repo, [server], () => true) |
| 488 | + |
| 489 | + // First tick pushes the manual import, second sees the command completed. |
| 490 | + await watcher.tick() |
| 491 | + expect(await Bun.file(row.destPath).exists()).toBe(true) |
| 492 | + await watcher.tick() |
| 493 | + |
| 494 | + expect(repo.get(row.id)?.status).toBe('imported') |
| 495 | + expect(await Bun.file(row.destPath).exists()).toBe(false) |
| 496 | + }) |
| 497 | + |
| 498 | + test('keeps the file while the row is still queued, and when the import fails', async () => { |
| 499 | + const repo = makeRepo() |
| 500 | + const queued = await queuedRowWithFile(repo, { name: 'queued.mkv', mode: 'jack_manual' }) |
| 501 | + const failing = await queuedRowWithFile(repo, { name: 'failing.mkv', mode: 'jack_manual' }) |
| 502 | + const server: ImportWatcherServer = { |
| 503 | + id: 'radarr-1', |
| 504 | + name: 'My Radarr', |
| 505 | + isInitialized: true, |
| 506 | + recentlyImportedDownloadIds: async () => new Set(), |
| 507 | + manualImport: async params => (params.paths[0] === failing.destPath ? 46 : 45), |
| 508 | + manualImportCommandStatus: async commandId => |
| 509 | + (commandId === 46 ? { state: 'failed', error: 'arr rejected it' } : { state: 'pending' }), |
| 510 | + } |
| 511 | + const watcher = cleanupWatcher(repo, [server], () => true) |
| 512 | + |
| 513 | + await watcher.tick() |
| 514 | + await watcher.tick() |
| 515 | + |
| 516 | + expect(repo.get(queued.id)?.status).toBe('import_queued') |
| 517 | + expect(repo.get(failing.id)?.status).toBe('failed') |
| 518 | + expect(await Bun.file(queued.destPath).exists()).toBe(true) |
| 519 | + expect(await Bun.file(failing.destPath).exists()).toBe(true) |
| 520 | + }) |
| 521 | + |
| 522 | + test('keeps the file when the option is off', async () => { |
| 523 | + const repo = makeRepo() |
| 524 | + const row = await queuedRowWithFile(repo) |
| 525 | + const watcher = cleanupWatcher(repo, [{ ...fakeServer('My Radarr', [HASH]), id: 'radarr-1' }], () => false) |
| 526 | + |
| 527 | + expect(await watcher.tick()).toBe(1) |
| 528 | + expect(repo.get(row.id)?.status).toBe('imported') |
| 529 | + expect(await Bun.file(row.destPath).exists()).toBe(true) |
| 530 | + }) |
| 531 | + |
| 532 | + test('reads the option per import, so toggling it applies without a restart', async () => { |
| 533 | + const repo = makeRepo() |
| 534 | + const kept = await queuedRowWithFile(repo, { name: 'kept.mkv' }) |
| 535 | + let enabled = false |
| 536 | + const watcher = cleanupWatcher(repo, [{ ...fakeServer('My Radarr', [HASH]), id: 'radarr-1' }], () => enabled) |
| 537 | + |
| 538 | + await watcher.tick() |
| 539 | + expect(await Bun.file(kept.destPath).exists()).toBe(true) |
| 540 | + |
| 541 | + enabled = true |
| 542 | + const unlinked = await queuedRowWithFile(repo, { name: 'unlinked.mkv' }) |
| 543 | + await watcher.tick() |
| 544 | + |
| 545 | + expect(await Bun.file(unlinked.destPath).exists()).toBe(false) |
| 546 | + }) |
| 547 | + |
| 548 | + test('leaves the row imported when the unlink fails', async () => { |
| 549 | + const repo = makeRepo() |
| 550 | + const row = await queuedRowWithFile(repo) |
| 551 | + // A directory where the file is expected: unlink(2) fails with EISDIR/EPERM. |
| 552 | + await rm(row.destPath) |
| 553 | + await mkdir(row.destPath) |
| 554 | + const watcher = cleanupWatcher(repo, [{ ...fakeServer('My Radarr', [HASH]), id: 'radarr-1' }], () => true) |
| 555 | + |
| 556 | + expect(await watcher.tick()).toBe(1) |
| 557 | + expect(repo.get(row.id)?.status).toBe('imported') |
| 558 | + }) |
| 559 | + |
| 560 | + // Two rows can share a destination: the in-flight duplicate guard only covers active |
| 561 | + // transfers, so a re-grab that lands while the first row is still import_queued gets |
| 562 | + // its own row on the same path. Whichever imports last has to do the cleanup — |
| 563 | + // otherwise both defer to each other and the file outlives both rows. |
| 564 | + test('defers cleanup to the last of two rows sharing a destination', async () => { |
| 565 | + const repo = makeRepo() |
| 566 | + const first = await queuedRowWithFile(repo, { name: 'shared.mkv' }) |
| 567 | + const second = repo.create({ |
| 568 | + torrentFilename: 't2.torrent', |
| 569 | + peerId: 'peer-1', |
| 570 | + peerName: 'Friend', |
| 571 | + itemId: 'movie:1', |
| 572 | + filename: release.filename, |
| 573 | + destPath: first.destPath, |
| 574 | + partPath: first.partPath, |
| 575 | + releaseSize: release.size, |
| 576 | + release, |
| 577 | + qbSourceServer: 'My Radarr', |
| 578 | + sourceServerId: 'radarr-1', |
| 579 | + }) |
| 580 | + repo.markImportQueued(second.id) |
| 581 | + |
| 582 | + // Only the first row's hash is in *arr history to begin with, so it imports alone. |
| 583 | + const importedHashes = [HASH] |
| 584 | + const server = { |
| 585 | + id: 'radarr-1', |
| 586 | + name: 'My Radarr', |
| 587 | + isInitialized: true, |
| 588 | + recentlyImportedDownloadIds: async () => new Set(importedHashes.map(h => h.toLowerCase())), |
| 589 | + } as any |
| 590 | + const watcher = cleanupWatcher(repo, [server], () => true) |
| 591 | + |
| 592 | + await watcher.tick() |
| 593 | + |
| 594 | + // Both rows flip on the same hash, so drive them one at a time by re-reading state. |
| 595 | + const imported = [first, second].filter(row => repo.get(row.id)?.status === 'imported') |
| 596 | + const stillQueued = [first, second].filter(row => repo.get(row.id)?.status === 'import_queued') |
| 597 | + expect(imported).toHaveLength(2) |
| 598 | + expect(stillQueued).toHaveLength(0) |
| 599 | + // The row that imported second saw an already-imported sibling and cleaned up. |
| 600 | + expect(await Bun.file(first.destPath).exists()).toBe(false) |
| 601 | + }) |
| 602 | + |
| 603 | + test('keeps the file while a row sharing the destination is still awaiting import', async () => { |
| 604 | + const repo = makeRepo() |
| 605 | + const importing = await queuedRowWithFile(repo, { name: 'shared.mkv' }) |
| 606 | + // A second row on the same path that *arr has not imported — it still needs the file. |
| 607 | + const waiting = repo.create({ |
| 608 | + torrentFilename: 't2.torrent', |
| 609 | + peerId: 'peer-1', |
| 610 | + peerName: 'Friend', |
| 611 | + itemId: 'movie:2', |
| 612 | + filename: release.filename, |
| 613 | + destPath: importing.destPath, |
| 614 | + partPath: importing.partPath, |
| 615 | + releaseSize: release.size, |
| 616 | + release, |
| 617 | + qbSourceServer: 'Other Radarr', |
| 618 | + sourceServerId: 'radarr-2', |
| 619 | + }) |
| 620 | + repo.markImportQueued(waiting.id) |
| 621 | + |
| 622 | + // Only radarr-1 is connected, so only `importing` can flip to imported this tick. |
| 623 | + const watcher = cleanupWatcher(repo, [{ ...fakeServer('My Radarr', [HASH]), id: 'radarr-1' }], () => true) |
| 624 | + |
| 625 | + expect(await watcher.tick()).toBe(1) |
| 626 | + expect(repo.get(importing.id)?.status).toBe('imported') |
| 627 | + expect(repo.get(waiting.id)?.status).toBe('import_queued') |
| 628 | + expect(await Bun.file(importing.destPath).exists()).toBe(true) |
| 629 | + }) |
| 630 | + |
| 631 | + test('never unlinks a path outside completedPath', async () => { |
| 632 | + const repo = makeRepo() |
| 633 | + const outside = await mkdtemp(join(tmpdir(), 'jack-outside-')) |
| 634 | + const destPath = join(outside, 'x.mkv') |
| 635 | + await Bun.write(destPath, 'payload') |
| 636 | + const row = repo.create({ |
| 637 | + torrentFilename: 't.torrent', |
| 638 | + peerId: 'peer-1', |
| 639 | + peerName: 'Friend', |
| 640 | + itemId: 'movie:1', |
| 641 | + filename: release.filename, |
| 642 | + destPath, |
| 643 | + partPath: `${destPath}.part`, |
| 644 | + releaseSize: release.size, |
| 645 | + release, |
| 646 | + qbSourceServer: 'My Radarr', |
| 647 | + sourceServerId: 'radarr-1', |
| 648 | + }) |
| 649 | + repo.markImportQueued(row.id) |
| 650 | + const watcher = cleanupWatcher(repo, [{ ...fakeServer('My Radarr', [HASH]), id: 'radarr-1' }], () => true) |
| 651 | + |
| 652 | + expect(await watcher.tick()).toBe(1) |
| 653 | + expect(await Bun.file(destPath).exists()).toBe(true) |
| 654 | + await rm(outside, { recursive: true, force: true }) |
| 655 | + }) |
| 656 | +}) |
0 commit comments