|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { readFile } from 'node:fs/promises'; |
| 4 | +import { isEPUBPath } from '../../../electron/main/services/documents.mjs'; |
| 5 | + |
| 6 | +// Exercise the composition root's real claim entry points without starting Electron. |
| 7 | +const source = await readFile(new URL('../../../electron/main.mjs', import.meta.url), 'utf8'); |
| 8 | +const claims = source.slice( |
| 9 | + source.indexOf(' const claimCompanionID ='), |
| 10 | + source.indexOf(' const companionOwnership ='), |
| 11 | +); |
| 12 | +const createClaims = new Function( |
| 13 | + 'isEPUBPath', |
| 14 | + 'recents', |
| 15 | + 'companion', |
| 16 | + ` |
| 17 | + const windows = new Map(), desktopClaimsInFlight = new Map(); |
| 18 | + const registry = { stateFor: () => undefined }; |
| 19 | + const desktopSessionID = () => 'session'; |
| 20 | + const handoffLockWaitMs = 30000; |
| 21 | + const recentIDForPath = path => recents.path('recent') === path ? 'recent' : undefined; |
| 22 | + const notifyCompanionLock = () => {}; |
| 23 | + ${claims} |
| 24 | + return { byID: claimCompanionID, byPath: claimCompanionPath }; |
| 25 | +`, |
| 26 | +); |
| 27 | + |
| 28 | +test('EPUB launch and recent opens bypass PDF-only companion claims', async () => { |
| 29 | + for (const path of ['/books/new.epub', '/books/RECENT.EPUB']) { |
| 30 | + const recents = { path: () => path, prepare: () => assert.fail('prepared EPUB for companion') }; |
| 31 | + const companion = { claimDesktopDocument: () => assert.fail('claimed EPUB as PDF') }; |
| 32 | + const claim = createClaims(isEPUBPath, recents, companion); |
| 33 | + assert.deepEqual(await claim.byPath(path, {}), { available: false }); |
| 34 | + assert.deepEqual(await claim.byID('recent', {}), { available: false }); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +test('PDF opens still claim companion ownership', async () => { |
| 39 | + const calls = []; |
| 40 | + const claim = createClaims( |
| 41 | + isEPUBPath, |
| 42 | + { path: () => '/books/book.pdf' }, |
| 43 | + { |
| 44 | + claimDesktopDocument: async (...args) => { |
| 45 | + calls.push(args); |
| 46 | + return { claimed: true }; |
| 47 | + }, |
| 48 | + }, |
| 49 | + ); |
| 50 | + assert.deepEqual(await claim.byPath('/books/book.pdf', {}), { claimed: true }); |
| 51 | + assert.equal(calls.length, 1); |
| 52 | + assert.deepEqual(calls[0], ['recent', 'session', 'This Mac']); |
| 53 | +}); |
0 commit comments