ZDDC/tests/browse.spec.js
ZDDC cb2cf1ebe3 fix(browse): re-implement markdown editor layout on CSS Grid
The previous nested-flexbox layout produced indeterminate heights
inside the Toast UI editor host and made the TOC pane width fragile —
visually the editor and outline weren't laying out reliably. This
swaps the whole shell to CSS Grid, which gives every cell a definite
size.

Layout:
   ┌──────────────────────────────────────────────────────────────┐
   │  toolbar (Save | ● modified | status | source)               │
   ├─────────────────────────────────────┬────────────────────────┤
   │                                     │  Outline               │
   │   Toast UI Editor                   │  • Heading 1           │
   │   (md / wysiwyg / preview)          │    • Subheading        │
   │                                     ├────────────────────────┤
   │                                     │  Front matter          │
   │                                     │  title: …  rev: …      │
   └─────────────────────────────────────┴────────────────────────┘

Notes:
  - The shell mounts as a single child of #previewBody (not by
    re-classing previewBody itself), so the outer flex layout that
    fills the preview pane is preserved.
  - Sidebar is its own grid (outline 1fr + front-matter auto/max 40%),
    each section independently scrollable.
  - Resizer is a 6 px element on the grid column boundary; drag
    updates grid-template-columns. Keyboard left/right adjust by 24 px.
    Width persists across mounts (lastTocWidth) within a session.
  - parseHeadings now skips front-matter envelope + fenced code so a
    "##" inside ```bash``` doesn't show up as an outline entry.
  - scrollEditorToHeading uses findScrollParent + scrollTo({behavior:
    'smooth'}) so jumps feel less jarring.
  - Class names follow BEM: .md-shell__*, .md-side__*, .md-toc__*,
    .md-fm__*. Tests updated to the new selectors.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 11:30:33 -05:00

107 lines
4.8 KiB
JavaScript

import { test, expect } from '@playwright/test';
import { MOCK_FS_INIT_SCRIPT } from './fixtures/mock-fs-api.js';
import * as path from 'path';
const HTML_PATH = path.resolve('browse/dist/browse.html');
test.describe('Browse', () => {
test.beforeEach(async ({ page }) => {
await page.addInitScript(MOCK_FS_INIT_SCRIPT);
});
test('loads with the empty state visible and add-directory button enabled', async ({ page }) => {
await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' });
await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 });
// file:// + no auto-server-root → the empty state is shown and the
// browse table stays hidden until the user picks a directory.
await expect(page.locator('#emptyState')).toBeVisible();
await expect(page.locator('#browseRoot')).toBeHidden();
const addDirBtn = page.locator('#addDirectoryBtn');
await expect(addDirBtn).toBeVisible();
await expect(addDirBtn).not.toBeDisabled();
});
test('renders rows from a mock directory after picking', async ({ page }) => {
await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' });
await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 });
await page.evaluate(() => {
window.__setMockDirectory('docs', [
{ name: 'spec.pdf', content: '%PDF-fixture', size: 1024 },
{ name: 'readme.md', content: '# Hello\n', size: 8 },
{ name: 'notes.txt', content: 'note one\nnote two\n', size: 16 },
]);
});
await page.locator('#addDirectoryBtn').click();
// Browse swaps from empty state to the two-pane layout. The
// tree pane is on the left; rows are <div class="tree-row">.
await page.waitForSelector('#browseRoot:not(.hidden)', { timeout: 10000 });
await page.waitForFunction(
() => document.querySelectorAll('#treeBody .tree-row').length >= 3,
{ timeout: 10000 }
);
const rows = await page.locator('#treeBody .tree-row').count();
expect(rows).toBeGreaterThanOrEqual(3);
// Right preview pane is present and starts in the empty state.
await expect(page.locator('#previewPane')).toBeVisible();
await expect(page.locator('#previewBody')).toContainText(/Click a file/);
});
test('clicking a file shows it in the preview pane', async ({ page }) => {
await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' });
await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 });
await page.evaluate(() => {
window.__setMockDirectory('docs', [
{ name: 'notes.txt', content: 'hello world', size: 11 },
]);
});
await page.locator('#addDirectoryBtn').click();
await page.waitForSelector('#treeBody .tree-row', { timeout: 10000 });
// Click the file row.
await page.locator('#treeBody .tree-row[data-isdir="false"]').first().click();
// Preview title updates to the file name; pop-out button appears.
await expect(page.locator('#previewTitle')).toHaveText(/notes\.txt/);
await expect(page.locator('#previewPopout')).toBeVisible();
});
test('clicking a .md file mounts the markdown editor with a TOC', async ({ page }) => {
await page.goto(`file://${HTML_PATH}`, { waitUntil: 'load' });
await page.waitForSelector('#addDirectoryBtn', { timeout: 15000 });
await page.evaluate(() => {
window.__setMockDirectory('notes', [
{
name: 'readme.md',
content: '# Title\n\nIntro.\n\n## Section One\n\nText.\n\n### Subsection\n\nDeeper.',
size: 100,
},
]);
});
await page.locator('#addDirectoryBtn').click();
await page.waitForSelector('#treeBody .tree-row[data-isdir="false"]', { timeout: 10000 });
await page.locator('#treeBody .tree-row[data-isdir="false"]').first().click();
// Markdown plugin DOM mounts: shell, toolbar, editor host, sidebar.
await expect(page.locator('.md-shell')).toBeVisible({ timeout: 15000 });
await expect(page.locator('.md-shell__toolbar')).toBeVisible();
await expect(page.locator('.md-shell__editor')).toBeVisible();
await expect(page.locator('.md-shell__sidebar')).toBeVisible();
// Outline lists the three headings.
await page.waitForSelector('.md-toc__list .md-toc__item', { timeout: 10000 });
const tocItems = await page.locator('.md-toc__list .md-toc__item').allTextContents();
expect(tocItems).toEqual(['Title', 'Section One', 'Subsection']);
// Source hint reflects local FS-API mode.
await expect(page.locator('.md-shell__source')).toHaveText(/local/i);
});
});