// Built-in table formulas: live recalculation, raw formula editing, Enter and Tab, undo, cycles, saving, the footer layout, the Read and Split views, // and the general setting that turns formulas off and on. import assert from "node:assert/strict"; import path from "node:path"; import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs"; const source = '# Table formulas\n\n| Name | Appearance | Charisma | Combined |\n| --- | ---: | ---: | ---: |\n| First | 9 | 10 | =AVERAGE(B2:C2) |\n| Second | 7 | 9 | =SUM(B3+C3)/2 |\n| Total | | | =SUM(D2:D3) |\n\nAfter.\n'; await runSmoke("tables/formulas", async ({ page }) => { const output = await outputDir("tables/formulas"); await mockTauri(page, { state: { files: { 'formulas.md': source } }, commands: test => { const workspace = test.workspace({ root: "C:\\smoke-workspace", files: test.state.files, writeEvents: true }); test.files = workspace.files; return workspace; }, }); async function quickOpen(name) { await page.keyboard.press("Control+p"); await page.getByRole("textbox", { name: "Find a file" }).fill(name); await page.getByRole("dialog").getByRole("button").filter({ hasText: name }).first().click(); await page.locator('.live-writer').waitFor(); await page.keyboard.press('Control+e'); await page.locator(".monaco-editor textarea").waitFor(); } const table = page.locator('.live-writer table'); const cell = (r, c) => table.locator('tr').nth(r).locator('th, td').nth(c); const edit = page.locator('.writer-cell-editor'); const result = (r, c) => cell(r, c).locator('.md-table-formula'); const shot = name => page.screenshot({ path: path.join(output, `${name}.png`) }); async function save() { await page.keyboard.press('Control+s'); await page.waitForFunction(() => !document.querySelector('[aria-label^="Save changes"]')); return page.evaluate(() => window.__TEST__.files.get('formulas.md')); } // Footer controls share one normal-flow block aligned to the table at any width. async function checkFooter() { const geometry = await page.evaluate(() => { const rect = selector => document.querySelector(selector).getBoundingClientRect().toJSON(); return { table: rect('.live-writer .md-table-scroll'), footer: rect('.table-footer'), tools: rect('.table-formula-tools'), help: rect('.table-edit-help'), helpHidden: document.querySelector('.table-edit-help').hidden, summary: rect('.table-formula-help summary'), status: rect('.table-formula-status') }; }); assert.ok(Math.abs(geometry.table.x - geometry.footer.x) < 2); assert.ok(Math.abs(geometry.footer.x - geometry.tools.x) < 2); if (!geometry.helpHidden) assert.ok(Math.abs(geometry.help.x - geometry.tools.x) < 2); assert.ok(geometry.footer.y >= geometry.table.bottom); assert.ok(geometry.footer.y - geometry.table.bottom <= 28, 'Only the add-row control sits between table and footer'); if (!geometry.helpHidden) { assert.ok(geometry.tools.y >= geometry.help.bottom, 'Help must not overlap the controls'); assert.ok(geometry.help.right <= geometry.footer.right + 1); } if (geometry.status.x > geometry.summary.right) assert.ok(Math.abs((geometry.status.y + geometry.status.height / 2) - (geometry.summary.y + geometry.summary.height / 2)) < 2); } await page.goto(smokeUrl); await page.getByText('smoke-workspace', { exact: true }).waitFor(); await quickOpen('formulas.md'); await page.keyboard.press('Control+e'); await table.waitFor(); assert.equal(await result(1, 3).innerText(), '9.5'); assert.equal(await result(3, 3).innerText(), '17.5'); await cell(1, 3).hover(); await page.locator('.rs-tooltip:popover-open').waitFor(); assert.equal(await cell(1, 3).getAttribute('title'), null); assert.match(await page.locator('.rs-tooltip:popover-open').innerText(), /D2.*AVERAGE/); await shot('cell-hint'); await page.keyboard.press('Escape'); assert.equal(await page.locator('.rs-tooltip:popover-open').count(), 0); await cell(1, 3).click(); assert.equal(await page.locator('.table-formula-status').innerText(), 'D2 = 9.5'); await edit.fill('=AVERAGE(B2+C2)'); assert.equal(await page.locator('.table-formula-status').innerText(), 'D2 = 19'); assert.equal(await page.locator('.table-formula-hint').isVisible(), false); await edit.fill('=AVERAGE(B2,C2)'); assert.equal(await page.locator('.table-formula-status').innerText(), 'D2 = 9.5'); assert.equal(await page.locator('.table-formula-hint').isVisible(), false); await edit.fill('=AVERAGE(B2:C2)'); await page.keyboard.press('Enter'); await cell(1, 1).click(); await edit.fill('5'); assert.equal(await result(1, 3).innerText(), '7.5'); assert.equal(await result(3, 3).innerText(), '15.5'); await page.keyboard.press('Tab'); await cell(1, 3).click(); assert.equal(await edit.innerText(), '=AVERAGE(B2:C2)'); assert.match(await page.locator('.table-formula-status').innerText(), /D2/); await edit.fill('=B2*C2'); await page.keyboard.press('Enter'); assert.equal(await result(1, 3).innerText(), '50'); assert.equal(await result(3, 3).innerText(), '58'); assert.match(await save(), /\| =B2\*C2 \|/); await cell(1, 3).click(); await page.keyboard.press('Control+z'); await page.waitForFunction(() => document.querySelector('.writer-cell-editor')?.textContent === '=AVERAGE(B2:C2)'); assert.equal(await result(3, 3).innerText(), '15.5'); await page.keyboard.press('Control+y'); await page.waitForFunction(() => document.querySelector('.writer-cell-editor')?.textContent === '=B2*C2'); assert.equal(await result(3, 3).innerText(), '58'); await cell(2, 3).click(); await edit.fill('=D3'); // Enter would start editing D4 below (src/lib/editor/tableControls.ts:283), so leave the table to render both results. await page.keyboard.press('Control+Enter'); // The table renders again after it loses focus. Wait for that before the check. for (const [r, c] of [[2, 3], [3, 3]]) await result(r, c).filter({ hasText: /^#CYCLE!$/ }).waitFor(); assert.equal(await result(2, 3).innerText(), '#CYCLE!'); assert.equal(await result(3, 3).innerText(), '#CYCLE!'); await cell(2, 3).click(); await edit.fill('=AVERAGE(B3:C3)'); await page.keyboard.press('Tab'); await cell(3, 1).click(); await edit.fill('=SUM(B2:B3)'); await page.keyboard.press('Enter'); assert.equal(await result(3, 1).innerText(), '12'); const saved = await save(); assert.match(saved, /=SUM\(B2:B3\)/); assert.doesNotMatch(saved, /\\=/); await cell(3, 1).click(); await checkFooter(); await shot('footer'); await page.setViewportSize({ width: 850, height: 1000 }); await checkFooter(); await shot('footer-narrow'); await page.locator('.table-formula-help summary').click(); assert.equal(await page.locator('.table-formula-help').getAttribute('open'), ''); await checkFooter(); await page.setViewportSize({ width: 1440, height: 1000 }); await shot('formulas'); await page.keyboard.press('Control+e'); await page.keyboard.press('Control+e'); assert.equal(await result(3, 1).innerText(), '12'); assert.equal(await result(3, 3).innerText(), '58'); for (const mode of ['Read', 'Split']) { await page.getByRole('group', { name: 'Document view', exact: true }).getByRole('button', { pressed: true }).press('ArrowRight'); const preview = page.locator('.md-page table').first(); await preview.waitFor(); // Enter in the last row added an empty row below Total (src/lib/editor/tableControls.ts:285). const total = preview.locator('tbody tr').filter({ hasText: 'Total' }); assert.equal(await total.locator('.md-table-formula').last().innerText(), '58', `${mode} view`); } // The general setting turns formulas off in all views. The source text shows and the file stays the same. const file = await page.evaluate(() => window.__TEST__.files.get('formulas.md')); const general = await page.evaluate(() => window.__TEST__.settings('general') ?? {}); await page.evaluate(value => window.__TEST__.writeSettings('general', value), { ...general, tableFormulas: false }); await page.waitForFunction(() => !document.querySelector('.md-table-formula')); const total = page.locator('.md-page table').first().locator('tbody tr').filter({ hasText: 'Total' }); assert.match(await total.innerText(), /=SUM\(D2:D3\)/); assert.match(await total.innerText(), /=SUM\(B2:B3\)/); await shot('formulas-off-split'); await page.getByRole('group', { name: 'Document view', exact: true }).getByRole('button', { name: 'Edit', exact: true }).click(); await table.waitFor(); assert.equal(await table.locator('.md-table-formula').count(), 0); assert.match(await cell(3, 3).innerText(), /=SUM\(D2:D3\)/); assert.equal(await page.locator('.table-formula-tools').count(), 0, 'Writer shows no formula help'); await shot('formulas-off-writer'); await page.evaluate(value => window.__TEST__.writeSettings('general', value), { ...general, tableFormulas: true }); await result(3, 3).waitFor(); assert.equal(await result(3, 3).innerText(), '58'); await page.locator('.table-formula-tools').first().waitFor(); assert.equal(await page.evaluate(() => window.__TEST__.files.get('formulas.md')), file); });