|
| 1 | +// @ts-check |
| 2 | +/** |
| 3 | + * Seed the catalog with books and music records. |
| 4 | + * This is a SEEDER — it does NOT clean up. Records persist for manual testing. |
| 5 | + * Run: /tmp/run-e2e.sh tests/seed-catalog.spec.js --config=tests/playwright.config.js --workers=1 |
| 6 | + */ |
| 7 | +const { test, expect } = require('@playwright/test'); |
| 8 | +const { execFileSync } = require('child_process'); |
| 9 | + |
| 10 | +const BASE = process.env.E2E_BASE_URL || 'http://localhost:8081'; |
| 11 | +const ADMIN_EMAIL = process.env.E2E_ADMIN_EMAIL || ''; |
| 12 | +const ADMIN_PASS = process.env.E2E_ADMIN_PASS || ''; |
| 13 | + |
| 14 | +// 10 music records via Discogs barcode scraping |
| 15 | +const MUSIC_BARCODES = [ |
| 16 | + { barcode: '0720642442524', note: 'Nirvana - Nevermind' }, |
| 17 | + { barcode: '5099902894225', note: 'Pink Floyd - Meddle' }, |
| 18 | + { barcode: '0094638246824', note: 'Beatles - Abbey Road' }, |
| 19 | + { barcode: '0888837168625', note: 'Daft Punk - RAM' }, |
| 20 | + { barcode: '5099751076322', note: 'AC/DC' }, |
| 21 | + { barcode: '0602547428714', note: 'Adele - 25' }, |
| 22 | + { barcode: '0602537615810', note: 'Arctic Monkeys - AM' }, |
| 23 | + { barcode: '0886971592924', note: 'Muse - The Resistance' }, |
| 24 | + { barcode: '0602527947747', note: 'Coldplay - Mylo Xyloto' }, |
| 25 | + { barcode: '0602557048032', note: 'Metallica - Hardwired' }, |
| 26 | +]; |
| 27 | + |
| 28 | +// 5 books via ISBN scraping |
| 29 | +const BOOK_ISBNS = [ |
| 30 | + { isbn: '9780061120084', note: 'To Kill a Mockingbird' }, |
| 31 | + { isbn: '9780451524935', note: '1984' }, |
| 32 | + { isbn: '9780141439518', note: 'Pride and Prejudice' }, |
| 33 | + { isbn: '9780060935467', note: 'Don Quixote' }, |
| 34 | + { isbn: '9780142437230', note: 'Moby Dick' }, |
| 35 | +]; |
| 36 | + |
| 37 | +// 1 manual entry (punk split without barcode) |
| 38 | +const MANUAL_ENTRIES = [ |
| 39 | + { titolo: 'Zeromila / Orsetti HC — Split', formato: 'vinile', tipo_media: 'disco' }, |
| 40 | +]; |
| 41 | + |
| 42 | +test.describe.serial('Seed Catalog (books + music)', () => { |
| 43 | + /** @type {import('@playwright/test').Page} */ |
| 44 | + let page; |
| 45 | + /** @type {import('@playwright/test').BrowserContext} */ |
| 46 | + let context; |
| 47 | + |
| 48 | + test.beforeAll(async ({ browser }) => { |
| 49 | + test.skip(!ADMIN_EMAIL || !ADMIN_PASS, 'Missing env vars'); |
| 50 | + context = await browser.newContext(); |
| 51 | + page = await context.newPage(); |
| 52 | + |
| 53 | + await page.goto(`${BASE}/accedi`); |
| 54 | + await page.fill('input[name="email"]', ADMIN_EMAIL); |
| 55 | + await page.fill('input[name="password"]', ADMIN_PASS); |
| 56 | + await page.click('button[type="submit"]'); |
| 57 | + await page.waitForURL(/\/admin\//, { timeout: 15000 }); |
| 58 | + }); |
| 59 | + |
| 60 | + test.afterAll(async () => { |
| 61 | + // DO NOT clean up — this is a seeder |
| 62 | + await context?.close(); |
| 63 | + }); |
| 64 | + |
| 65 | + // Seed music records via barcode scraping |
| 66 | + for (let i = 0; i < MUSIC_BARCODES.length; i++) { |
| 67 | + const rec = MUSIC_BARCODES[i]; |
| 68 | + test(`Music ${i + 1}: ${rec.note}`, async () => { |
| 69 | + test.setTimeout(30000); |
| 70 | + await page.goto(`${BASE}/admin/libri/crea`); |
| 71 | + await page.waitForLoadState('domcontentloaded'); |
| 72 | + |
| 73 | + const importBtn = page.locator('#btnImportIsbn'); |
| 74 | + if (await importBtn.isVisible({ timeout: 3000 }).catch(() => false)) { |
| 75 | + await page.fill('#importIsbn', rec.barcode); |
| 76 | + await importBtn.click(); |
| 77 | + await page.waitForTimeout(8000); |
| 78 | + |
| 79 | + const title = await page.locator('input[name="titolo"]').inputValue(); |
| 80 | + if (!title) { |
| 81 | + await page.locator('input[name="titolo"]').fill(`CD (${rec.barcode})`); |
| 82 | + await page.locator('input[name="ean"]').fill(rec.barcode); |
| 83 | + await page.locator('input[name="formato"]').fill('cd_audio'); |
| 84 | + } |
| 85 | + } else { |
| 86 | + await page.locator('input[name="titolo"]').fill(`CD (${rec.barcode})`); |
| 87 | + await page.locator('input[name="ean"]').fill(rec.barcode); |
| 88 | + await page.locator('input[name="formato"]').fill('cd_audio'); |
| 89 | + } |
| 90 | + |
| 91 | + const copie = await page.locator('input[name="copie_totali"]').inputValue(); |
| 92 | + if (!copie || copie === '0') await page.locator('input[name="copie_totali"]').fill('1'); |
| 93 | + |
| 94 | + await page.locator('button[type="submit"]').first().click(); |
| 95 | + const swal = page.locator('.swal2-confirm'); |
| 96 | + if (await swal.isVisible({ timeout: 3000 }).catch(() => false)) await swal.click(); |
| 97 | + await page.waitForURL(/\/admin\/libri\/\d+/, { timeout: 15000 }).catch(() => {}); |
| 98 | + console.log(` ✓ ${rec.note}`); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + // Seed books via ISBN |
| 103 | + for (let i = 0; i < BOOK_ISBNS.length; i++) { |
| 104 | + const book = BOOK_ISBNS[i]; |
| 105 | + test(`Book ${i + 1}: ${book.note}`, async () => { |
| 106 | + test.setTimeout(30000); |
| 107 | + await page.goto(`${BASE}/admin/libri/crea`); |
| 108 | + await page.waitForLoadState('domcontentloaded'); |
| 109 | + |
| 110 | + const importBtn = page.locator('#btnImportIsbn'); |
| 111 | + if (await importBtn.isVisible({ timeout: 3000 }).catch(() => false)) { |
| 112 | + await page.fill('#importIsbn', book.isbn); |
| 113 | + await importBtn.click(); |
| 114 | + await page.waitForTimeout(8000); |
| 115 | + |
| 116 | + const title = await page.locator('input[name="titolo"]').inputValue(); |
| 117 | + if (!title) { |
| 118 | + await page.locator('input[name="titolo"]').fill(book.note); |
| 119 | + await page.locator('input[name="isbn13"]').fill(book.isbn); |
| 120 | + } |
| 121 | + } else { |
| 122 | + await page.locator('input[name="titolo"]').fill(book.note); |
| 123 | + await page.locator('input[name="isbn13"]').fill(book.isbn); |
| 124 | + } |
| 125 | + |
| 126 | + const copie = await page.locator('input[name="copie_totali"]').inputValue(); |
| 127 | + if (!copie || copie === '0') await page.locator('input[name="copie_totali"]').fill('1'); |
| 128 | + |
| 129 | + await page.locator('button[type="submit"]').first().click(); |
| 130 | + const swal = page.locator('.swal2-confirm'); |
| 131 | + if (await swal.isVisible({ timeout: 3000 }).catch(() => false)) await swal.click(); |
| 132 | + await page.waitForURL(/\/admin\/libri\/\d+/, { timeout: 15000 }).catch(() => {}); |
| 133 | + console.log(` ✓ ${book.note}`); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + // Manual entries |
| 138 | + for (let i = 0; i < MANUAL_ENTRIES.length; i++) { |
| 139 | + const entry = MANUAL_ENTRIES[i]; |
| 140 | + test(`Manual ${i + 1}: ${entry.titolo}`, async () => { |
| 141 | + test.setTimeout(15000); |
| 142 | + await page.goto(`${BASE}/admin/libri/crea`); |
| 143 | + await page.waitForLoadState('domcontentloaded'); |
| 144 | + |
| 145 | + await page.locator('input[name="titolo"]').fill(entry.titolo); |
| 146 | + await page.locator('input[name="formato"]').fill(entry.formato); |
| 147 | + if (entry.tipo_media) { |
| 148 | + const sel = page.locator('#tipo_media'); |
| 149 | + if (await sel.isVisible({ timeout: 2000 }).catch(() => false)) { |
| 150 | + await sel.selectOption(entry.tipo_media); |
| 151 | + } |
| 152 | + } |
| 153 | + await page.locator('input[name="copie_totali"]').fill('1'); |
| 154 | + |
| 155 | + await page.locator('button[type="submit"]').first().click(); |
| 156 | + const swal = page.locator('.swal2-confirm'); |
| 157 | + if (await swal.isVisible({ timeout: 3000 }).catch(() => false)) await swal.click(); |
| 158 | + await page.waitForURL(/\/admin\/libri\/\d+/, { timeout: 15000 }).catch(() => {}); |
| 159 | + console.log(` ✓ ${entry.titolo}`); |
| 160 | + }); |
| 161 | + } |
| 162 | +}); |
0 commit comments