import type { Page } from "@playwright/test"; import { expect, openFromTree, stubDesktop, stubSettingsWrites, test } from "./fixtures"; const NOTEBOOK_PATH = "/dev/acme-api/run-cells.ipynb"; async function chooseSoftLineBreaks(page: Page, mode: "Standard Markdown" | "Break on single newline") { await page.keyboard.press("Meta+,"); const settings = page.getByRole("dialog", { name: "Settings", exact: true }); await settings.getByRole("textbox", { name: "Search settings" }).fill("Soft line breaks"); await settings.getByRole("treeitem", { name: "Markdown", exact: true, level: 2 }).click(); const select = settings.getByRole("button", { name: "Soft line breaks", exact: true }); await expect(select).toBeVisible(); await select.click(); await page.getByRole("option", { name: mode, exact: true }).click(); await settings.getByRole("button", { name: "OK", exact: true }).click(); await expect(settings).toBeHidden(); } /** Opens a notebook whose kernel answers a cell with its source as output. */ async function openNotebook(page: Page, markdownSource = "## Totals") { await openFromTree(page, "README.md"); await stubDesktop(page); await stubSettingsWrites(page); await page.evaluate( async ({ path, markdownSource }) => { const cell = (id: string, cellType: string, source: string) => ({ id, cellType, source, metadata: {}, attachments: {}, executionCount: null as number | null, outputs: [] as unknown[], }); let runs = 0; let added = 0; const snapshot = { id: "run-cells", path, version: 1, dirty: false, nbformat: 4, nbformatMinor: 5, metadata: {}, canUndo: false, canRedo: false, cells: [ cell("load", "code", "orders = [12, 30, 8]"), cell("notes", "markdown", markdownSource), cell("total", "code", "sum(orders)"), ], }; const answer = () => { snapshot.version++; return structuredClone(snapshot); }; Object.assign(window.__ipc.handlers, { notebook_open: () => structuredClone(snapshot), notebook_get: () => structuredClone(snapshot), notebook_cell_docs: () => [], sdk_detect: () => [], sdk_python_effective: () => null, jupyter_servers: () => [], notebook_edit: ({ edit }: { edit: Record }) => { const at = snapshot.cells.findIndex((c) => c.id === (edit.cell ?? edit.after)); if (edit.type === "setSource") snapshot.cells[at]!.source = edit.source!; else if (edit.type === "insertCell") snapshot.cells.splice(at + 1, 0, cell(`added-${++added}`, edit.cellType!, "")); return answer(); }, notebook_execute: ({ cells }: { cells: string[] }) => { const run = snapshot.cells.find((c) => c.id === cells[0])!; run.executionCount = ++runs; run.outputs = [{ output_type: "stream", name: "stdout", text: `ran: ${run.source}\n` }]; return answer(); }, }); const module = "/src/workbench/contributions.ts"; const { editorService } = await import(/* @vite-ignore */ module); await editorService().openFile(path, { preview: false }); }, { path: NOTEBOOK_PATH, markdownSource }, ); await expect(region(page, 1)).toBeVisible(); } const region = (page: Page, n: number) => page.getByRole("region", { name: new RegExp(`^Cell ${n}: `) }); const text = (page: Page, n: number) => region(page, n).locator(".cm-content"); const runs = (page: Page) => page.evaluate(() => window.__ipc.calls.filter((c) => c.cmd === "notebook_execute").flatMap((c) => c.args.cells as string[]), ); test("Run Cell keeps the caret in the cell; Shift+Enter steps through and adds a cell at the end", async ({ workbench: page, }) => { await openNotebook(page); await text(page, 1).click(); await page.keyboard.press("Meta+Enter"); await expect(region(page, 1)).toContainText("ran: orders = [12, 30, 8]"); await expect(text(page, 1)).toBeFocused(); await page.keyboard.press("Control+Enter"); await expect.poll(() => runs(page)).toEqual(["load", "load"]); await page.keyboard.press("Shift+Enter"); await expect(region(page, 2)).toBeFocused(); await expect(region(page, 2)).toHaveClass(/selected/); await page.keyboard.press("Enter"); await expect(text(page, 2)).toBeFocused(); await page.keyboard.press("Shift+Enter"); await expect(text(page, 2)).toHaveCount(0); await expect(region(page, 3)).toBeFocused(); await page.keyboard.press("Shift+Enter"); await expect(text(page, 4)).toBeFocused(); await page.keyboard.type("1"); await expect(text(page, 4)).toHaveText("1"); expect(await runs(page)).toEqual(["load", "load", "load", "total"]); await expect(text(page, 1)).toHaveText("orders = [12, 30, 8]"); await expect(text(page, 3)).toHaveText("sum(orders)"); }); test("the run actions are in Find Action and act on the notebook behind it", async ({ workbench: page }) => { await openNotebook(page); await text(page, 1).click(); await page.keyboard.press("Meta+Shift+A"); await page.keyboard.type("Run Cell and Insert Below"); await page.getByRole("option", { name: /Run Cell and Insert Below/ }).waitFor(); await page.keyboard.press("Enter"); await expect(text(page, 2)).toBeFocused(); await expect(region(page, 3)).toContainText("Totals"); expect(await runs(page)).toEqual(["load"]); }); test("Markdown soft line breaks setting applies to notebook Markdown cells", async ({ workbench: page }) => { await openNotebook(page, "first line\nsecond line\nthird line\n\nhard one \nhard two\n\nnext paragraph"); const paragraphs = region(page, 2).locator("p"); await expect(paragraphs).toHaveCount(3); await expect(paragraphs.nth(0).locator("br")).toHaveCount(0); await expect(paragraphs.nth(1).locator("br")).toHaveCount(1); await chooseSoftLineBreaks(page, "Break on single newline"); await expect(paragraphs).toHaveCount(3); await expect(paragraphs.nth(0).locator("br")).toHaveCount(2); await expect(paragraphs.nth(1).locator("br")).toHaveCount(1); await chooseSoftLineBreaks(page, "Standard Markdown"); await expect(paragraphs).toHaveCount(3); await expect(paragraphs.nth(0).locator("br")).toHaveCount(0); await expect(paragraphs.nth(1).locator("br")).toHaveCount(1); });