const assert = require('node:assert/strict'); const { chromium } = require(process.env.PLAYWRIGHT_PACKAGE || 'playwright'); const base = process.env.REVIEW_BASE || 'http://localhost:3114'; const snapshot = added => ({ request_id: '3580', title: 'Suits', year: 2011, request_type: 'tv', state: added ? 'IMPORTING' : 'AVAILABLE', timeline: [], actions: [], presentation: { status: { label: added ? 'Partially collected — 32 episodes still missing' : 'Available to watch', meaning: added ? 'Seasons 8 and 9 are now monitored and collection is in progress.' : 'The originally requested collection is complete and available on the media server.', }, nextStep: { title: added ? 'Wait for search results' : 'Ready to watch', description: 'Magent is checking Sonarr.', actionIds: [] }, pipeline: [ { id: 'library', label: 'Library collection', state: added ? 'partial' : 'complete', summary: added ? 'Seasons 8 and 9 are being collected.' : 'Collection complete — no search needed', available: 124, missing: added ? 32 : 0, total: added ? 156 : 124, seasons: added ? [ { seasonNumber: 7, available: 16, missing: 0, total: 16 }, { seasonNumber: 8, available: 0, missing: 16, total: 16 }, { seasonNumber: 9, available: 0, missing: 16, total: 16 }, ] : [{ seasonNumber: 7, available: 16, missing: 0, total: 16 }], unmonitoredSeasons: added ? [] : [ { seasonNumber: 8, episodeCount: 16, available: 0 }, { seasonNumber: 9, episodeCount: 16, available: 0 }, ], canAddSeasons: true, }, { id: 'available', label: 'Available to watch', state: added ? 'partial' : 'complete', summary: 'Suits is available.', link: 'https://watch.example.test/suits' }, ], }, }); (async () => { const browser = await chromium.launch(); try { const calls = []; const errors = []; for (const width of [1440, 390]) { let added = false; const context = await browser.newContext({ viewport: { width, height: 950 } }); await context.addCookies([{ name: 'magent_logged_in', value: '1', url: base }]); await context.route('**/api/**', async route => { const request = route.request(); const path = new URL(request.url()).pathname; const reply = json => route.fulfill({ json }); if (path === '/api/auth/me') return reply({ username: 'Viewer', role: 'user', auto_search_enabled: true, features: { requests: true, new_requests: true, stats: true, issues: true } }); if (path === '/api/site/info') return reply({ mediaServerUrl: 'https://watch.example.test/' }); if (path === '/api/requests/3580/snapshot') return reply(snapshot(added)); if (path === '/api/requests/3580/language') return reply({ language: null }); if (path === '/api/requests/3580/actions/add-seasons' && request.method() === 'POST') { calls.push(request.postDataJSON()); added = true; return reply({ status: 'ok', message: 'Seasons 8, 9 added to Sonarr. Searching for 32 released missing episodes.', snapshot: snapshot(true) }); } if (path.startsWith('/api/operations/')) return reply({ id: 'fixture', label: 'Add seasons', status: 'complete', events: [] }); if (path.includes('/events/stream')) return route.fulfill({ contentType: 'text/event-stream', body: ': fixture\n\n' }); if (path.includes('/branding/')) return route.fulfill({ status: 404 }); return reply({ navigation: { showRequests: true }, items: [], total: 0, services: [] }); }); const page = await context.newPage(); page.on('pageerror', error => errors.push(error.message)); await page.goto(base + '/requests/3580'); await page.getByRole('heading', { name: 'Add more seasons' }).waitFor(); await page.getByText('Season 8', { exact: true }).waitFor(); await page.getByText('Season 9', { exact: true }).waitFor(); assert.equal(await page.getByRole('checkbox').count(), 2); await page.getByRole('button', { name: 'Select all' }).click(); assert(await page.getByText('2 seasons selected', { exact: true }).isVisible()); await page.getByRole('button', { name: 'Add selected seasons' }).click(); await page.getByText('Seasons 8, 9 added to Sonarr. Searching for 32 released missing episodes.', { exact: true }).waitFor(); assert.equal(await page.getByRole('heading', { name: 'Add more seasons' }).count(), 0); assert(await page.getByRole('heading', { name: 'Where your request is now' }).isVisible()); assert(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); await context.close(); } assert.deepEqual(calls, [{ season_numbers: [8, 9] }, { season_numbers: [8, 9] }]); assert.deepEqual(errors, []); console.log('Passed: completed TV requests show unmonitored seasons and add them through Sonarr on desktop/mobile. APIs intercepted.'); } finally { await browser.close(); } })().catch(error => { console.error(error); process.exitCode = 1; });