import { test } from 'node:test'; import assert from 'node:assert/strict'; import { launch, openChart, withChart } from './helpers.mjs'; async function createLine(page, note) { return page.evaluate(async label => { const c = window.__chart; const first = c.bars[c.bars.length - 80]; const second = c.bars[c.bars.length - 60]; const response = await fetch('/api/lines', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ tf: '1m', side: 'support', anchor_t: first.t, anchor_p: first.l, end_t: second.t, end_p: second.l, note: label, }), }); return response.json(); }, note); } test('test cleanup never deletes a drawing made by another browser', { timeout: 180000 }, async () => { const { browser, page } = await launch(); let external = null; let owned = null; try { await openChart(page); await withChart(async testPage => { external = await createLine(page, 'external during test'); owned = await createLine(testPage, 'owned by test'); }); const ids = await page.evaluate(async () => { const response = await fetch('/api/drawings'); return (await response.json()).drawings.map(drawing => drawing.id); }); assert.ok(ids.includes(external.id), 'cleanup deleted another browser\'s drawing'); assert.ok(!ids.includes(owned.id), 'cleanup left its own drawing behind'); } finally { if (external?.id) { await page.evaluate(id => fetch(`/api/lines/${encodeURIComponent(id)}`, { method: 'DELETE', }), external.id); } await browser.close(); } });