// Selecting, upgrading, retargeting and selling every kind of placed tower, including walls (regression: selecting a // wall used to throw because walls have a single level). module.exports = async function ({ browser, url: URL }) { const assert = (c, m) => { if (!c) throw new Error('ASSERT: ' + m); console.log('ok -', m); }; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const errors = []; const ctx = await browser.newContext({ viewport: { width: 1100, height: 700 } }); const page = await ctx.newPage(); page.on('pageerror', (e) => errors.push('pageerror: ' + e.message)); page.on('console', (m) => { if (m.type() === 'error') errors.push('console.error: ' + m.text()); }); await page.goto(URL); await sleep(300); await page.click('#btnSolo'); await sleep(200); await page.click('#btnHelpClose'); const box = await page.locator('#board').boundingBox(); const pt = async (cx, cy) => { const r = await page.evaluate(([a, b]) => NW.app.renderer.cellToScreen(a, b), [cx, cy]); return { x: box.x + r.x, y: box.y + r.y }; }; const view = () => page.evaluate(() => { const v = NW.app.ctrl.view; return { gold: v.gold, towers: v.towers.map((t) => ({ type: t.type, x: t.x, y: t.y, level: t.level, mode: t.mode })) }; }); // wall: build, select, sell (full refund) await page.click('.tbtn[data-type="wall"]'); let p = await pt(10, 6); await page.mouse.click(p.x, p.y); await page.keyboard.press('Escape'); p = await pt(10, 6); await page.mouse.click(p.x, p.y); await sleep(150); assert(await page.evaluate(() => NW.app.selTower > 0), 'clicking a placed wall selects it'); assert((await page.locator('#info').innerText()).includes('Firewall'), 'wall info panel is shown'); assert(await page.locator('#info [data-act="up"]').count() === 0, 'a wall has no upgrade button'); const before = (await view()).gold; await page.click('#info [data-act="sell"]'); await sleep(150); let v = await view(); assert(v.towers.length === 0 && v.gold === before + 5, 'selling a wall removes it and refunds it in full'); // turret: upgrade to max, cycle targeting, sell at a loss await page.click('.tbtn[data-type="filter"]'); p = await pt(12, 5); await page.mouse.click(p.x, p.y); await page.keyboard.press('Escape'); await page.mouse.click(p.x, p.y); await sleep(150); await page.click('#info [data-act="up"]'); await sleep(100); await page.click('#info [data-act="up"]'); await sleep(100); v = await view(); assert(v.towers[0].level === 3, 'turret upgraded to level 3'); assert(await page.locator('#info [data-act="up"]').isDisabled(), 'upgrade button disabled at max level'); await page.click('#info [data-act="mode"]'); await sleep(100); v = await view(); assert(v.towers[0].mode === 'last', 'target mode cycles first -> last'); await page.click('#info [data-act="sell"]'); await sleep(150); v = await view(); assert(v.towers.length === 0, 'turret sold'); if (errors.length) throw new Error('console errors:\n' + errors.join('\n')); console.log('no console errors'); await ctx.close(); };