| 1 | // code.read: además del fichero entero (comportamiento de siempre), ahora |
| 2 | // pagina de 100 en 100 líneas (offset/limit) y puede centrarse en una línea |
| 3 | // dada (around) — para encadenar con los números de línea que ya devuelve |
| 4 | // code.search y traer contexto antes/después sin volcar el fichero completo. |
| 5 | import { chromium } from 'playwright'; |
| 6 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 7 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 8 | |
| 9 | const b = await chromium.launch(); |
| 10 | const ctx = await b.newContext(); |
| 11 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 12 | const p = await ctx.newPage(); |
| 13 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 14 | await p.waitForTimeout(1500); |
| 15 | |
| 16 | // siembra un fichero de 250 líneas numeradas L1..L250, con una marca en la 173 |
| 17 | await p.evaluate(async () => { |
| 18 | const root = await navigator.storage.getDirectory(); |
| 19 | const lines = Array.from({ length: 250 }, (_, i) => (i + 1 === 173 ? 'MARCA_BUSCADA_XYZ' : `L${i + 1}`)); |
| 20 | const fh = await root.getFileHandle('big.py', { create: true }); |
| 21 | const w = await fh.createWritable(); |
| 22 | await w.write(lines.join('\n')); |
| 23 | await w.close(); |
| 24 | }); |
| 25 | |
| 26 | const read = async args => p.evaluate(async a => { |
| 27 | const c = await import('/js/tools/code.js'); |
| 28 | return c.read(a); |
| 29 | }, args); |
| 30 | const search = async args => p.evaluate(async a => { |
| 31 | const c = await import('/js/tools/code.js'); |
| 32 | return c.search(a); |
| 33 | }, args); |
| 34 | |
| 35 | // 1) sin parámetros: fichero ENTERO, comportamiento de siempre (no roto) |
| 36 | const whole = await read({ path: 'big.py' }); |
| 37 | ok('sin offset/limit/around, lee el fichero entero', whole.split('\n').length === 250 && whole.includes('L1') && whole.includes('L250')); |
| 38 | ok('el fichero entero NO lleva números de línea (compat con code.edit)', !whole.includes('1→')); |
| 39 | |
| 40 | // 2) paginado: offset+limit trae exactamente ese rango, numerado |
| 41 | const page1 = await read({ path: 'big.py', offset: 1, limit: 100 }); |
| 42 | ok('página 1 (offset:1, limit:100) empieza en la línea 1 numerada', page1.includes('1→L1')); |
| 43 | ok('página 1 llega hasta la línea 100', page1.includes('100→L100') && !page1.includes('101→')); |
| 44 | ok('avisa de que quedan más líneas y cómo seguir', /offset:101/.test(page1), page1.split('\n').pop()); |
| 45 | |
| 46 | const page2 = await read({ path: 'big.py', offset: 101, limit: 100 }); |
| 47 | ok('página 2 (offset:101) sigue justo donde acabó la 1', page2.includes('101→L101') && !page2.includes('100→')); |
| 48 | |
| 49 | const lastPage = await read({ path: 'big.py', offset: 201, limit: 100 }); |
| 50 | ok('última página no promete más líneas de las que hay (fichero tiene 250)', lastPage.includes('250→L250') && !/offset:251/.test(lastPage)); |
| 51 | |
| 52 | // 3) code.search sigue devolviendo número de línea real |
| 53 | const found = await search({ query: 'MARCA_BUSCADA_XYZ' }); |
| 54 | ok('code.search devuelve el número de línea del hit', /big\.py:173:/.test(found), found); |
| 55 | |
| 56 | // 4) around: centra ~100 líneas alrededor del número que dio la búsqueda |
| 57 | const around = await read({ path: 'big.py', around: 173 }); |
| 58 | ok('around:173 incluye la línea exacta de la marca, numerada', around.includes('173→MARCA_BUSCADA_XYZ')); |
| 59 | ok('around:173 trae líneas de ANTES', around.includes('150→L150') || around.includes('140→L140')); |
| 60 | ok('around:173 trae líneas de DESPUÉS', around.includes('190→L190') || around.includes('200→L200')); |
| 61 | |
| 62 | // 5) code.edit (que usa read({path}) internamente) sigue funcionando sin cambios |
| 63 | const editOut = await p.evaluate(async () => { |
| 64 | const c = await import('/js/tools/code.js'); |
| 65 | return c.edit({ path: 'big.py', search: 'MARCA_BUSCADA_XYZ', replace: 'MARCA_EDITADA' }); |
| 66 | }); |
| 67 | ok('code.edit sigue funcionando tal cual (no afectado por el nuevo modo paginado)', /Escrito big\.py/.test(editOut), editOut); |
| 68 | const afterEdit = await read({ path: 'big.py', around: 173 }); |
| 69 | ok('el cambio de code.edit se ve reflejado', afterEdit.includes('MARCA_EDITADA')); |
| 70 | |
| 71 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ code.read PAGINADO + around OK, code.search/code.edit intactos'); |
| 72 | await b.close(); |
| 73 | process.exit(fails ? 1 : 0); |