ZDDC/tests/browse.spec.js
ZDDC 0b382716e3 feat(browse): TOC pane + FS-API saves in the markdown plugin
Completes the markdown plugin's deferred v2 items:

1. TOC pane

A third pane to the right of the Toast UI editor lists every heading
in the current document, hierarchically indented by level. Click an
item → editor scrolls to that heading (markdown-mode uses
setSelection + preview scroll; WYSIWYG mode uses DOM text matching;
the target heading flashes briefly via primary-light background).
The TOC re-renders on every editor change (debounced 250ms) so it
stays in sync with edits.

Heading parser supports ATX-style `^#{1,6}\s+` lines, strips inline
markdown emphasis/code/links/strike from the displayed label.
Empty file → "Empty file." Headingless file → "No headings."

2. FS-API writes

Saves now route to whichever source the file came from:

  - node.handle + createWritable available → FileSystemWritableFileStream
    (local folder picker). The user's chosen file gets overwritten
    via the browser's File System Access API.
  - node.url + server source → PUT to the server URL (as before).
  - zip-virtual file → save disabled (no writable stream from JSZip).
  - Anything else → save disabled with a tooltip.

Save status surfaces via the existing toolbar (`Saved 10:42:18`) AND
a shared toast notification ("Saved readme.md" / "Save failed: …")
so the success/failure is visible regardless of whether the user is
looking at the toolbar.

Source-hint chip on the toolbar shows "local" / "server" /
"read-only (inside zip)" so the user knows which write path is
active before they make changes.

CSS additions in browse/css/tree.css for .md-toolbar, .md-split,
.md-editor-host, .md-toc-pane, .toc-list, and the .toc-level-1..6
indentation rules.

A new Playwright test exercises the markdown plugin end-to-end:
mounts the editor on a .md click, asserts the three DOM regions are
visible, verifies the TOC contains the three expected headings from
the test fixture's markdown content, and confirms the source hint
reads "local" for FS-API mode.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 19:02:32 -05:00

106 lines
4.7 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: toolbar, editor host, TOC pane.
await expect(page.locator('.md-toolbar')).toBeVisible({ timeout: 15000 });
await expect(page.locator('.md-editor-host')).toBeVisible();
await expect(page.locator('.md-toc-pane')).toBeVisible();
// TOC enumerates the three headings.
await page.waitForSelector('.toc-list li', { timeout: 10000 });
const tocItems = await page.locator('.toc-list li a').allTextContents();
expect(tocItems).toEqual(['Title', 'Section One', 'Subsection']);
// Source hint reflects local FS-API mode.
await expect(page.locator('.md-toolbar__source')).toHaveText(/local/i);
});
});